Security update for permissions_by_term
[yaffs-website] / vendor / behat / behat / src / Behat / Testwork / Output / ServiceContainer / OutputExtension.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\Output\ServiceContainer;
12
13 use Behat\Testwork\Cli\ServiceContainer\CliExtension;
14 use Behat\Testwork\EventDispatcher\ServiceContainer\EventDispatcherExtension;
15 use Behat\Testwork\Output\ServiceContainer\Formatter\FormatterFactory;
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 use Symfony\Component\DependencyInjection\Reference;
23
24 /**
25  * Provides output management services for testwork.
26  *
27  * @author Konstantin Kudryashov <ever.zet@gmail.com>
28  */
29 final class OutputExtension implements Extension
30 {
31     /*
32      * Available services
33      */
34     const MANAGER_ID = 'output.manager';
35
36     /*
37      * Available extension points
38      */
39     const FORMATTER_TAG = 'output.formatter';
40
41     /**
42      * @var string
43      */
44     private $defaultFormatter;
45     /**
46      * @var FormatterFactory[]
47      */
48     private $factories;
49     /**
50      * @var ServiceProcessor
51      */
52     private $processor;
53
54     /**
55      * Initializes extension.
56      *
57      * @param string                $defaultFormatter
58      * @param FormatterFactory[]    $formatterFactories
59      * @param null|ServiceProcessor $processor
60      */
61     public function __construct($defaultFormatter, array $formatterFactories, ServiceProcessor $processor = null)
62     {
63         $this->defaultFormatter = $defaultFormatter;
64         $this->factories = $formatterFactories;
65         $this->processor = $processor ? : new ServiceProcessor();
66     }
67
68     /**
69      * Registers formatter factory.
70      *
71      * @param FormatterFactory $factory
72      */
73     public function registerFormatterFactory(FormatterFactory $factory)
74     {
75         $this->factories[] = $factory;
76     }
77
78     /**
79      * {@inheritdoc}
80      */
81     public function getConfigKey()
82     {
83         return 'formatters';
84     }
85
86     /**
87      * {@inheritdoc}
88      */
89     public function initialize(ExtensionManager $extensionManager)
90     {
91     }
92
93     /**
94      * {@inheritdoc}
95      */
96     public function configure(ArrayNodeDefinition $builder)
97     {
98         $builder
99             ->defaultValue(array($this->defaultFormatter => array('enabled' => true)))
100             ->useAttributeAsKey('name')
101             ->prototype('array')
102                 ->beforeNormalization()
103                     ->ifTrue(function ($a) {
104                         return is_array($a) && !isset($a['enabled']);
105                     })
106                     ->then(function ($a) {
107                         return array_merge($a, array('enabled' => true));
108                     })
109                 ->end()
110                 ->useAttributeAsKey('name')
111                 ->treatTrueLike(array('enabled' => true))
112                 ->treatNullLike(array('enabled' => true))
113                 ->treatFalseLike(array('enabled' => false))
114                 ->prototype('variable')->end()
115             ->end()
116         ;
117     }
118
119     /**
120      * {@inheritdoc}
121      */
122     public function load(ContainerBuilder $container, array $config)
123     {
124         $this->loadOutputController($container);
125         $this->loadFormatters($container);
126         $this->loadManager($container, $config);
127     }
128
129     /**
130      * {@inheritdoc}
131      */
132     public function process(ContainerBuilder $container)
133     {
134         $this->processFormatters($container);
135         $this->processDynamicallyRegisteredFormatters($container);
136     }
137
138     /**
139      * Loads output controller.
140      *
141      * @param ContainerBuilder $container
142      */
143     private function loadOutputController(ContainerBuilder $container)
144     {
145         $definition = new Definition('Behat\Testwork\Output\Cli\OutputController', array(
146             new Reference(self::MANAGER_ID)
147         ));
148         $definition->addTag(CliExtension::CONTROLLER_TAG, array('priority' => 1000));
149         $container->setDefinition(CliExtension::CONTROLLER_TAG . '.output', $definition);
150     }
151
152     /**
153      * Loads output manager.
154      *
155      * @param ContainerBuilder $container
156      * @param array            $formatters
157      */
158     private function loadManager(ContainerBuilder $container, array $formatters)
159     {
160         $definition = new Definition('Behat\Testwork\Output\OutputManager', array(
161             new Reference(EventDispatcherExtension::DISPATCHER_ID)
162         ));
163
164         foreach ($formatters as $name => $parameters) {
165             if ($parameters['enabled']) {
166                 $definition->addMethodCall('enableFormatter', array($name));
167             } else {
168                 $definition->addMethodCall('disableFormatter', array($name));
169             }
170
171             unset($parameters['enabled']);
172             $definition->addMethodCall('setFormatterParameters', array($name, $parameters));
173         }
174
175         $container->setDefinition(self::MANAGER_ID, $definition);
176     }
177
178     /**
179      * Loads default formatters using registered factories.
180      *
181      * @param ContainerBuilder $container
182      */
183     private function loadFormatters(ContainerBuilder $container)
184     {
185         foreach ($this->factories as $factory) {
186             $factory->buildFormatter($container);
187         }
188     }
189
190     /**
191      * Processes formatters using registered factories.
192      *
193      * @param ContainerBuilder $container
194      */
195     private function processFormatters(ContainerBuilder $container)
196     {
197         foreach ($this->factories as $factory) {
198             $factory->processFormatter($container);
199         }
200     }
201
202     /**
203      * Processes all available output formatters.
204      *
205      * @param ContainerBuilder $container
206      */
207     private function processDynamicallyRegisteredFormatters(ContainerBuilder $container)
208     {
209         $references = $this->processor->findAndSortTaggedServices($container, self::FORMATTER_TAG);
210         $definition = $container->getDefinition(self::MANAGER_ID);
211
212         $previousCalls = $definition->getMethodCalls();
213         $definition->setMethodCalls();
214
215         foreach ($references as $reference) {
216             $definition->addMethodCall('registerFormatter', array($reference));
217         }
218
219         foreach ($previousCalls as $call) {
220             $definition->addMethodCall($call[0], $call[1]);
221         }
222     }
223 }