1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Isolate\LazyObjects\Proxy\Adapter\OcramiusProxyManager\ProxyGenerator; |
4
|
|
|
|
5
|
|
|
use Isolate\LazyObjects\Proxy\Adapter\OcramiusProxyManager\MethodGenerator\Constructor; |
6
|
|
|
use Isolate\LazyObjects\Proxy\Adapter\OcramiusProxyManager\MethodGenerator\GetLazyProperties; |
7
|
|
|
use Isolate\LazyObjects\Proxy\Adapter\OcramiusProxyManager\MethodGenerator\GetMethodReplacement; |
8
|
|
|
use Isolate\LazyObjects\Proxy\Adapter\OcramiusProxyManager\MethodGenerator\GetMethodReplacements; |
9
|
|
|
use Isolate\LazyObjects\Proxy\Adapter\OcramiusProxyManager\MethodGenerator\GetWrappedObject; |
10
|
|
|
use Isolate\LazyObjects\Proxy\Adapter\OcramiusProxyManager\MethodGenerator\HasMethodReplacement; |
11
|
|
|
use Isolate\LazyObjects\Proxy\Adapter\OcramiusProxyManager\MethodGenerator\MethodProxy; |
12
|
|
|
use Isolate\LazyObjects\Proxy\Adapter\OcramiusProxyManager\MethodGenerator\Sleep; |
13
|
|
|
use Isolate\LazyObjects\Proxy\Adapter\OcramiusProxyManager\PropertyGenerator\LazyProperties; |
14
|
|
|
use Isolate\LazyObjects\Proxy\Adapter\OcramiusProxyManager\PropertyGenerator\Initializer; |
15
|
|
|
use Isolate\LazyObjects\Proxy\Adapter\OcramiusProxyManager\PropertyGenerator\MethodReplacements; |
16
|
|
|
use Isolate\LazyObjects\Proxy\Adapter\OcramiusProxyManager\PropertyGenerator\WrappedObject; |
17
|
|
|
use ProxyManager\Generator\Util\ClassGeneratorUtils; |
18
|
|
|
use ProxyManager\ProxyGenerator\Assertion\CanProxyAssertion; |
19
|
|
|
use ProxyManager\ProxyGenerator\ProxyGeneratorInterface; |
20
|
|
|
use ProxyManager\ProxyGenerator\Util\ProxiedMethodsFilter; |
21
|
|
|
use ReflectionClass; |
22
|
|
|
use ReflectionMethod; |
23
|
|
|
use Zend\Code\Generator\ClassGenerator; |
24
|
|
|
use Zend\Code\Generator\MethodGenerator; |
25
|
|
|
use Zend\Code\Reflection\MethodReflection; |
26
|
|
|
|
27
|
|
|
class LazyObjectsProxyGenerator implements ProxyGeneratorInterface |
28
|
|
|
{ |
29
|
|
|
public function generate(ReflectionClass $originalClass, ClassGenerator $classGenerator) |
30
|
|
|
{ |
31
|
|
|
CanProxyAssertion::assertClassCanBeProxied($originalClass); |
32
|
|
|
|
33
|
|
|
$interfaces = ['Isolate\\LazyObjects\\WrappedObject']; |
34
|
|
|
|
35
|
|
|
if ($originalClass->isInterface()) { |
36
|
|
|
$interfaces[] = $originalClass->name; |
37
|
|
|
} else { |
38
|
|
|
$classGenerator->setExtendedClass($originalClass->name); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
$classGenerator->setImplementedInterfaces($interfaces); |
42
|
|
|
$classGenerator->addPropertyFromGenerator($wrappedObjectProperty = new WrappedObject()); |
43
|
|
|
$classGenerator->addPropertyFromGenerator($lazyPropertiesProperty = new LazyProperties()); |
44
|
|
|
$classGenerator->addPropertyFromGenerator($methodReplacementsProperty = new MethodReplacements()); |
45
|
|
|
$classGenerator->addPropertyFromGenerator($initializerProperty = new Initializer()); |
46
|
|
|
|
47
|
|
|
array_map( |
48
|
|
|
function (MethodGenerator $generatedMethod) use ($originalClass, $classGenerator) { |
49
|
|
|
ClassGeneratorUtils::addMethodIfNotFinal($originalClass, $classGenerator, $generatedMethod); |
50
|
|
|
}, |
51
|
|
|
array_merge( |
52
|
|
|
array_map( |
53
|
|
|
function (ReflectionMethod $method) use ($wrappedObjectProperty, $lazyPropertiesProperty, $initializerProperty) { |
54
|
|
|
return MethodProxy::generateMethod( |
55
|
|
|
new MethodReflection($method->getDeclaringClass()->name, $method->name), |
56
|
|
|
$wrappedObjectProperty, |
57
|
|
|
$lazyPropertiesProperty, |
58
|
|
|
$initializerProperty |
59
|
|
|
); |
60
|
|
|
}, |
61
|
|
|
ProxiedMethodsFilter::getProxiedMethods($originalClass) |
62
|
|
|
), |
63
|
|
|
[ |
64
|
|
|
new Constructor( |
65
|
|
|
$originalClass, |
66
|
|
|
$wrappedObjectProperty, |
67
|
|
|
$lazyPropertiesProperty, |
68
|
|
|
$methodReplacementsProperty, |
69
|
|
|
$initializerProperty |
70
|
|
|
), |
71
|
|
|
new Sleep( |
72
|
|
|
$wrappedObjectProperty, |
73
|
|
|
$initializerProperty, |
74
|
|
|
$lazyPropertiesProperty, |
75
|
|
|
$methodReplacementsProperty |
76
|
|
|
), |
77
|
|
|
new GetWrappedObject($wrappedObjectProperty), |
78
|
|
|
new GetLazyProperties($lazyPropertiesProperty), |
79
|
|
|
new GetMethodReplacements($methodReplacementsProperty), |
80
|
|
|
new HasMethodReplacement($methodReplacementsProperty), |
81
|
|
|
new GetMethodReplacement($methodReplacementsProperty) |
82
|
|
|
] |
83
|
|
|
) |
84
|
|
|
); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|