Security update for permissions_by_term
[yaffs-website] / vendor / behat / behat / src / Behat / Testwork / Autoloader / ServiceContainer / AutoloaderExtension.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\Autoloader\ServiceContainer;
12
13 use Behat\Testwork\Cli\ServiceContainer\CliExtension;
14 use Behat\Testwork\ServiceContainer\Extension;
15 use Behat\Testwork\ServiceContainer\ExtensionManager;
16 use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
17 use Symfony\Component\DependencyInjection\ContainerBuilder;
18 use Symfony\Component\DependencyInjection\Definition;
19 use Symfony\Component\DependencyInjection\Reference;
20
21 /**
22  * Extends Testwork with class-loading services.
23  *
24  * @author Konstantin Kudryashov <ever.zet@gmail.com>
25  */
26 final class AutoloaderExtension implements Extension
27 {
28     /*
29      * Available services
30      */
31     const CLASS_LOADER_ID = 'class_loader';
32
33     /**
34      * @var array
35      */
36     private $defaultPaths = array();
37
38     /**
39      * Initializes extension.
40      *
41      * @param array $defaultPaths
42      */
43     public function __construct(array $defaultPaths = array())
44     {
45         $this->defaultPaths = $defaultPaths;
46     }
47
48     /**
49      * Returns the extension config key.
50      *
51      * @return string
52      */
53     public function getConfigKey()
54     {
55         return 'autoload';
56     }
57
58     /**
59      * {@inheritdoc}
60      */
61     public function initialize(ExtensionManager $extensionManager)
62     {
63     }
64
65     /**
66      * {@inheritdoc}
67      */
68     public function configure(ArrayNodeDefinition $builder)
69     {
70         $builder
71             ->beforeNormalization()
72                 ->ifString()->then(function ($path) {
73                     return array('' => $path);
74                 })
75             ->end()
76
77             ->defaultValue($this->defaultPaths)
78             ->treatTrueLike($this->defaultPaths)
79             ->treatNullLike(array())
80             ->treatFalseLike(array())
81
82             ->prototype('scalar')->end()
83         ;
84     }
85
86     /**
87      * {@inheritdoc}
88      */
89     public function load(ContainerBuilder $container, array $config)
90     {
91         $this->loadAutoloader($container);
92         $this->loadController($container);
93         $this->setLoaderPrefixes($container, $config);
94     }
95
96     /**
97      * {@inheritdoc}
98      */
99     public function process(ContainerBuilder $container)
100     {
101         $this->processLoaderPrefixes($container);
102     }
103
104     /**
105      * Loads Symfony2 autoloader.
106      *
107      * @param ContainerBuilder $container
108      */
109     private function loadAutoloader(ContainerBuilder $container)
110     {
111         $definition = new Definition('Symfony\Component\ClassLoader\ClassLoader');
112         $container->setDefinition(self::CLASS_LOADER_ID, $definition);
113     }
114
115     /**
116      * Loads controller.
117      *
118      * @param ContainerBuilder $container
119      */
120     private function loadController(ContainerBuilder $container)
121     {
122         $definition = new Definition('Behat\Testwork\Autoloader\Cli\AutoloaderController', array(
123             new Reference(self::CLASS_LOADER_ID)
124         ));
125         $definition->addTag(CliExtension::CONTROLLER_TAG, array('priority' => 9999));
126
127         $container->setDefinition(CliExtension::CONTROLLER_TAG . '.autoloader', $definition);
128     }
129
130     /**
131      * Sets provided prefixes to container.
132      *
133      * @param ContainerBuilder $container
134      * @param array            $prefixes
135      */
136     private function setLoaderPrefixes(ContainerBuilder $container, array $prefixes)
137     {
138         $container->setParameter('class_loader.prefixes', $prefixes);
139     }
140
141     /**
142      * Processes container loader prefixes.
143      *
144      * @param ContainerBuilder $container
145      */
146     private function processLoaderPrefixes(ContainerBuilder $container)
147     {
148         $loaderDefinition = $container->getDefinition(self::CLASS_LOADER_ID);
149         $loaderDefinition->addMethodCall('addPrefixes', array($container->getParameter('class_loader.prefixes')));
150     }
151 }