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