1 | <?php |
||
34 | */ |
||
35 | class InterceptorGenerator |
||
36 | { |
||
37 | /** |
||
38 | * @param string $methodBody the body of the previously generated code. |
||
39 | * It MUST assign the return value to a variable |
||
40 | * `$returnValue` instead of directly returning |
||
41 | * @param \ProxyManager\Generator\MethodGenerator $method |
||
42 | * @param \Zend\Code\Generator\PropertyGenerator $prefixInterceptors |
||
43 | * @param \Zend\Code\Generator\PropertyGenerator $suffixInterceptors |
||
44 | * @param \ReflectionMethod|null $originalMethod |
||
45 | * |
||
46 | 1 | * @return string |
|
47 | */ |
||
48 | public static function createInterceptedMethodBody( |
||
49 | string $methodBody, |
||
50 | MethodGenerator $method, |
||
51 | PropertyGenerator $prefixInterceptors, |
||
52 | 1 | PropertyGenerator $suffixInterceptors, |
|
53 | 1 | ?\ReflectionMethod $originalMethod |
|
|
|||
54 | 1 | ) : string { |
|
55 | 1 | $name = var_export($method->getName(), true); |
|
56 | $prefixInterceptorsName = $prefixInterceptors->getName(); |
||
57 | 1 | $suffixInterceptorsName = $suffixInterceptors->getName(); |
|
58 | 1 | $params = []; |
|
59 | 1 | ||
60 | foreach ($method->getParameters() as $parameter) { |
||
61 | $parameterName = $parameter->getName(); |
||
62 | 1 | $params[] = var_export($parameterName, true) . ' => $' . $parameter->getName(); |
|
63 | } |
||
64 | 1 | ||
65 | 1 | $paramsString = 'array(' . implode(', ', $params) . ')'; |
|
66 | 1 | ||
67 | 1 | return "if (isset(\$this->$prefixInterceptorsName" . "[$name])) {\n" |
|
68 | 1 | . " \$returnEarly = false;\n" |
|
69 | 1 | . " \$prefixReturnValue = \$this->$prefixInterceptorsName" . "[$name]->__invoke(" |
|
70 | 1 | . "\$this, \$this, $name, $paramsString, \$returnEarly);\n\n" |
|
71 | 1 | . " if (\$returnEarly) {\n" |
|
72 | 1 | . ' ' . ProxiedMethodReturnExpression::generate('$prefixReturnValue', $originalMethod) . "\n" |
|
73 | 1 | . " }\n" |
|
74 | 1 | . "}\n\n" |
|
75 | 1 | . $methodBody . "\n\n" |
|
76 | 1 | . "if (isset(\$this->$suffixInterceptorsName" . "[$name])) {\n" |
|
77 | 1 | . " \$returnEarly = false;\n" |
|
78 | 1 | . " \$suffixReturnValue = \$this->$suffixInterceptorsName" . "[$name]->__invoke(" |
|
79 | 1 | . "\$this, \$this, $name, $paramsString, \$returnValue, \$returnEarly);\n\n" |
|
80 | 1 | . " if (\$returnEarly) {\n" |
|
81 | 1 | . ' ' . ProxiedMethodReturnExpression::generate('$suffixReturnValue', $originalMethod) . "\n" |
|
82 | . " }\n" |
||
83 | . "}\n\n" |
||
84 | . ProxiedMethodReturnExpression::generate('$returnValue', $originalMethod); |
||
87 |