|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace ProxyManager\ProxyGenerator; |
|
6
|
|
|
|
|
7
|
|
|
use Laminas\Code\Generator\ClassGenerator; |
|
8
|
|
|
use Laminas\Code\Generator\Exception\InvalidArgumentException; |
|
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\RemoteObjectInterface; |
|
14
|
|
|
use ProxyManager\ProxyGenerator\Assertion\CanProxyAssertion; |
|
15
|
|
|
use ProxyManager\ProxyGenerator\RemoteObject\MethodGenerator\MagicGet; |
|
16
|
|
|
use ProxyManager\ProxyGenerator\RemoteObject\MethodGenerator\MagicIsset; |
|
17
|
|
|
use ProxyManager\ProxyGenerator\RemoteObject\MethodGenerator\MagicSet; |
|
18
|
|
|
use ProxyManager\ProxyGenerator\RemoteObject\MethodGenerator\MagicUnset; |
|
19
|
|
|
use ProxyManager\ProxyGenerator\RemoteObject\MethodGenerator\RemoteObjectMethod; |
|
20
|
|
|
use ProxyManager\ProxyGenerator\RemoteObject\MethodGenerator\StaticProxyConstructor; |
|
21
|
|
|
use ProxyManager\ProxyGenerator\RemoteObject\PropertyGenerator\AdapterProperty; |
|
22
|
|
|
use ProxyManager\ProxyGenerator\Util\ProxiedMethodsFilter; |
|
23
|
|
|
use ReflectionClass; |
|
24
|
|
|
use ReflectionMethod; |
|
25
|
|
|
use function array_map; |
|
26
|
|
|
use function array_merge; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Generator for proxies implementing {@see \ProxyManager\Proxy\RemoteObjectInterface} |
|
30
|
|
|
* |
|
31
|
|
|
* {@inheritDoc} |
|
32
|
|
|
*/ |
|
33
|
|
|
class RemoteObjectGenerator implements ProxyGeneratorInterface |
|
34
|
|
|
{ |
|
35
|
|
|
/** |
|
36
|
|
|
* {@inheritDoc} |
|
37
|
|
|
* |
|
38
|
|
|
* @return void |
|
39
|
|
|
* |
|
40
|
|
|
* @throws InvalidProxiedClassException |
|
41
|
|
|
* @throws InvalidArgumentException |
|
42
|
5 |
|
*/ |
|
43
|
|
|
public function generate(ReflectionClass $originalClass, ClassGenerator $classGenerator) |
|
44
|
5 |
|
{ |
|
45
|
|
|
CanProxyAssertion::assertClassCanBeProxied($originalClass); |
|
46
|
5 |
|
|
|
47
|
|
|
$interfaces = [RemoteObjectInterface::class]; |
|
48
|
5 |
|
|
|
49
|
1 |
|
if ($originalClass->isInterface()) { |
|
50
|
|
|
$interfaces[] = $originalClass->getName(); |
|
51
|
4 |
|
} else { |
|
52
|
|
|
$classGenerator->setExtendedClass($originalClass->getName()); |
|
53
|
|
|
} |
|
54
|
5 |
|
|
|
55
|
5 |
|
$classGenerator->setImplementedInterfaces($interfaces); |
|
56
|
|
|
$classGenerator->addPropertyFromGenerator($adapter = new AdapterProperty()); |
|
57
|
5 |
|
|
|
58
|
|
|
array_map( |
|
59
|
5 |
|
static function (MethodGenerator $generatedMethod) use ($originalClass, $classGenerator) : void { |
|
60
|
5 |
|
ClassGeneratorUtils::addMethodIfNotFinal($originalClass, $classGenerator, $generatedMethod); |
|
61
|
5 |
|
}, |
|
62
|
5 |
|
array_merge( |
|
63
|
|
|
array_map( |
|
64
|
4 |
|
static function (ReflectionMethod $method) use ($adapter, $originalClass) : RemoteObjectMethod { |
|
65
|
4 |
|
return RemoteObjectMethod::generateMethod( |
|
66
|
4 |
|
new MethodReflection($method->getDeclaringClass()->getName(), $method->getName()), |
|
67
|
4 |
|
$adapter, |
|
68
|
|
|
$originalClass |
|
69
|
5 |
|
); |
|
70
|
5 |
|
}, |
|
71
|
5 |
|
ProxiedMethodsFilter::getProxiedMethods( |
|
72
|
5 |
|
$originalClass, |
|
73
|
|
|
['__get', '__set', '__isset', '__unset'] |
|
74
|
|
|
) |
|
75
|
|
|
), |
|
76
|
5 |
|
[ |
|
77
|
5 |
|
new StaticProxyConstructor($originalClass, $adapter), |
|
78
|
5 |
|
new MagicGet($originalClass, $adapter), |
|
79
|
5 |
|
new MagicSet($originalClass, $adapter), |
|
80
|
5 |
|
new MagicIsset($originalClass, $adapter), |
|
81
|
|
|
new MagicUnset($originalClass, $adapter), |
|
82
|
|
|
] |
|
83
|
|
|
) |
|
84
|
5 |
|
); |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|