Skip to content

Commit d83d85a

Browse files
refactored complete bundle architecture
1 parent bc97696 commit d83d85a

File tree

10 files changed

+49
-27
lines changed

10 files changed

+49
-27
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
/vendor/
22
composer.lock
33
composer.phar
4+
5+
*.iml
6+
/.idea

src/Command/RunCommand.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use Symfony\Component\Console\Command\Command;
66
use Symfony\Component\Console\Input\InputInterface;
77
use Symfony\Component\Console\Output\OutputInterface;
8-
use Task\TaskRunner\TaskRunnerInterface;
8+
use Task\SchedulerInterface;
99

1010
/**
1111
* Run pending tasks.
@@ -15,15 +15,15 @@
1515
class RunCommand extends Command
1616
{
1717
/**
18-
* @var TaskRunnerInterface
18+
* @var SchedulerInterface
1919
*/
20-
private $taskRunner;
20+
private $scheduler;
2121

22-
public function __construct(TaskRunnerInterface $taskRunner)
22+
public function __construct(SchedulerInterface $scheduler)
2323
{
2424
parent::__construct('task:run');
2525

26-
$this->taskRunner = $taskRunner;
26+
$this->scheduler = $scheduler;
2727
}
2828

2929
/**
@@ -39,6 +39,6 @@ protected function configure()
3939
*/
4040
protected function execute(InputInterface $input, OutputInterface $output)
4141
{
42-
$this->taskRunner->run();
42+
$this->scheduler->run();
4343
}
4444
}

src/Command/ScheduleTaskCommand.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
use Symfony\Component\Console\Input\InputArgument;
77
use Symfony\Component\Console\Input\InputInterface;
88
use Symfony\Component\Console\Output\OutputInterface;
9-
use Task\Scheduler\SchedulerInterface;
10-
use Task\Scheduler\Task;
9+
use Task\SchedulerInterface;
1110

1211
/**
1312
* Schedule task.
@@ -44,6 +43,8 @@ protected function configure()
4443
*/
4544
protected function execute(InputInterface $input, OutputInterface $output)
4645
{
47-
$this->scheduler->schedule(new Task($input->getArgument('workerName'), $input->getArgument('workload')));
46+
$this->scheduler
47+
->createTask($input->getArgument('workerName'), $input->getArgument('workload'))
48+
->schedule();
4849
}
4950
}

src/DependencyInjection/Configuration.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ public function getConfigTreeBuilder()
1919

2020
$treeBuilder->root('task')
2121
->children()
22-
->scalarNode('scheduler_service')->isRequired()->cannotBeEmpty()->end()
23-
->scalarNode('runner_service')->isRequired()->cannotBeEmpty()->end()
22+
->enumNode('storage')->values(['array', 'doctrine'])->defaultValue('array')->end()
2423
->end();
2524

2625
return $treeBuilder;

src/DependencyInjection/WorkerCompilerPass.php renamed to src/DependencyInjection/HandlerCompilerPass.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,29 +11,29 @@
1111
*
1212
* @author @wachterjohannes <[email protected]>
1313
*/
14-
class WorkerCompilerPass implements CompilerPassInterface
14+
class HandlerCompilerPass implements CompilerPassInterface
1515
{
16-
const TASK_RUNNER_ID = 'task.runner';
17-
const WORKER_TAG = 'task.worker';
18-
const ADD_FUNCTION_NAME = 'addWorker';
16+
const REGISTRY_ID = 'task.handler_registry';
17+
const HANDLER_TAG = 'task.handler';
18+
const ADD_FUNCTION_NAME = 'add';
1919

2020
/**
2121
* {@inheritdoc}
2222
*/
2323
public function process(ContainerBuilder $container)
2424
{
25-
if (!$container->has(self::TASK_RUNNER_ID)) {
25+
if (!$container->has(self::REGISTRY_ID)) {
2626
return;
2727
}
2828

29-
$definition = $container->findDefinition(self::TASK_RUNNER_ID);
29+
$definition = $container->findDefinition(self::REGISTRY_ID);
3030

31-
$taggedServices = $container->findTaggedServiceIds(self::WORKER_TAG);
31+
$taggedServices = $container->findTaggedServiceIds(self::HANDLER_TAG);
3232
foreach ($taggedServices as $id => $tags) {
3333
foreach ($tags as $attributes) {
3434
$definition->addMethodCall(
3535
self::ADD_FUNCTION_NAME,
36-
array($attributes['namespace'], $attributes['worker-name'], new Reference($id))
36+
[$attributes['name'], new Reference($id)]
3737
);
3838
}
3939
}

src/DependencyInjection/TaskExtension.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,8 @@ public function load(array $configs, ContainerBuilder $container)
2222
$configuration = new Configuration();
2323
$config = $this->processConfiguration($configuration, $configs);
2424

25-
$container->setParameter('task.scheduler_service', $config['scheduler_service']);
26-
$container->setParameter('task.runner_service', $config['runner_service']);
27-
2825
$loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
26+
$loader->load(sprintf('storage/%s'), $config['storage']);
2927
$loader->load('services.xml');
30-
31-
$container->setAlias('task.scheduler', $container->getParameter('task.scheduler_service'));
32-
$container->setAlias('task.runner', $container->getParameter('task.runner_service'));
3328
}
3429
}
File renamed without changes.

src/Resources/config/scheduler.xml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" ?>
2+
<container xmlns="http://symfony.com/schema/dic/services"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
5+
<services>
6+
<service id="task.handler_registry" class="Task\Handler\Registry"/>
7+
<service id="task.builder_factory" class="Task\TaskBuilderFactory"/>
8+
9+
<service id="task.scheduler" class="Task\Scheduler">
10+
<argument type="service" id="task.storage"/>
11+
<argument type="service" id="task.handler_registry"/>
12+
<argument type="service" id="task.builder_factory"/>
13+
<argument type="service" id="logger" on-invalid="ignore"/>
14+
</service>
15+
</services>
16+
</container>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" ?>
2+
<container xmlns="http://symfony.com/schema/dic/services"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
5+
<services>
6+
<service id="task.storage" class="Task\Storage\ArrayStorage"/>
7+
</services>
8+
</container>

src/TaskBundle.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use Symfony\Component\DependencyInjection\ContainerBuilder;
66
use Symfony\Component\HttpKernel\Bundle\Bundle;
7-
use Task\TaskBundle\DependencyInjection\WorkerCompilerPass;
7+
use Task\TaskBundle\DependencyInjection\HandlerCompilerPass;
88

99
/**
1010
* Integrates php-task into symfony.
@@ -17,6 +17,6 @@ public function build(ContainerBuilder $container)
1717
{
1818
parent::build($container);
1919

20-
$container->addCompilerPass(new WorkerCompilerPass());
20+
$container->addCompilerPass(new HandlerCompilerPass());
2121
}
2222
}

0 commit comments

Comments
 (0)