1 | <?php |
||
24 | class AdviceMatcher |
||
25 | { |
||
26 | /** |
||
27 | * Loader of aspects |
||
28 | * |
||
29 | * @var AspectLoader |
||
30 | */ |
||
31 | protected $loader; |
||
32 | |||
33 | /** |
||
34 | * Flag to enable/disable support of global function interception |
||
35 | * |
||
36 | * @var bool |
||
37 | */ |
||
38 | private $isInterceptFunctions = false; |
||
39 | |||
40 | /** |
||
41 | * Constructor |
||
42 | * |
||
43 | * @param AspectLoader $loader Instance of aspect loader |
||
44 | * @param bool $isInterceptFunctions Optional flag to enable function interception |
||
45 | */ |
||
46 | 4 | public function __construct(AspectLoader $loader, bool $isInterceptFunctions = false) |
|
52 | |||
53 | /** |
||
54 | * Returns list of function advices for namespace |
||
55 | * |
||
56 | * @param ReflectionFileNamespace $namespace |
||
57 | * @param array|Aop\Advisor[] $advisors List of advisor to match |
||
58 | * |
||
59 | * @return array |
||
60 | */ |
||
61 | 9 | public function getAdvicesForFunctions(ReflectionFileNamespace $namespace, array $advisors) : array |
|
62 | { |
||
63 | 9 | if (!$this->isInterceptFunctions) { |
|
64 | 9 | return []; |
|
65 | } |
||
66 | |||
67 | $advices = []; |
||
68 | |||
69 | foreach ($advisors as $advisorId => $advisor) { |
||
|
|||
70 | |||
71 | if ($advisor instanceof Aop\PointcutAdvisor) { |
||
72 | |||
73 | $pointcut = $advisor->getPointcut(); |
||
74 | $isFunctionAdvisor = $pointcut->getKind() & Aop\PointFilter::KIND_FUNCTION; |
||
75 | if ($isFunctionAdvisor && $pointcut->getClassFilter()->matches($namespace)) { |
||
76 | $advices = array_merge_recursive( |
||
77 | $advices, |
||
78 | $this->getFunctionAdvicesFromAdvisor($namespace, $advisor, $advisorId, $pointcut) |
||
79 | ); |
||
80 | } |
||
81 | } |
||
82 | } |
||
83 | |||
84 | return $advices; |
||
85 | } |
||
86 | |||
87 | /** |
||
88 | * Return list of advices for class |
||
89 | * |
||
90 | * @param ReflectionClass $class Class to advise |
||
91 | * @param array|Aop\Advisor[] $advisors List of advisor to match |
||
92 | * |
||
93 | * @return array|Aop\Advice[] List of advices for class |
||
94 | */ |
||
95 | 3 | public function getAdvicesForClass(ReflectionClass $class, array $advisors) : array |
|
96 | { |
||
97 | 3 | $classAdvices = []; |
|
98 | 3 | $parentClass = $class->getParentClass(); |
|
99 | |||
100 | 3 | if ($parentClass && preg_match('/' . AspectContainer::AOP_PROXIED_SUFFIX . '$/', $parentClass->name)) { |
|
101 | $originalClass = $parentClass; |
||
102 | } else { |
||
103 | 3 | $originalClass = $class; |
|
104 | } |
||
105 | |||
106 | 3 | foreach ($advisors as $advisorId => $advisor) { |
|
107 | |||
108 | 2 | if ($advisor instanceof Aop\PointcutAdvisor) { |
|
109 | |||
110 | 2 | $pointcut = $advisor->getPointcut(); |
|
111 | 2 | if ($pointcut->getClassFilter()->matches($class)) { |
|
112 | 2 | $classAdvices = array_merge_recursive( |
|
113 | 2 | $classAdvices, |
|
114 | 2 | $this->getAdvicesFromAdvisor($originalClass, $advisor, $advisorId, $pointcut) |
|
115 | ); |
||
116 | } |
||
117 | } |
||
118 | |||
119 | 2 | if ($advisor instanceof Aop\IntroductionAdvisor) { |
|
120 | if ($advisor->getClassFilter()->matches($class)) { |
||
121 | $classAdvices = array_merge_recursive( |
||
122 | $classAdvices, |
||
123 | 2 | $this->getIntroductionFromAdvisor($originalClass, $advisor, $advisorId) |
|
124 | ); |
||
125 | } |
||
126 | } |
||
127 | } |
||
128 | |||
129 | 3 | return $classAdvices; |
|
130 | } |
||
131 | |||
132 | /** |
||
133 | * Returns list of advices from advisor and point filter |
||
134 | * |
||
135 | * @param ReflectionClass $class Class to inject advices |
||
136 | * @param Aop\PointcutAdvisor $advisor Advisor for class |
||
137 | * @param string $advisorId Identifier of advisor |
||
138 | * @param Aop\PointFilter $filter Filter for points |
||
139 | * |
||
140 | * @return array |
||
141 | */ |
||
142 | 2 | private function getAdvicesFromAdvisor( |
|
189 | |||
190 | /** |
||
191 | * Returns list of introduction advices from advisor |
||
192 | * |
||
193 | * @param ReflectionClass $class Class to inject advices |
||
194 | * @param Aop\IntroductionAdvisor $advisor Advisor for class |
||
195 | * @param string $advisorId Identifier of advisor |
||
196 | * |
||
197 | * @return array |
||
198 | */ |
||
199 | private function getIntroductionFromAdvisor( |
||
216 | |||
217 | /** |
||
218 | * Returns list of function advices for specific namespace |
||
219 | * |
||
220 | * @param ReflectionFileNamespace $namespace |
||
221 | * @param Aop\PointcutAdvisor $advisor Advisor for class |
||
222 | * @param string $advisorId Identifier of advisor |
||
223 | * @param Aop\PointFilter $pointcut Filter for points |
||
224 | * |
||
225 | * @return array |
||
226 | */ |
||
227 | private function getFunctionAdvicesFromAdvisor( |
||
249 | } |
||
250 |