|
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\Reflection\MethodReflection; |
|
10
|
|
|
use ProxyManager\Exception\InvalidProxiedClassException; |
|
11
|
|
|
use ProxyManager\Generator\Util\ClassGeneratorUtils; |
|
12
|
|
|
use ProxyManager\Proxy\NullObjectInterface; |
|
13
|
|
|
use ProxyManager\ProxyGenerator\Assertion\CanProxyAssertion; |
|
14
|
|
|
use ProxyManager\ProxyGenerator\NullObject\MethodGenerator\NullObjectMethodInterceptor; |
|
15
|
|
|
use ProxyManager\ProxyGenerator\NullObject\MethodGenerator\StaticProxyConstructor; |
|
16
|
|
|
use ProxyManager\ProxyGenerator\Util\ProxiedMethodsFilter; |
|
17
|
|
|
use ReflectionClass; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Generator for proxies implementing {@see \ProxyManager\Proxy\NullObjectInterface} |
|
21
|
|
|
* |
|
22
|
|
|
* {@inheritDoc} |
|
23
|
|
|
*/ |
|
24
|
|
|
class NullObjectGenerator implements ProxyGeneratorInterface |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* {@inheritDoc} |
|
28
|
|
|
* |
|
29
|
|
|
* @throws InvalidProxiedClassException |
|
30
|
|
|
* @throws InvalidArgumentException |
|
31
|
|
|
*/ |
|
32
|
|
|
public function generate(ReflectionClass $originalClass, ClassGenerator $classGenerator) : void |
|
33
|
5 |
|
{ |
|
34
|
|
|
CanProxyAssertion::assertClassCanBeProxied($originalClass); |
|
35
|
5 |
|
|
|
36
|
|
|
$interfaces = [NullObjectInterface::class]; |
|
37
|
5 |
|
|
|
38
|
|
|
if ($originalClass->isInterface()) { |
|
39
|
5 |
|
$interfaces[] = $originalClass->getName(); |
|
40
|
1 |
|
} else { |
|
41
|
|
|
$classGenerator->setExtendedClass($originalClass->getName()); |
|
42
|
4 |
|
} |
|
43
|
|
|
|
|
44
|
|
|
$classGenerator->setImplementedInterfaces($interfaces); |
|
45
|
5 |
|
|
|
46
|
|
|
foreach (ProxiedMethodsFilter::getProxiedMethods($originalClass, []) as $method) { |
|
47
|
5 |
|
$classGenerator->addMethodFromGenerator( |
|
48
|
4 |
|
NullObjectMethodInterceptor::generateMethod( |
|
49
|
4 |
|
new MethodReflection($method->getDeclaringClass()->getName(), $method->getName()) |
|
50
|
4 |
|
) |
|
51
|
|
|
); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
ClassGeneratorUtils::addMethodIfNotFinal( |
|
55
|
5 |
|
$originalClass, |
|
56
|
5 |
|
$classGenerator, |
|
57
|
5 |
|
new StaticProxyConstructor($originalClass) |
|
58
|
5 |
|
); |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|