Last active
November 21, 2018 14:37
-
-
Save mickaelandrieu/61ccd7c4fc8df085dea5f45de01636d0 to your computer and use it in GitHub Desktop.
Enable an event subscriber dynamically (the right way)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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