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
|
|
|
* @return void |
40
|
|
|
* |
41
|
|
|
* @throws NotClassDefinitionException |
42
|
|
|
*/ |
43
|
|
|
private function registerService(ServiceDefinition $definition) |
44
|
|
|
{ |
45
|
|
|
$service = $this->getContainer()->add( |
46
|
|
|
$definition->getName(), |
47
|
|
|
$definition->getClass(), |
48
|
|
|
$definition->isSingleton() |
49
|
|
|
); |
50
|
|
|
|
51
|
|
|
if (!$service instanceof ClassDefinition) { |
52
|
|
|
throw NotClassDefinitionException::fromServiceName($definition->getName()); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$service->withArguments($definition->getArguments()); |
56
|
|
|
$this->addMethodCalls($service, $definition); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param ClassDefinition $service |
61
|
|
|
* @param ServiceDefinition $definition |
62
|
|
|
*/ |
63
|
|
|
private function addMethodCalls(ClassDefinition $service, ServiceDefinition $definition) |
64
|
|
|
{ |
65
|
|
|
foreach ($definition->getMethods() as $method => $args) { |
66
|
|
|
$service->withMethodCall($method, $args); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
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..