Skip to content

Commit d591d7c

Browse files
added collect tagged services
1 parent f8771b7 commit d591d7c

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace DependencyInjection;
4+
5+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
6+
use Symfony\Component\DependencyInjection\ContainerBuilder;
7+
use Symfony\Component\DependencyInjection\Reference;
8+
9+
/**
10+
* Compiler pass which collects worker services.
11+
*
12+
* @author @wachterjohannes <johannes.wachter@massiveart.com>
13+
*/
14+
class WorkerCompilerPass implements CompilerPassInterface
15+
{
16+
const TASK_RUNNER_ID = 'task.runner';
17+
const WORKER_TAG = 'task.worker';
18+
const ADD_FUNCTION_NAME = 'addWorker';
19+
20+
/**
21+
* {@inheritdoc}
22+
*/
23+
public function process(ContainerBuilder $container)
24+
{
25+
if (!$container->has(self::TASK_RUNNER_ID)) {
26+
return;
27+
}
28+
29+
$definition = $container->findDefinition(self::TASK_RUNNER_ID);
30+
31+
$taggedServices = $container->findTaggedServiceIds(self::WORKER_TAG);
32+
foreach ($taggedServices as $id => $tags) {
33+
$definition->addMethodCall(self::ADD_FUNCTION_NAME, array(new Reference($id)));
34+
}
35+
}
36+
}

src/TaskBundle.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Task\TaskBundle;
44

5+
use DependencyInjection\WorkerCompilerPass;
6+
use Symfony\Component\DependencyInjection\ContainerBuilder;
57
use Symfony\Component\HttpKernel\Bundle\Bundle;
68

79
/**
@@ -11,5 +13,11 @@
1113
*/
1214
class TaskBundle extends Bundle
1315
{
16+
public function build(ContainerBuilder $container)
17+
{
18+
parent::build($container);
19+
20+
$container->addCompilerPass(new WorkerCompilerPass());
21+
}
1422

1523
}

0 commit comments

Comments
 (0)