1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
/* |
4
|
|
|
* Go! AOP framework |
5
|
|
|
* |
6
|
|
|
* @copyright Copyright 2012, 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\Core; |
13
|
|
|
|
14
|
|
|
use Closure; |
15
|
|
|
use Go\Aop\Advisor; |
16
|
|
|
use Go\Aop\Aspect; |
17
|
|
|
use Go\Aop\Framework; |
18
|
|
|
use Go\Aop\Intercept\Interceptor; |
19
|
|
|
use Go\Aop\Pointcut; |
20
|
|
|
use Go\Aop\Support\DefaultPointcutAdvisor; |
21
|
|
|
use Go\Lang\Annotation; |
22
|
|
|
use Go\Lang\Annotation\BaseInterceptor; |
23
|
|
|
use ReflectionMethod; |
24
|
|
|
use Reflector; |
25
|
|
|
|
26
|
|
|
use function get_class; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* General aspect loader add common support for general advices, declared as annotations |
30
|
|
|
*/ |
31
|
|
|
class GeneralAspectLoaderExtension extends AbstractAspectLoaderExtension |
32
|
|
|
{ |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* General aspect loader works with annotations from aspect |
36
|
|
|
*/ |
37
|
1 |
|
public function getKind(): string |
38
|
|
|
{ |
39
|
1 |
|
return self::KIND_ANNOTATION; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* General aspect loader works only with methods of aspect |
44
|
|
|
*/ |
45
|
2 |
|
public function getTargets(): array |
46
|
|
|
{ |
47
|
2 |
|
return [self::TARGET_METHOD]; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Checks if loader is able to handle specific point of aspect |
52
|
|
|
* |
53
|
|
|
* @param Aspect $aspect Instance of aspect |
54
|
|
|
* @param mixed|\ReflectionClass|\ReflectionMethod|\ReflectionProperty $reflection Reflection of point |
55
|
|
|
* @param mixed|null $metaInformation Additional meta-information, e.g. annotation for method |
56
|
|
|
* |
57
|
|
|
* @return boolean true if extension is able to create an advisor from reflection and metaInformation |
58
|
|
|
*/ |
59
|
1 |
|
public function supports(Aspect $aspect, $reflection, $metaInformation = null): bool |
60
|
|
|
{ |
61
|
1 |
|
return $metaInformation instanceof Annotation\Interceptor |
62
|
1 |
|
|| $metaInformation instanceof Annotation\Pointcut; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Loads definition from specific point of aspect into the container |
67
|
|
|
* |
68
|
|
|
* @param Aspect $aspect Instance of aspect |
69
|
|
|
* @param Reflector|ReflectionMethod $reflection Reflection of point |
70
|
|
|
* @param mixed|null $metaInformation Additional meta-information, e.g. annotation for method |
71
|
|
|
* |
72
|
|
|
* @return array|Pointcut[]|Advisor[] |
73
|
|
|
* |
74
|
|
|
* @throws \UnexpectedValueException |
75
|
|
|
*/ |
76
|
1 |
|
public function load(Aspect $aspect, Reflector $reflection, $metaInformation = null): array |
77
|
|
|
{ |
78
|
1 |
|
$loadedItems = []; |
79
|
1 |
|
$pointcut = $this->parsePointcut($aspect, $reflection, $metaInformation->value); |
80
|
1 |
|
$methodId = get_class($aspect) . '->' . $reflection->name; |
|
|
|
|
81
|
1 |
|
$adviceCallback = $reflection->getClosure($aspect); |
|
|
|
|
82
|
|
|
|
83
|
|
|
switch (true) { |
84
|
|
|
// Register a pointcut by its name |
85
|
1 |
|
case ($metaInformation instanceof Annotation\Pointcut): |
86
|
|
|
$loadedItems[$methodId] = $pointcut; |
87
|
|
|
break; |
88
|
|
|
|
89
|
1 |
|
case ($pointcut instanceof Pointcut): |
90
|
1 |
|
$advice = $this->getInterceptor($metaInformation, $adviceCallback); |
91
|
|
|
|
92
|
1 |
|
$loadedItems[$methodId] = new DefaultPointcutAdvisor($pointcut, $advice); |
93
|
1 |
|
break; |
94
|
|
|
|
95
|
|
|
default: |
96
|
|
|
throw new \UnexpectedValueException('Unsupported pointcut class: ' . get_class($pointcut)); |
97
|
|
|
} |
98
|
|
|
|
99
|
1 |
|
return $loadedItems; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Returns an interceptor instance by meta-type annotation and closure |
104
|
|
|
* |
105
|
|
|
* @throws \UnexpectedValueException For unsupported annotations |
106
|
|
|
*/ |
107
|
1 |
|
protected function getInterceptor(BaseInterceptor $metaInformation, Closure $adviceCallback): Interceptor |
108
|
|
|
{ |
109
|
1 |
|
$adviceOrder = $metaInformation->order; |
110
|
1 |
|
$pointcutExpression = $metaInformation->value; |
111
|
|
|
switch (true) { |
112
|
1 |
|
case ($metaInformation instanceof Annotation\Before): |
113
|
1 |
|
return new Framework\BeforeInterceptor($adviceCallback, $adviceOrder, $pointcutExpression); |
114
|
|
|
|
115
|
1 |
|
case ($metaInformation instanceof Annotation\After): |
116
|
1 |
|
return new Framework\AfterInterceptor($adviceCallback, $adviceOrder, $pointcutExpression); |
117
|
|
|
|
118
|
|
|
case ($metaInformation instanceof Annotation\Around): |
119
|
|
|
return new Framework\AroundInterceptor($adviceCallback, $adviceOrder, $pointcutExpression); |
120
|
|
|
|
121
|
|
|
case ($metaInformation instanceof Annotation\AfterThrowing): |
122
|
|
|
return new Framework\AfterThrowingInterceptor($adviceCallback, $adviceOrder, $pointcutExpression); |
123
|
|
|
|
124
|
|
|
default: |
125
|
|
|
throw new \UnexpectedValueException('Unsupported method meta class: ' . get_class($metaInformation)); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: