1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
4
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
5
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
6
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
7
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
8
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
9
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
10
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
11
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
12
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
13
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
14
|
|
|
* |
15
|
|
|
* This software consists of voluntary contributions made by many individuals |
16
|
|
|
* and is licensed under the MIT license. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
declare(strict_types=1); |
20
|
|
|
|
21
|
|
|
namespace ProxyManager\ProxyGenerator; |
22
|
|
|
|
23
|
|
|
use ProxyManager\Generator\Util\ClassGeneratorUtils; |
24
|
|
|
use ProxyManager\Proxy\AccessInterceptorInterface; |
25
|
|
|
use ProxyManager\ProxyGenerator\AccessInterceptor\MethodGenerator\SetMethodPrefixInterceptor; |
26
|
|
|
use ProxyManager\ProxyGenerator\AccessInterceptor\MethodGenerator\SetMethodSuffixInterceptor; |
27
|
|
|
use ProxyManager\ProxyGenerator\AccessInterceptor\PropertyGenerator\MethodPrefixInterceptors; |
28
|
|
|
use ProxyManager\ProxyGenerator\AccessInterceptor\PropertyGenerator\MethodSuffixInterceptors; |
29
|
|
|
use ProxyManager\ProxyGenerator\AccessInterceptorScopeLocalizer\MethodGenerator\BindProxyProperties; |
30
|
|
|
use ProxyManager\ProxyGenerator\AccessInterceptorScopeLocalizer\MethodGenerator\InterceptedMethod; |
31
|
|
|
use ProxyManager\ProxyGenerator\AccessInterceptorScopeLocalizer\MethodGenerator\MagicClone; |
32
|
|
|
use ProxyManager\ProxyGenerator\AccessInterceptorScopeLocalizer\MethodGenerator\MagicGet; |
33
|
|
|
use ProxyManager\ProxyGenerator\AccessInterceptorScopeLocalizer\MethodGenerator\MagicIsset; |
34
|
|
|
use ProxyManager\ProxyGenerator\AccessInterceptorScopeLocalizer\MethodGenerator\MagicSet; |
35
|
|
|
use ProxyManager\ProxyGenerator\AccessInterceptorScopeLocalizer\MethodGenerator\MagicSleep; |
36
|
|
|
use ProxyManager\ProxyGenerator\AccessInterceptorScopeLocalizer\MethodGenerator\MagicUnset; |
37
|
|
|
use ProxyManager\ProxyGenerator\AccessInterceptorScopeLocalizer\MethodGenerator\StaticProxyConstructor; |
38
|
|
|
use ProxyManager\ProxyGenerator\Assertion\CanProxyAssertion; |
39
|
|
|
use ProxyManager\ProxyGenerator\Util\ProxiedMethodsFilter; |
40
|
|
|
use ReflectionClass; |
41
|
|
|
use ReflectionMethod; |
42
|
|
|
use Zend\Code\Generator\ClassGenerator; |
43
|
|
|
use Zend\Code\Generator\MethodGenerator; |
44
|
|
|
use Zend\Code\Reflection\MethodReflection; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Generator for proxies implementing {@see \ProxyManager\Proxy\ValueHolderInterface} |
48
|
|
|
* and localizing scope of the proxied object at instantiation |
49
|
|
|
* |
50
|
|
|
* {@inheritDoc} |
51
|
|
|
* |
52
|
|
|
* @author Marco Pivetta <[email protected]> |
53
|
|
|
* @license MIT |
54
|
|
|
*/ |
55
|
|
|
class AccessInterceptorScopeLocalizerGenerator implements ProxyGeneratorInterface |
56
|
|
|
{ |
57
|
8 |
|
/** |
58
|
|
|
* {@inheritDoc} |
59
|
8 |
|
*/ |
60
|
|
|
public function generate(ReflectionClass $originalClass, ClassGenerator $classGenerator) |
61
|
6 |
|
{ |
62
|
6 |
|
CanProxyAssertion::assertClassCanBeProxied($originalClass, false); |
63
|
6 |
|
|
64
|
6 |
|
$classGenerator->setExtendedClass($originalClass->getName()); |
65
|
|
|
$classGenerator->setImplementedInterfaces([AccessInterceptorInterface::class]); |
66
|
6 |
|
$classGenerator->addPropertyFromGenerator($prefixInterceptors = new MethodPrefixInterceptors()); |
67
|
|
|
$classGenerator->addPropertyFromGenerator($suffixInterceptors = new MethodSuffixInterceptors()); |
68
|
6 |
|
|
69
|
6 |
|
array_map( |
70
|
|
|
function (MethodGenerator $generatedMethod) use ($originalClass, $classGenerator) { |
71
|
|
|
ClassGeneratorUtils::addMethodIfNotFinal($originalClass, $classGenerator, $generatedMethod); |
72
|
6 |
|
}, |
73
|
5 |
|
array_merge( |
74
|
5 |
|
array_map( |
75
|
|
|
$this->buildMethodInterceptor($prefixInterceptors, $suffixInterceptors), |
76
|
|
|
ProxiedMethodsFilter::getProxiedMethods( |
77
|
|
|
$originalClass, |
78
|
6 |
|
['__get', '__set', '__isset', '__unset', '__clone', '__sleep'] |
79
|
6 |
|
) |
80
|
|
|
), |
81
|
6 |
|
[ |
82
|
|
|
new StaticProxyConstructor($originalClass, $prefixInterceptors, $suffixInterceptors), |
83
|
|
|
new BindProxyProperties($originalClass, $prefixInterceptors, $suffixInterceptors), |
84
|
|
|
new SetMethodPrefixInterceptor($prefixInterceptors), |
85
|
6 |
|
new SetMethodSuffixInterceptor($suffixInterceptors), |
86
|
6 |
|
new MagicGet($originalClass, $prefixInterceptors, $suffixInterceptors), |
87
|
6 |
|
new MagicSet($originalClass, $prefixInterceptors, $suffixInterceptors), |
88
|
6 |
|
new MagicIsset($originalClass, $prefixInterceptors, $suffixInterceptors), |
89
|
6 |
|
new MagicUnset($originalClass, $prefixInterceptors, $suffixInterceptors), |
90
|
6 |
|
new MagicSleep($originalClass, $prefixInterceptors, $suffixInterceptors), |
91
|
6 |
|
new MagicClone($originalClass, $prefixInterceptors, $suffixInterceptors), |
92
|
6 |
|
] |
93
|
6 |
|
) |
94
|
6 |
|
); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
private function buildMethodInterceptor( |
98
|
6 |
|
MethodPrefixInterceptors $prefixInterceptors, |
99
|
|
|
MethodSuffixInterceptors $suffixInterceptors |
100
|
|
|
) : callable { |
|
|
|
|
101
|
|
|
return function (ReflectionMethod $method) use ($prefixInterceptors, $suffixInterceptors) : InterceptedMethod { |
102
|
|
|
return InterceptedMethod::generateMethod( |
103
|
|
|
new MethodReflection($method->getDeclaringClass()->getName(), $method->getName()), |
104
|
|
|
$prefixInterceptors, |
105
|
|
|
$suffixInterceptors |
106
|
|
|
); |
107
|
|
|
}; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|