|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace ProxyManager\ProxyGenerator; |
|
6
|
|
|
|
|
7
|
|
|
use InvalidArgumentException; |
|
8
|
|
|
use Laminas\Code\Generator\ClassGenerator; |
|
9
|
|
|
use Laminas\Code\Generator\MethodGenerator; |
|
10
|
|
|
use Laminas\Code\Reflection\MethodReflection; |
|
11
|
|
|
use ProxyManager\Exception\InvalidProxiedClassException; |
|
12
|
|
|
use ProxyManager\Generator\Util\ClassGeneratorUtils; |
|
13
|
|
|
use ProxyManager\Proxy\AccessInterceptorValueHolderInterface; |
|
14
|
|
|
use ProxyManager\ProxyGenerator\AccessInterceptor\MethodGenerator\MagicWakeup; |
|
15
|
|
|
use ProxyManager\ProxyGenerator\AccessInterceptor\MethodGenerator\SetMethodPrefixInterceptor; |
|
16
|
|
|
use ProxyManager\ProxyGenerator\AccessInterceptor\MethodGenerator\SetMethodSuffixInterceptor; |
|
17
|
|
|
use ProxyManager\ProxyGenerator\AccessInterceptor\PropertyGenerator\MethodPrefixInterceptors; |
|
18
|
|
|
use ProxyManager\ProxyGenerator\AccessInterceptor\PropertyGenerator\MethodSuffixInterceptors; |
|
19
|
|
|
use ProxyManager\ProxyGenerator\AccessInterceptorValueHolder\MethodGenerator\InterceptedMethod; |
|
20
|
|
|
use ProxyManager\ProxyGenerator\AccessInterceptorValueHolder\MethodGenerator\MagicClone; |
|
21
|
|
|
use ProxyManager\ProxyGenerator\AccessInterceptorValueHolder\MethodGenerator\MagicGet; |
|
22
|
|
|
use ProxyManager\ProxyGenerator\AccessInterceptorValueHolder\MethodGenerator\MagicIsset; |
|
23
|
|
|
use ProxyManager\ProxyGenerator\AccessInterceptorValueHolder\MethodGenerator\MagicSet; |
|
24
|
|
|
use ProxyManager\ProxyGenerator\AccessInterceptorValueHolder\MethodGenerator\MagicUnset; |
|
25
|
|
|
use ProxyManager\ProxyGenerator\AccessInterceptorValueHolder\MethodGenerator\StaticProxyConstructor; |
|
26
|
|
|
use ProxyManager\ProxyGenerator\Assertion\CanProxyAssertion; |
|
27
|
|
|
use ProxyManager\ProxyGenerator\LazyLoadingValueHolder\PropertyGenerator\ValueHolderProperty; |
|
28
|
|
|
use ProxyManager\ProxyGenerator\PropertyGenerator\PublicPropertiesMap; |
|
29
|
|
|
use ProxyManager\ProxyGenerator\Util\Properties; |
|
30
|
|
|
use ProxyManager\ProxyGenerator\Util\ProxiedMethodsFilter; |
|
31
|
|
|
use ProxyManager\ProxyGenerator\ValueHolder\MethodGenerator\Constructor; |
|
32
|
|
|
use ProxyManager\ProxyGenerator\ValueHolder\MethodGenerator\GetWrappedValueHolderValue; |
|
33
|
|
|
use ProxyManager\ProxyGenerator\ValueHolder\MethodGenerator\MagicSleep; |
|
34
|
|
|
use ReflectionClass; |
|
35
|
|
|
use ReflectionMethod; |
|
36
|
|
|
use function array_map; |
|
37
|
|
|
use function array_merge; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Generator for proxies implementing {@see \ProxyManager\Proxy\ValueHolderInterface} |
|
41
|
|
|
* and {@see \ProxyManager\Proxy\AccessInterceptorInterface} |
|
42
|
|
|
* |
|
43
|
|
|
* {@inheritDoc} |
|
44
|
|
|
*/ |
|
45
|
|
|
class AccessInterceptorValueHolderGenerator implements ProxyGeneratorInterface |
|
46
|
|
|
{ |
|
47
|
|
|
/** |
|
48
|
|
|
* {@inheritDoc} |
|
49
|
|
|
* |
|
50
|
|
|
* @return void |
|
51
|
|
|
* |
|
52
|
|
|
* @throws InvalidArgumentException |
|
53
|
|
|
* @throws InvalidProxiedClassException |
|
54
|
|
|
*/ |
|
55
|
12 |
|
public function generate(ReflectionClass $originalClass, ClassGenerator $classGenerator) |
|
56
|
|
|
{ |
|
57
|
12 |
|
CanProxyAssertion::assertClassCanBeProxied($originalClass); |
|
58
|
|
|
|
|
59
|
12 |
|
$publicProperties = new PublicPropertiesMap(Properties::fromReflectionClass($originalClass)); |
|
60
|
12 |
|
$interfaces = [AccessInterceptorValueHolderInterface::class]; |
|
61
|
|
|
|
|
62
|
12 |
|
if ($originalClass->isInterface()) { |
|
63
|
5 |
|
$interfaces[] = $originalClass->getName(); |
|
64
|
|
|
} else { |
|
65
|
7 |
|
$classGenerator->setExtendedClass($originalClass->getName()); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
12 |
|
$classGenerator->setImplementedInterfaces($interfaces); |
|
69
|
12 |
|
$classGenerator->addPropertyFromGenerator($valueHolder = new ValueHolderProperty($originalClass)); |
|
70
|
12 |
|
$classGenerator->addPropertyFromGenerator($prefixInterceptors = new MethodPrefixInterceptors()); |
|
71
|
12 |
|
$classGenerator->addPropertyFromGenerator($suffixInterceptors = new MethodSuffixInterceptors()); |
|
72
|
12 |
|
$classGenerator->addPropertyFromGenerator($publicProperties); |
|
73
|
|
|
|
|
74
|
12 |
|
array_map( |
|
75
|
|
|
static function (MethodGenerator $generatedMethod) use ($originalClass, $classGenerator) : void { |
|
76
|
12 |
|
ClassGeneratorUtils::addMethodIfNotFinal($originalClass, $classGenerator, $generatedMethod); |
|
77
|
12 |
|
}, |
|
78
|
12 |
|
array_merge( |
|
79
|
12 |
|
array_map( |
|
80
|
12 |
|
$this->buildMethodInterceptor($prefixInterceptors, $suffixInterceptors, $valueHolder), |
|
81
|
12 |
|
ProxiedMethodsFilter::getProxiedMethods($originalClass) |
|
82
|
|
|
), |
|
83
|
|
|
[ |
|
84
|
12 |
|
Constructor::generateMethod($originalClass, $valueHolder), |
|
85
|
12 |
|
new StaticProxyConstructor($originalClass, $valueHolder, $prefixInterceptors, $suffixInterceptors), |
|
86
|
12 |
|
new GetWrappedValueHolderValue($valueHolder), |
|
87
|
12 |
|
new SetMethodPrefixInterceptor($prefixInterceptors), |
|
88
|
12 |
|
new SetMethodSuffixInterceptor($suffixInterceptors), |
|
89
|
12 |
|
new MagicGet( |
|
90
|
12 |
|
$originalClass, |
|
91
|
12 |
|
$valueHolder, |
|
92
|
12 |
|
$prefixInterceptors, |
|
93
|
12 |
|
$suffixInterceptors, |
|
94
|
12 |
|
$publicProperties |
|
95
|
|
|
), |
|
96
|
12 |
|
new MagicSet( |
|
97
|
12 |
|
$originalClass, |
|
98
|
12 |
|
$valueHolder, |
|
99
|
12 |
|
$prefixInterceptors, |
|
100
|
12 |
|
$suffixInterceptors, |
|
101
|
12 |
|
$publicProperties |
|
102
|
|
|
), |
|
103
|
12 |
|
new MagicIsset( |
|
104
|
12 |
|
$originalClass, |
|
105
|
12 |
|
$valueHolder, |
|
106
|
12 |
|
$prefixInterceptors, |
|
107
|
12 |
|
$suffixInterceptors, |
|
108
|
12 |
|
$publicProperties |
|
109
|
|
|
), |
|
110
|
12 |
|
new MagicUnset( |
|
111
|
12 |
|
$originalClass, |
|
112
|
12 |
|
$valueHolder, |
|
113
|
12 |
|
$prefixInterceptors, |
|
114
|
12 |
|
$suffixInterceptors, |
|
115
|
12 |
|
$publicProperties |
|
116
|
|
|
), |
|
117
|
12 |
|
new MagicClone($originalClass, $valueHolder, $prefixInterceptors, $suffixInterceptors), |
|
118
|
12 |
|
new MagicSleep($originalClass, $valueHolder), |
|
119
|
12 |
|
new MagicWakeup($originalClass), |
|
120
|
|
|
] |
|
121
|
|
|
) |
|
122
|
|
|
); |
|
123
|
12 |
|
} |
|
124
|
|
|
|
|
125
|
12 |
|
private function buildMethodInterceptor( |
|
126
|
|
|
MethodPrefixInterceptors $prefixes, |
|
127
|
|
|
MethodSuffixInterceptors $suffixes, |
|
128
|
|
|
ValueHolderProperty $valueHolder |
|
129
|
|
|
) : callable { |
|
130
|
|
|
return static function (ReflectionMethod $method) use ($prefixes, $suffixes, $valueHolder) : InterceptedMethod { |
|
131
|
9 |
|
return InterceptedMethod::generateMethod( |
|
132
|
9 |
|
new MethodReflection($method->getDeclaringClass()->getName(), $method->getName()), |
|
133
|
9 |
|
$valueHolder, |
|
134
|
9 |
|
$prefixes, |
|
135
|
9 |
|
$suffixes |
|
136
|
|
|
); |
|
137
|
12 |
|
}; |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
|