1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace ProxyManager\ProxyGenerator\AccessInterceptorValueHolder\MethodGenerator; |
6
|
|
|
|
7
|
|
|
use Laminas\Code\Generator\Exception\InvalidArgumentException; |
8
|
|
|
use Laminas\Code\Generator\ParameterGenerator; |
9
|
|
|
use Laminas\Code\Generator\PropertyGenerator; |
10
|
|
|
use ProxyManager\Generator\MethodGenerator; |
11
|
|
|
use ProxyManager\ProxyGenerator\Util\Properties; |
12
|
|
|
use ProxyManager\ProxyGenerator\Util\UnsetPropertiesGenerator; |
13
|
|
|
use ReflectionClass; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* The `staticProxyConstructor` implementation for access interceptor value holders |
17
|
|
|
*/ |
18
|
|
|
class StaticProxyConstructor extends MethodGenerator |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* Constructor |
22
|
|
|
* |
23
|
|
|
* @throws InvalidArgumentException |
24
|
|
|
*/ |
25
|
|
|
public function __construct( |
26
|
|
|
ReflectionClass $originalClass, |
27
|
3 |
|
PropertyGenerator $valueHolder, |
28
|
|
|
PropertyGenerator $prefixInterceptors, |
29
|
|
|
PropertyGenerator $suffixInterceptors |
30
|
|
|
) { |
31
|
|
|
parent::__construct('staticProxyConstructor', [], self::FLAG_PUBLIC | self::FLAG_STATIC); |
32
|
|
|
|
33
|
3 |
|
$prefix = new ParameterGenerator('prefixInterceptors'); |
34
|
|
|
$suffix = new ParameterGenerator('suffixInterceptors'); |
35
|
3 |
|
|
36
|
3 |
|
$prefix->setDefaultValue([]); |
37
|
|
|
$suffix->setDefaultValue([]); |
38
|
3 |
|
$prefix->setType('array'); |
39
|
3 |
|
$suffix->setType('array'); |
40
|
3 |
|
|
41
|
3 |
|
$this->setParameter(new ParameterGenerator('wrappedObject')); |
42
|
|
|
$this->setParameter($prefix); |
43
|
3 |
|
$this->setParameter($suffix); |
44
|
3 |
|
$this->setReturnType($originalClass->getName()); |
45
|
3 |
|
|
46
|
3 |
|
$this->setDocBlock( |
47
|
|
|
"Constructor to setup interceptors\n\n" |
48
|
3 |
|
. '@param \\' . $originalClass->getName() . " \$wrappedObject\n" |
49
|
|
|
. "@param \\Closure[] \$prefixInterceptors method interceptors to be used before method logic\n" |
50
|
3 |
|
. "@param \\Closure[] \$suffixInterceptors method interceptors to be used before method logic\n\n" |
51
|
3 |
|
. '@return self' |
52
|
3 |
|
); |
53
|
3 |
|
|
54
|
|
|
$this->setBody( |
55
|
|
|
'static $reflection;' . "\n\n" |
56
|
3 |
|
. '$reflection = $reflection ?? new \ReflectionClass(__CLASS__);' . "\n" |
57
|
|
|
. '$instance = $reflection->newInstanceWithoutConstructor();' . "\n\n" |
58
|
|
|
. UnsetPropertiesGenerator::generateSnippet(Properties::fromReflectionClass($originalClass), 'instance') |
59
|
|
|
. '$instance->' . $valueHolder->getName() . " = \$wrappedObject;\n" |
60
|
3 |
|
. '$instance->' . $prefixInterceptors->getName() . " = \$prefixInterceptors;\n" |
61
|
3 |
|
. '$instance->' . $suffixInterceptors->getName() . " = \$suffixInterceptors;\n\n" |
62
|
3 |
|
. 'return $instance;' |
63
|
3 |
|
); |
64
|
3 |
|
} |
65
|
|
|
} |
66
|
|
|
|