-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathHandlerServiceLocatorCompilerPass.php
More file actions
106 lines (80 loc) · 3.65 KB
/
HandlerServiceLocatorCompilerPass.php
File metadata and controls
106 lines (80 loc) · 3.65 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?php
declare(strict_types=1);
namespace Patchlevel\EventSourcingBundle\DependencyInjection;
use Patchlevel\EventSourcing\Attribute\Inject;
use Patchlevel\EventSourcing\CommandBus\Handler\ServiceNotResolvable;
use Patchlevel\EventSourcing\CommandBus\HandlerFinder;
use Patchlevel\EventSourcingBundle\CommandBus\SymfonyParameterResolver;
use Psr\Container\ContainerInterface;
use ReflectionAttribute;
use ReflectionMethod;
use Symfony\Component\DependencyInjection\Argument\ServiceLocatorArgument;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\TypeInfo\Type\ObjectType;
use Symfony\Component\TypeInfo\TypeResolver\TypeResolver;
use function sprintf;
use function strtolower;
/** @internal */
final class HandlerServiceLocatorCompilerPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container): void
{
if (!$container->hasParameter('patchlevel_event_sourcing.aggregate_handlers.bus')) {
return;
}
foreach ($container->getParameter('event_sourcing.aggregates') as $aggregateName => $aggregateClass) {
$parameterResolverId = sprintf('.event_sourcing.handler_parameter_resolver.%s', $aggregateName);
$services = [];
foreach (HandlerFinder::findInClass($aggregateClass) as $aggregateHandler) {
$services += $this->services(new ReflectionMethod($aggregateClass, $aggregateHandler->method), $container);
}
$container->register($parameterResolverId, SymfonyParameterResolver::class)
->setArguments([
new ServiceLocatorArgument($services),
]);
}
}
/** @return array<string, mixed> */
private function services(ReflectionMethod $method, ContainerInterface $container): array
{
$services = [];
$prefix = strtolower($method->getName()) . '.';
foreach ($method->getParameters() as $index => $parameter) {
if ($index === 0) {
continue; // skip first parameter (command)
}
$key = $prefix . $parameter->getName();
$attributes = $parameter->getAttributes(Inject::class);
if ($attributes !== []) {
$services[$key] = new Reference($attributes[0]->newInstance()->service);
continue;
}
$attributes = $parameter->getAttributes(Autowire::class, ReflectionAttribute::IS_INSTANCEOF);
if ($attributes !== []) {
$services[$key] = $attributes[0]->newInstance()->value;
continue;
}
$reflectionType = $parameter->getType();
if ($reflectionType === null) {
throw ServiceNotResolvable::missingType($method->getDeclaringClass()->getName(), $parameter->getName());
}
$type = TypeResolver::create()->resolve($reflectionType);
if (!$type instanceof ObjectType) {
throw ServiceNotResolvable::typeNotObject(
$method->getDeclaringClass()->getName(),
$parameter->getName(),
);
}
$binding = sprintf('%s $%s', $type->getClassName(), $parameter->getName());
if ($container->has($binding)) {
$services[$key] = new Reference($binding);
continue;
}
$services[$key] = new Reference($type->getClassName());
}
return $services;
}
}