Security update for permissions_by_term
[yaffs-website] / vendor / behat / behat / src / Behat / Behat / Context / ServiceContainer / ContextExtension.php
1 <?php
2
3 /*
4  * This file is part of the Behat.
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\Behat\Context\ServiceContainer;
12
13 use Behat\Behat\Definition\ServiceContainer\DefinitionExtension;
14 use Behat\Behat\Snippet\ServiceContainer\SnippetExtension;
15 use Behat\Testwork\Argument\ServiceContainer\ArgumentExtension;
16 use Behat\Testwork\Autoloader\ServiceContainer\AutoloaderExtension;
17 use Behat\Testwork\Cli\ServiceContainer\CliExtension;
18 use Behat\Testwork\Environment\ServiceContainer\EnvironmentExtension;
19 use Behat\Testwork\Filesystem\ServiceContainer\FilesystemExtension;
20 use Behat\Testwork\ServiceContainer\Extension;
21 use Behat\Testwork\ServiceContainer\ExtensionManager;
22 use Behat\Testwork\ServiceContainer\ServiceProcessor;
23 use Behat\Testwork\Suite\ServiceContainer\SuiteExtension;
24 use Behat\Testwork\Translator\ServiceContainer\TranslatorExtension;
25 use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
26 use Symfony\Component\DependencyInjection\ContainerBuilder;
27 use Symfony\Component\DependencyInjection\Definition;
28 use Symfony\Component\DependencyInjection\Reference;
29
30 /**
31  * Behat context extension.
32  *
33  * Extends Behat with context services.
34  *
35  * @author Konstantin Kudryashov <ever.zet@gmail.com>
36  */
37 final class ContextExtension implements Extension
38 {
39     /**
40      * Available services
41      */
42     const FACTORY_ID = 'context.factory';
43     const CONTEXT_SNIPPET_GENERATOR_ID = 'snippet.generator.context';
44     const AGGREGATE_RESOLVER_FACTORY_ID = 'context.argument.aggregate_resolver_factory';
45
46     /*
47      * Available extension points
48      */
49     const CLASS_RESOLVER_TAG = 'context.class_resolver';
50     const ARGUMENT_RESOLVER_TAG = 'context.argument_resolver';
51     const INITIALIZER_TAG = 'context.initializer';
52     const READER_TAG = 'context.reader';
53     const ANNOTATION_READER_TAG = 'context.annotation_reader';
54     const CLASS_GENERATOR_TAG = 'context.class_generator';
55     const SUITE_SCOPED_RESOLVER_FACTORY_TAG = 'context.argument.suite_resolver_factory';
56
57     /**
58      * @var ServiceProcessor
59      */
60     private $processor;
61
62     /**
63      * Initializes compiler pass.
64      *
65      * @param null|ServiceProcessor $processor
66      */
67     public function __construct(ServiceProcessor $processor = null)
68     {
69         $this->processor = $processor ? : new ServiceProcessor();
70     }
71
72     /**
73      * {@inheritdoc}
74      */
75     public function getConfigKey()
76     {
77         return 'contexts';
78     }
79
80     /**
81      * {@inheritdoc}
82      */
83     public function initialize(ExtensionManager $extensionManager)
84     {
85     }
86
87     /**
88      * {@inheritdoc}
89      */
90     public function configure(ArrayNodeDefinition $builder)
91     {
92     }
93
94     /**
95      * {@inheritdoc}
96      */
97     public function load(ContainerBuilder $container, array $config)
98     {
99         $this->loadFactory($container);
100         $this->loadArgumentResolverFactory($container);
101         $this->loadEnvironmentHandler($container);
102         $this->loadEnvironmentReader($container);
103         $this->loadSuiteSetup($container);
104         $this->loadSnippetAppender($container);
105         $this->loadSnippetGenerators($container);
106         $this->loadSnippetsController($container);
107         $this->loadDefaultClassGenerators($container);
108         $this->loadDefaultContextReaders($container);
109     }
110
111     /**
112      * {@inheritdoc}
113      */
114     public function process(ContainerBuilder $container)
115     {
116         $this->processClassResolvers($container);
117         $this->processArgumentResolverFactories($container);
118         $this->processArgumentResolvers($container);
119         $this->processContextInitializers($container);
120         $this->processContextReaders($container);
121         $this->processClassGenerators($container);
122         $this->processAnnotationReaders($container);
123     }
124
125     /**
126      * Loads context factory.
127      *
128      * @param ContainerBuilder $container
129      */
130     private function loadFactory(ContainerBuilder $container)
131     {
132         $definition = new Definition('Behat\Behat\Context\ContextFactory', array(
133             new Reference(ArgumentExtension::CONSTRUCTOR_ARGUMENT_ORGANISER_ID)
134         ));
135         $container->setDefinition(self::FACTORY_ID, $definition);
136     }
137
138     /**
139      * Loads argument resolver factory used in the environment handler.
140      *
141      * @param ContainerBuilder $container
142      */
143     private function loadArgumentResolverFactory(ContainerBuilder $container)
144     {
145         $definition = new Definition('Behat\Behat\Context\Argument\CompositeArgumentResolverFactory');
146         $container->setDefinition(self::AGGREGATE_RESOLVER_FACTORY_ID, $definition);
147     }
148
149     /**
150      * Loads context environment handlers.
151      *
152      * @param ContainerBuilder $container
153      */
154     private function loadEnvironmentHandler(ContainerBuilder $container)
155     {
156         $definition = new Definition('Behat\Behat\Context\Environment\Handler\ContextEnvironmentHandler', array(
157             new Reference(self::FACTORY_ID),
158             new Reference(self::AGGREGATE_RESOLVER_FACTORY_ID)
159         ));
160         $definition->addTag(EnvironmentExtension::HANDLER_TAG, array('priority' => 50));
161         $container->setDefinition(self::getEnvironmentHandlerId(), $definition);
162     }
163
164     /**
165      * Loads context environment readers.
166      *
167      * @param ContainerBuilder $container
168      */
169     private function loadEnvironmentReader(ContainerBuilder $container)
170     {
171         $definition = new Definition('Behat\Behat\Context\Environment\Reader\ContextEnvironmentReader');
172         $definition->addTag(EnvironmentExtension::READER_TAG, array('priority' => 50));
173         $container->setDefinition(self::getEnvironmentReaderId(), $definition);
174     }
175
176     /**
177      * Loads context environment setup.
178      *
179      * @param ContainerBuilder $container
180      */
181     private function loadSuiteSetup(ContainerBuilder $container)
182     {
183         $definition = new Definition('Behat\Behat\Context\Suite\Setup\SuiteWithContextsSetup', array(
184             new Reference(AutoloaderExtension::CLASS_LOADER_ID),
185             new Reference(FilesystemExtension::LOGGER_ID)
186         ));
187         $definition->addTag(SuiteExtension::SETUP_TAG, array('priority' => 20));
188         $container->setDefinition(self::getSuiteSetupId(), $definition);
189     }
190
191     /**
192      * Loads context snippet appender.
193      *
194      * @param ContainerBuilder $container
195      */
196     private function loadSnippetAppender(ContainerBuilder $container)
197     {
198         $definition = new Definition('Behat\Behat\Context\Snippet\Appender\ContextSnippetAppender', array(
199             new Reference(FilesystemExtension::LOGGER_ID)
200         ));
201         $definition->addTag(SnippetExtension::APPENDER_TAG, array('priority' => 50));
202         $container->setDefinition(SnippetExtension::APPENDER_TAG . '.context', $definition);
203     }
204
205     /**
206      * Loads context snippet generators.
207      *
208      * @param ContainerBuilder $container
209      */
210     private function loadSnippetGenerators(ContainerBuilder $container)
211     {
212         $definition = new Definition('Behat\Behat\Context\Snippet\Generator\ContextSnippetGenerator', array(
213             new Reference(DefinitionExtension::PATTERN_TRANSFORMER_ID)
214         ));
215         $definition->addTag(SnippetExtension::GENERATOR_TAG, array('priority' => 50));
216         $container->setDefinition(self::CONTEXT_SNIPPET_GENERATOR_ID, $definition);
217     }
218
219     /**
220      * @param ContainerBuilder $container
221      */
222     protected function loadSnippetsController(ContainerBuilder $container)
223     {
224         $definition = new Definition('Behat\Behat\Context\Cli\ContextSnippetsController', array(
225             new Reference(self::CONTEXT_SNIPPET_GENERATOR_ID),
226             new Reference(TranslatorExtension::TRANSLATOR_ID)
227         ));
228         $definition->addTag(CliExtension::CONTROLLER_TAG, array('priority' => 410));
229         $container->setDefinition(CliExtension::CONTROLLER_TAG . '.context_snippets', $definition);
230     }
231
232     /**
233      * Loads default context class generators.
234      *
235      * @param ContainerBuilder $container
236      */
237     private function loadDefaultClassGenerators(ContainerBuilder $container)
238     {
239         $definition = new Definition('Behat\Behat\Context\ContextClass\SimpleClassGenerator');
240         $definition->addTag(self::CLASS_GENERATOR_TAG, array('priority' => 50));
241         $container->setDefinition(self::CLASS_GENERATOR_TAG . '.simple', $definition);
242     }
243
244     /**
245      * Loads default context readers.
246      *
247      * @param ContainerBuilder $container
248      */
249     private function loadDefaultContextReaders(ContainerBuilder $container)
250     {
251         $definition = new Definition('Behat\Behat\Context\Reader\AnnotatedContextReader');
252         $container->setDefinition(self::getAnnotatedContextReaderId(), $definition);
253
254         $definition = new Definition('Behat\Behat\Context\Reader\ContextReaderCachedPerContext', array(
255             new Reference(self::getAnnotatedContextReaderId())
256         ));
257         $definition->addTag(self::READER_TAG, array('priority' => 50));
258         $container->setDefinition(self::getAnnotatedContextReaderId() . '.cached', $definition);
259
260         $definition = new Definition('Behat\Behat\Context\Reader\TranslatableContextReader', array(
261             new Reference(TranslatorExtension::TRANSLATOR_ID)
262         ));
263         $container->setDefinition(self::READER_TAG . '.translatable', $definition);
264
265         $definition = new Definition('Behat\Behat\Context\Reader\ContextReaderCachedPerSuite', array(
266             new Reference(self::READER_TAG . '.translatable')
267         ));
268         $definition->addTag(self::READER_TAG, array('priority' => 50));
269         $container->setDefinition(self::READER_TAG . '.translatable.cached', $definition);
270     }
271
272     /**
273      * Processes all class resolvers.
274      *
275      * @param ContainerBuilder $container
276      */
277     private function processClassResolvers(ContainerBuilder $container)
278     {
279         $references = $this->processor->findAndSortTaggedServices($container, self::CLASS_RESOLVER_TAG);
280         $definition = $container->getDefinition(self::getEnvironmentHandlerId());
281
282         foreach ($references as $reference) {
283             $definition->addMethodCall('registerClassResolver', array($reference));
284         }
285     }
286
287     /**
288      * Processes all argument resolver factories.
289      *
290      * @param ContainerBuilder $container
291      */
292     private function processArgumentResolverFactories($container)
293     {
294         $references = $this->processor->findAndSortTaggedServices($container, self::SUITE_SCOPED_RESOLVER_FACTORY_TAG);
295         $definition = $container->getDefinition(self::AGGREGATE_RESOLVER_FACTORY_ID);
296
297         foreach ($references as $reference) {
298             $definition->addMethodCall('registerFactory', array($reference));
299         }
300     }
301
302     /**
303      * Processes all argument resolvers.
304      *
305      * @param ContainerBuilder $container
306      */
307     private function processArgumentResolvers(ContainerBuilder $container)
308     {
309         $references = $this->processor->findAndSortTaggedServices($container, self::ARGUMENT_RESOLVER_TAG);
310         $definition = $container->getDefinition(self::FACTORY_ID);
311
312         foreach ($references as $reference) {
313             $definition->addMethodCall('registerArgumentResolver', array($reference));
314         }
315     }
316
317     /**
318      * Processes all context initializers.
319      *
320      * @param ContainerBuilder $container
321      */
322     private function processContextInitializers(ContainerBuilder $container)
323     {
324         $references = $this->processor->findAndSortTaggedServices($container, self::INITIALIZER_TAG);
325         $definition = $container->getDefinition(self::FACTORY_ID);
326
327         foreach ($references as $reference) {
328             $definition->addMethodCall('registerContextInitializer', array($reference));
329         }
330     }
331
332     /**
333      * Processes all context readers.
334      *
335      * @param ContainerBuilder $container
336      */
337     private function processContextReaders(ContainerBuilder $container)
338     {
339         $references = $this->processor->findAndSortTaggedServices($container, self::READER_TAG);
340         $definition = $container->getDefinition(self::getEnvironmentReaderId());
341
342         foreach ($references as $reference) {
343             $definition->addMethodCall('registerContextReader', array($reference));
344         }
345     }
346
347     /**
348      * Processes all class generators.
349      *
350      * @param ContainerBuilder $container
351      */
352     private function processClassGenerators(ContainerBuilder $container)
353     {
354         $references = $this->processor->findAndSortTaggedServices($container, self::CLASS_GENERATOR_TAG);
355         $definition = $container->getDefinition(self::getSuiteSetupId());
356
357         foreach ($references as $reference) {
358             $definition->addMethodCall('registerClassGenerator', array($reference));
359         }
360     }
361
362     /**
363      * Processes all annotation readers.
364      *
365      * @param ContainerBuilder $container
366      */
367     private function processAnnotationReaders(ContainerBuilder $container)
368     {
369         $references = $this->processor->findAndSortTaggedServices($container, self::ANNOTATION_READER_TAG);
370         $definition = $container->getDefinition(self::getAnnotatedContextReaderId());
371
372         foreach ($references as $reference) {
373             $definition->addMethodCall('registerAnnotationReader', array($reference));
374         }
375     }
376
377     /**
378      * Returns context environment handler service id.
379      *
380      * @return string
381      */
382     private static function getEnvironmentHandlerId()
383     {
384         return EnvironmentExtension::HANDLER_TAG . '.context';
385     }
386
387     /**
388      * Returns context environment reader id.
389      *
390      * @return string
391      */
392     private static function getEnvironmentReaderId()
393     {
394         return EnvironmentExtension::READER_TAG . '.context';
395     }
396
397     /**
398      * Returns context suite setup id.
399      *
400      * @return string
401      */
402     private static function getSuiteSetupId()
403     {
404         return SuiteExtension::SETUP_TAG . '.suite_with_contexts';
405     }
406
407     /**
408      * Returns annotated context reader id.
409      *
410      * @return string
411      */
412     private static function getAnnotatedContextReaderId()
413     {
414         return self::READER_TAG . '.annotated';
415     }
416 }