Skip to content

Instantly share code, notes, and snippets.

@mickaelandrieu
Last active November 21, 2018 14:37
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save mickaelandrieu/61ccd7c4fc8df085dea5f45de01636d0 to your computer and use it in GitHub Desktop.
Enable an event subscriber dynamically (the right way)
<?php
namespace AppBundle;
use AppBundle\DependencyInjection\Compiler\SecureApplicationPass;
use Symfony\Component\DependencyInjection\Compiler\PassConfig
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Bundle\Bundle;
class AppBundle extends Bundle
{
public function build(ContainerBuilder $container)
{
$container->addCompilerPass(new SecureApplicationPass(), PassConfig::TYPE_BEFORE_REMOVING, 10);
}
}
<?php
class SecureApplicationPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
{
if ($container->hasParameter('enable_subscriber')) {
$definition = $container->getDefinition('app.token_subscriber');
$definition->addTag('kernel.event_subscriber');
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment