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