|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the Ray.Di package. |
|
4
|
|
|
* |
|
5
|
|
|
* @license http://opensource.org/licenses/MIT MIT |
|
6
|
|
|
*/ |
|
7
|
|
|
namespace Ray\Di; |
|
8
|
|
|
|
|
9
|
|
|
final class Argument |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* @var string |
|
13
|
|
|
*/ |
|
14
|
|
|
private $index; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @var bool |
|
18
|
|
|
*/ |
|
19
|
|
|
private $isDefaultAvailable; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var |
|
23
|
|
|
*/ |
|
24
|
|
|
private $default; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var string |
|
28
|
|
|
*/ |
|
29
|
|
|
private $meta; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var \ReflectionParameter |
|
33
|
|
|
*/ |
|
34
|
|
|
private $reflection; |
|
35
|
|
|
|
|
36
|
61 |
|
public function __construct(\ReflectionParameter $parameter, $name) |
|
37
|
|
|
{ |
|
38
|
61 |
|
$interface = $this->getTypeHint($parameter); |
|
39
|
61 |
|
$interface = ($interface === 'array') ? '' : $interface; // hhvm |
|
40
|
61 |
|
$isOptional = $parameter->isOptional(); |
|
41
|
61 |
|
$this->isDefaultAvailable = $parameter->isDefaultValueAvailable() || $isOptional; |
|
42
|
61 |
|
if ($isOptional) { |
|
43
|
12 |
|
$this->default = null; |
|
44
|
|
|
} |
|
45
|
61 |
|
$this->setDefaultValue($parameter); |
|
46
|
61 |
|
$this->index = $interface . '-' . $name; |
|
47
|
61 |
|
$this->reflection = $parameter; |
|
48
|
61 |
|
$this->meta = sprintf( |
|
49
|
61 |
|
"dependency '%s' with name '%s' used in %s:%d ($%s)", |
|
50
|
|
|
$interface, |
|
51
|
|
|
$name, |
|
52
|
61 |
|
$this->reflection->getDeclaringFunction()->getFileName(), |
|
53
|
61 |
|
$this->reflection->getDeclaringFunction()->getStartLine(), |
|
54
|
61 |
|
$parameter->getName() |
|
55
|
|
|
); |
|
56
|
61 |
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Return reflection |
|
60
|
|
|
* |
|
61
|
|
|
* @return \ReflectionParameter |
|
62
|
|
|
*/ |
|
63
|
34 |
|
public function get() |
|
64
|
|
|
{ |
|
65
|
34 |
|
return $this->reflection; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @param \ReflectionParameter $parameter |
|
70
|
|
|
* |
|
71
|
|
|
* @return string |
|
72
|
|
|
*/ |
|
73
|
61 |
|
private function getTypeHint(\ReflectionParameter $parameter) |
|
74
|
|
|
{ |
|
75
|
61 |
|
if (defined('HHVM_VERSION')) { |
|
76
|
|
|
/* @noinspection PhpUndefinedFieldInspection */ |
|
77
|
|
|
return $parameter->info['type_hint']; // @codeCoverageIgnore |
|
78
|
|
|
} |
|
79
|
61 |
|
$typHint = $parameter->getClass(); |
|
80
|
|
|
|
|
81
|
61 |
|
return $typHint instanceof \ReflectionClass ? $typHint->name : ''; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @return string |
|
86
|
|
|
*/ |
|
87
|
36 |
|
public function __toString() |
|
88
|
|
|
{ |
|
89
|
36 |
|
return $this->index; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @return bool |
|
94
|
|
|
*/ |
|
95
|
13 |
|
public function isDefaultAvailable() |
|
96
|
|
|
{ |
|
97
|
13 |
|
return $this->isDefaultAvailable; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @return mixed |
|
102
|
|
|
*/ |
|
103
|
7 |
|
public function getDefaultValue() |
|
104
|
|
|
{ |
|
105
|
7 |
|
return $this->default; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
7 |
|
public function getMeta() |
|
109
|
|
|
{ |
|
110
|
7 |
|
return $this->meta; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
61 |
|
private function setDefaultValue(\ReflectionParameter $parameter) |
|
114
|
|
|
{ |
|
115
|
61 |
|
if (! $this->isDefaultAvailable) { |
|
116
|
59 |
|
return; |
|
117
|
|
|
} |
|
118
|
|
|
try { |
|
119
|
12 |
|
$this->default = $parameter->getDefaultValue(); |
|
120
|
1 |
|
} catch (\ReflectionException $e) { |
|
121
|
|
|
// probably it is internal class like \PDO |
|
122
|
1 |
|
$this->default = null; |
|
123
|
|
|
} |
|
124
|
12 |
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|