|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace ProxyManager\ProxyGenerator\AccessInterceptorValueHolder\MethodGenerator\Util; |
|
6
|
|
|
|
|
7
|
|
|
use Laminas\Code\Generator\PropertyGenerator; |
|
8
|
|
|
use ProxyManager\Generator\MethodGenerator; |
|
9
|
|
|
use ProxyManager\Generator\Util\ProxiedMethodReturnExpression; |
|
10
|
|
|
use ReflectionMethod; |
|
11
|
|
|
use function array_keys; |
|
12
|
|
|
use function implode; |
|
13
|
|
|
use function str_replace; |
|
14
|
|
|
use function var_export; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Utility to create pre- and post- method interceptors around a given method body |
|
18
|
|
|
* |
|
19
|
|
|
* @private - this class is just here as a small utility for this component, don't use it in your own code |
|
20
|
|
|
*/ |
|
21
|
|
|
class InterceptorGenerator |
|
22
|
|
|
{ |
|
23
|
|
|
private const TEMPLATE = <<<'PHP' |
|
24
|
|
|
if (isset($this->{{$prefixInterceptorsName}}[{{$name}}])) { |
|
25
|
|
|
$returnEarly = false; |
|
26
|
|
|
$prefixReturnValue = $this->{{$prefixInterceptorsName}}[{{$name}}]->__invoke($this, $this->{{$valueHolderName}}, {{$name}}, {{$paramsString}}, $returnEarly); |
|
27
|
|
|
|
|
28
|
|
|
if ($returnEarly) { |
|
29
|
|
|
{{$returnEarlyPrefixExpression}} |
|
30
|
|
|
} |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
{{$methodBody}} |
|
34
|
|
|
|
|
35
|
|
|
if (isset($this->{{$suffixInterceptorsName}}[{{$name}}])) { |
|
36
|
|
|
$returnEarly = false; |
|
37
|
|
|
$suffixReturnValue = $this->{{$suffixInterceptorsName}}[{{$name}}]->__invoke($this, $this->{{$valueHolderName}}, {{$name}}, {{$paramsString}}, $returnValue, $returnEarly); |
|
38
|
|
|
|
|
39
|
|
|
if ($returnEarly) { |
|
40
|
|
|
{{$returnEarlySuffixExpression}} |
|
41
|
|
|
} |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
{{$returnExpression}} |
|
45
|
|
|
PHP; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @param string $methodBody the body of the previously generated code. |
|
49
|
|
|
* It MUST assign the return value to a variable |
|
50
|
|
|
* `$returnValue` instead of directly returning |
|
51
|
3 |
|
*/ |
|
52
|
|
|
public static function createInterceptedMethodBody( |
|
53
|
|
|
string $methodBody, |
|
54
|
|
|
MethodGenerator $method, |
|
55
|
|
|
PropertyGenerator $valueHolder, |
|
56
|
|
|
PropertyGenerator $prefixInterceptors, |
|
57
|
|
|
PropertyGenerator $suffixInterceptors, |
|
58
|
|
|
?ReflectionMethod $originalMethod |
|
59
|
3 |
|
) : string { |
|
60
|
3 |
|
$name = var_export($method->getName(), true); |
|
61
|
3 |
|
$valueHolderName = $valueHolder->getName(); |
|
62
|
3 |
|
$prefixInterceptorsName = $prefixInterceptors->getName(); |
|
63
|
3 |
|
$suffixInterceptorsName = $suffixInterceptors->getName(); |
|
64
|
|
|
$params = []; |
|
65
|
3 |
|
|
|
66
|
3 |
|
foreach ($method->getParameters() as $parameter) { |
|
67
|
3 |
|
$parameterName = $parameter->getName(); |
|
68
|
|
|
$params[] = var_export($parameterName, true) . ' => $' . $parameter->getName(); |
|
69
|
|
|
} |
|
70
|
3 |
|
|
|
71
|
|
|
$paramsString = 'array(' . implode(', ', $params) . ')'; |
|
72
|
|
|
|
|
73
|
3 |
|
$replacements = [ |
|
74
|
3 |
|
'{{$prefixInterceptorsName}}' => $prefixInterceptorsName, |
|
75
|
3 |
|
'{{$name}}' => $name, |
|
76
|
3 |
|
'{{$valueHolderName}}' => $valueHolderName, |
|
77
|
3 |
|
'{{$paramsString}}' => $paramsString, |
|
78
|
3 |
|
'{{$returnEarlyPrefixExpression}}' => ProxiedMethodReturnExpression::generate('$prefixReturnValue', $originalMethod), |
|
79
|
3 |
|
'{{$methodBody}}' => $methodBody, |
|
80
|
3 |
|
'{{$suffixInterceptorsName}}' => $suffixInterceptorsName, |
|
81
|
3 |
|
'{{$returnEarlySuffixExpression}}' => ProxiedMethodReturnExpression::generate('$suffixReturnValue', $originalMethod), |
|
82
|
|
|
'{{$returnExpression}}' => ProxiedMethodReturnExpression::generate('$returnValue', $originalMethod), |
|
83
|
|
|
|
|
84
|
|
|
]; |
|
85
|
3 |
|
|
|
86
|
|
|
return str_replace(array_keys($replacements), $replacements, self::TEMPLATE); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|