-
Notifications
You must be signed in to change notification settings - Fork 43
/
TagBasedMapping.php
50 lines (40 loc) · 1.71 KB
/
TagBasedMapping.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<?php
declare(strict_types=1);
namespace League\Tactician\Bundle\DependencyInjection\HandlerMapping;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
abstract class TagBasedMapping implements HandlerMapping
{
const TAG_NAME = 'tactician.handler';
public function build(ContainerBuilder $container, Routing $routing): Routing
{
foreach ($container->findTaggedServiceIds(self::TAG_NAME) as $serviceId => $tags) {
foreach ($tags as $attributes) {
$this->mapServiceByTag($container, $routing, $serviceId, $attributes);
}
}
return $routing;
}
/**
* @param ContainerBuilder $container
* @param Routing $routing
* @param $serviceId
* @param $attributes
*/
private function mapServiceByTag(ContainerBuilder $container, Routing $routing, $serviceId, $attributes)
{
$definition = $container->getDefinition($serviceId);
if (!$this->isSupported($container, $definition, $attributes)) {
return;
}
foreach ($this->findCommandsForService($container, $definition, $attributes) as $commandClassName) {
if (isset($attributes['bus'])) {
$routing->routeToBus($attributes['bus'], $commandClassName, $serviceId);
} else {
$routing->routeToAllBuses($commandClassName, $serviceId);
}
}
}
abstract protected function isSupported(ContainerBuilder $container, Definition $definition, array $tagAttributes): bool;
abstract protected function findCommandsForService(ContainerBuilder $container, Definition $definition, array $tagAttributes): array;
}