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\Exception\NotClassDefinitionException; |
8
|
|
|
use TomPHP\ContainerConfigurator\ServiceConfig; |
9
|
|
|
use TomPHP\ContainerConfigurator\ServiceDefinition; |
10
|
|
|
|
11
|
|
|
final class ServiceServiceProvider extends AbstractServiceProvider |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var array |
15
|
|
|
*/ |
16
|
|
|
private $config; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @api |
20
|
|
|
* |
21
|
|
|
* @param ServiceConfig $config |
22
|
|
|
*/ |
23
|
|
|
public function __construct(ServiceConfig $config) |
24
|
|
|
{ |
25
|
|
|
$this->config = $config; |
|
|
|
|
26
|
|
|
$this->provides = $config->getKeys(); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function register() |
30
|
|
|
{ |
31
|
|
|
foreach ($this->config as $config) { |
32
|
|
|
$this->registerService($config); |
33
|
|
|
} |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param ServiceDefinition $definition |
38
|
|
|
* |
39
|
|
|
* @throws NotClassDefinitionException |
40
|
|
|
* |
41
|
|
|
* @return void |
42
|
|
|
*/ |
43
|
|
|
private function registerService(ServiceDefinition $definition) |
44
|
|
|
{ |
45
|
|
|
if ($definition->isFactory()) { |
46
|
|
|
$this->getContainer()->add( |
47
|
|
|
$definition->getName(), |
48
|
|
|
$this->createFactoryFactory($definition), |
49
|
|
|
$definition->isSingleton() |
50
|
|
|
); |
51
|
|
|
|
52
|
|
|
return; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
if ($definition->isAlias()) { |
56
|
|
|
$this->getContainer()->add( |
57
|
|
|
$definition->getName(), |
58
|
|
|
$this->createAliasFactory($definition) |
59
|
|
|
); |
60
|
|
|
|
61
|
|
|
return; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$service = $this->getContainer()->add( |
65
|
|
|
$definition->getName(), |
66
|
|
|
$definition->getClass(), |
67
|
|
|
$definition->isSingleton() |
68
|
|
|
); |
69
|
|
|
|
70
|
|
|
if (!$service instanceof ClassDefinition) { |
71
|
|
|
throw NotClassDefinitionException::fromServiceName($definition->getName()); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$service->withArguments($definition->getArguments()); |
75
|
|
|
$this->addMethodCalls($service, $definition); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param ClassDefinition $service |
80
|
|
|
* @param ServiceDefinition $definition |
81
|
|
|
*/ |
82
|
|
|
private function addMethodCalls(ClassDefinition $service, ServiceDefinition $definition) |
83
|
|
|
{ |
84
|
|
|
foreach ($definition->getMethods() as $method => $args) { |
85
|
|
|
$service->withMethodCall($method, $args); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param ServiceDefinition $definition |
91
|
|
|
* |
92
|
|
|
* @return \Closure |
93
|
|
|
*/ |
94
|
|
|
private function createAliasFactory(ServiceDefinition $definition) |
95
|
|
|
{ |
96
|
|
|
return function () use ($definition) { |
97
|
|
|
return $this->getContainer()->get($definition->getClass()); |
98
|
|
|
}; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param ServiceDefinition $definition |
103
|
|
|
* |
104
|
|
|
* @return \Closure |
105
|
|
|
*/ |
106
|
|
View Code Duplication |
private function createFactoryFactory(ServiceDefinition $definition) |
|
|
|
|
107
|
|
|
{ |
108
|
|
|
return function () use ($definition) { |
109
|
|
|
$className = $definition->getClass(); |
110
|
|
|
$factory = new $className(); |
111
|
|
|
|
112
|
|
|
return $factory(...$this->resolveArguments($definition->getArguments())); |
113
|
|
|
}; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param array $arguments |
118
|
|
|
* |
119
|
|
|
* @return array |
120
|
|
|
*/ |
121
|
|
|
private function resolveArguments(array $arguments) |
122
|
|
|
{ |
123
|
|
|
return array_map( |
124
|
|
|
function ($argument) { |
125
|
|
|
if ($this->container->has($argument)) { |
126
|
|
|
return $this->container->get($argument); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
return $argument; |
130
|
|
|
}, |
131
|
|
|
$arguments |
132
|
|
|
); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
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..