-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathWovenClassBuilder.php
More file actions
312 lines (255 loc) · 8.89 KB
/
WovenClassBuilder.php
File metadata and controls
312 lines (255 loc) · 8.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
<?php
/** @noinspection PhpPropertyOnlyWrittenInspection */
namespace Okapi\Aop\Core\Transform;
use DI\Attribute\Inject;
use Nette\PhpGenerator\ClassLike;
use Nette\PhpGenerator\ClassType;
use Nette\PhpGenerator\Factory;
use Nette\PhpGenerator\Method;
use Nette\PhpGenerator\PhpNamespace;
use Nette\PhpGenerator\PromotedParameter;
use Nette\PhpGenerator\Property;
use Okapi\Aop\Core\Cache\CachePaths;
use Okapi\Aop\Core\Container\AdviceContainer;
use Okapi\Aop\Core\Container\AdviceType\MethodAdviceContainer;
use Okapi\Aop\Core\JoinPoint\JoinPoint;
use Okapi\Aop\Core\JoinPoint\JoinPointInjector;
use Okapi\CodeTransformer\Core\DI;
use Okapi\CodeTransformer\Transformer\Code;
use Roave\BetterReflection\Reflection\Adapter\ReflectionMethod;
use Roave\BetterReflection\Reflection\ReflectionMethod as BetterReflectionMethod;
/**
* # Woven Class Builder
*
* This class is used to build woven classes.
*/
class WovenClassBuilder
{
// region DI
#[Inject]
private CachePaths $cachePaths;
// endregion
/**
* WeavingClassBuilder constructor.
*
* @param Code $code
* @param AdviceContainer[] $adviceContainers
*/
public function __construct(
private readonly Code $code,
private readonly array $adviceContainers,
) {}
// region Build
/**
* Build the weaving class.
*
* @return string
*/
public function build(): string
{
// Build the namespace
$phpNamespace = $this->buildNamespace();
// Build the class
$class = $this->buildClass($phpNamespace);
// Build the join points
$class->addMember($this->buildJoinPoints());
// Build the methods
$methods = $this->buildMethods();
foreach ($methods as $method) {
$class->addMember($method);
}
// Add the class to the namespace
$phpNamespace->add($class);
$phpNamespace->addUse(DI::class);
$phpNamespace->addUse(JoinPointInjector::class);
// Build the file
$file = (string)$phpNamespace;
// Inject the JoinPoints
$this->injectJoinPoints($file);
return "<?php\n\n" . $file;
}
/**
* Build the namespace.
*
* @return PhpNamespace
*/
private function buildNamespace(): PhpNamespace
{
$reflectionClass = $this->code->getReflectionClass();
return new PhpNamespace($reflectionClass->getNamespaceName());
}
/**
* Build the class.
*
* @param PhpNamespace $phpNamespace
*
* @return ClassType
*/
private function buildClass(PhpNamespace $phpNamespace): ClassType
{
$class = new ClassType();
$reflectionClass = $this->code->getReflectionClass();
// Set the class name
$shortClassName = $reflectionClass->getShortName();
$class->setName($shortClassName);
// Add the use statement
$className = $reflectionClass->getName();
$proxyClassName = $className . $this->cachePaths::PROXIED_SUFFIX;
$phpNamespace->addUse($proxyClassName);
// Set the class extends
$class->setExtends($proxyClassName);
// Set abstract
$class->setAbstract($reflectionClass->isAbstract());
// Set attributes
$attributes = $reflectionClass->getAttributes();
foreach ($attributes as $attribute) {
$class->addAttribute($attribute->getName(), $attribute->getArguments());
}
return $class;
}
/**
* Build the join points.
*
* @return Property
*/
private function buildJoinPoints(): Property
{
// Build property
$property = new Property(JoinPoint::JOIN_POINTS_PARAMETER_NAME);
$property->setVisibility(ClassLike::VisibilityPrivate);
$property->setStatic();
$property->setType('array');
// Build value
$value = [];
// Add interceptors
foreach ($this->adviceContainers as $adviceContainer) {
if ($adviceContainer instanceof MethodAdviceContainer) {
$methodType = JoinPoint::TYPE_METHOD;
foreach ($adviceContainer->getMatchedMethods() as $matchedMethod) {
$matchedRefMethod = $matchedMethod->matchedRefMethod;
$matchedMethodName = $matchedRefMethod->getName();
$adviceContainerName = $adviceContainer->getName();
if (!in_array($adviceContainerName, $value[$methodType][$matchedMethodName] ?? [])) {
$value[$methodType][$matchedMethodName][] = $adviceContainerName;
}
}
}
}
$property->setValue($value);
return $property;
}
/**
* Build the methods.
*
* @return Method[]
*/
private function buildMethods(): array
{
$methods = [];
foreach ($this->adviceContainers as $adviceContainer) {
if ($adviceContainer instanceof MethodAdviceContainer) {
foreach ($adviceContainer->getMatchedMethods() as $matchedMethod) {
$refMethod = $matchedMethod->matchedRefMethod;
$methodName = $refMethod->getName();
// Internal methods cannot be woven,
// so we skip them
if ($refMethod->getDeclaringClass()->isInternal()) {
continue;
}
// Check if the method was already built
if (array_key_exists($methodName, $methods)) {
continue;
}
// Build the method
$methods[$methodName] = $this->buildMethod($refMethod);
}
}
}
return $methods;
}
/**
* Build the method.
*
* @param BetterReflectionMethod $refMethod
*
* @return Method
*
* @noinspection PhpDocMissingThrowsInspection
*/
private function buildMethod(BetterReflectionMethod $refMethod): Method
{
$refMethod = new ReflectionMethod($refMethod);
/** @noinspection PhpUnhandledExceptionInspection */
$method = (new Factory)->fromMethodReflection(
$refMethod,
);
$methodName = $refMethod->getName();
foreach ($method->getParameters() as $parameter) {
if ($parameter instanceof PromotedParameter) {
$parameter->setReadOnly(false);
}
}
// Add "return" if the method has a return type
$return = (string)$method->getReturnType() !== 'void' ? 'return ' : '';
// Add parameters as an array with the parameter name as key
$parametersArray = $this->getParametersArray($refMethod);
$parameters = $parametersArray ? ", $parametersArray" : '';
// Static methods don't have $this
$isStatic = $refMethod->isStatic();
$context = $isStatic ? 'null' : '$this';
$body = $return
. 'call_user_func_array('
. 'self::$' . JoinPoint::JOIN_POINTS_PARAMETER_NAME
. '[\'' . JoinPoint::TYPE_METHOD . '\']'
. '[\'' . $methodName . '\'], '
. "[$context"
. "$parameters]);";
/**
* @example
* return call_user_func_array(self::$__joinPoints['method']['methodName'], [$this]);
* return call_user_func_array(self::$__joinPoints['method']['methodName'], [null]);
* return call_user_func_array(self::$__joinPoints['method']['methodName'], [$this, ['param1' => $param1, 'param2' => $param2]]);
*/
$method->setBody($body);
// Convert the method to public
$method->setVisibility(ClassLike::VisibilityPublic);
return $method;
}
/**
* Create an associative array with the parameter name as key and the
* parameter as value.
*
* @param ReflectionMethod $method
*
* @return string|null
*/
private function getParametersArray(ReflectionMethod $method): ?string
{
$parameters = $method->getParameters();
if (empty($parameters)) {
return null;
}
$arguments = [];
foreach ($parameters as $parameter) {
$isRef = $parameter->isPassedByReference() ? '&' : '';
$arguments[] = '\'' . $parameter->getName() . '\' => ' . $isRef . '$' . $parameter->getName();
}
return '[' . implode(', ', $arguments) . ']';
}
/**
* Inject the JoinPoints.
*
* @param string $file
*
* @return void
*/
private function injectJoinPoints(string &$file): void
{
$reflectionClass = $this->code->getReflectionClass();
$shortClassName = $reflectionClass->getShortName();
// language=PHP
$code = "DI::get(JoinPointInjector::class)->injectJoinPoints($shortClassName::class);";
$file .= "\n" . $code;
}
// endregion
}