|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* Go! AOP framework |
|
4
|
|
|
* |
|
5
|
|
|
* @copyright Copyright 2012, Lisachenko Alexander <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* This source file is subject to the license that is bundled |
|
8
|
|
|
* with this source code in the file LICENSE. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Go\Proxy; |
|
12
|
|
|
|
|
13
|
|
|
use ReflectionParameter as Parameter; |
|
14
|
|
|
use TokenReflection\ReflectionMethod as ParsedMethod; |
|
15
|
|
|
use TokenReflection\ReflectionParameter as ParsedParameter; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Abstract class for building different proxies |
|
19
|
|
|
*/ |
|
20
|
|
|
abstract class AbstractProxy |
|
21
|
|
|
{ |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Indent for source code |
|
25
|
|
|
* |
|
26
|
|
|
* @var int |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $indent = 4; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* List of advices that are used for generation of child |
|
32
|
|
|
* |
|
33
|
|
|
* @var array |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $advices = []; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* PHP expression string for accessing LSB information |
|
39
|
|
|
* |
|
40
|
|
|
* @var string |
|
41
|
|
|
*/ |
|
42
|
|
|
protected static $staticLsbExpression = 'static::class'; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Should proxy use variadics support or not |
|
46
|
|
|
* |
|
47
|
|
|
* @var bool |
|
48
|
|
|
*/ |
|
49
|
|
|
protected $useVariadics = false; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Constructs an abstract proxy class |
|
53
|
|
|
* |
|
54
|
|
|
* @param array $advices List of advices |
|
55
|
|
|
* @param bool $useVariadics Should proxy use variadics syntax or not |
|
56
|
|
|
*/ |
|
57
|
5 |
|
public function __construct(array $advices = [], $useVariadics = false) |
|
58
|
|
|
{ |
|
59
|
5 |
|
$this->advices = $this->flattenAdvices($advices); |
|
60
|
5 |
|
$this->useVariadics = $useVariadics; |
|
61
|
5 |
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Returns text representation of class |
|
65
|
|
|
* |
|
66
|
|
|
* @return string |
|
67
|
|
|
*/ |
|
68
|
|
|
abstract public function __toString(); |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Indent block of code |
|
72
|
|
|
* |
|
73
|
|
|
* @param string $text Non-indented text |
|
74
|
|
|
* |
|
75
|
|
|
* @return string Indented text |
|
76
|
|
|
*/ |
|
77
|
5 |
|
protected function indent($text) |
|
78
|
|
|
{ |
|
79
|
5 |
|
$pad = str_pad('', $this->indent, ' '); |
|
80
|
|
|
$lines = array_map(function($line) use ($pad) { |
|
|
|
|
|
|
81
|
5 |
|
return $pad . $line; |
|
82
|
5 |
|
}, explode("\n", $text)); |
|
83
|
|
|
|
|
84
|
5 |
|
return join("\n", $lines); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Returns list of string representation of parameters |
|
89
|
|
|
* |
|
90
|
|
|
* @param array|Parameter[]|ParsedParameter[] $parameters List of parameters |
|
91
|
|
|
* |
|
92
|
|
|
* @return array |
|
93
|
|
|
*/ |
|
94
|
5 |
|
protected function getParameters(array $parameters) |
|
95
|
|
|
{ |
|
96
|
5 |
|
$parameterDefinitions = []; |
|
97
|
5 |
|
foreach ($parameters as $parameter) { |
|
98
|
|
|
// Deprecated since PHP5.6 in the favor of variadics, needed for BC only |
|
99
|
2 |
|
if ($parameter->name == '...') { |
|
100
|
|
|
continue; |
|
101
|
|
|
} |
|
102
|
2 |
|
$parameterDefinitions[] = $this->getParameterCode($parameter); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
5 |
|
return $parameterDefinitions; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Return string representation of parameter |
|
110
|
|
|
* |
|
111
|
|
|
* @param Parameter|ParsedParameter $parameter Reflection parameter |
|
112
|
|
|
* |
|
113
|
|
|
* @return string |
|
114
|
|
|
*/ |
|
115
|
2 |
|
protected function getParameterCode($parameter) |
|
116
|
|
|
{ |
|
117
|
2 |
|
$type = ''; |
|
118
|
2 |
|
if ($parameter->isArray()) { |
|
119
|
|
|
$type = 'array'; |
|
120
|
2 |
|
} elseif ($parameter->isCallable()) { |
|
121
|
|
|
$type = 'callable'; |
|
122
|
2 |
|
} elseif ($parameter->getClass()) { |
|
123
|
|
|
$type = '\\' . $parameter->getClass()->name; |
|
124
|
|
|
} |
|
125
|
2 |
|
$defaultValue = null; |
|
126
|
2 |
|
$isDefaultValueAvailable = $parameter->isDefaultValueAvailable(); |
|
127
|
2 |
|
if ($isDefaultValueAvailable) { |
|
128
|
2 |
|
if ($parameter instanceof ParsedParameter) { |
|
|
|
|
|
|
129
|
2 |
|
$defaultValue = $parameter->getDefaultValueDefinition(); |
|
130
|
|
|
} else { |
|
131
|
2 |
|
$defaultValue = var_export($parameter->getDefaultValue(), true); |
|
132
|
|
|
} |
|
133
|
2 |
|
} elseif ($parameter->isOptional()) { |
|
134
|
|
|
$defaultValue = 'null'; |
|
135
|
|
|
} |
|
136
|
|
|
$code = ( |
|
137
|
2 |
|
($type ? "$type " : '') . // Typehint |
|
138
|
2 |
|
($parameter->isPassedByReference() ? '&' : '') . // By reference sign |
|
139
|
2 |
|
($this->useVariadics && $parameter->isVariadic() ? '...' : '') . // Variadic symbol |
|
140
|
2 |
|
'$' . // Variable symbol |
|
141
|
2 |
|
($parameter->name) . // Name of the argument |
|
142
|
2 |
|
($defaultValue !== null ? (" = " . $defaultValue) : '') // Default value if present |
|
143
|
|
|
); |
|
144
|
|
|
|
|
145
|
2 |
|
return $code; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Replace concrete advices with list of ids |
|
150
|
|
|
* |
|
151
|
|
|
* @param $advices |
|
152
|
|
|
* |
|
153
|
|
|
* @return array flatten list of advices |
|
154
|
|
|
*/ |
|
155
|
5 |
|
private function flattenAdvices($advices) |
|
156
|
|
|
{ |
|
157
|
5 |
|
$flattenAdvices = []; |
|
158
|
5 |
|
foreach ($advices as $type => $typedAdvices) { |
|
159
|
5 |
|
foreach ($typedAdvices as $name => $concreteAdvices) { |
|
160
|
5 |
|
if (is_array($concreteAdvices)) { |
|
161
|
5 |
|
$flattenAdvices[$type][$name] = array_keys($concreteAdvices); |
|
162
|
|
|
} |
|
163
|
|
|
} |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
5 |
|
return $flattenAdvices; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* Prepares a line with args from the method definition |
|
171
|
|
|
* |
|
172
|
|
|
* @param ParsedMethod $method |
|
173
|
|
|
* |
|
174
|
|
|
* @return string |
|
175
|
|
|
*/ |
|
176
|
|
|
protected function prepareArgsLine(ParsedMethod $method) |
|
177
|
|
|
{ |
|
178
|
5 |
|
$args = join(', ', array_map(function(ParsedParameter $param) { |
|
|
|
|
|
|
179
|
2 |
|
$byReference = $param->isPassedByReference() ? '&' : ''; |
|
180
|
|
|
|
|
181
|
2 |
|
return $byReference . '$' . $param->name; |
|
182
|
5 |
|
}, $method->getParameters())); |
|
183
|
|
|
|
|
184
|
5 |
|
return $args; |
|
185
|
|
|
} |
|
186
|
|
|
} |
|
187
|
|
|
|