1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
/* |
4
|
|
|
* Go! AOP framework |
5
|
|
|
* |
6
|
|
|
* @copyright Copyright 2011, Lisachenko Alexander <[email protected]> |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the license that is bundled |
9
|
|
|
* with this source code in the file LICENSE. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Go\Aop\Framework; |
13
|
|
|
|
14
|
|
|
use Go\Aop\AspectException; |
15
|
|
|
use Go\Aop\Intercept\FieldAccess; |
16
|
|
|
use Go\Aop\Support\AnnotatedReflectionProperty; |
17
|
|
|
use ReflectionProperty; |
18
|
|
|
use function get_class; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Represents a field access joinpoint |
22
|
|
|
*/ |
23
|
|
|
class ClassFieldAccess extends AbstractJoinpoint implements FieldAccess |
24
|
|
|
{ |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Instance of object for accessing |
28
|
|
|
* |
29
|
|
|
* @var object |
30
|
|
|
*/ |
31
|
|
|
protected $instance; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Instance of reflection property |
35
|
|
|
* |
36
|
|
|
* @var ReflectionProperty |
37
|
|
|
*/ |
38
|
|
|
protected $reflectionProperty; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* New value to set |
42
|
|
|
* |
43
|
|
|
* @var mixed |
44
|
|
|
*/ |
45
|
|
|
protected $newValue; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Access type for field access |
49
|
|
|
* |
50
|
|
|
* @var integer |
51
|
|
|
*/ |
52
|
|
|
private $accessType; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Copy of the original value of property |
56
|
|
|
* |
57
|
|
|
* @var mixed |
58
|
|
|
*/ |
59
|
|
|
private $value; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Constructor for field access |
63
|
|
|
* |
64
|
|
|
* @param array $advices List of advices for this invocation |
65
|
|
|
*/ |
66
|
4 |
|
public function __construct(string $className, string $fieldName, array $advices) |
67
|
|
|
{ |
68
|
4 |
|
parent::__construct($advices); |
69
|
|
|
|
70
|
4 |
|
$this->reflectionProperty = $reflectionProperty = new AnnotatedReflectionProperty($className, $fieldName); |
71
|
|
|
// Give an access to protected field |
72
|
4 |
|
if ($reflectionProperty->isProtected()) { |
73
|
4 |
|
$reflectionProperty->setAccessible(true); |
74
|
|
|
} |
75
|
4 |
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Returns the access type. |
79
|
|
|
*/ |
80
|
|
|
public function getAccessType(): int |
81
|
|
|
{ |
82
|
|
|
return $this->accessType; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Gets the field being accessed. |
87
|
|
|
* |
88
|
|
|
* @return AnnotatedReflectionProperty the property which is being accessed. |
89
|
|
|
*/ |
90
|
3 |
|
public function getField(): ReflectionProperty |
91
|
|
|
{ |
92
|
3 |
|
return $this->reflectionProperty; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Gets the current value of property |
97
|
|
|
*/ |
98
|
|
|
public function &getValue() |
99
|
|
|
{ |
100
|
|
|
$value = &$this->value; |
101
|
|
|
|
102
|
|
|
return $value; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Gets the value that must be set to the field. |
107
|
|
|
*/ |
108
|
|
|
public function &getValueToSet() |
109
|
|
|
{ |
110
|
|
|
$newValue = &$this->newValue; |
111
|
|
|
|
112
|
|
|
return $newValue; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Checks scope rules for accessing property |
117
|
|
|
*/ |
118
|
|
|
public function ensureScopeRule(int $stackLevel = 2): void |
119
|
|
|
{ |
120
|
|
|
$property = $this->reflectionProperty; |
121
|
|
|
$isProtected = $property->isProtected(); |
122
|
|
|
$isPrivate = $property->isPrivate(); |
123
|
|
|
if ($isProtected || $isPrivate) { |
124
|
|
|
$backTrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, $stackLevel+1); |
125
|
|
|
$accessor = $backTrace[$stackLevel] ?? []; |
126
|
|
|
$propertyClass = $property->class; |
127
|
|
|
if (isset($accessor['class'])) { |
128
|
|
|
// For private and protected properties its ok to access from the same class |
129
|
|
|
if ($accessor['class'] === $propertyClass) { |
130
|
|
|
return; |
131
|
|
|
} |
132
|
|
|
// For protected properties its ok to access from any subclass |
133
|
|
|
if ($isProtected && is_subclass_of($accessor['class'], $propertyClass)) { |
134
|
|
|
return; |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
throw new AspectException("Cannot access property {$propertyClass}::{$property->name}"); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Proceed to the next interceptor in the Chain |
143
|
|
|
* |
144
|
|
|
* @return void For field interceptor there is no return values |
145
|
|
|
*/ |
146
|
|
|
final public function proceed() |
147
|
|
|
{ |
148
|
|
|
if (isset($this->advices[$this->current])) { |
149
|
|
|
$currentInterceptor = $this->advices[$this->current++]; |
150
|
|
|
|
151
|
|
|
$currentInterceptor->invoke($this); |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Invokes current field access with all interceptors |
157
|
|
|
* |
158
|
|
|
* @param object $instance Instance of object |
159
|
|
|
* @param integer $accessType Type of access: READ or WRITE |
160
|
|
|
* @param mixed $originalValue Original value of property |
161
|
|
|
* @param mixed $newValue New value to set |
162
|
|
|
* |
163
|
|
|
* @return mixed |
164
|
|
|
*/ |
165
|
|
|
final public function &__invoke(object $instance, int $accessType, &$originalValue, $newValue = NAN) |
166
|
|
|
{ |
167
|
|
|
if ($this->level > 0) { |
168
|
|
|
$this->stackFrames[] = [$this->instance, $this->accessType, &$this->value, &$this->newValue]; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
try { |
172
|
|
|
++$this->level; |
173
|
|
|
|
174
|
|
|
$this->current = 0; |
175
|
|
|
$this->instance = $instance; |
176
|
|
|
$this->accessType = $accessType; |
177
|
|
|
$this->value = &$originalValue; |
178
|
|
|
$this->newValue = $newValue; |
179
|
|
|
|
180
|
|
|
$this->proceed(); |
181
|
|
|
|
182
|
|
|
if ($accessType === self::READ) { |
183
|
|
|
$result = &$this->value; |
184
|
|
|
} else { |
185
|
|
|
$result = &$this->newValue; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
return $result; |
189
|
|
|
} finally { |
190
|
|
|
--$this->level; |
191
|
|
|
|
192
|
|
|
if ($this->level > 0) { |
193
|
|
|
[$this->instance, $this->accessType, $this->value, $this->newValue] = array_pop($this->stackFrames); |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Returns the object for which current joinpoint is invoked |
200
|
|
|
* |
201
|
|
|
* @return object|null Instance of object or null for static call/unavailable context |
202
|
|
|
*/ |
203
|
|
|
final public function getThis(): ?object |
204
|
|
|
{ |
205
|
|
|
return $this->instance; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Checks if the current joinpoint is dynamic or static |
210
|
|
|
* |
211
|
|
|
* Dynamic joinpoint contains a reference to an object that can be received via getThis() method call |
212
|
|
|
* |
213
|
|
|
* @see ClassJoinpoint::getThis() |
214
|
|
|
*/ |
215
|
1 |
|
final public function isDynamic(): bool |
216
|
|
|
{ |
217
|
1 |
|
return true; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* Returns the static scope name (class name) of this joinpoint. |
222
|
|
|
*/ |
223
|
|
|
final public function getScope(): string |
224
|
|
|
{ |
225
|
|
|
// $this->instance always contains an object, so we can take it's name as a scope name |
226
|
|
|
return get_class($this->instance); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* Returns a friendly description of current joinpoint |
231
|
|
|
*/ |
232
|
|
|
final public function __toString(): string |
233
|
|
|
{ |
234
|
|
|
return sprintf( |
235
|
|
|
'%s(%s->%s)', |
236
|
|
|
$this->accessType === self::READ ? 'get' : 'set', |
237
|
|
|
$this->getScope(), |
238
|
|
|
$this->reflectionProperty->name |
239
|
|
|
); |
240
|
|
|
} |
241
|
|
|
} |
242
|
|
|
|