1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of slick/di package |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Slick\Di\DependencyInspector; |
11
|
|
|
|
12
|
|
|
use Slick\Common\Base; |
13
|
|
|
use Slick\Common\Inspector; |
14
|
|
|
use Slick\Di\Definition\Object as ObjectDefinition; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Constructor Inspector for dependency definition |
18
|
|
|
* |
19
|
|
|
* @package Slick\Di\DependencyInspector |
20
|
|
|
* @author Filipe Silva <[email protected]> |
21
|
|
|
* |
22
|
|
|
* @property ObjectDefinition $definition The definition to work with |
23
|
|
|
* |
24
|
|
|
* @method ObjectDefinition getDefinition() Returns current definition |
25
|
|
|
* @method bool isSatisfiable() Check if construction can be achieved |
26
|
|
|
*/ |
27
|
|
|
class ConstructorInspector extends Base |
28
|
|
|
{ |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @readwrite |
32
|
|
|
* @var ObjectDefinition |
33
|
|
|
*/ |
34
|
|
|
protected $definition; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @read |
38
|
|
|
* @var Parameter[] |
39
|
|
|
*/ |
40
|
|
|
protected $arguments; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @read |
44
|
|
|
* @var bool |
45
|
|
|
*/ |
46
|
|
|
protected $satisfiable = true; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param ObjectDefinition $definition |
50
|
|
|
* @return $this|self|ConstructorInspector |
51
|
|
|
*/ |
52
|
14 |
|
public function setDefinition(ObjectDefinition $definition) |
53
|
|
|
{ |
54
|
14 |
|
$this->definition = $definition; |
55
|
14 |
|
$this->arguments = null; |
|
|
|
|
56
|
14 |
|
$this->satisfiable = true; |
57
|
14 |
|
$this->updateDefinition(); |
58
|
14 |
|
return $this; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Set definition constructor arguments |
63
|
|
|
*/ |
64
|
14 |
|
protected function updateDefinition() |
65
|
|
|
{ |
66
|
14 |
|
$arguments = []; |
67
|
14 |
View Code Duplication |
foreach ($this->getArguments() as $param) { |
|
|
|
|
68
|
12 |
|
if ($this->definition->getContainer()->has($param->getId())) { |
69
|
12 |
|
$arguments[$param->name] = $this->definition |
70
|
12 |
|
->getContainer() |
71
|
12 |
|
->get($param->getId()); |
72
|
12 |
|
continue; |
73
|
|
|
} |
74
|
|
|
|
75
|
12 |
|
if (!$param->isOptional()) { |
76
|
2 |
|
$this->satisfiable = false; |
77
|
2 |
|
return; |
78
|
|
|
} |
79
|
|
|
|
80
|
12 |
|
$arguments[$param->name] = $param->default; |
81
|
14 |
|
} |
82
|
14 |
|
$this->definition->constructArgs = $arguments; |
83
|
14 |
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Gets constructor arguments |
87
|
|
|
* |
88
|
|
|
* @return Parameter[] |
89
|
|
|
*/ |
90
|
14 |
|
protected function getArguments() |
91
|
|
|
{ |
92
|
14 |
|
if (is_null($this->arguments)) { |
93
|
14 |
|
$methodInspector = new MethodInspector( |
94
|
|
|
[ |
95
|
14 |
|
'name' => '__construct', |
96
|
14 |
|
'definition' => $this->definition |
97
|
14 |
|
] |
98
|
14 |
|
); |
99
|
14 |
|
$this->arguments = $methodInspector->getArguments(); |
100
|
14 |
|
} |
101
|
14 |
|
return $this->arguments; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
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..