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\Advice; |
16
|
|
|
use Go\Aop\AdviceAfter; |
17
|
|
|
use Go\Aop\AdviceAround; |
18
|
|
|
use Go\Aop\AdviceBefore; |
19
|
|
|
use Go\Aop\Intercept\Interceptor; |
20
|
|
|
use Go\Aop\Intercept\Joinpoint; |
21
|
|
|
|
22
|
|
|
use function is_array; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Abstract joinpoint for framework |
26
|
|
|
* |
27
|
|
|
* Join points are points in the execution of the system, such as method calls, |
28
|
|
|
* where behavior supplied by aspects is combined. A join point is a point in |
29
|
|
|
* the execution of the program, which is used to define the dynamic structure |
30
|
|
|
* of a crosscutting concern. |
31
|
|
|
* |
32
|
|
|
* @link http://en.wikipedia.org/wiki/Aspect-oriented_software_development#Join_point_model |
33
|
|
|
*/ |
34
|
|
|
abstract class AbstractJoinpoint implements Joinpoint |
35
|
|
|
{ |
36
|
|
|
/** |
37
|
|
|
* List of advices (interceptors) |
38
|
|
|
* |
39
|
|
|
* NB: All current children assume that each advice is Interceptor now. |
40
|
|
|
* Whereas, it isn't correct logically, this can be used to satisfy PHPStan check now. |
41
|
|
|
* |
42
|
|
|
* @var array<Interceptor> |
43
|
|
|
*/ |
44
|
|
|
protected array $advices = []; |
|
|
|
|
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Current advice index |
48
|
|
|
*/ |
49
|
|
|
protected int $current = 0; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Stack frames to work with recursive calls or with cross-calls inside object |
53
|
|
|
*/ |
54
|
|
|
protected array $stackFrames = []; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Recursion level for invocation |
58
|
|
|
*/ |
59
|
|
|
protected int $level = 0; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Initializes list of advices for current joinpoint |
63
|
|
|
* |
64
|
|
|
* @param array<Interceptor> $advices List of advices (interceptors) |
65
|
|
|
*/ |
66
|
|
|
public function __construct(array $advices) |
67
|
27 |
|
{ |
68
|
|
|
$this->advices = $advices; |
69
|
27 |
|
} |
70
|
27 |
|
|
71
|
|
|
/** |
72
|
|
|
* Sorts advices by priority |
73
|
|
|
* |
74
|
|
|
* @param array<Advice|Interceptor> $advices |
75
|
|
|
* |
76
|
|
|
* @return array<Advice|Interceptor> Sorted list of advices |
77
|
|
|
*/ |
78
|
14 |
|
public static function sortAdvices(array $advices): array |
79
|
|
|
{ |
80
|
14 |
|
$sortedAdvices = $advices; |
81
|
|
|
uasort( |
82
|
|
|
$sortedAdvices, |
83
|
9 |
|
function (Advice $first, Advice $second) { |
84
|
4 |
|
switch (true) { |
85
|
|
|
case $first instanceof AdviceBefore && !($second instanceof AdviceBefore): |
86
|
6 |
|
return -1; |
87
|
3 |
|
|
88
|
|
|
case $first instanceof AdviceAround && !($second instanceof AdviceAround): |
89
|
4 |
|
return 1; |
90
|
2 |
|
|
91
|
|
|
case $first instanceof AdviceAfter && !($second instanceof AdviceAfter): |
92
|
2 |
|
return $second instanceof AdviceBefore ? 1 : -1; |
93
|
1 |
|
|
94
|
|
|
case ($first instanceof OrderedAdvice && $second instanceof OrderedAdvice): |
95
|
|
|
return $first->getAdviceOrder() - $second->getAdviceOrder(); |
96
|
1 |
|
|
97
|
|
|
default: |
98
|
14 |
|
return 0; |
99
|
|
|
} |
100
|
14 |
|
} |
101
|
|
|
); |
102
|
|
|
|
103
|
|
|
return $sortedAdvices; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Replace concrete advices with list of ids |
108
|
6 |
|
* |
109
|
|
|
* @param Advice[][][] $advices List of advices |
110
|
6 |
|
*/ |
111
|
6 |
|
public static function flatAndSortAdvices(array $advices): array |
112
|
6 |
|
{ |
113
|
6 |
|
$flattenAdvices = []; |
114
|
6 |
|
foreach ($advices as $type => $typedAdvices) { |
115
|
|
|
foreach ($typedAdvices as $name => $concreteAdvices) { |
116
|
|
|
$flattenAdvices[$type][$name] = array_keys(self::sortAdvices($concreteAdvices)); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
return $flattenAdvices; |
121
|
6 |
|
} |
122
|
|
|
} |
123
|
|
|
|