Security update for permissions_by_term
[yaffs-website] / vendor / behat / behat / src / Behat / Behat / Transformation / ServiceContainer / TransformationExtension.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\Transformation\ServiceContainer;
12
13 use Behat\Behat\Context\ServiceContainer\ContextExtension;
14 use Behat\Behat\Definition\ServiceContainer\DefinitionExtension;
15 use Behat\Testwork\Call\ServiceContainer\CallExtension;
16 use Behat\Testwork\Environment\ServiceContainer\EnvironmentExtension;
17 use Behat\Testwork\ServiceContainer\Extension;
18 use Behat\Testwork\ServiceContainer\ExtensionManager;
19 use Behat\Testwork\ServiceContainer\ServiceProcessor;
20 use Behat\Testwork\Translator\ServiceContainer\TranslatorExtension;
21 use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
22 use Symfony\Component\DependencyInjection\ContainerBuilder;
23 use Symfony\Component\DependencyInjection\Definition;
24 use Symfony\Component\DependencyInjection\Reference;
25
26 /**
27  * Provides definition arguments transformation functionality.
28  *
29  * @author Konstantin Kudryashov <ever.zet@gmail.com>
30  */
31 class TransformationExtension implements Extension
32 {
33     /*
34      * Available services
35      */
36     const REPOSITORY_ID = 'transformation.repository';
37
38     /*
39      * Available extension points
40      */
41     const ARGUMENT_TRANSFORMER_TAG = 'transformation.argument_transformer';
42
43     /**
44      * @var ServiceProcessor
45      */
46     private $processor;
47
48     /**
49      * Initializes extension.
50      *
51      * @param null|ServiceProcessor $processor
52      */
53     public function __construct(ServiceProcessor $processor = null)
54     {
55         $this->processor = $processor ?: new ServiceProcessor();
56     }
57
58     /**
59      * {@inheritdoc}
60      */
61     public function getConfigKey()
62     {
63         return 'transformations';
64     }
65
66     /**
67      * {@inheritdoc}
68      */
69     public function initialize(ExtensionManager $extensionManager)
70     {
71     }
72
73     /**
74      * {@inheritdoc}
75      */
76     public function configure(ArrayNodeDefinition $builder)
77     {
78     }
79
80     /**
81      * {@inheritdoc}
82      */
83     public function load(ContainerBuilder $container, array $config)
84     {
85         $this->loadDefinitionArgumentsTransformer($container);
86         $this->loadDefaultTransformers($container);
87         $this->loadAnnotationReader($container);
88         $this->loadRepository($container);
89     }
90
91     /**
92      * {@inheritdoc}
93      */
94     public function process(ContainerBuilder $container)
95     {
96         $this->processArgumentsTransformers($container);
97     }
98
99     /**
100      * Loads definition arguments transformer.
101      *
102      * @param ContainerBuilder $container
103      */
104     protected function loadDefinitionArgumentsTransformer(ContainerBuilder $container)
105     {
106         $definition = new Definition('Behat\Behat\Transformation\Call\Filter\DefinitionArgumentsTransformer');
107         $definition->addTag(CallExtension::CALL_FILTER_TAG, array('priority' => 200));
108         $container->setDefinition($this->getDefinitionArgumentTransformerId(), $definition);
109     }
110
111     /**
112      * Loads default transformers.
113      *
114      * @param ContainerBuilder $container
115      */
116     protected function loadDefaultTransformers(ContainerBuilder $container)
117     {
118         $definition = new Definition('Behat\Behat\Transformation\Transformer\RepositoryArgumentTransformer', array(
119             new Reference(self::REPOSITORY_ID),
120             new Reference(CallExtension::CALL_CENTER_ID),
121             new Reference(DefinitionExtension::PATTERN_TRANSFORMER_ID),
122             new Reference(TranslatorExtension::TRANSLATOR_ID)
123         ));
124         $definition->addTag(self::ARGUMENT_TRANSFORMER_TAG, array('priority' => 50));
125         $container->setDefinition(self::ARGUMENT_TRANSFORMER_TAG . '.repository', $definition);
126     }
127
128     /**
129      * Loads transformation context annotation reader.
130      *
131      * @param ContainerBuilder $container
132      */
133     protected function loadAnnotationReader(ContainerBuilder $container)
134     {
135         $definition = new Definition('Behat\Behat\Transformation\Context\Annotation\TransformationAnnotationReader');
136         $definition->addTag(ContextExtension::ANNOTATION_READER_TAG, array('priority' => 50));
137         $container->setDefinition(ContextExtension::ANNOTATION_READER_TAG . '.transformation', $definition);
138     }
139
140     /**
141      * Loads transformations repository.
142      *
143      * @param ContainerBuilder $container
144      */
145     protected function loadRepository(ContainerBuilder $container)
146     {
147         $definition = new Definition('Behat\Behat\Transformation\TransformationRepository', array(
148             new Reference(EnvironmentExtension::MANAGER_ID)
149         ));
150         $container->setDefinition(self::REPOSITORY_ID, $definition);
151     }
152
153     /**
154      * Processes all available argument transformers.
155      *
156      * @param ContainerBuilder $container
157      */
158     protected function processArgumentsTransformers(ContainerBuilder $container)
159     {
160         $references = $this->processor->findAndSortTaggedServices($container, self::ARGUMENT_TRANSFORMER_TAG);
161         $definition = $container->getDefinition($this->getDefinitionArgumentTransformerId());
162
163         foreach ($references as $reference) {
164             $definition->addMethodCall('registerArgumentTransformer', array($reference));
165         }
166     }
167
168     /**
169      * Returns definition argument transformer service id.
170      *
171      * @return string
172      */
173     protected function getDefinitionArgumentTransformerId()
174     {
175         return CallExtension::CALL_FILTER_TAG . '.definition_argument_transformer';
176     }
177 }