From bc3dae9c483c331872a5822a3820e1460c7c6e09 Mon Sep 17 00:00:00 2001 From: Oliver Davies Date: Wed, 10 Jan 2018 17:39:00 +0000 Subject: [PATCH 1/6] Start using Symfony Components --- bin/generate-filters | 12 +------ bin/generate-filters.php | 12 +++++++ composer.json | 18 ++++++++-- config/services.yml | 10 ++++++ src/Console/Command/GenerateCommand.php | 46 +++++++++++++++++++++++++ src/Container/CommandCompilerClass.php | 22 ++++++++++++ src/Container/Container.php | 24 +++++++++++++ src/{ => Model}/Filter.php | 0 src/{ => Service}/Builder.php | 0 9 files changed, 131 insertions(+), 13 deletions(-) create mode 100644 bin/generate-filters.php create mode 100644 config/services.yml create mode 100644 src/Console/Command/GenerateCommand.php create mode 100644 src/Container/CommandCompilerClass.php create mode 100644 src/Container/Container.php rename src/{ => Model}/Filter.php (100%) rename src/{ => Service}/Builder.php (100%) diff --git a/bin/generate-filters b/bin/generate-filters index 8215e67..cdb2755 100755 --- a/bin/generate-filters +++ b/bin/generate-filters @@ -1,14 +1,4 @@ #!/usr/bin/env php get('app.cli'); +$application->run(); diff --git a/composer.json b/composer.json index 04bc605..f6e6514 100644 --- a/composer.json +++ b/composer.json @@ -10,20 +10,34 @@ } ], "require": { + "symfony/config": "^3.4", + "symfony/console": "^3.4", + "symfony/dependency-injection": "^3.4", + "symfony/filesystem": "^3.4", + "symfony/yaml": "^3.4", "tightenco/collect": "^5.4" }, "require-dev": { - "phpunit/phpunit": "^5.7" + "phpunit/phpunit": "^5.7", + "symfony/var-dumper": "^3.4" }, "autoload": { "psr-4": { "Opdavies\\GmailFilterBuilder\\": "src/" } }, + "autoload-dev": { + "psr-4": { + "Tests\\Opdavies\\GmailFilterBuilder\\": "tests/" + } + }, "extra": { "branch-alias": { "dev-master": "1.1-dev" } }, - "bin": ["bin/generate-filters"] + "bin": ["bin/generate-filters"], + "config": { + "sort-packages": true + } } diff --git a/config/services.yml b/config/services.yml new file mode 100644 index 0000000..234a02f --- /dev/null +++ b/config/services.yml @@ -0,0 +1,10 @@ +services: + app.cli: + class: Symfony\Component\Console\Application + autowire: true + + app.generator.command: + class: Opdavies\GmailFilterBuilder\Console\Command\GenerateCommand + autowire: true + tags: + - { name: ConsoleCommand } diff --git a/src/Console/Command/GenerateCommand.php b/src/Console/Command/GenerateCommand.php new file mode 100644 index 0000000..194ace6 --- /dev/null +++ b/src/Console/Command/GenerateCommand.php @@ -0,0 +1,46 @@ +setName('generate') + ->setDefinition([ + new InputArgument('input-file', InputArgument::OPTIONAL, 'The name of the PHP file containing your filters.', 'filters.php'), + new InputArgument('output-file', InputArgument::OPTIONAL, 'The name of the XML file to generate.', 'filters.xml') + ]) + ->setDescription('Generates XML for Gmail filters.') + ; + } + + /** + * {@inheritdoc} + */ + protected function execute(InputInterface $input, OutputInterface $output) + { + $inputFile = $input->getArgument('input-file'); + $outputFile = $input->getArgument('output-file'); + + if (file_exists(__DIR__.'/../../../../'.$inputFile)) { + $filters = require(__DIR__.'/../../../../'.$inputFile); + } elseif (file_exists(__DIR__.'/../../../'.$inputFile)) { + $filters = require(__DIR__.'/../../../'.$inputFile); + } else { + throw new \Exception('No filters.php file found.'); + } + + echo new Builder($filters); + } +} diff --git a/src/Container/CommandCompilerClass.php b/src/Container/CommandCompilerClass.php new file mode 100644 index 0000000..2bf07b6 --- /dev/null +++ b/src/Container/CommandCompilerClass.php @@ -0,0 +1,22 @@ +findDefinition('app.cli'); + $taggedServices = $container->findTaggedServiceIds('ConsoleCommand'); + + foreach ($taggedServices as $id => $tags) { + $definition->addMethodCall('add', [new Reference($id)]); + } + } +} diff --git a/src/Container/Container.php b/src/Container/Container.php new file mode 100644 index 0000000..905a9e4 --- /dev/null +++ b/src/Container/Container.php @@ -0,0 +1,24 @@ +containerBuilder = new ContainerBuilder(); + $loader = new YamlFileLoader($this->containerBuilder, new FileLocator(__DIR__.'/../../config')); + $loader->load('services.yml'); + $this->containerBuilder->addCompilerPass(new CommandCompilerClass()); + $this->containerBuilder->compile(); + } + + public function get($className) + { + return $this->containerBuilder->get($className); + } +} diff --git a/src/Filter.php b/src/Model/Filter.php similarity index 100% rename from src/Filter.php rename to src/Model/Filter.php diff --git a/src/Builder.php b/src/Service/Builder.php similarity index 100% rename from src/Builder.php rename to src/Service/Builder.php From fc629ead569114194163e7cd4fa2108eddeda2d5 Mon Sep 17 00:00:00 2001 From: Oliver Davies Date: Wed, 10 Jan 2018 17:39:50 +0000 Subject: [PATCH 2/6] Re-organise classes --- src/Builder.php | 12 ++++++++++++ src/Filter.php | 12 ++++++++++++ src/Model/Filter.php | 2 +- src/Service/Builder.php | 4 +++- 4 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 src/Builder.php create mode 100644 src/Filter.php diff --git a/src/Builder.php b/src/Builder.php new file mode 100644 index 0000000..a5cd6d7 --- /dev/null +++ b/src/Builder.php @@ -0,0 +1,12 @@ + Date: Wed, 10 Jan 2018 23:52:22 +0000 Subject: [PATCH 3/6] Set default command --- bin/generate-filters.php | 2 ++ config/services.yml | 2 +- src/Console/Command/GenerateCommand.php | 4 +++- 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/bin/generate-filters.php b/bin/generate-filters.php index bcb8964..00f7171 100644 --- a/bin/generate-filters.php +++ b/bin/generate-filters.php @@ -1,5 +1,6 @@ get('app.cli'); +$application->setDefaultCommand(GenerateCommand::NAME); $application->run(); diff --git a/config/services.yml b/config/services.yml index 234a02f..afd00b9 100644 --- a/config/services.yml +++ b/config/services.yml @@ -3,7 +3,7 @@ services: class: Symfony\Component\Console\Application autowire: true - app.generator.command: + app.generate.command: class: Opdavies\GmailFilterBuilder\Console\Command\GenerateCommand autowire: true tags: diff --git a/src/Console/Command/GenerateCommand.php b/src/Console/Command/GenerateCommand.php index 194ace6..70dea4e 100644 --- a/src/Console/Command/GenerateCommand.php +++ b/src/Console/Command/GenerateCommand.php @@ -10,13 +10,15 @@ use Symfony\Component\Console\Output\OutputInterface; class GenerateCommand extends Command { + const NAME = 'generate'; + /** * {@inheritdoc} */ public function configure() { $this - ->setName('generate') + ->setName(self::NAME) ->setDefinition([ new InputArgument('input-file', InputArgument::OPTIONAL, 'The name of the PHP file containing your filters.', 'filters.php'), new InputArgument('output-file', InputArgument::OPTIONAL, 'The name of the XML file to generate.', 'filters.xml') From 94106095889c4ca5a20e1fd4495c2c484c43a64c Mon Sep 17 00:00:00 2001 From: Oliver Davies Date: Thu, 11 Jan 2018 00:48:02 +0000 Subject: [PATCH 4/6] Dump file, add output --- config/services.yml | 3 +++ src/Console/Command/GenerateCommand.php | 7 ++++++- src/Service/Builder.php | 19 ++++++++++++++++--- 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/config/services.yml b/config/services.yml index afd00b9..75f8706 100644 --- a/config/services.yml +++ b/config/services.yml @@ -3,6 +3,9 @@ services: class: Symfony\Component\Console\Application autowire: true + app.builder: + class: Opdavies\GmailFilterBuilder\Service\Builder + app.generate.command: class: Opdavies\GmailFilterBuilder\Console\Command\GenerateCommand autowire: true diff --git a/src/Console/Command/GenerateCommand.php b/src/Console/Command/GenerateCommand.php index 70dea4e..05c256e 100644 --- a/src/Console/Command/GenerateCommand.php +++ b/src/Console/Command/GenerateCommand.php @@ -7,6 +7,7 @@ use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\Console\Style\SymfonyStyle; class GenerateCommand extends Command { @@ -43,6 +44,10 @@ class GenerateCommand extends Command throw new \Exception('No filters.php file found.'); } - echo new Builder($filters); + // TOOD: Check return code. + new Builder($filters, $outputFile); + + $io = new SymfonyStyle($input, $output); + $io->success(sprintf('%s file generated.', $outputFile)); } } diff --git a/src/Service/Builder.php b/src/Service/Builder.php index 9ee9e3d..f8cf70f 100644 --- a/src/Service/Builder.php +++ b/src/Service/Builder.php @@ -3,17 +3,28 @@ namespace Opdavies\GmailFilterBuilder\Service; use Opdavies\GmailFilterBuilder\Filter; +use Symfony\Component\Filesystem\Filesystem; class Builder { + private $filesystem; + /** * @var array */ private $filters = []; - public function __construct(array $filters) - { + /** + * @var + */ + private $outputFile; + + public function __construct(array $filters, $outputFile = 'filters.xml') { + $this->filesystem = new Filesystem(); $this->filters = $filters; + $this->outputFile = $outputFile; + + $this->build(); } public function __toString() @@ -35,7 +46,9 @@ class Builder return $this->buildEntry($items); })->implode(PHP_EOL); - return collect([$prefix, $xml, $suffix])->implode(PHP_EOL); + $content = collect([$prefix, $xml, $suffix])->implode(PHP_EOL); + + $this->filesystem->dumpFile($this->outputFile, $content); } /** From 6d6f5596cee2b8c2786880d31913cb7d445bde4d Mon Sep 17 00:00:00 2001 From: Oliver Davies Date: Thu, 11 Jan 2018 09:03:31 +0000 Subject: [PATCH 5/6] Change arguments to options --- src/Console/Command/GenerateCommand.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Console/Command/GenerateCommand.php b/src/Console/Command/GenerateCommand.php index 05c256e..6de81d7 100644 --- a/src/Console/Command/GenerateCommand.php +++ b/src/Console/Command/GenerateCommand.php @@ -4,8 +4,8 @@ namespace Opdavies\GmailFilterBuilder\Console\Command; use Opdavies\GmailFilterBuilder\Builder; use Symfony\Component\Console\Command\Command; -use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Style\SymfonyStyle; @@ -21,8 +21,8 @@ class GenerateCommand extends Command $this ->setName(self::NAME) ->setDefinition([ - new InputArgument('input-file', InputArgument::OPTIONAL, 'The name of the PHP file containing your filters.', 'filters.php'), - new InputArgument('output-file', InputArgument::OPTIONAL, 'The name of the XML file to generate.', 'filters.xml') + new InputOption('input-file', null, InputOption::VALUE_OPTIONAL, 'The name of the PHP file containing your filters.', 'filters.php'), + new InputOption('output-file', null, InputOption::VALUE_OPTIONAL, 'The name of the XML file to generate.', 'filters.xml') ]) ->setDescription('Generates XML for Gmail filters.') ; @@ -33,8 +33,8 @@ class GenerateCommand extends Command */ protected function execute(InputInterface $input, OutputInterface $output) { - $inputFile = $input->getArgument('input-file'); - $outputFile = $input->getArgument('output-file'); + $inputFile = $input->getOption('input-file'); + $outputFile = $input->getOption('output-file'); if (file_exists(__DIR__.'/../../../../'.$inputFile)) { $filters = require(__DIR__.'/../../../../'.$inputFile); From b9b41aa437b0dd76d73fbcce20a46c835897ddba Mon Sep 17 00:00:00 2001 From: Oliver Davies Date: Mon, 15 Jan 2018 09:24:38 +0000 Subject: [PATCH 6/6] Fix output for tests to pass again --- src/Console/Command/GenerateCommand.php | 13 +++++++---- src/Service/Builder.php | 29 ++++++++++++++++++++++--- tests/Unit/BuilderTest.php | 4 ++-- 3 files changed, 37 insertions(+), 9 deletions(-) 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()); } }