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\Inspector; |
11
|
|
|
|
12
|
|
|
use ReflectionClass; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* ConstructorArgumentInspector |
16
|
|
|
* |
17
|
|
|
* @package Slick\Di\Inspector |
18
|
|
|
* @author Filipe Silva <[email protected]> |
19
|
|
|
*/ |
20
|
|
|
class ConstructorArgumentInspector |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var ReflectionClass |
24
|
|
|
*/ |
25
|
|
|
private $reflectionClass; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var array |
29
|
|
|
*/ |
30
|
|
|
private $override; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Creates a ConstructorArgumentInspector |
34
|
|
|
* |
35
|
|
|
* @param ReflectionClass $reflectionClass |
36
|
|
|
* @param array $override |
37
|
|
|
*/ |
38
|
|
|
public function __construct(ReflectionClass $reflectionClass, array $override = []) |
39
|
|
|
{ |
40
|
|
|
$this->reflectionClass = $reflectionClass; |
41
|
|
|
$this->override = $override; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Returns the list of alias to used as arguments on object definition |
46
|
|
|
* |
47
|
|
|
* @return array |
48
|
|
|
*/ |
49
|
|
|
public function arguments() |
50
|
|
|
{ |
51
|
|
|
$arguments = $this->definedArguments(); |
52
|
|
|
return array_replace($arguments, $this->override); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Get the list of arguments from constructor defined parameters |
57
|
|
|
* |
58
|
|
|
* @return string[] |
59
|
|
|
*/ |
60
|
|
|
private function definedArguments() |
61
|
|
|
{ |
62
|
|
|
$arguments = []; |
63
|
|
|
$constructor = $this->reflectionClass->getConstructor(); |
64
|
|
|
|
65
|
|
|
if (null === $constructor) { |
66
|
|
|
return $arguments; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$parameters = $constructor->getParameters(); |
70
|
|
|
|
71
|
|
|
foreach ($parameters as $parameter) { |
72
|
|
|
$class = $parameter->getType(); |
73
|
|
|
if (is_null($class)) { |
74
|
|
|
break; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$arguments[] = "@{$parameter->getType()->getName()}"; |
|
|
|
|
78
|
|
|
} |
79
|
|
|
return $arguments; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
} |
83
|
|
|
|