Security update for permissions_by_term
[yaffs-website] / vendor / behat / behat / src / Behat / Testwork / Output / Printer / Factory / ConsoleOutputFactory.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\Testwork\Output\Printer\Factory;
12
13 use Behat\Testwork\Output\Exception\BadOutputPathException;
14 use Symfony\Component\Console\Formatter\OutputFormatter;
15 use Symfony\Component\Console\Formatter\OutputFormatterStyle;
16 use Symfony\Component\Console\Output\OutputInterface;
17 use Symfony\Component\Console\Output\StreamOutput;
18
19 /**
20  * Creates an output stream for the console.
21  *
22  * @author Wouter J <wouter@wouterj.nl>
23  * @author Konstantin Kudryashov <ever.zet@gmail.com>
24  */
25 class ConsoleOutputFactory extends OutputFactory
26 {
27     /**
28      * Creates output formatter that is used to create a stream.
29      *
30      * @return OutputFormatter
31      */
32     protected function createOutputFormatter()
33     {
34         return new OutputFormatter();
35     }
36
37     /**
38      * Configure output stream parameters.
39      *
40      * @param OutputInterface $output
41      */
42     protected function configureOutputStream(OutputInterface $output)
43     {
44         $verbosity = $this->getOutputVerbosity() ? OutputInterface::VERBOSITY_VERBOSE : OutputInterface::VERBOSITY_NORMAL;
45         $output->setVerbosity($verbosity);
46
47         if (null !== $this->isOutputDecorated()) {
48             $output->getFormatter()->setDecorated($this->isOutputDecorated());
49         }
50     }
51
52     /**
53      * Returns new output stream.
54      *
55      * Override this method & call flush() to write output in another stream
56      *
57      * @return resource
58      *
59      * @throws BadOutputPathException
60      */
61     protected function createOutputStream()
62     {
63         if (null === $this->getOutputPath()) {
64             $stream = fopen('php://stdout', 'w');
65         } elseif (!is_dir($this->getOutputPath())) {
66             $stream = fopen($this->getOutputPath(), 'w');
67         } else {
68             throw new BadOutputPathException(sprintf(
69                 'Filename expected as `output_path` parameter, but got `%s`.',
70                 $this->getOutputPath()
71             ), $this->getOutputPath());
72         }
73
74         return $stream;
75     }
76
77     /**
78      * {@inheritdoc}
79      */
80     public function createOutput($stream = null)
81     {
82         $stream = $stream ? : $this->createOutputStream();
83         $format = $this->createOutputFormatter();
84
85         // set user-defined styles
86         foreach ($this->getOutputStyles() as $name => $options) {
87             $style = new OutputFormatterStyle();
88
89             if (isset($options[0])) {
90                 $style->setForeground($options[0]);
91             }
92             if (isset($options[1])) {
93                 $style->setBackground($options[1]);
94             }
95             if (isset($options[2])) {
96                 $style->setOptions($options[2]);
97             }
98
99             $format->setStyle($name, $style);
100         }
101
102         $output = new StreamOutput(
103             $stream,
104             StreamOutput::VERBOSITY_NORMAL,
105             $this->isOutputDecorated(),
106             $format
107         );
108         $this->configureOutputStream($output);
109
110         return $output;
111     }
112 }