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 = false; |
||
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 | |||
70 | if ($advisor instanceof Aop\PointcutAdvisor) { |
||
71 | |||
72 | $pointcut = $advisor->getPointcut(); |
||
73 | $isFunctionAdvisor = $pointcut->getKind() & Aop\PointFilter::KIND_FUNCTION; |
||
74 | if ($isFunctionAdvisor && $pointcut->getClassFilter()->matches($namespace)) { |
||
75 | $advices = array_merge_recursive( |
||
76 | $advices, |
||
77 | $this->getFunctionAdvicesFromAdvisor($namespace, $advisor, $advisorId, $pointcut) |
||
78 | ); |
||
79 | } |
||
80 | } |
||
81 | } |
||
82 | |||
83 | return $advices; |
||
84 | } |
||
85 | |||
86 | /** |
||
87 | * Return list of advices for class |
||
88 | * |
||
89 | * @param ReflectionClass $class Class to advise |
||
90 | * @param array|Aop\Advisor[] $advisors List of advisor to match |
||
91 | * |
||
92 | * @return array|Aop\Advice[] List of advices for class |
||
93 | */ |
||
94 | 3 | public function getAdvicesForClass(ReflectionClass $class, array $advisors) |
|
95 | { |
||
96 | 3 | $classAdvices = []; |
|
97 | 3 | $parentClass = $class->getParentClass(); |
|
98 | |||
99 | 3 | if ($parentClass && preg_match('/' . AspectContainer::AOP_PROXIED_SUFFIX . '$/', $parentClass->name)) { |
|
100 | $originalClass = $parentClass; |
||
101 | } else { |
||
102 | 3 | $originalClass = $class; |
|
103 | } |
||
104 | |||
105 | 3 | foreach ($advisors as $advisorId => $advisor) { |
|
106 | |||
107 | 2 | if ($advisor instanceof Aop\PointcutAdvisor) { |
|
108 | |||
109 | 2 | $pointcut = $advisor->getPointcut(); |
|
110 | 2 | if ($pointcut->getClassFilter()->matches($class)) { |
|
111 | 2 | $classAdvices = array_merge_recursive( |
|
112 | 2 | $classAdvices, |
|
113 | 2 | $this->getAdvicesFromAdvisor($originalClass, $advisor, $advisorId, $pointcut) |
|
114 | ); |
||
115 | } |
||
116 | } |
||
117 | |||
118 | 2 | if ($advisor instanceof Aop\IntroductionAdvisor) { |
|
119 | if ($advisor->getClassFilter()->matches($class)) { |
||
120 | $classAdvices = array_merge_recursive( |
||
121 | $classAdvices, |
||
122 | 2 | $this->getIntroductionFromAdvisor($originalClass, $advisor, $advisorId) |
|
123 | ); |
||
124 | } |
||
125 | } |
||
126 | } |
||
127 | |||
128 | 3 | return $classAdvices; |
|
129 | } |
||
130 | |||
131 | /** |
||
132 | * Returns list of advices from advisor and point filter |
||
133 | * |
||
134 | * @param ReflectionClass $class Class to inject advices |
||
135 | * @param Aop\PointcutAdvisor $advisor Advisor for class |
||
136 | * @param string $advisorId Identifier of advisor |
||
137 | * @param Aop\PointFilter $filter Filter for points |
||
138 | * |
||
139 | * @return array |
||
140 | */ |
||
141 | 2 | private function getAdvicesFromAdvisor( |
|
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 |