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\Output\OutputInterface;
15 use Symfony\Component\Console\Output\StreamOutput;
18 * Creates an output stream for the filesystem.
20 * @author Wouter J <wouter@wouterj.nl>
22 class FilesystemOutputFactory extends OutputFactory
26 public function setFileName($fileName)
28 $this->fileName = $fileName;
32 * Configure output stream parameters.
34 * @param OutputInterface $output
36 protected function configureOutputStream(OutputInterface $output)
38 $verbosity = $this->getOutputVerbosity() ? OutputInterface::VERBOSITY_VERBOSE : OutputInterface::VERBOSITY_NORMAL;
39 $output->setVerbosity($verbosity);
45 public function createOutput($stream = null)
47 if (is_file($this->getOutputPath())) {
48 throw new BadOutputPathException(
49 'Directory expected for the `output_path` option, but a filename was given.',
50 $this->getOutputPath()
52 } elseif (!is_dir($this->getOutputPath())) {
53 mkdir($this->getOutputPath(), 0777, true);
56 if (null === $this->fileName) {
57 throw new \LogicException('Unable to create file, no file name specified');
60 $filePath = $this->getOutputPath().'/'.$this->fileName;
62 $stream = new StreamOutput(
63 fopen($filePath, 'w'),
64 StreamOutput::VERBOSITY_NORMAL,
65 false // a file is never decorated
67 $this->configureOutputStream($stream);