4 * This file is part of the Behat.
5 * (c) Konstantin Kudryashov <ever.zet@gmail.com>
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
11 namespace Behat\Testwork\Output\Printer\Factory;
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;
20 * Creates an output stream for the console.
22 * @author Wouter J <wouter@wouterj.nl>
23 * @author Konstantin Kudryashov <ever.zet@gmail.com>
25 class ConsoleOutputFactory extends OutputFactory
28 * Creates output formatter that is used to create a stream.
30 * @return OutputFormatter
32 protected function createOutputFormatter()
34 return new OutputFormatter();
38 * Configure output stream parameters.
40 * @param OutputInterface $output
42 protected function configureOutputStream(OutputInterface $output)
44 $verbosity = $this->getOutputVerbosity() ? OutputInterface::VERBOSITY_VERBOSE : OutputInterface::VERBOSITY_NORMAL;
45 $output->setVerbosity($verbosity);
47 if (null !== $this->isOutputDecorated()) {
48 $output->getFormatter()->setDecorated($this->isOutputDecorated());
53 * Returns new output stream.
55 * Override this method & call flush() to write output in another stream
59 * @throws BadOutputPathException
61 protected function createOutputStream()
63 if (null === $this->getOutputPath()) {
64 $stream = fopen('php://stdout', 'w');
65 } elseif (!is_dir($this->getOutputPath())) {
66 $stream = fopen($this->getOutputPath(), 'w');
68 throw new BadOutputPathException(sprintf(
69 'Filename expected as `output_path` parameter, but got `%s`.',
70 $this->getOutputPath()
71 ), $this->getOutputPath());
80 public function createOutput($stream = null)
82 $stream = $stream ? : $this->createOutputStream();
83 $format = $this->createOutputFormatter();
85 // set user-defined styles
86 foreach ($this->getOutputStyles() as $name => $options) {
87 $style = new OutputFormatterStyle();
89 if (isset($options[0])) {
90 $style->setForeground($options[0]);
92 if (isset($options[1])) {
93 $style->setBackground($options[1]);
95 if (isset($options[2])) {
96 $style->setOptions($options[2]);
99 $format->setStyle($name, $style);
102 $output = new StreamOutput(
104 StreamOutput::VERBOSITY_NORMAL,
105 $this->isOutputDecorated(),
108 $this->configureOutputStream($output);