Security update for permissions_by_term
[yaffs-website] / vendor / behat / behat / src / Behat / Behat / HelperContainer / ServiceContainer / HelperContainerExtension.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\HelperContainer\ServiceContainer;
12
13 use Behat\Behat\Context\ServiceContainer\ContextExtension;
14 use Behat\Behat\HelperContainer\Exception\WrongServicesConfigurationException;
15 use Behat\Testwork\Call\ServiceContainer\CallExtension;
16 use Behat\Testwork\ServiceContainer\Extension;
17 use Behat\Testwork\ServiceContainer\ExtensionManager;
18 use Behat\Testwork\ServiceContainer\ServiceProcessor;
19 use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
20 use Symfony\Component\DependencyInjection\ContainerBuilder;
21 use Symfony\Component\DependencyInjection\Definition;
22
23 /**
24  * Behat helper container extension.
25  *
26  * Extends Behat with helper containers support.
27  *
28  * @author Konstantin Kudryashov <ever.zet@gmail.com>
29  */
30 final class HelperContainerExtension implements Extension
31 {
32     /*
33      * Available extension points
34      */
35     const HELPER_CONTAINER_TAG = 'helper_container.container';
36
37     /**
38      * @var ServiceProcessor
39      */
40     private $processor;
41
42     /**
43      * Initializes compiler pass.
44      *
45      * @param null|ServiceProcessor $processor
46      */
47     public function __construct(ServiceProcessor $processor = null)
48     {
49         $this->processor = $processor ? : new ServiceProcessor();
50     }
51
52     /**
53      * {@inheritdoc}
54      */
55     public function getConfigKey()
56     {
57         return 'helper_container';
58     }
59
60     /**
61      * {@inheritdoc}
62      */
63     public function initialize(ExtensionManager $extensionManager)
64     {
65     }
66
67     /**
68      * {@inheritdoc}
69      */
70     public function configure(ArrayNodeDefinition $builder)
71     {
72     }
73
74     /**
75      * {@inheritdoc}
76      */
77     public function load(ContainerBuilder $container, array $config)
78     {
79         $definition = new Definition('Behat\Behat\HelperContainer\Argument\ServicesResolverFactory', array($container));
80         $definition->addTag(ContextExtension::SUITE_SCOPED_RESOLVER_FACTORY_TAG, array('priority' => 0));
81         $container->setDefinition(ContextExtension::SUITE_SCOPED_RESOLVER_FACTORY_TAG . '.helper_container', $definition);
82
83         $definition = new Definition('Behat\Behat\HelperContainer\Call\Filter\ServicesResolver');
84         $definition->addTag(CallExtension::CALL_FILTER_TAG, array('priority' => 0));
85         $container->setDefinition(CallExtension::CALL_FILTER_TAG . '.helper_container', $definition);
86     }
87
88     /**
89      * {@inheritdoc}
90      */
91     public function process(ContainerBuilder $container)
92     {
93         $references = $this->processor->findAndSortTaggedServices($container, self::HELPER_CONTAINER_TAG);
94
95         foreach ($references as $reference) {
96             if ($this->isDefinitionShared($container->getDefinition((string) $reference))) {
97                 throw new WrongServicesConfigurationException(sprintf(
98                     'Container services must not be configured as shared, but `@%s` is.', $reference
99                 ));
100             }
101         }
102     }
103
104     /**
105      * Checks if provided definition is shared.
106      *
107      * @param Definition $definition
108      *
109      * @return bool
110      *
111      * @todo Remove after upgrading to Symfony 2.8+
112      */
113     private function isDefinitionShared(Definition $definition)
114     {
115         if (method_exists($definition, 'isShared')) {
116             return $definition->isShared();
117         } else if (method_exists($definition, 'getScope')) {
118             return $definition->getScope() !== ContainerBuilder::SCOPE_PROTOTYPE;
119         }
120
121         return false;
122     }
123 }