Created
February 2, 2015 14:56
-
-
Save xphere/cb5c78fcb6bee0db680d to your computer and use it in GitHub Desktop.
Add listeners that launch on particular firewalls
This file contains hidden or 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 | |
| use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | |
| use Symfony\Component\DependencyInjection\ContainerBuilder; | |
| use Symfony\Component\DependencyInjection\Reference; | |
| /** | |
| * Class FirewallListenerCompilerPass | |
| * | |
| * @author Berny Cantos <be@rny.cc> | |
| */ | |
| class FirewallListenerCompilerPass implements CompilerPassInterface | |
| { | |
| /** | |
| * You can modify the container here before it is dumped to PHP code. | |
| * | |
| * @param ContainerBuilder $container | |
| * | |
| * @api | |
| */ | |
| public function process(ContainerBuilder $container) | |
| { | |
| if (!$container->has('security.firewall.context')) { | |
| return; | |
| } | |
| $listenersByContext = $this->collectListenersByContext($container); | |
| foreach ($listenersByContext as $context_id => $listeners) { | |
| $context_definition = $container->findDefinition($context_id); | |
| $arguments = $context_definition->getArgument(0); | |
| $arguments = array_merge($arguments, $listeners); | |
| $context_definition->replaceArgument(0, $arguments); | |
| } | |
| } | |
| /** | |
| * @param ContainerBuilder $container | |
| * | |
| * @return array | |
| */ | |
| protected function collectListenersByContext(ContainerBuilder $container) | |
| { | |
| $listenersByContext = array(); | |
| $taggedListeners = $container->findTaggedServiceIds('firewall_listener'); | |
| foreach ($taggedListeners as $listener_id => $tags) { | |
| foreach ($tags as $tag) { | |
| if (!isset($tag['firewall'])) { | |
| throw new \RuntimeException(sprintf( | |
| 'Must define "firewall" in "firewall_listener" tag of "%s" definition.', | |
| $listener_id | |
| )); | |
| } | |
| $firewall_id = $tag['firewall']; | |
| $context_id = 'security.firewall.map.context.' . $firewall_id; | |
| if (!$container->has($context_id)) { | |
| throw new \RuntimeException(sprintf( | |
| 'Context for firewall "%s" does not exist for "%s" definition.', | |
| $firewall_id, | |
| $listener_id | |
| )); | |
| } | |
| $listenersByContext[$context_id][] = new Reference($listener_id); | |
| } | |
| } | |
| return $listenersByContext; | |
| } | |
| } |
This file contains hidden or 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
| services: | |
| # | |
| # Event Listeners | |
| # | |
| store.core.event_listener.store_disabled: | |
| class: %store.core.event_listener.store_disabled.class% | |
| tags: | |
| - { name: firewall_listener, firewall: secured_area } | |
| store.core.event_listener.store_under_construction: | |
| class: %store.core.event_listener.store_under_construction.class% | |
| tags: | |
| - { name: firewall_listener, firewall: secured_area } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment