1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the LaravelYaml package. |
5
|
|
|
* |
6
|
|
|
* (c) Théo FIDRY <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Fidry\LaravelYaml\DependencyInjection\Builder; |
13
|
|
|
|
14
|
|
|
use Fidry\LaravelYaml\DependencyInjection\Definition; |
15
|
|
|
use Fidry\LaravelYaml\DependencyInjection\Definition\Alias; |
16
|
|
|
use Fidry\LaravelYaml\DependencyInjection\Definition\DecorationInterface; |
17
|
|
|
use Fidry\LaravelYaml\DependencyInjection\Definition\Reference; |
18
|
|
|
use Fidry\LaravelYaml\DependencyInjection\Definition\Service; |
19
|
|
|
use Fidry\LaravelYaml\DependencyInjection\Definition\ServiceInterface; |
20
|
|
|
use Illuminate\Contracts\Foundation\Application; |
21
|
|
|
use Symfony\Component\ExpressionLanguage\Expression; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @author Théo FIDRY <[email protected]> |
25
|
|
|
*/ |
26
|
|
|
final class ContainerBuilder implements BuilderInterface |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @var Alias[] |
30
|
|
|
*/ |
31
|
|
|
private $aliases = []; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var array |
35
|
|
|
*/ |
36
|
|
|
private $parameters = []; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var ServiceInterface[] |
40
|
|
|
*/ |
41
|
|
|
private $services = []; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var BuilderInterface|null |
45
|
|
|
*/ |
46
|
|
|
private $parametersBuilder; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var BuilderInterface|null |
50
|
|
|
*/ |
51
|
|
|
private $aliasesBuilder; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var BuilderInterface|null |
55
|
|
|
*/ |
56
|
|
|
private $servicesBuilder; |
57
|
|
|
|
58
|
3 |
|
public function __construct( |
59
|
|
|
BuilderInterface $parametersBuilder = null, |
60
|
|
|
BuilderInterface $aliasesBuilder = null, |
61
|
|
|
BuilderInterface $servicesBuilder = null |
62
|
|
|
) { |
63
|
3 |
|
$this->parametersBuilder = $parametersBuilder; |
64
|
3 |
|
$this->aliasesBuilder = $aliasesBuilder; |
65
|
3 |
|
$this->servicesBuilder = $servicesBuilder; |
66
|
3 |
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param string $key |
70
|
|
|
* @param array|Reference|string|Expression $value |
71
|
|
|
*/ |
72
|
|
|
public function setParameter($key, $value) |
73
|
|
|
{ |
74
|
|
|
$this->parameters[$key] = $value; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param Alias $alias |
79
|
|
|
*/ |
80
|
|
|
public function addAlias(Alias $alias) |
81
|
|
|
{ |
82
|
|
|
$this->aliases[$alias->getAlias()] = $alias; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param ServiceInterface $service |
87
|
|
|
*/ |
88
|
|
|
public function addService(ServiceInterface $service) |
89
|
|
|
{ |
90
|
|
|
if ($service instanceof DecorationInterface) { |
91
|
|
|
$this->getAndRebuilderDeprecatedService($service); |
92
|
|
|
$service = $this->rebuildServiceWithName($service->getDecorates(), $service); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
$this->services[$service->getName()] = $service; |
96
|
|
|
} |
97
|
|
|
|
98
|
3 |
|
public function build(Application $application) |
99
|
|
|
{ |
100
|
3 |
|
$parametersBuilder = (null === $this->parametersBuilder) |
101
|
1 |
|
? new ParametersBuilder($this->parameters) |
102
|
3 |
|
: $this->parametersBuilder |
103
|
1 |
|
; |
104
|
3 |
|
$parameters = $parametersBuilder->build($application); |
105
|
|
|
|
106
|
3 |
|
$servicesBuilder = (null === $this->servicesBuilder) |
107
|
1 |
|
? new ServicesBuilder($this->services, $parameters) |
108
|
3 |
|
: $this->servicesBuilder |
109
|
1 |
|
; |
110
|
3 |
|
$servicesBuilder->build($application); |
111
|
|
|
|
112
|
3 |
|
$aliasesBuilder = (null === $this->aliasesBuilder) |
113
|
1 |
|
? new AliasesBuilder($this->aliases) |
114
|
3 |
|
: $this->aliasesBuilder |
115
|
1 |
|
; |
116
|
3 |
|
$aliasesBuilder->build($application); |
117
|
3 |
|
} |
118
|
|
|
|
119
|
|
|
private function getAndRebuilderDeprecatedService(DecorationInterface $service) |
120
|
|
|
{ |
121
|
|
|
$oldDecorated = $this->services[$service->getDecorates()]; |
122
|
|
|
$newDecorated = $this->rebuildServiceWithName($service->getDecorationInnerName(), $oldDecorated); |
123
|
|
|
|
124
|
|
|
$this->addService($newDecorated); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param string $name |
129
|
|
|
* @param ServiceInterface $service |
130
|
|
|
* |
131
|
|
|
* @return Service |
132
|
|
|
*/ |
133
|
|
|
private function rebuildServiceWithName($name, ServiceInterface $service) |
134
|
|
|
{ |
135
|
|
|
return new Service( |
136
|
|
|
$name, |
137
|
|
|
$service->getClass(), |
138
|
|
|
$service->getArguments(), |
139
|
|
|
$service->getAutowiringTypes(), |
140
|
|
|
$service->getTags() |
141
|
|
|
); |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|