|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
/** |
|
5
|
|
|
* This file is part of the Ray.Di package. |
|
6
|
|
|
* |
|
7
|
|
|
* @license http://opensource.org/licenses/MIT MIT |
|
8
|
|
|
*/ |
|
9
|
|
|
namespace Ray\Di; |
|
10
|
|
|
|
|
11
|
|
|
final class Argument implements \Serializable |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @var string |
|
15
|
|
|
*/ |
|
16
|
|
|
private $index; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var bool |
|
20
|
|
|
*/ |
|
21
|
|
|
private $isDefaultAvailable; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var mixed |
|
25
|
|
|
*/ |
|
26
|
|
|
private $default; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var string |
|
30
|
|
|
*/ |
|
31
|
|
|
private $meta; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var \ReflectionParameter |
|
35
|
|
|
*/ |
|
36
|
|
|
private $reflection; |
|
37
|
|
|
|
|
38
|
67 |
|
public function __construct(\ReflectionParameter $parameter, string $name) |
|
39
|
|
|
{ |
|
40
|
67 |
|
$type = $this->getType($parameter); |
|
41
|
67 |
|
$isOptional = $parameter->isOptional(); |
|
42
|
67 |
|
$this->isDefaultAvailable = $parameter->isDefaultValueAvailable() || $isOptional; |
|
43
|
67 |
|
if ($isOptional) { |
|
44
|
12 |
|
$this->default = null; |
|
45
|
|
|
} |
|
46
|
67 |
|
$this->setDefaultValue($parameter); |
|
47
|
67 |
|
$this->index = $type . '-' . $name; |
|
48
|
67 |
|
$this->reflection = $parameter; |
|
49
|
67 |
|
$this->meta = sprintf( |
|
50
|
67 |
|
"dependency '%s' with name '%s' used in %s:%d ($%s)", |
|
51
|
67 |
|
$type, |
|
52
|
67 |
|
$name, |
|
53
|
67 |
|
$this->reflection->getDeclaringFunction()->getFileName(), |
|
54
|
67 |
|
$this->reflection->getDeclaringFunction()->getStartLine(), |
|
55
|
67 |
|
$parameter->getName() |
|
|
|
|
|
|
56
|
|
|
); |
|
57
|
67 |
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @return string |
|
61
|
|
|
*/ |
|
62
|
41 |
|
public function __toString() |
|
63
|
|
|
{ |
|
64
|
41 |
|
return $this->index; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Return reflection |
|
69
|
|
|
*/ |
|
70
|
39 |
|
public function get() : \ReflectionParameter |
|
71
|
|
|
{ |
|
72
|
39 |
|
return $this->reflection; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @return bool |
|
77
|
|
|
*/ |
|
78
|
13 |
|
public function isDefaultAvailable() : bool |
|
79
|
|
|
{ |
|
80
|
13 |
|
return $this->isDefaultAvailable; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @return mixed |
|
85
|
|
|
*/ |
|
86
|
7 |
|
public function getDefaultValue() |
|
87
|
|
|
{ |
|
88
|
7 |
|
return $this->default; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
7 |
|
public function getMeta() : string |
|
92
|
|
|
{ |
|
93
|
7 |
|
return $this->meta; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
4 |
|
public function serialize() : string |
|
97
|
|
|
{ |
|
98
|
|
|
/** @var \ReflectionMethod $method */ |
|
99
|
4 |
|
$method = $this->reflection->getDeclaringFunction(); |
|
100
|
|
|
$ref = [ |
|
101
|
4 |
|
$method->class, |
|
102
|
4 |
|
$method->name, |
|
103
|
4 |
|
$this->reflection->getName() |
|
|
|
|
|
|
104
|
|
|
]; |
|
105
|
|
|
|
|
106
|
4 |
|
return \serialize([ |
|
107
|
4 |
|
$this->index, |
|
108
|
4 |
|
$this->isDefaultAvailable, |
|
109
|
4 |
|
$this->default, |
|
110
|
4 |
|
$this->meta, |
|
111
|
4 |
|
$ref |
|
112
|
|
|
]); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
4 |
|
public function unserialize($serialized) |
|
116
|
|
|
{ |
|
117
|
4 |
|
list($this->index, |
|
118
|
4 |
|
$this->isDefaultAvailable, |
|
119
|
4 |
|
$this->default, |
|
120
|
4 |
|
$this->meta, |
|
121
|
|
|
$ref |
|
122
|
4 |
|
) = unserialize($serialized); |
|
123
|
4 |
|
$this->reflection = new \ReflectionParameter([$ref[0], $ref[1]], $ref[2]); |
|
124
|
4 |
|
} |
|
125
|
|
|
|
|
126
|
67 |
|
private function setDefaultValue(\ReflectionParameter $parameter) |
|
127
|
|
|
{ |
|
128
|
67 |
|
if (! $this->isDefaultAvailable) { |
|
129
|
65 |
|
return; |
|
130
|
|
|
} |
|
131
|
|
|
try { |
|
132
|
12 |
|
$this->default = $parameter->getDefaultValue(); |
|
133
|
1 |
|
} /* @noinspection PhpRedundantCatchClauseInspection */ catch (\ReflectionException $e) { |
|
134
|
|
|
// probably it is internal class like \PDO |
|
135
|
1 |
|
$this->default = null; |
|
136
|
|
|
} |
|
137
|
12 |
|
} |
|
138
|
|
|
|
|
139
|
67 |
|
private function getType(\ReflectionParameter $parameter) : string |
|
140
|
|
|
{ |
|
141
|
67 |
|
$type = $parameter->getType(); |
|
142
|
67 |
|
if ($type instanceof \ReflectionType && \in_array((string) $type, ['bool', 'int', 'string', 'array', 'resource', 'callable'], true)) { |
|
|
|
|
|
|
143
|
3 |
|
return ''; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
67 |
|
return (string) $type; |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
|