|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
/* |
|
5
|
|
|
* Go! AOP framework |
|
6
|
|
|
* |
|
7
|
|
|
* @copyright Copyright 2018, Lisachenko Alexander <[email protected]> |
|
8
|
|
|
* |
|
9
|
|
|
* This source file is subject to the license that is bundled |
|
10
|
|
|
* with this source code in the file LICENSE. |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace Go\Proxy\Part; |
|
14
|
|
|
|
|
15
|
|
|
use Laminas\Code\Generator\AbstractGenerator; |
|
16
|
|
|
use Laminas\Code\Generator\DocBlockGenerator; |
|
17
|
|
|
use Laminas\Code\Generator\ParameterGenerator; |
|
18
|
|
|
use Laminas\Code\Generator\TypeGenerator; |
|
19
|
|
|
use Laminas\Code\Reflection\DocBlockReflection; |
|
20
|
|
|
use ReflectionFunction; |
|
21
|
|
|
use ReflectionNamedType; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Prepares the definition of intercepted function |
|
25
|
|
|
*/ |
|
26
|
|
|
final class InterceptedFunctionGenerator extends AbstractGenerator |
|
27
|
|
|
{ |
|
28
|
|
|
protected string $functionName; |
|
|
|
|
|
|
29
|
|
|
|
|
30
|
|
|
protected ?DocBlockGenerator $docBlock = null; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var ParameterGenerator[] |
|
34
|
|
|
*/ |
|
35
|
|
|
protected array $parameters = []; |
|
36
|
|
|
|
|
37
|
|
|
protected string $functionBody; |
|
38
|
|
|
|
|
39
|
|
|
private ?TypeGenerator $returnType = null; |
|
40
|
|
|
|
|
41
|
|
|
private bool $returnsReference; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* InterceptedMethod constructor. |
|
45
|
|
|
* |
|
46
|
|
|
* @param ReflectionFunction $reflectionFunction Instance of original method |
|
47
|
|
|
* @param string $body Method body |
|
48
|
|
|
* @param bool $useTypeWidening Should generator use parameter widening for PHP>=7.2 |
|
49
|
|
|
*/ |
|
50
|
|
|
public function __construct(ReflectionFunction $reflectionFunction, string $body, bool $useTypeWidening = false) |
|
51
|
|
|
{ |
|
52
|
|
|
parent::__construct(); |
|
53
|
|
|
|
|
54
|
|
|
if ($reflectionFunction->hasReturnType()) { |
|
55
|
|
|
$reflectionReturnType = $reflectionFunction->getReturnType(); |
|
56
|
|
|
if ($reflectionReturnType instanceof ReflectionNamedType) { |
|
57
|
|
|
$returnTypeName = $reflectionReturnType->getName(); |
|
58
|
|
|
} else { |
|
59
|
|
|
$returnTypeName = (string)$reflectionReturnType; |
|
60
|
|
|
} |
|
61
|
|
|
$this->returnType = TypeGenerator::fromTypeString($returnTypeName); |
|
62
|
|
|
} |
|
63
|
5 |
|
|
|
64
|
|
|
if ($reflectionFunction->getDocComment()) { |
|
65
|
5 |
|
$reflectionDocBlock = new DocBlockReflection($reflectionFunction->getDocComment()); |
|
66
|
|
|
$this->docBlock = DocBlockGenerator::fromReflection($reflectionDocBlock); |
|
67
|
5 |
|
} |
|
68
|
1 |
|
|
|
69
|
|
|
$this->returnsReference = $reflectionFunction->returnsReference(); |
|
70
|
|
|
$this->functionName = $reflectionFunction->getShortName(); |
|
71
|
5 |
|
|
|
72
|
1 |
|
$parameterList = new FunctionParameterList($reflectionFunction, $useTypeWidening); |
|
73
|
1 |
|
$this->parameters = $parameterList->getGeneratedParameters(); |
|
74
|
|
|
$this->functionBody = $body; |
|
75
|
|
|
} |
|
76
|
5 |
|
|
|
77
|
5 |
|
public function generate(): string |
|
78
|
|
|
{ |
|
79
|
5 |
|
$output = ''; |
|
80
|
5 |
|
$indent = $this->getIndentation(); |
|
81
|
5 |
|
|
|
82
|
5 |
|
if ($this->docBlock !== null) { |
|
83
|
|
|
$this->docBlock->setIndentation($indent); |
|
84
|
|
|
$output .= $this->docBlock->generate(); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
5 |
|
$output .= 'function ' |
|
88
|
|
|
. ($this->returnsReference ? '& ' : '') |
|
89
|
5 |
|
. $this->functionName . '('; |
|
90
|
5 |
|
|
|
91
|
|
|
if (!empty($this->parameters)) { |
|
92
|
5 |
|
$parameterOutput = []; |
|
93
|
1 |
|
foreach ($this->parameters as $parameter) { |
|
94
|
1 |
|
$parameterOutput[] = $parameter->generate(); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
$output .= implode(', ', $parameterOutput); |
|
98
|
5 |
|
} |
|
99
|
5 |
|
|
|
100
|
|
|
$output .= ')'; |
|
101
|
5 |
|
|
|
102
|
4 |
|
if ($this->returnType !== null) { |
|
103
|
4 |
|
$output .= ' : ' . $this->returnType->generate(); |
|
104
|
4 |
|
} |
|
105
|
|
|
|
|
106
|
|
|
$output .= self::LINE_FEED . '{' . self::LINE_FEED; |
|
107
|
4 |
|
|
|
108
|
|
|
if ($this->functionBody !== '') { |
|
109
|
|
|
$output .= preg_replace('#^((?![a-zA-Z0-9_-]+;).+?)$#m', $indent . '$1', trim($this->functionBody)) |
|
110
|
5 |
|
. self::LINE_FEED; |
|
111
|
|
|
} |
|
112
|
5 |
|
|
|
113
|
1 |
|
$output .= '}' . self::LINE_FEED; |
|
114
|
|
|
|
|
115
|
|
|
return $output; |
|
116
|
5 |
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|