|
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 |
|
38
|
|
|
* |
|
39
|
|
|
* @var array<Advice|Interceptor> |
|
40
|
|
|
*/ |
|
41
|
|
|
protected array $advices = []; |
|
|
|
|
|
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Current advice index |
|
45
|
|
|
*/ |
|
46
|
|
|
protected int $current = 0; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Stack frames to work with recursive calls or with cross-calls inside object |
|
50
|
|
|
*/ |
|
51
|
|
|
protected array $stackFrames = []; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Recursion level for invocation |
|
55
|
|
|
*/ |
|
56
|
|
|
protected int $level = 0; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Initializes list of advices for current joinpoint |
|
60
|
|
|
* |
|
61
|
|
|
* @param array<Advice|Interceptor> $advices List of advices |
|
62
|
|
|
*/ |
|
63
|
|
|
public function __construct(array $advices) |
|
64
|
|
|
{ |
|
65
|
|
|
$this->advices = $advices; |
|
66
|
|
|
} |
|
67
|
27 |
|
|
|
68
|
|
|
/** |
|
69
|
27 |
|
* Sorts advices by priority |
|
70
|
27 |
|
* |
|
71
|
|
|
* @param array<Advice|Interceptor> $advices |
|
72
|
|
|
* |
|
73
|
|
|
* @return array<Advice|Interceptor> Sorted list of advices |
|
74
|
|
|
*/ |
|
75
|
|
|
public static function sortAdvices(array $advices): array |
|
76
|
|
|
{ |
|
77
|
|
|
$sortedAdvices = $advices; |
|
78
|
14 |
|
uasort( |
|
79
|
|
|
$sortedAdvices, |
|
80
|
14 |
|
function (Advice $first, Advice $second) { |
|
81
|
|
|
switch (true) { |
|
82
|
|
|
case $first instanceof AdviceBefore && !($second instanceof AdviceBefore): |
|
83
|
9 |
|
return -1; |
|
84
|
4 |
|
|
|
85
|
|
|
case $first instanceof AdviceAround && !($second instanceof AdviceAround): |
|
86
|
6 |
|
return 1; |
|
87
|
3 |
|
|
|
88
|
|
|
case $first instanceof AdviceAfter && !($second instanceof AdviceAfter): |
|
89
|
4 |
|
return $second instanceof AdviceBefore ? 1 : -1; |
|
90
|
2 |
|
|
|
91
|
|
|
case ($first instanceof OrderedAdvice && $second instanceof OrderedAdvice): |
|
92
|
2 |
|
return $first->getAdviceOrder() - $second->getAdviceOrder(); |
|
93
|
1 |
|
|
|
94
|
|
|
default: |
|
95
|
|
|
return 0; |
|
96
|
1 |
|
} |
|
97
|
|
|
} |
|
98
|
14 |
|
); |
|
99
|
|
|
|
|
100
|
14 |
|
return $sortedAdvices; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Replace concrete advices with list of ids |
|
105
|
|
|
* |
|
106
|
|
|
* @param Advice[][][] $advices List of advices |
|
107
|
|
|
*/ |
|
108
|
6 |
|
public static function flatAndSortAdvices(array $advices): array |
|
109
|
|
|
{ |
|
110
|
6 |
|
$flattenAdvices = []; |
|
111
|
6 |
|
foreach ($advices as $type => $typedAdvices) { |
|
112
|
6 |
|
foreach ($typedAdvices as $name => $concreteAdvices) { |
|
113
|
6 |
|
if (is_array($concreteAdvices)) { |
|
114
|
6 |
|
$flattenAdvices[$type][$name] = array_keys(self::sortAdvices($concreteAdvices)); |
|
115
|
|
|
} else { |
|
116
|
|
|
$flattenAdvices[$type][$name] = $concreteAdvices; |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
6 |
|
return $flattenAdvices; |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|