Security update for permissions_by_term
[yaffs-website] / vendor / behat / behat / src / Behat / Testwork / Output / OutputManager.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;
12
13 use Behat\Testwork\Output\Exception\FormatterNotFoundException;
14 use Symfony\Component\EventDispatcher\EventDispatcherInterface;
15
16 /**
17  * Manages formatters and their configuration.
18  *
19  * @author Konstantin Kudryashov <ever.zet@gmail.com>
20  */
21 final class OutputManager
22 {
23     /**
24      * @var EventDispatcherInterface
25      */
26     private $eventDispatcher;
27     /**
28      * @var Formatter[]
29      */
30     private $formatters = array();
31
32     /**
33      * Initializes manager.
34      *
35      * @param EventDispatcherInterface $eventDispatcher
36      */
37     public function __construct(EventDispatcherInterface $eventDispatcher)
38     {
39         $this->eventDispatcher = $eventDispatcher;
40     }
41
42     /**
43      * Registers formatter.
44      *
45      * @param Formatter $formatter
46      */
47     public function registerFormatter(Formatter $formatter)
48     {
49         if (isset($this->formatters[$formatter->getName()])) {
50             $this->disableFormatter($formatter->getName());
51         }
52
53         $this->formatters[$formatter->getName()] = $formatter;
54     }
55
56     /**
57      * Checks if formatter is registered.
58      *
59      * @param string $name
60      *
61      * @return Boolean
62      */
63     public function isFormatterRegistered($name)
64     {
65         return isset($this->formatters[$name]);
66     }
67
68     /**
69      * Returns formatter by name provided.
70      *
71      * @param string $name
72      *
73      * @return Formatter
74      *
75      * @throws FormatterNotFoundException
76      */
77     public function getFormatter($name)
78     {
79         if (!$this->isFormatterRegistered($name)) {
80             throw new FormatterNotFoundException(sprintf(
81                 '`%s` formatter is not found or has not been properly registered. Registered formatters: `%s`.',
82                 $name,
83                 implode('`, `', array_keys($this->formatters))
84             ), $name);
85         }
86
87         return $this->formatters[$name];
88     }
89
90     /**
91      * Returns all registered formatters.
92      *
93      * @return Formatter[]
94      */
95     public function getFormatters()
96     {
97         return $this->formatters;
98     }
99
100     /**
101      * Enable formatter by name provided.
102      *
103      * @param string $formatter
104      */
105     public function enableFormatter($formatter)
106     {
107         if (!$this->isFormatterRegistered($formatter) && class_exists($formatter)) {
108             $formatterInstance = new $formatter();
109             $formatter = $formatterInstance->getName();
110
111             if (!$this->isFormatterRegistered($formatter)) {
112                 $this->registerFormatter($formatterInstance);
113             }
114         }
115
116         $this->eventDispatcher->addSubscriber($this->getFormatter($formatter));
117     }
118
119     /**
120      * Disable formatter by name provided.
121      *
122      * @param string $formatter
123      */
124     public function disableFormatter($formatter)
125     {
126         $this->eventDispatcher->removeSubscriber($this->getFormatter($formatter));
127     }
128
129     /**
130      * Disable all registered formatters.
131      */
132     public function disableAllFormatters()
133     {
134         array_map(array($this, 'disableFormatter'), array_keys($this->formatters));
135     }
136
137     /**
138      * Sets provided parameter to said formatter.
139      *
140      * @param string $formatter
141      * @param string $parameterName
142      * @param mixed  $parameterValue
143      */
144     public function setFormatterParameter($formatter, $parameterName, $parameterValue)
145     {
146         $formatter = $this->getFormatter($formatter);
147         $printer = $formatter->getOutputPrinter();
148
149         switch ($parameterName) {
150             case 'output_verbosity':
151                 $printer->setOutputVerbosity($parameterValue);
152
153                 return;
154             case 'output_path':
155                 $printer->setOutputPath($parameterValue);
156
157                 return;
158             case 'output_decorate':
159                 $printer->setOutputDecorated($parameterValue);
160
161                 return;
162             case 'output_styles':
163                 $printer->setOutputStyles($parameterValue);
164
165                 return;
166         }
167
168         $formatter->setParameter($parameterName, $parameterValue);
169     }
170
171     /**
172      * Sets provided formatter parameters.
173      *
174      * @param string $formatter
175      * @param array  $parameters
176      */
177     public function setFormatterParameters($formatter, array $parameters)
178     {
179         foreach ($parameters as $key => $val) {
180             $this->setFormatterParameter($formatter, $key, $val);
181         }
182     }
183
184     /**
185      * Sets provided parameter to all registered formatters.
186      *
187      * @param string $parameterName
188      * @param mixed  $parameterValue
189      */
190     public function setFormattersParameter($parameterName, $parameterValue)
191     {
192         foreach (array_keys($this->formatters) as $formatter) {
193             $this->setFormatterParameter($formatter, $parameterName, $parameterValue);
194         }
195     }
196
197     /**
198      * Sets provided parameters to all registered formatters.
199      *
200      * @param array $parameters
201      */
202     public function setFormattersParameters(array $parameters)
203     {
204         foreach ($parameters as $key => $val) {
205             $this->setFormattersParameter($key, $val);
206         }
207     }
208 }