1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace ProxyManager\ProxyGenerator\ValueHolder\MethodGenerator; |
6
|
|
|
|
7
|
|
|
use ProxyManager\Generator\MethodGenerator; |
8
|
|
|
use ProxyManager\ProxyGenerator\Util\Properties; |
9
|
|
|
use ProxyManager\ProxyGenerator\Util\UnsetPropertiesGenerator; |
10
|
|
|
use ReflectionClass; |
11
|
|
|
use Zend\Code\Generator\ParameterGenerator; |
12
|
|
|
use Zend\Code\Generator\PropertyGenerator; |
13
|
|
|
use Zend\Code\Reflection\MethodReflection; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* The `__construct` implementation for lazy loading proxies |
17
|
|
|
* |
18
|
|
|
* @author Marco Pivetta <[email protected]> |
19
|
|
|
* @license MIT |
20
|
|
|
*/ |
21
|
|
|
class Constructor extends MethodGenerator |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @throws \Zend\Code\Generator\Exception\InvalidArgumentException |
25
|
|
|
*/ |
26
|
4 |
|
public static function generateMethod(ReflectionClass $originalClass, PropertyGenerator $valueHolder) : self |
27
|
|
|
{ |
28
|
4 |
|
$originalConstructor = self::getConstructor($originalClass); |
29
|
|
|
|
30
|
|
|
/* @var $constructor self */ |
31
|
4 |
|
$constructor = $originalConstructor |
32
|
1 |
|
? self::fromReflection($originalConstructor) |
33
|
4 |
|
: new self('__construct'); |
34
|
|
|
|
35
|
4 |
|
$constructor->setBody( |
36
|
|
|
'static $reflection;' . "\n\n" |
37
|
4 |
|
. 'if (! $this->' . $valueHolder->getName() . ') {' . "\n" |
38
|
4 |
|
. ' $reflection = $reflection ?: new \ReflectionClass(' |
39
|
4 |
|
. var_export($originalClass->getName(), true) |
40
|
4 |
|
. ");\n" |
41
|
4 |
|
. ' $this->' . $valueHolder->getName() . ' = $reflection->newInstanceWithoutConstructor();' . "\n" |
42
|
4 |
|
. UnsetPropertiesGenerator::generateSnippet(Properties::fromReflectionClass($originalClass), 'this') |
43
|
4 |
|
. '}' |
44
|
4 |
|
. self::generateOriginalConstructorCall($originalClass, $valueHolder) |
45
|
|
|
); |
46
|
|
|
|
47
|
4 |
|
return $constructor; |
48
|
|
|
} |
49
|
|
|
|
50
|
4 |
|
private static function generateOriginalConstructorCall( |
51
|
|
|
ReflectionClass $class, |
52
|
|
|
PropertyGenerator $valueHolder |
53
|
|
|
) : string { |
54
|
4 |
|
$originalConstructor = self::getConstructor($class); |
55
|
|
|
|
56
|
4 |
|
if (! $originalConstructor) { |
57
|
3 |
|
return ''; |
58
|
|
|
} |
59
|
|
|
|
60
|
1 |
|
$constructor = self::fromReflection($originalConstructor); |
61
|
|
|
|
62
|
|
|
return "\n\n" |
63
|
1 |
|
. '$this->' . $valueHolder->getName() . '->' . $constructor->getName() . '(' |
64
|
1 |
|
. implode( |
65
|
1 |
|
', ', |
66
|
1 |
|
array_map( |
67
|
|
|
function (ParameterGenerator $parameter) : string { |
68
|
1 |
|
return ($parameter->getVariadic() ? '...' : '') . '$' . $parameter->getName(); |
69
|
1 |
|
}, |
70
|
1 |
|
$constructor->getParameters() |
71
|
|
|
) |
72
|
|
|
) |
73
|
1 |
|
. ');'; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param ReflectionClass $class |
78
|
|
|
* |
79
|
|
|
* @return MethodReflection|null |
80
|
|
|
*/ |
81
|
4 |
|
private static function getConstructor(ReflectionClass $class) |
82
|
|
|
{ |
83
|
4 |
|
$constructors = array_map( |
84
|
|
|
function (\ReflectionMethod $method) : MethodReflection { |
85
|
1 |
|
return new MethodReflection( |
86
|
1 |
|
$method->getDeclaringClass()->getName(), |
87
|
1 |
|
$method->getName() |
88
|
|
|
); |
89
|
4 |
|
}, |
90
|
4 |
|
array_filter( |
91
|
4 |
|
$class->getMethods(), |
92
|
4 |
|
function (\ReflectionMethod $method) : bool { |
93
|
1 |
|
return $method->isConstructor(); |
94
|
4 |
|
} |
95
|
|
|
) |
96
|
|
|
); |
97
|
|
|
|
98
|
4 |
|
return reset($constructors) ?: null; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|