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\MethodGenerator; |
16
|
|
|
use LogicException; |
17
|
|
|
use ReflectionMethod; |
18
|
|
|
|
19
|
|
|
use function count; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Prepares the definition of intercepted constructor |
23
|
|
|
*/ |
24
|
|
|
final class InterceptedConstructorGenerator extends MethodGenerator |
25
|
|
|
{ |
26
|
|
|
private MethodGenerator $constructorGenerator; |
|
|
|
|
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* InterceptedConstructor |
30
|
|
|
* |
31
|
|
|
* @param array $interceptedProperties List of intercepted properties for the class |
32
|
|
|
* @param ReflectionMethod|null $constructor Instance of original constructor or null |
33
|
|
|
* @param MethodGenerator|null $constructorGenerator Constructor body generator (if present) |
34
|
|
|
* @param bool $useTypeWidening Should generator use parameter widening for PHP>=7.2 |
35
|
|
|
*/ |
36
|
|
|
public function __construct( |
37
|
|
|
array $interceptedProperties, |
38
|
|
|
ReflectionMethod $constructor = null, |
39
|
|
|
MethodGenerator $constructorGenerator = null, |
40
|
|
|
bool $useTypeWidening = false |
41
|
|
|
) { |
42
|
7 |
|
$constructorBody = count($interceptedProperties) > 0 ? $this->getConstructorBody($interceptedProperties) : ''; |
43
|
|
|
if ($constructor !== null && $constructor->isPrivate()) { |
44
|
|
|
throw new LogicException( |
45
|
|
|
"Constructor in the class {$constructor->class} is declared as private. " . |
46
|
|
|
'Properties could not be intercepted.' |
47
|
|
|
); |
48
|
7 |
|
} |
49
|
7 |
|
if ($constructor !== null) { |
50
|
1 |
|
if ($constructorGenerator === null) { |
51
|
1 |
|
$callArguments = new FunctionCallArgumentListGenerator($constructor); |
52
|
1 |
|
$splatPrefix = $constructor->getNumberOfParameters() > 0 ? '...' : ''; |
53
|
|
|
$parentCallBody = 'parent::__construct(' . $splatPrefix . $callArguments->generate() . ');'; |
54
|
|
|
$constructorGenerator = new InterceptedMethodGenerator($constructor, $parentCallBody, $useTypeWidening); |
55
|
6 |
|
} |
56
|
3 |
|
$constructorBody .= PHP_EOL . $constructorGenerator->getBody(); |
57
|
3 |
|
$constructorGenerator->setBody($constructorBody); |
58
|
3 |
|
} else { |
59
|
3 |
|
$constructorGenerator = new MethodGenerator('__construct', [], [], $constructorBody); |
60
|
3 |
|
} |
61
|
|
|
assert($constructorGenerator !== null, "Constructor generator should be initialized"); |
62
|
3 |
|
$this->constructorGenerator = $constructorGenerator; |
63
|
3 |
|
} |
64
|
|
|
|
65
|
3 |
|
/** |
66
|
|
|
* @inheritdoc |
67
|
6 |
|
*/ |
68
|
6 |
|
public function generate(): string |
69
|
|
|
{ |
70
|
|
|
return $this->constructorGenerator->generate(); |
71
|
|
|
} |
72
|
|
|
|
73
|
6 |
|
/** |
74
|
|
|
* Returns constructor code |
75
|
6 |
|
* |
76
|
|
|
* @param array $interceptedProperties List of properties to intercept |
77
|
|
|
*/ |
78
|
|
|
private function getConstructorBody(array $interceptedProperties): string |
79
|
|
|
{ |
80
|
|
|
$assocProperties = []; |
81
|
|
|
$listProperties = []; |
82
|
|
|
foreach ($interceptedProperties as $propertyName) { |
83
|
3 |
|
$assocProperties[] = " '{$propertyName}' => &\$target->{$propertyName}"; |
84
|
|
|
$listProperties[] = " \$target->{$propertyName}"; |
85
|
3 |
|
} |
86
|
3 |
|
$lines = [ |
87
|
3 |
|
'$accessor = function(array &$propertyStorage, object $target) {', |
88
|
3 |
|
' $propertyStorage = [', |
89
|
3 |
|
implode(',' . PHP_EOL, $assocProperties), |
90
|
|
|
' ];', |
91
|
|
|
' unset(', |
92
|
3 |
|
implode(',' . PHP_EOL, $listProperties), |
93
|
3 |
|
' );', |
94
|
3 |
|
'};', |
95
|
3 |
|
'($accessor->bindTo($this, parent::class))($this->__properties, $this);' |
96
|
3 |
|
]; |
97
|
3 |
|
|
98
|
3 |
|
return implode(PHP_EOL, $lines); |
99
|
3 |
|
} |
100
|
|
|
} |
101
|
|
|
|