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
|
12 |
|
} |
45
|
61 |
|
$this->setDefaultValue($parameter); |
46
|
|
|
$this->index = $interface . '-' . $name; |
47
|
12 |
|
$this->reflection = $parameter; |
48
|
12 |
|
$this->meta = sprintf( |
49
|
|
|
"dependency '%s' with name '%s' used in %s:%d ($%s)", |
50
|
1 |
|
$interface, |
51
|
|
|
$name, |
52
|
12 |
|
$this->reflection->getDeclaringFunction()->getFileName(), |
53
|
61 |
|
$this->reflection->getDeclaringFunction()->getStartLine(), |
54
|
61 |
|
$parameter->getName() |
55
|
61 |
|
); |
56
|
61 |
|
} |
57
|
61 |
|
|
58
|
61 |
|
/** |
59
|
61 |
|
* Return reflection |
60
|
61 |
|
* |
61
|
61 |
|
* @return \ReflectionParameter |
62
|
61 |
|
*/ |
63
|
|
|
public function get() |
64
|
|
|
{ |
65
|
|
|
return $this->reflection; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
34 |
|
* @param \ReflectionParameter $parameter |
70
|
|
|
* |
71
|
34 |
|
* @return string |
72
|
|
|
*/ |
73
|
|
|
private function getTypeHint(\ReflectionParameter $parameter) |
74
|
|
|
{ |
75
|
|
|
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
|
61 |
|
* @return string |
86
|
|
|
*/ |
87
|
61 |
|
public function __toString() |
88
|
|
|
{ |
89
|
|
|
return $this->index; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
36 |
|
* @return bool |
94
|
|
|
*/ |
95
|
36 |
|
public function isDefaultAvailable() |
96
|
|
|
{ |
97
|
|
|
return $this->isDefaultAvailable; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
13 |
|
* @return mixed |
102
|
|
|
*/ |
103
|
13 |
|
public function getDefaultValue() |
104
|
|
|
{ |
105
|
|
|
return $this->default; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function getMeta() |
109
|
7 |
|
{ |
110
|
|
|
return $this->meta; |
111
|
7 |
|
} |
112
|
|
|
|
113
|
|
|
private function setDefaultValue(\ReflectionParameter $parameter) |
114
|
7 |
|
{ |
115
|
|
|
if (! $this->isDefaultAvailable) { |
116
|
7 |
|
return; |
117
|
|
|
} |
118
|
|
|
try { |
119
|
|
|
$this->default = $parameter->getDefaultValue(); |
120
|
|
|
} catch (\ReflectionException $e) { |
121
|
|
|
// probably it is internal class like \PDO |
122
|
|
|
$this->default = null; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|