|
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
|
13 |
|
public function process($annotation, $class, ContainerBuilder $container): void |
|
27
|
|
|
{ |
|
28
|
13 |
|
if (!($annotation instanceof Inject)) { |
|
29
|
1 |
|
return; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
12 |
|
$environmentGroups = $container->getParameter('injection.environment_groups'); |
|
33
|
|
|
|
|
34
|
12 |
|
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
|
12 |
|
if ($annotation->environmentStrategy === 'exclude') { |
|
45
|
10 |
|
if (\in_array($container->getParameter('kernel.environment'), $annotation->environments, true)) { |
|
46
|
10 |
|
return; |
|
47
|
|
|
} |
|
48
|
|
|
} else { |
|
49
|
2 |
|
if (!\in_array($container->getParameter('kernel.environment'), $annotation->environments, true)) { |
|
50
|
|
|
return; |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
10 |
|
if ($annotation->parent) { |
|
55
|
3 |
|
$definition = new ChildDefinition($annotation->parent); |
|
56
|
|
|
} else { |
|
57
|
7 |
|
$definition = new Definition($class); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
10 |
|
if ($annotation->factory) { |
|
61
|
1 |
|
$definition->setFactory([$annotation->factory->class, $annotation->factory->method]); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
10 |
|
foreach ($annotation->aliases as $alias) { |
|
65
|
1 |
|
$container->setAlias($alias, new Alias($class)); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** @var Argument $argument */ |
|
69
|
10 |
|
foreach ($annotation->arguments as $argument) { |
|
70
|
2 |
|
if ($argument->value[0] === '@') { |
|
71
|
1 |
|
$definition->setArgument('$'.$argument->name, new Reference(substr($argument->value, 1))); |
|
72
|
|
|
} else { |
|
73
|
2 |
|
$definition->setArgument('$'.$argument->name, $argument->value); |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** @var MethodCall $methodCall */ |
|
78
|
10 |
|
foreach ($annotation->methodCalls as $methodCall) { |
|
79
|
1 |
|
$definition->addMethodCall($methodCall->method, $methodCall->arguments); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @var Tag $tag |
|
84
|
|
|
*/ |
|
85
|
10 |
|
foreach ($annotation->tags as $tag) { |
|
86
|
1 |
|
$definition->addTag($tag->name, $tag->attributes); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
10 |
|
if (!$annotation->parent) { |
|
90
|
7 |
|
$definition->setAutoconfigured($annotation->autoconfigured); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
10 |
|
$definition->setAutowired($annotation->autowired); |
|
94
|
10 |
|
$definition->setPublic($annotation->public); |
|
95
|
10 |
|
$definition->setLazy($annotation->lazy); |
|
96
|
10 |
|
$definition->setAbstract($annotation->abstract); |
|
97
|
10 |
|
$definition->setShared($annotation->shared); |
|
98
|
|
|
|
|
99
|
10 |
|
$container->setDefinition($annotation->id ?: $class, $definition); |
|
100
|
|
|
} |
|
101
|
|
|
} |