1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
/** |
3
|
|
|
* This file belongs to Bandit. All rights reserved |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace Incompass\InjectionBundle; |
7
|
|
|
|
8
|
|
|
use Incompass\InjectionBundle\Annotation\Argument; |
9
|
|
|
use Incompass\InjectionBundle\Annotation\Factory; |
10
|
|
|
use Incompass\InjectionBundle\Annotation\Inject; |
11
|
|
|
use Incompass\InjectionBundle\Annotation\MethodCall; |
12
|
|
|
use Incompass\InjectionBundle\Annotation\Tag; |
13
|
|
|
use Symfony\Component\DependencyInjection\Alias; |
14
|
|
|
use Symfony\Component\DependencyInjection\ChildDefinition; |
15
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
16
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
17
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class InjectProcessor |
21
|
|
|
* @package InjectionBundle |
22
|
|
|
* @author Joe Mizzi <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
class InjectProcessor |
25
|
|
|
{ |
26
|
16 |
|
public function process($annotation, $class, ContainerBuilder $container): void |
27
|
|
|
{ |
28
|
16 |
|
if (!($annotation instanceof Inject)) { |
29
|
1 |
|
return; |
30
|
|
|
} |
31
|
|
|
|
32
|
15 |
|
$environmentGroups = $container->getParameter('injection.environment_groups'); |
33
|
|
|
|
34
|
15 |
|
foreach ($annotation->environmentGroups as $group) { |
35
|
2 |
|
if (isset($environmentGroups[$group]['environments'])) { |
36
|
2 |
|
foreach ($environmentGroups[$group]['environments'] as $environment) { |
37
|
2 |
|
if (!\in_array($environment, $annotation->environments, true)) { |
38
|
2 |
|
$annotation->environments[] = $environment; |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
|
44
|
15 |
|
if ($annotation->environmentStrategy === 'exclude') { |
45
|
13 |
|
if (\in_array($container->getParameter('kernel.environment'), $annotation->environments, true)) { |
46
|
13 |
|
return; |
47
|
|
|
} |
48
|
|
|
} else { |
49
|
2 |
|
if (!\in_array($container->getParameter('kernel.environment'), $annotation->environments, true)) { |
50
|
|
|
return; |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
13 |
|
if ($annotation->parent) { |
55
|
3 |
|
$definition = new ChildDefinition($annotation->parent); |
56
|
|
|
} else { |
57
|
10 |
|
$definition = new Definition($class); |
58
|
|
|
} |
59
|
|
|
|
60
|
13 |
|
if ($annotation->factory) { |
61
|
1 |
|
$definition->setFactory([$annotation->factory->class, $annotation->factory->method]); |
62
|
|
|
} |
63
|
|
|
|
64
|
13 |
|
foreach ($annotation->aliases as $alias) { |
65
|
1 |
|
$container->setAlias($alias, new Alias($class)); |
66
|
|
|
} |
67
|
|
|
|
68
|
13 |
|
if ($annotation->decoratedService) { |
69
|
2 |
|
$definition->setDecoratedService($annotation->decoratedService); |
70
|
|
|
} |
71
|
|
|
|
72
|
13 |
|
if ($annotation->class) { |
73
|
1 |
|
$definition->setClass($annotation->class); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** @var Argument $argument */ |
77
|
13 |
|
foreach ($annotation->arguments as $argument) { |
78
|
3 |
|
if ($argument->value[0] === '@') { |
79
|
1 |
|
$definition->setArgument('$' . $argument->name, new Reference(substr($argument->value, 1))); |
80
|
|
|
} else { |
81
|
2 |
|
if ($annotation->decoratedService) { |
82
|
1 |
|
$definition->setArgument('$' . $argument->name, new Reference($argument->value . '.inner')); |
83
|
|
|
} else { |
84
|
3 |
|
$definition->setArgument('$' . $argument->name, $argument->value); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** @var MethodCall $methodCall */ |
90
|
13 |
|
foreach ($annotation->methodCalls as $methodCall) { |
91
|
1 |
|
$definition->addMethodCall($methodCall->method, $methodCall->arguments); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @var Tag $tag |
96
|
|
|
*/ |
97
|
13 |
|
foreach ($annotation->tags as $tag) { |
98
|
1 |
|
$definition->addTag($tag->name, $tag->attributes); |
99
|
|
|
} |
100
|
|
|
|
101
|
13 |
|
if (!$annotation->parent) { |
102
|
10 |
|
$definition->setAutoconfigured($annotation->autoconfigured); |
103
|
|
|
} |
104
|
|
|
|
105
|
13 |
|
$definition->setAutowired($annotation->autowired); |
106
|
13 |
|
$definition->setPublic($annotation->public); |
107
|
13 |
|
$definition->setLazy($annotation->lazy); |
108
|
13 |
|
$definition->setAbstract($annotation->abstract); |
109
|
13 |
|
$definition->setShared($annotation->shared); |
110
|
|
|
|
111
|
13 |
|
$container->setDefinition($annotation->id ?: $class, $definition); |
112
|
|
|
} |
113
|
|
|
} |