digipolisgent/domainator9k-apptype-symfony-bundle
Installation Add the bundle via Composer in your Symfony project:
composer require district09/domainator9k-apptype-symfony-bundle
Enable the bundle in config/bundles.php:
return [
// ...
District09\Domainator9kApptypeBundle\Domainator9kApptypeBundle::class => ['all' => true],
];
Configuration Publish the default config (if needed):
php bin/console config:dump-reference District09\Domainator9kApptypeBundle
Override defaults in config/packages/domainator9k_apptype.yaml:
district09_domainator9k_apptype:
app_types: ['web', 'api'] # Example: Define allowed app types
First Use Case
Use the AppTypeChecker service to validate app types in controllers:
use District09\Domainator9kApptypeBundle\Service\AppTypeChecker;
class MyController extends AbstractController
{
public function __construct(private AppTypeChecker $appTypeChecker) {}
public function index()
{
$this->appTypeChecker->validate('web'); // Throws exception if invalid
// Proceed with logic
}
}
App Type Validation
AppTypeChecker in controllers/services to enforce allowed app types:
$this->appTypeChecker->validate($request->get('app_type'));
kernel.request) to auto-validate:
$event->setResponse($this->appTypeChecker->validate($event->getRequest()->attributes->get('app_type')));
Dynamic Configuration
dev vs. prod):
# config/packages/dev/domainator9k_apptype.yaml
district09_domainator9k_apptype:
app_types: ['web', 'cli'] # Extend for development
Dependency Injection
AppTypeChecker interface:
services:
District09\Domainator9kApptypeBundle\Service\AppTypeChecker:
arguments:
$allowedTypes: '%district09_domainator9k_apptype.app_types%'
$customValidator: '@app.custom_app_type_validator'
API Gateway Integration
$container->get('kernel')->addControllerMiddleware(
new AppTypeMiddleware($container->get('district09_domainator9k_apptype.app_type_checker'))
);
Deprecated Symfony Version
symfony/dependency-injection to ^5.0).No Built-in Exception Handling
validate() method throws generic InvalidArgumentException. Override the service to customize:
class CustomAppTypeChecker extends AppTypeChecker
{
protected function createException(string $type): \Exception
{
return new \RuntimeException("App type '$type' is not allowed.");
}
}
Configuration Overrides
config/packages/domainator9k_apptype.yaml is loaded after the default config to avoid precedence issues.Circular Dependencies
php bin/console debug:config district09_domainator9k_apptype
try {
$this->appTypeChecker->validate($type);
} catch (\Exception $e) {
$this->logger->error('Invalid app type', ['type' => $type]);
throw $e;
}
Custom Validators
Extend AppTypeChecker to add logic (e.g., database-backed types):
class DatabaseAppTypeChecker extends AppTypeChecker
{
public function __construct(private EntityManagerInterface $em) {}
protected function getAllowedTypes(): array
{
return $this->em->getRepository(AppType::class)->findAll()->map(fn($t) => $t->getName())->toArray();
}
}
Event Listeners Trigger events on validation (e.g., log app type changes):
// src/EventListener/AppTypeListener.php
class AppTypeListener implements EventSubscriberInterface
{
public static function getSubscribedEvents(): array
{
return [
AppTypeValidatedEvent::class => 'onAppTypeValidated',
];
}
public function onAppTypeValidated(AppTypeValidatedEvent $event): void
{
$this->logger->info('App type validated', ['type' => $event->getType()]);
}
}
Twig Integration Pass app types to templates:
{% if app.request.attributes.get('_app_type') in app_types %}
<div class="app-type-badge">{{ app.request.attributes.get('_app_type') }}</div>
{% endif %}
How can I help you explore Laravel packages today?