Security update for permissions_by_term
[yaffs-website] / vendor / behat / behat / src / Behat / Testwork / EventDispatcher / ServiceContainer / EventDispatcherExtension.php
1 <?php
2
3 /*
4  * This file is part of the Behat Testwork.
5  * (c) Konstantin Kudryashov <ever.zet@gmail.com>
6  *
7  * For the full copyright and license information, please view the LICENSE
8  * file that was distributed with this source code.
9  */
10
11 namespace Behat\Testwork\EventDispatcher\ServiceContainer;
12
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;
22
23 /**
24  * Provides event dispatching service.
25  *
26  * @author Konstantin Kudryashov <ever.zet@gmail.com>
27  */
28 class EventDispatcherExtension implements Extension
29 {
30     /*
31      * Available services
32      */
33     const DISPATCHER_ID = 'event_dispatcher';
34
35     /*
36      * Available extension points
37      */
38     const SUBSCRIBER_TAG = 'event_dispatcher.subscriber';
39
40     /**
41      * @var ServiceProcessor
42      */
43     protected $processor;
44
45     /**
46      * Initializes extension.
47      *
48      * @param null|ServiceProcessor $processor
49      */
50     public function __construct(ServiceProcessor $processor = null)
51     {
52         $this->processor = $processor ? : new ServiceProcessor();
53     }
54
55     /**
56      * {@inheritdoc}
57      */
58     public function getConfigKey()
59     {
60         return 'events';
61     }
62
63     /**
64      * {@inheritdoc}
65      */
66     public function initialize(ExtensionManager $extensionManager)
67     {
68     }
69
70     /**
71      * {@inheritdoc}
72      */
73     public function configure(ArrayNodeDefinition $builder)
74     {
75     }
76
77     /**
78      * {@inheritdoc}
79      */
80     public function load(ContainerBuilder $container, array $config)
81     {
82         $this->loadSigintController($container);
83         $this->loadEventDispatcher($container);
84         $this->loadEventDispatchingExercise($container);
85         $this->loadEventDispatchingSuiteTester($container);
86     }
87
88     /**
89      * {@inheritdoc}
90      */
91     public function process(ContainerBuilder $container)
92     {
93         $this->processSubscribers($container);
94     }
95
96     /**
97      * Loads sigint controller
98      *
99      * @param ContainerBuilder $container
100      */
101     protected function loadSigintController(ContainerBuilder $container)
102     {
103         $definition = new Definition('Behat\Testwork\EventDispatcher\Cli\SigintController', array(
104             new Reference(EventDispatcherExtension::DISPATCHER_ID)
105         ));
106         $definition->addTag(CliExtension::CONTROLLER_TAG, array('priority' => 9999));
107         $container->setDefinition(CliExtension::CONTROLLER_TAG . '.sigint', $definition);
108     }
109
110     /**
111      * Loads event dispatcher.
112      *
113      * @param ContainerBuilder $container
114      */
115     protected function loadEventDispatcher(ContainerBuilder $container)
116     {
117         $definition = new Definition('Behat\Testwork\EventDispatcher\TestworkEventDispatcher');
118         $container->setDefinition(self::DISPATCHER_ID, $definition);
119     }
120
121     /**
122      * Loads event-dispatching exercise.
123      *
124      * @param ContainerBuilder $container
125      */
126     protected function loadEventDispatchingExercise(ContainerBuilder $container)
127     {
128         $definition = new Definition('Behat\Testwork\EventDispatcher\Tester\EventDispatchingExercise', array(
129             new Reference(TesterExtension::EXERCISE_ID),
130             new Reference(self::DISPATCHER_ID)
131         ));
132         $definition->addTag(TesterExtension::EXERCISE_WRAPPER_TAG);
133         $container->setDefinition(TesterExtension::EXERCISE_WRAPPER_TAG . '.event_dispatching', $definition);
134     }
135
136     /**
137      * Loads event-dispatching suite tester.
138      *
139      * @param ContainerBuilder $container
140      */
141     protected function loadEventDispatchingSuiteTester(ContainerBuilder $container)
142     {
143         $definition = new Definition('Behat\Testwork\EventDispatcher\Tester\EventDispatchingSuiteTester', array(
144             new Reference(TesterExtension::SUITE_TESTER_ID),
145             new Reference(self::DISPATCHER_ID)
146         ));
147         $definition->addTag(TesterExtension::SUITE_TESTER_WRAPPER_TAG, array('priority' => -9999));
148         $container->setDefinition(TesterExtension::SUITE_TESTER_WRAPPER_TAG . '.event_dispatching', $definition);
149     }
150
151     /**
152      * Registers all available event subscribers.
153      *
154      * @param ContainerBuilder $container
155      */
156     protected function processSubscribers(ContainerBuilder $container)
157     {
158         $references = $this->processor->findAndSortTaggedServices($container, self::SUBSCRIBER_TAG);
159         $definition = $container->getDefinition(self::DISPATCHER_ID);
160
161         foreach ($references as $reference) {
162             $definition->addMethodCall('addSubscriber', array($reference));
163         }
164     }
165 }