Start using Symfony Components

This commit is contained in:
Oliver Davies 2018-01-10 17:39:00 +00:00
parent b61178b9b0
commit bc3dae9c48
9 changed files with 131 additions and 13 deletions

View file

@ -1,14 +1,4 @@
#!/usr/bin/env php
<?php
use Opdavies\GmailFilterBuilder\Builder;
if (file_exists(__DIR__.'/../../../../filters.php')) {
$filters = require(__DIR__.'/../../../../filters.php');
} elseif (file_exists(__DIR__.'/../filters.php')) {
$filters = require(__DIR__.'/../filters.php');
} else {
throw new \Exception('No filters.php file found.');
}
echo new Builder($filters);
require_once __DIR__.'/generate-filters.php';

12
bin/generate-filters.php Normal file
View file

@ -0,0 +1,12 @@
<?php
use Opdavies\GmailFilterBuilder\Container\Container;
use Symfony\Component\Console\Application;
require_once __DIR__.'/../vendor/autoload.php';
$container = new Container();
/** @var Application $application */
$application = $container->get('app.cli');
$application->run();

View file

@ -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
}
}

10
config/services.yml Normal file
View file

@ -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 }

View file

@ -0,0 +1,46 @@
<?php
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\Output\OutputInterface;
class GenerateCommand extends Command
{
/**
* {@inheritdoc}
*/
public function configure()
{
$this
->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);
}
}

View file

@ -0,0 +1,22 @@
<?php
namespace Opdavies\GmailFilterBuilder\Container;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
class CommandCompilerClass implements CompilerPassInterface
{
/**
* {@inheritdoc}
*/
public function process(ContainerBuilder $container) {
$definition = $container->findDefinition('app.cli');
$taggedServices = $container->findTaggedServiceIds('ConsoleCommand');
foreach ($taggedServices as $id => $tags) {
$definition->addMethodCall('add', [new Reference($id)]);
}
}
}

View file

@ -0,0 +1,24 @@
<?php
namespace Opdavies\GmailFilterBuilder\Container;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
class Container
{
public function __construct()
{
$this->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);
}
}