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