Security update for permissions_by_term
[yaffs-website] / vendor / behat / behat / src / Behat / Testwork / Call / ServiceContainer / CallExtension.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\Call\ServiceContainer;
12
13 use Behat\Testwork\ServiceContainer\Extension;
14 use Behat\Testwork\ServiceContainer\ExtensionManager;
15 use Behat\Testwork\ServiceContainer\ServiceProcessor;
16 use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
17 use Symfony\Component\DependencyInjection\ContainerBuilder;
18 use Symfony\Component\DependencyInjection\Definition;
19
20 /**
21  * Provides call services for testwork.
22  *
23  * @author Konstantin Kudryashov <ever.zet@gmail.com>
24  */
25 final class CallExtension implements Extension
26 {
27     /*
28      * Available services
29      */
30     const CALL_CENTER_ID = 'call.center';
31
32     /*
33      * Available extension points
34      */
35     const CALL_FILTER_TAG = 'call.call_filter';
36     const CALL_HANDLER_TAG = 'call.call_handler';
37     const RESULT_FILTER_TAG = 'call.result_filter';
38     const EXCEPTION_HANDLER_TAG = 'call.exception_handler';
39
40     /**
41      * @var ServiceProcessor
42      */
43     private $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 'calls';
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         $builder
76             ->addDefaultsIfNotSet()
77             ->children()
78                 ->scalarNode('error_reporting')
79                     ->info('Call executor will catch exceptions matching this level')
80                     ->defaultValue(E_ALL | E_STRICT)
81                 ->end()
82             ->end()
83         ;
84     }
85
86     /**
87      * {@inheritdoc}
88      */
89     public function load(ContainerBuilder $container, array $config)
90     {
91         $this->loadCallCenter($container);
92         $this->loadCallHandlers($container, $config['error_reporting']);
93     }
94
95     /**
96      * {@inheritdoc}
97      */
98     public function process(ContainerBuilder $container)
99     {
100         $this->processCallFilters($container);
101         $this->processCallHandlers($container);
102         $this->processResultFilters($container);
103         $this->processExceptionHandlers($container);
104     }
105
106     /**
107      * Loads call center service.
108      *
109      * @param ContainerBuilder $container
110      */
111     protected function loadCallCenter(ContainerBuilder $container)
112     {
113         $definition = new Definition('Behat\Testwork\Call\CallCenter');
114         $container->setDefinition(self::CALL_CENTER_ID, $definition);
115     }
116
117     /**
118      * Loads prebuilt call handlers.
119      *
120      * @param ContainerBuilder $container
121      * @param integer          $errorReporting
122      */
123     protected function loadCallHandlers(ContainerBuilder $container, $errorReporting)
124     {
125         $definition = new Definition('Behat\Testwork\Call\Handler\RuntimeCallHandler', array($errorReporting));
126         $definition->addTag(self::CALL_HANDLER_TAG, array('priority' => 50));
127         $container->setDefinition(self::CALL_HANDLER_TAG . '.runtime', $definition);
128     }
129
130     /**
131      * Registers all call filters to the CallCenter.
132      *
133      * @param ContainerBuilder $container
134      */
135     protected function processCallFilters(ContainerBuilder $container)
136     {
137         $references = $this->processor->findAndSortTaggedServices($container, CallExtension::CALL_FILTER_TAG);
138         $definition = $container->getDefinition(CallExtension::CALL_CENTER_ID);
139
140         foreach ($references as $reference) {
141             $definition->addMethodCall('registerCallFilter', array($reference));
142         }
143     }
144
145     /**
146      * Registers all call handlers to the CallCenter.
147      *
148      * @param ContainerBuilder $container
149      */
150     protected function processCallHandlers(ContainerBuilder $container)
151     {
152         $references = $this->processor->findAndSortTaggedServices($container, CallExtension::CALL_HANDLER_TAG);
153         $definition = $container->getDefinition(CallExtension::CALL_CENTER_ID);
154
155         foreach ($references as $reference) {
156             $definition->addMethodCall('registerCallHandler', array($reference));
157         }
158     }
159
160     /**
161      * Registers all call result filters to the CallCenter.
162      *
163      * @param ContainerBuilder $container
164      */
165     protected function processResultFilters(ContainerBuilder $container)
166     {
167         $references = $this->processor->findAndSortTaggedServices($container, CallExtension::RESULT_FILTER_TAG);
168         $definition = $container->getDefinition(CallExtension::CALL_CENTER_ID);
169
170         foreach ($references as $reference) {
171             $definition->addMethodCall('registerResultFilter', array($reference));
172         }
173     }
174
175     /**
176      * Registers all exception handlers to the CallCenter.
177      *
178      * @param ContainerBuilder $container
179      */
180     private function processExceptionHandlers(ContainerBuilder $container)
181     {
182         $references = $this->processor->findAndSortTaggedServices($container, CallExtension::EXCEPTION_HANDLER_TAG);
183         $definition = $container->getDefinition(CallExtension::CALL_CENTER_ID);
184
185         foreach ($references as $reference) {
186             $definition->addMethodCall('registerExceptionHandler', array($reference));
187         }
188     }
189 }