1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TomPHP\ContainerConfigurator\League; |
4
|
|
|
|
5
|
|
|
use League\Container\Definition\ClassDefinition; |
6
|
|
|
use League\Container\ServiceProvider\AbstractServiceProvider; |
7
|
|
|
use TomPHP\ContainerConfigurator\Configurator; |
8
|
|
|
use TomPHP\ContainerConfigurator\Exception\NotClassDefinitionException; |
9
|
|
|
use TomPHP\ContainerConfigurator\ServiceConfig; |
10
|
|
|
use TomPHP\ContainerConfigurator\ServiceDefinition; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @internal |
14
|
|
|
*/ |
15
|
|
|
final class ServiceServiceProvider extends AbstractServiceProvider |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var array |
19
|
|
|
*/ |
20
|
|
|
private $config; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param ServiceConfig $config |
24
|
|
|
*/ |
25
|
|
|
public function __construct(ServiceConfig $config) |
26
|
|
|
{ |
27
|
|
|
$this->config = $config; |
|
|
|
|
28
|
|
|
$this->provides = $config->getKeys(); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function register() |
32
|
|
|
{ |
33
|
|
|
foreach ($this->config as $config) { |
34
|
|
|
$this->registerService($config); |
35
|
|
|
} |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param ServiceDefinition $definition |
40
|
|
|
* |
41
|
|
|
* @throws NotClassDefinitionException |
42
|
|
|
* |
43
|
|
|
* @return void |
44
|
|
|
*/ |
45
|
|
|
private function registerService(ServiceDefinition $definition) |
46
|
|
|
{ |
47
|
|
|
if ($definition->isFactory()) { |
48
|
|
|
$this->getContainer()->add( |
49
|
|
|
$definition->getName(), |
50
|
|
|
$this->createFactoryFactory($definition), |
51
|
|
|
$definition->isSingleton() |
52
|
|
|
); |
53
|
|
|
|
54
|
|
|
return; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
if ($definition->isAlias()) { |
58
|
|
|
$this->getContainer()->add( |
59
|
|
|
$definition->getName(), |
60
|
|
|
$this->createAliasFactory($definition) |
61
|
|
|
); |
62
|
|
|
|
63
|
|
|
return; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$service = $this->getContainer()->add( |
67
|
|
|
$definition->getName(), |
68
|
|
|
$definition->getClass(), |
69
|
|
|
$definition->isSingleton() |
70
|
|
|
); |
71
|
|
|
|
72
|
|
|
if (!$service instanceof ClassDefinition) { |
73
|
|
|
throw NotClassDefinitionException::fromServiceName($definition->getName()); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$service->withArguments($this->injectContainer($definition->getArguments())); |
77
|
|
|
$this->addMethodCalls($service, $definition); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param ClassDefinition $service |
82
|
|
|
* @param ServiceDefinition $definition |
83
|
|
|
*/ |
84
|
|
|
private function addMethodCalls(ClassDefinition $service, ServiceDefinition $definition) |
85
|
|
|
{ |
86
|
|
|
foreach ($definition->getMethods() as $method => $args) { |
87
|
|
|
$service->withMethodCall($method, $this->injectContainer($args)); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param ServiceDefinition $definition |
93
|
|
|
* |
94
|
|
|
* @return \Closure |
95
|
|
|
*/ |
96
|
|
|
private function createAliasFactory(ServiceDefinition $definition) |
97
|
|
|
{ |
98
|
|
|
return function () use ($definition) { |
99
|
|
|
return $this->getContainer()->get($definition->getClass()); |
100
|
|
|
}; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param ServiceDefinition $definition |
105
|
|
|
* |
106
|
|
|
* @return \Closure |
107
|
|
|
*/ |
108
|
|
View Code Duplication |
private function createFactoryFactory(ServiceDefinition $definition) |
|
|
|
|
109
|
|
|
{ |
110
|
|
|
return function () use ($definition) { |
111
|
|
|
$className = $definition->getClass(); |
112
|
|
|
$factory = new $className(); |
113
|
|
|
|
114
|
|
|
return $factory(...$this->resolveArguments($definition->getArguments())); |
115
|
|
|
}; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param array $arguments |
120
|
|
|
* |
121
|
|
|
* @return array |
122
|
|
|
*/ |
123
|
|
|
private function injectContainer(array $arguments) |
124
|
|
|
{ |
125
|
|
|
return array_map( |
126
|
|
|
function ($argument) { |
127
|
|
|
return ($argument === Configurator::container()) |
128
|
|
|
? $this->container |
129
|
|
|
: $argument; |
130
|
|
|
}, |
131
|
|
|
$arguments |
132
|
|
|
); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @param array $arguments |
137
|
|
|
* |
138
|
|
|
* @return array |
139
|
|
|
*/ |
140
|
|
|
private function resolveArguments(array $arguments) |
141
|
|
|
{ |
142
|
|
|
return array_map( |
143
|
|
|
function ($argument) { |
144
|
|
|
if ($argument === Configurator::container()) { |
145
|
|
|
return $this->container; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
if ($this->container->has($argument)) { |
149
|
|
|
return $this->container->get($argument); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
return $argument; |
153
|
|
|
}, |
154
|
|
|
$arguments |
155
|
|
|
); |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..