1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
/* |
5
|
|
|
* Go! AOP framework |
6
|
|
|
* |
7
|
|
|
* @copyright Copyright 2011, Lisachenko Alexander <[email protected]> |
8
|
|
|
* |
9
|
|
|
* This source file is subject to the license that is bundled |
10
|
|
|
* with this source code in the file LICENSE. |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace Go\Aop\Framework; |
14
|
|
|
|
15
|
|
|
use Go\Aop\Intercept\ConstructorInvocation; |
16
|
|
|
use Go\Core\AspectContainer; |
17
|
|
|
use ReflectionClass; |
18
|
|
|
use ReflectionMethod; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Reflection constructor invocation implementation |
22
|
|
|
*/ |
23
|
|
|
class ReflectionConstructorInvocation extends AbstractInvocation implements ConstructorInvocation |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* Reflection class |
27
|
|
|
*/ |
28
|
|
|
protected ReflectionClass $class; |
|
|
|
|
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Instance of created class, can be used for Around or After types of advices. |
32
|
|
|
*/ |
33
|
|
|
protected ?object $instance = null; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Instance of reflection constructor for class (if present) |
37
|
|
|
*/ |
38
|
|
|
private ?ReflectionMethod $constructor; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Constructor for constructor invocation :) |
42
|
|
|
* |
43
|
|
|
* @param array $advices List of advices for this invocation |
44
|
|
|
*/ |
45
|
|
|
public function __construct(array $advices, string $className) |
46
|
|
|
{ |
47
|
|
|
$originalClass = $className; |
48
|
|
|
if (strpos($originalClass, AspectContainer::AOP_PROXIED_SUFFIX) !== false) { |
49
|
1 |
|
$originalClass = substr($originalClass, 0, -strlen(AspectContainer::AOP_PROXIED_SUFFIX)); |
50
|
|
|
} |
51
|
1 |
|
|
52
|
1 |
|
$this->class = new ReflectionClass($originalClass); |
53
|
|
|
$this->constructor = $constructor = $this->class->getConstructor(); |
54
|
|
|
|
55
|
1 |
|
// Give an access to call protected/private constructors |
56
|
|
|
if ($constructor !== null && !$constructor->isPublic()) { |
57
|
|
|
$constructor->setAccessible(true); |
58
|
|
|
} |
59
|
1 |
|
|
60
|
1 |
|
parent::__construct($advices); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Proceed to the next interceptor in the Chain |
65
|
|
|
* |
66
|
|
|
* Typically this method is called inside previous closure, as instance of Joinpoint is passed to callback |
67
|
|
|
* Do not call this method directly, only inside callback closures. |
68
|
|
|
* |
69
|
|
|
* @return object Covariant, always new object. |
70
|
|
|
*/ |
71
|
|
|
final public function proceed(): object |
72
|
|
|
{ |
73
|
|
|
if (isset($this->advices[$this->current])) { |
74
|
|
|
$currentInterceptor = $this->advices[$this->current]; |
75
|
|
|
$this->current++; |
76
|
|
|
|
77
|
|
|
return $currentInterceptor->invoke($this); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$this->instance = $this->class->newInstanceWithoutConstructor(); |
81
|
|
|
$constructor = $this->getConstructor(); |
82
|
|
|
if ($constructor !== null) { |
83
|
|
|
$constructor->invoke($this->instance, ...$this->arguments); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return $this->instance; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Gets the constructor being called or null if it is absent. |
91
|
|
|
*/ |
92
|
|
|
public function getConstructor(): ?ReflectionMethod |
93
|
|
|
{ |
94
|
|
|
return $this->constructor; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Returns the object for which current joinpoint is invoked |
99
|
|
|
* |
100
|
|
|
* @return object|null Instance of object or null if object hasn't been created yet (Before) |
101
|
|
|
*/ |
102
|
|
|
public function getThis(): ?object |
103
|
|
|
{ |
104
|
|
|
return $this->instance; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Invokes current constructor invocation with all interceptors |
109
|
|
|
*/ |
110
|
|
|
final public function __invoke(array $arguments = []): object |
111
|
|
|
{ |
112
|
|
|
$this->current = 0; |
113
|
|
|
$this->arguments = $arguments; |
114
|
|
|
|
115
|
|
|
return $this->proceed(); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Checks if the current joinpoint is dynamic or static |
120
|
|
|
* |
121
|
|
|
* Dynamic joinpoint contains a reference to an object that can be received via getThis() method call |
122
|
|
|
* |
123
|
|
|
* @see ClassJoinpoint::getThis() |
124
|
|
|
*/ |
125
|
|
|
public function isDynamic(): bool |
126
|
|
|
{ |
127
|
|
|
return true; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Returns the static scope name (class name) of this joinpoint. |
132
|
|
|
*/ |
133
|
|
|
public function getScope(): string |
134
|
|
|
{ |
135
|
|
|
return $this->class->getName(); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Returns a friendly description of current joinpoint |
140
|
|
|
*/ |
141
|
|
|
final public function __toString(): string |
142
|
|
|
{ |
143
|
|
|
return sprintf( |
144
|
|
|
'initialization(%s)', |
145
|
|
|
$this->getScope() |
146
|
|
|
); |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|