1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Symfony package. |
5
|
|
|
* |
6
|
|
|
* (c) Fabien Potencier <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Gendoria\CommandQueueBundle\DependencyInjection\Pass; |
13
|
|
|
|
14
|
|
|
use Gendoria\CommandQueue\CommandProcessor\CommandProcessorInterface; |
15
|
|
|
use InvalidArgumentException; |
16
|
|
|
use ReflectionClass; |
17
|
|
|
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
18
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
19
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Compiler pass to register tagged services for an event dispatcher. |
23
|
|
|
*/ |
24
|
|
|
class CommandProcessorPass implements CompilerPassInterface |
25
|
|
|
{ |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Command processor factory service ID. |
29
|
|
|
* |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
protected $processorFactoryService; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Processor listener tag name. |
36
|
|
|
* |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
protected $listenerTag; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Class constructor. |
43
|
|
|
* |
44
|
|
|
* @param string $serviceFactoryService Service name of the event dispatcher in processed container. |
45
|
|
|
* @param string $listenerTag Tag name used for listener. |
46
|
|
|
*/ |
47
|
13 |
|
public function __construct($serviceFactoryService = 'gendoria_command_queue.processor_factory', $listenerTag = 'gendoria_command_queue.processor') |
48
|
|
|
{ |
49
|
13 |
|
$this->processorFactoryService = $serviceFactoryService; |
50
|
13 |
|
$this->listenerTag = $listenerTag; |
51
|
13 |
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Process command processor tagged services and add to command processor factory. |
55
|
|
|
* |
56
|
|
|
* @param ContainerBuilder $container |
57
|
|
|
* |
58
|
|
|
* @throws InvalidArgumentException |
59
|
|
|
*/ |
60
|
10 |
|
public function process(ContainerBuilder $container) |
61
|
|
|
{ |
62
|
10 |
|
if (!$container->hasDefinition($this->processorFactoryService) && !$container->hasAlias($this->processorFactoryService)) { |
63
|
3 |
|
return; |
64
|
|
|
} |
65
|
|
|
|
66
|
7 |
|
$dispatcher = $container->findDefinition($this->processorFactoryService); |
67
|
|
|
|
68
|
7 |
|
foreach ($container->findTaggedServiceIds($this->listenerTag) as $id => $tags) { |
69
|
7 |
|
$def = $container->getDefinition($id); |
70
|
7 |
|
$this->assertProcessorDefinition($def, $id); |
71
|
4 |
|
foreach ($tags as $attributes) { |
72
|
4 |
|
if (empty($attributes['command'])) { |
73
|
1 |
|
throw new InvalidArgumentException(sprintf('The service "%s" is tagged as processor without specifying "command" attribute', $id)); |
74
|
|
|
} |
75
|
3 |
|
$dispatcher->addMethodCall('registerProcessorIdForCommand', array( |
76
|
3 |
|
$attributes['command'], |
77
|
3 |
|
$id, |
78
|
3 |
|
)); |
79
|
3 |
|
} |
80
|
3 |
|
} |
81
|
3 |
|
} |
82
|
|
|
|
83
|
7 |
|
private function assertProcessorDefinition(Definition $def, $id) |
84
|
|
|
{ |
85
|
7 |
|
if (!$def->isPublic()) { |
86
|
1 |
|
throw new InvalidArgumentException(sprintf('The service "%s" must be public as services are lazy-loaded.', $id)); |
87
|
|
|
} |
88
|
|
|
|
89
|
6 |
|
if ($def->isAbstract()) { |
90
|
1 |
|
throw new InvalidArgumentException(sprintf('The service "%s" must not be abstract as services are lazy-loaded.', $id)); |
91
|
|
|
} |
92
|
5 |
|
$refl = new ReflectionClass($def->getClass()); |
93
|
5 |
|
if (!$refl->implementsInterface(CommandProcessorInterface::class)) { |
94
|
1 |
|
throw new InvalidArgumentException(sprintf('The service "%s" has to implement ' . CommandProcessorInterface::class . '.', $id)); |
95
|
|
|
} |
96
|
4 |
|
} |
97
|
|
|
} |
98
|
|
|
|