|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace TomPHP\ConfigServiceProvider\League; |
|
4
|
|
|
|
|
5
|
|
|
use League\Container\Definition\ClassDefinition; |
|
6
|
|
|
use League\Container\ServiceProvider\AbstractServiceProvider; |
|
7
|
|
|
use TomPHP\ConfigServiceProvider\Exception\NotClassDefinitionException; |
|
8
|
|
|
use TomPHP\ConfigServiceProvider\ServiceConfig; |
|
9
|
|
|
use TomPHP\ConfigServiceProvider\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
|
|
|
private function registerService(ServiceDefinition $definition) |
|
40
|
|
|
{ |
|
41
|
|
|
$service = $this->getContainer()->add( |
|
42
|
|
|
$definition->getName(), |
|
43
|
|
|
$definition->getClass(), |
|
44
|
|
|
$definition->isSingleton() |
|
45
|
|
|
); |
|
46
|
|
|
|
|
47
|
|
|
if (!$service instanceof ClassDefinition) { |
|
48
|
|
|
throw new NotClassDefinitionException(sprintf( |
|
49
|
|
|
'DI definition for %s does not create a class definition', |
|
50
|
|
|
$definition->getName() |
|
51
|
|
|
)); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
$service->withArguments($definition->getArguments()); |
|
55
|
|
|
$this->addMethodCalls($service, $definition); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @param ClassDefinition $service |
|
60
|
|
|
* @param ServiceDefinition $definition |
|
61
|
|
|
*/ |
|
62
|
|
|
private function addMethodCalls(ClassDefinition $service, ServiceDefinition $definition) |
|
63
|
|
|
{ |
|
64
|
|
|
foreach ($definition->getMethods() as $method => $args) { |
|
65
|
|
|
$service->withMethodCall($method, $args); |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|
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..