From bc3dae9c483c331872a5822a3820e1460c7c6e09 Mon Sep 17 00:00:00 2001 From: Oliver Davies Date: Wed, 10 Jan 2018 17:39:00 +0000 Subject: [PATCH] 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