4 * This file is part of the Behat Testwork.
5 * (c) Konstantin Kudryashov <ever.zet@gmail.com>
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
11 namespace Behat\Testwork\EventDispatcher\ServiceContainer;
13 use Behat\Testwork\Cli\ServiceContainer\CliExtension;
14 use Behat\Testwork\ServiceContainer\Extension;
15 use Behat\Testwork\ServiceContainer\ExtensionManager;
16 use Behat\Testwork\ServiceContainer\ServiceProcessor;
17 use Behat\Testwork\Tester\ServiceContainer\TesterExtension;
18 use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
19 use Symfony\Component\DependencyInjection\ContainerBuilder;
20 use Symfony\Component\DependencyInjection\Definition;
21 use Symfony\Component\DependencyInjection\Reference;
24 * Provides event dispatching service.
26 * @author Konstantin Kudryashov <ever.zet@gmail.com>
28 class EventDispatcherExtension implements Extension
33 const DISPATCHER_ID = 'event_dispatcher';
36 * Available extension points
38 const SUBSCRIBER_TAG = 'event_dispatcher.subscriber';
41 * @var ServiceProcessor
46 * Initializes extension.
48 * @param null|ServiceProcessor $processor
50 public function __construct(ServiceProcessor $processor = null)
52 $this->processor = $processor ? : new ServiceProcessor();
58 public function getConfigKey()
66 public function initialize(ExtensionManager $extensionManager)
73 public function configure(ArrayNodeDefinition $builder)
80 public function load(ContainerBuilder $container, array $config)
82 $this->loadSigintController($container);
83 $this->loadEventDispatcher($container);
84 $this->loadEventDispatchingExercise($container);
85 $this->loadEventDispatchingSuiteTester($container);
91 public function process(ContainerBuilder $container)
93 $this->processSubscribers($container);
97 * Loads sigint controller
99 * @param ContainerBuilder $container
101 protected function loadSigintController(ContainerBuilder $container)
103 $definition = new Definition('Behat\Testwork\EventDispatcher\Cli\SigintController', array(
104 new Reference(EventDispatcherExtension::DISPATCHER_ID)
106 $definition->addTag(CliExtension::CONTROLLER_TAG, array('priority' => 9999));
107 $container->setDefinition(CliExtension::CONTROLLER_TAG . '.sigint', $definition);
111 * Loads event dispatcher.
113 * @param ContainerBuilder $container
115 protected function loadEventDispatcher(ContainerBuilder $container)
117 $definition = new Definition('Behat\Testwork\EventDispatcher\TestworkEventDispatcher');
118 $container->setDefinition(self::DISPATCHER_ID, $definition);
122 * Loads event-dispatching exercise.
124 * @param ContainerBuilder $container
126 protected function loadEventDispatchingExercise(ContainerBuilder $container)
128 $definition = new Definition('Behat\Testwork\EventDispatcher\Tester\EventDispatchingExercise', array(
129 new Reference(TesterExtension::EXERCISE_ID),
130 new Reference(self::DISPATCHER_ID)
132 $definition->addTag(TesterExtension::EXERCISE_WRAPPER_TAG);
133 $container->setDefinition(TesterExtension::EXERCISE_WRAPPER_TAG . '.event_dispatching', $definition);
137 * Loads event-dispatching suite tester.
139 * @param ContainerBuilder $container
141 protected function loadEventDispatchingSuiteTester(ContainerBuilder $container)
143 $definition = new Definition('Behat\Testwork\EventDispatcher\Tester\EventDispatchingSuiteTester', array(
144 new Reference(TesterExtension::SUITE_TESTER_ID),
145 new Reference(self::DISPATCHER_ID)
147 $definition->addTag(TesterExtension::SUITE_TESTER_WRAPPER_TAG, array('priority' => -9999));
148 $container->setDefinition(TesterExtension::SUITE_TESTER_WRAPPER_TAG . '.event_dispatching', $definition);
152 * Registers all available event subscribers.
154 * @param ContainerBuilder $container
156 protected function processSubscribers(ContainerBuilder $container)
158 $references = $this->processor->findAndSortTaggedServices($container, self::SUBSCRIBER_TAG);
159 $definition = $container->getDefinition(self::DISPATCHER_ID);
161 foreach ($references as $reference) {
162 $definition->addMethodCall('addSubscriber', array($reference));