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 Closure; |
16
|
|
|
use Go\Aop\Intercept\Interceptor; |
17
|
|
|
use Go\Core\AspectKernel; |
18
|
|
|
use ReflectionFunction; |
19
|
|
|
use ReflectionMethod; |
20
|
|
|
use Serializable; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Base class for all framework interceptor implementations |
24
|
|
|
* |
25
|
|
|
* This class describe an action taken by the interceptor at a particular joinpoint. |
26
|
|
|
* Different types of interceptors include "around", "before" and "after" advices. |
27
|
|
|
* |
28
|
|
|
* Around interceptor is an advice that surrounds a joinpoint such as a method invocation. This is the most powerful |
29
|
|
|
* kind of advice. Around advices will perform custom behavior before and after the method invocation. They are |
30
|
|
|
* responsible for choosing whether to proceed to the joinpoint or to shortcut executing by returning their own return |
31
|
|
|
* value or throwing an exception. |
32
|
|
|
* |
33
|
|
|
* After and before interceptors are simple closures that will be invoked after and before main invocation. |
34
|
|
|
* |
35
|
|
|
* Framework models an interceptor as an PHP-closure, maintaining a chain of interceptors "around" the joinpoint: |
36
|
|
|
* public function (Joinpoint $joinPoint) |
37
|
|
|
* { |
38
|
|
|
* echo 'Before action'; |
39
|
|
|
* // call chain here with Joinpoint->proceed() method |
40
|
|
|
* $result = $joinPoint->proceed(); |
41
|
|
|
* echo 'After action'; |
42
|
|
|
* |
43
|
|
|
* return $result; |
44
|
|
|
* } |
45
|
|
|
*/ |
46
|
|
|
abstract class AbstractInterceptor implements Interceptor, OrderedAdvice, Serializable |
47
|
|
|
{ |
48
|
|
|
/** |
49
|
|
|
* Local cache of advices for faster unserialization on big projects |
50
|
|
|
* |
51
|
|
|
* @var array<Closure> |
52
|
|
|
*/ |
53
|
|
|
protected static array $localAdvicesCache = []; |
|
|
|
|
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Pointcut expression string which was used for this interceptor |
57
|
|
|
*/ |
58
|
|
|
protected string $pointcutExpression; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Closure to call |
62
|
|
|
*/ |
63
|
|
|
protected Closure $adviceMethod; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Advice order |
67
|
|
|
*/ |
68
|
|
|
private int $adviceOrder; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Default constructor for interceptor |
72
|
|
|
*/ |
73
|
12 |
|
public function __construct(Closure $adviceMethod, int $adviceOrder = 0, string $pointcutExpression = '') |
74
|
|
|
{ |
75
|
12 |
|
$this->adviceMethod = $adviceMethod; |
76
|
12 |
|
$this->adviceOrder = $adviceOrder; |
77
|
12 |
|
$this->pointcutExpression = $pointcutExpression; |
78
|
12 |
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Serialize advice method into array |
82
|
|
|
*/ |
83
|
1 |
|
public static function serializeAdvice(Closure $adviceMethod): array |
84
|
|
|
{ |
85
|
1 |
|
$refAdvice = new ReflectionFunction($adviceMethod); |
86
|
|
|
|
87
|
|
|
return [ |
88
|
1 |
|
'method' => $refAdvice->name, |
89
|
1 |
|
'class' => $refAdvice->getClosureScopeClass()->name |
90
|
|
|
]; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Unserialize an advice |
95
|
|
|
* |
96
|
|
|
* @param array $adviceData Information about advice |
97
|
|
|
*/ |
98
|
|
|
public static function unserializeAdvice(array $adviceData): Closure |
99
|
|
|
{ |
100
|
|
|
$aspectName = $adviceData['class']; |
101
|
|
|
$methodName = $adviceData['method']; |
102
|
|
|
|
103
|
|
|
if (!isset(static::$localAdvicesCache["$aspectName->$methodName"])) { |
104
|
|
|
$aspect = AspectKernel::getInstance()->getContainer()->getAspect($aspectName); |
105
|
|
|
$refMethod = new ReflectionMethod($aspectName, $methodName); |
106
|
|
|
$advice = $refMethod->getClosure($aspect); |
107
|
|
|
|
108
|
|
|
static::$localAdvicesCache["$aspectName->$methodName"] = $advice; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
return static::$localAdvicesCache["$aspectName->$methodName"]; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Returns the advice order |
116
|
|
|
*/ |
117
|
|
|
public function getAdviceOrder(): int |
118
|
|
|
{ |
119
|
|
|
return $this->adviceOrder; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Getter for extracting the advice closure from Interceptor |
124
|
|
|
*/ |
125
|
2 |
|
public function getRawAdvice(): Closure |
126
|
|
|
{ |
127
|
2 |
|
return $this->adviceMethod; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Serializes an interceptor into string representation |
132
|
|
|
*/ |
133
|
2 |
|
final public function serialize(): string |
134
|
|
|
{ |
135
|
2 |
|
$vars = array_filter(get_object_vars($this)); |
136
|
2 |
|
|
137
|
|
|
$vars['adviceMethod'] = static::serializeAdvice($this->adviceMethod); |
138
|
2 |
|
|
139
|
|
|
return serialize($vars); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Unserialize an interceptor from the string |
144
|
|
|
* |
145
|
|
|
* @param string $serialized The string representation of the object. |
146
|
1 |
|
*/ |
147
|
|
|
final public function unserialize($serialized): void |
148
|
1 |
|
{ |
149
|
1 |
|
$vars = unserialize($serialized, ['allowed_classes' => false]); |
150
|
1 |
|
|
151
|
1 |
|
$vars['adviceMethod'] = static::unserializeAdvice($vars['adviceMethod']); |
152
|
|
|
foreach ($vars as $key => $value) { |
153
|
1 |
|
$this->$key = $value; |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|