1 | <?php |
||
23 | class AdviceMatcher |
||
24 | { |
||
25 | /** |
||
26 | * Loader of aspects |
||
27 | * |
||
28 | * @var AspectLoader |
||
29 | */ |
||
30 | protected $loader; |
||
31 | |||
32 | /** |
||
33 | * Flag to enable/disable support of global function interception |
||
34 | * |
||
35 | * @var bool |
||
36 | */ |
||
37 | private $isInterceptFunctions; |
||
38 | |||
39 | /** |
||
40 | * Constructor |
||
41 | * |
||
42 | * @param AspectLoader $loader Instance of aspect loader |
||
43 | * @param bool $isInterceptFunctions Optional flag to enable function interception |
||
44 | */ |
||
45 | 4 | public function __construct(AspectLoader $loader, $isInterceptFunctions = false) |
|
51 | |||
52 | /** |
||
53 | * Returns list of function advices for namespace |
||
54 | * |
||
55 | * @param ReflectionFileNamespace $namespace |
||
56 | * @param array|Aop\Advisor[] $advisors List of advisor to match |
||
57 | * |
||
58 | * @return array |
||
59 | */ |
||
60 | public function getAdvicesForFunctions(ReflectionFileNamespace $namespace, array $advisors) |
||
61 | { |
||
62 | if (!$this->isInterceptFunctions || $namespace->getName() === 'no-namespace') { |
||
63 | return []; |
||
64 | } |
||
65 | |||
66 | $advices = []; |
||
67 | |||
68 | foreach ($advisors as $advisorId => $advisor) { |
||
69 | if ($advisor instanceof Aop\PointcutAdvisor) { |
||
70 | $pointcut = $advisor->getPointcut(); |
||
71 | $isFunctionAdvisor = $pointcut->getKind() & Aop\PointFilter::KIND_FUNCTION; |
||
72 | if ($isFunctionAdvisor && $pointcut->getClassFilter()->matches($namespace)) { |
||
73 | $advices = array_merge_recursive( |
||
74 | $advices, |
||
75 | $this->getFunctionAdvicesFromAdvisor($namespace, $advisor, $advisorId, $pointcut) |
||
76 | ); |
||
77 | } |
||
78 | } |
||
79 | } |
||
80 | |||
81 | return $advices; |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * Return list of advices for class |
||
86 | * |
||
87 | * @param ReflectionClass $class Class to advise |
||
88 | * @param array|Aop\Advisor[] $advisors List of advisor to match |
||
89 | * |
||
90 | * @return array|Aop\Advice[] List of advices for class |
||
91 | */ |
||
92 | 3 | public function getAdvicesForClass(ReflectionClass $class, array $advisors) |
|
93 | { |
||
94 | 3 | $classAdvices = []; |
|
95 | 3 | $parentClass = $class->getParentClass(); |
|
96 | |||
97 | 3 | $originalClass = $class; |
|
98 | 3 | if ($parentClass && preg_match('/' . AspectContainer::AOP_PROXIED_SUFFIX . '$/', $parentClass->name)) { |
|
99 | $originalClass = $parentClass; |
||
100 | } |
||
101 | |||
102 | 3 | foreach ($advisors as $advisorId => $advisor) { |
|
103 | 2 | if ($advisor instanceof Aop\PointcutAdvisor) { |
|
104 | 2 | $pointcut = $advisor->getPointcut(); |
|
105 | 2 | if ($pointcut->getClassFilter()->matches($class)) { |
|
106 | 2 | $classAdvices = array_merge_recursive( |
|
107 | 2 | $classAdvices, |
|
108 | 2 | $this->getAdvicesFromAdvisor($originalClass, $advisor, $advisorId, $pointcut) |
|
109 | ); |
||
110 | } |
||
111 | } |
||
112 | |||
113 | 2 | if ($advisor instanceof Aop\IntroductionAdvisor) { |
|
114 | if ($advisor->getClassFilter()->matches($class)) { |
||
115 | $classAdvices = array_merge_recursive( |
||
116 | $classAdvices, |
||
117 | 2 | $this->getIntroductionFromAdvisor($originalClass, $advisor, $advisorId) |
|
118 | ); |
||
119 | } |
||
120 | } |
||
121 | } |
||
122 | |||
123 | 3 | return $classAdvices; |
|
124 | } |
||
125 | |||
126 | /** |
||
127 | * Returns list of advices from advisor and point filter |
||
128 | * |
||
129 | * @param ReflectionClass $class Class to inject advices |
||
130 | * @param Aop\PointcutAdvisor $advisor Advisor for class |
||
131 | * @param string $advisorId Identifier of advisor |
||
132 | * @param Aop\PointFilter $filter Filter for points |
||
133 | * |
||
134 | * @return array |
||
135 | */ |
||
136 | 2 | private function getAdvicesFromAdvisor( |
|
137 | ReflectionClass $class, |
||
138 | Aop\PointcutAdvisor $advisor, |
||
139 | $advisorId, |
||
140 | Aop\PointFilter $filter |
||
141 | ) { |
||
142 | 2 | $classAdvices = []; |
|
143 | 2 | $filterKind = $filter->getKind(); |
|
144 | |||
145 | // Check class only for class filters |
||
146 | 2 | if ($filterKind & Aop\PointFilter::KIND_CLASS) { |
|
147 | if ($filter->matches($class)) { |
||
148 | // Dynamic initialization |
||
149 | if ($filterKind & Aop\PointFilter::KIND_INIT) { |
||
150 | $classAdvices[AspectContainer::INIT_PREFIX]['root'][$advisorId] = $advisor->getAdvice(); |
||
151 | } |
||
152 | // Static initalization |
||
153 | if ($filterKind & Aop\PointFilter::KIND_STATIC_INIT) { |
||
154 | $classAdvices[AspectContainer::STATIC_INIT_PREFIX]['root'][$advisorId] = $advisor->getAdvice(); |
||
155 | } |
||
156 | } |
||
157 | } |
||
158 | |||
159 | // Check methods in class only for method filters |
||
160 | 2 | if ($filterKind & Aop\PointFilter::KIND_METHOD) { |
|
161 | 1 | $mask = ReflectionMethod::IS_PUBLIC | ReflectionMethod::IS_PROTECTED; |
|
162 | 1 | foreach ($class->getMethods($mask) as $method) { |
|
163 | // abstract and parent final methods could not be woven |
||
164 | 1 | $isParentFinalMethod = ($method->getDeclaringClass()->name !== $class->name) && $method->isFinal(); |
|
165 | 1 | if ($isParentFinalMethod || $method->isAbstract()) { |
|
166 | continue; |
||
167 | } |
||
168 | |||
169 | 1 | if ($filter->matches($method, $class)) { |
|
170 | 1 | $prefix = $method->isStatic() ? AspectContainer::STATIC_METHOD_PREFIX : AspectContainer::METHOD_PREFIX; |
|
171 | 1 | $classAdvices[$prefix][$method->name][$advisorId] = $advisor->getAdvice(); |
|
172 | } |
||
173 | } |
||
174 | } |
||
175 | |||
176 | // Check properties in class only for property filters |
||
177 | 2 | if ($filterKind & Aop\PointFilter::KIND_PROPERTY) { |
|
178 | 1 | $mask = ReflectionProperty::IS_PUBLIC | ReflectionProperty::IS_PROTECTED; |
|
179 | 1 | foreach ($class->getProperties($mask) as $property) { |
|
180 | 1 | if ($filter->matches($property, $class) && !$property->isStatic()) { |
|
181 | 1 | $classAdvices[AspectContainer::PROPERTY_PREFIX][$property->name][$advisorId] = $advisor->getAdvice(); |
|
182 | } |
||
183 | } |
||
184 | } |
||
185 | |||
186 | 2 | return $classAdvices; |
|
187 | } |
||
188 | |||
189 | /** |
||
190 | * Returns list of introduction advices from advisor |
||
191 | * |
||
192 | * @param ReflectionClass $class Class to inject advices |
||
193 | * @param Aop\IntroductionAdvisor $advisor Advisor for class |
||
194 | * @param string $advisorId Identifier of advisor |
||
195 | * |
||
196 | * @return array |
||
197 | */ |
||
198 | private function getIntroductionFromAdvisor( |
||
215 | |||
216 | /** |
||
217 | * Returns list of function advices for specific namespace |
||
218 | * |
||
219 | * @param ReflectionFileNamespace $namespace |
||
220 | * @param Aop\PointcutAdvisor $advisor Advisor for class |
||
221 | * @param string $advisorId Identifier of advisor |
||
222 | * @param Aop\PointFilter $pointcut Filter for points |
||
223 | * |
||
224 | * @return array |
||
225 | */ |
||
226 | private function getFunctionAdvicesFromAdvisor( |
||
248 | } |
||
249 |