Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
93.55% |
29 / 31 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
AutoTagActionsCompilerPass | |
93.55% |
29 / 31 |
|
0.00% |
0 / 2 |
12.04 | |
0.00% |
0 / 1 |
process | |
95.45% |
21 / 22 |
|
0.00% |
0 / 1 |
8 | |||
createDefinition | |
88.89% |
8 / 9 |
|
0.00% |
0 / 1 |
4.02 |
1 | <?php |
2 | namespace Apie\ApieBundle\DependencyInjection\Compiler; |
3 | |
4 | use Apie\Common\Wrappers\BoundedContextHashmapFactory; |
5 | use Apie\Core\Exceptions\InvalidTypeException; |
6 | use ReflectionClass; |
7 | use ReflectionNamedType; |
8 | use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
9 | use Symfony\Component\DependencyInjection\ContainerBuilder; |
10 | use Symfony\Component\DependencyInjection\Definition; |
11 | use Symfony\Component\DependencyInjection\Reference; |
12 | |
13 | /** |
14 | * Actions ony work properly in a Symfony application if they are services with the |
15 | * tag 'apie.context'. |
16 | * |
17 | * This compiler pass makes sure they exist as service definition and contain the tag. |
18 | */ |
19 | class AutoTagActionsCompilerPass implements CompilerPassInterface |
20 | { |
21 | private const SYMFONY_SERVICES = [ |
22 | 'cache_warmer', |
23 | ]; |
24 | public function process(ContainerBuilder $container): void |
25 | { |
26 | $boundedContextConfig = $container->getParameter('apie.bounded_contexts'); |
27 | $scanBoundedContextConfig = $container->getParameter('apie.scan_bounded_contexts'); |
28 | $factory = new BoundedContextHashmapFactory($boundedContextConfig, $scanBoundedContextConfig); |
29 | $hashmap = $factory->create(); |
30 | foreach ($hashmap as $boundedContext) { |
31 | foreach ($boundedContext->actions as $action) { |
32 | $class = $action->getDeclaringClass(); |
33 | if (!$class->isInstantiable()) { |
34 | continue; |
35 | } |
36 | $className = $class->name; |
37 | if (!$container->hasDefinition($className)) { |
38 | $container->addDefinitions([ |
39 | $className => $this->createDefinition($class) |
40 | ]); |
41 | } |
42 | $definition = $container->getDefinition($className); |
43 | $tag = $definition->getTag('apie.context'); |
44 | if (empty($tag)) { |
45 | $definition->addTag('apie.context'); |
46 | } |
47 | } |
48 | } |
49 | foreach (self::SYMFONY_SERVICES as $serviceId) { |
50 | if ($container->hasDefinition($serviceId)) { |
51 | $definition = $container->getDefinition($serviceId); |
52 | $definition->addTag('apie.context'); |
53 | } |
54 | } |
55 | } |
56 | |
57 | /** |
58 | * @param ReflectionClass<object> $refl |
59 | */ |
60 | private function createDefinition(ReflectionClass $refl): Definition |
61 | { |
62 | $arguments = []; |
63 | $constructor = $refl->getConstructor(); |
64 | if ($constructor) { |
65 | foreach ($constructor->getParameters() as $parameter) { |
66 | $type = $parameter->getType(); |
67 | if ($type instanceof ReflectionNamedType) { |
68 | $arguments[] = new Reference($type->getName()); |
69 | } else { |
70 | throw new InvalidTypeException($type, 'ReflectionNamedType'); |
71 | } |
72 | } |
73 | } |
74 | return new Definition($refl->name, $arguments); |
75 | } |
76 | } |