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