diff --git a/src/Console/Command/GenerateCommand.php b/src/Console/Command/GenerateCommand.php index 6de81d7..0ea5751 100644 --- a/src/Console/Command/GenerateCommand.php +++ b/src/Console/Command/GenerateCommand.php @@ -8,6 +8,7 @@ use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Style\SymfonyStyle; +use Symfony\Component\Filesystem\Exception\IOException; class GenerateCommand extends Command { @@ -44,10 +45,14 @@ class GenerateCommand extends Command throw new \Exception('No filters.php file found.'); } - // TOOD: Check return code. - new Builder($filters, $outputFile); - $io = new SymfonyStyle($input, $output); - $io->success(sprintf('%s file generated.', $outputFile)); + + try { + new Builder($filters, $outputFile); + + $io->success(sprintf('%s file generated.', $outputFile)); + } catch (IOException $e) { + $io->error($e->getMessage()); + } } } diff --git a/src/Service/Builder.php b/src/Service/Builder.php index f8cf70f..9acdbb7 100644 --- a/src/Service/Builder.php +++ b/src/Service/Builder.php @@ -19,10 +19,21 @@ class Builder */ private $outputFile; - public function __construct(array $filters, $outputFile = 'filters.xml') { + /** + * @var bool + */ + private $writeFile; + + /** + * @var string + */ + private $xml; + + public function __construct(array $filters, $outputFile = 'filters.xml', $writeFile = true) { $this->filesystem = new Filesystem(); $this->filters = $filters; $this->outputFile = $outputFile; + $this->writeFile = $writeFile; $this->build(); } @@ -32,6 +43,16 @@ class Builder return $this->build(); } + /** + * Returns the generated XML. + * + * @return string + */ + public function getXml() + { + return $this->xml; + } + /** * Build XML for a set of filters. * @@ -46,9 +67,11 @@ class Builder return $this->buildEntry($items); })->implode(PHP_EOL); - $content = collect([$prefix, $xml, $suffix])->implode(PHP_EOL); + $this->xml = collect([$prefix, $xml, $suffix])->implode(PHP_EOL); - $this->filesystem->dumpFile($this->outputFile, $content); + if ($this->writeFile) { + $this->filesystem->dumpFile($this->outputFile, $this->xml); + } } /** diff --git a/tests/Unit/BuilderTest.php b/tests/Unit/BuilderTest.php index 25f24d3..cf70229 100644 --- a/tests/Unit/BuilderTest.php +++ b/tests/Unit/BuilderTest.php @@ -18,7 +18,7 @@ class BuilderTest extends TestCase ->star() ->important(); - $result = new Builder([$filterA, $filterB]); + $result = new Builder([$filterA, $filterB], '', false); $expected = << @@ -36,6 +36,6 @@ class BuilderTest extends TestCase EOF; - $this->assertEquals($expected, $result->__toString()); + $this->assertEquals($expected, $result->getXml()); } }