1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
/* |
5
|
|
|
* Go! AOP framework |
6
|
|
|
* |
7
|
|
|
* @copyright Copyright 2013, Lisachenko Alexander <[email protected]> |
8
|
|
|
* |
9
|
|
|
* This source file is subject to the license that is bundled |
10
|
|
|
* with this source code in the file LICENSE. |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace Go\Aop\Framework; |
14
|
|
|
|
15
|
|
|
use Go\Aop\Intercept\Interceptor; |
16
|
|
|
use Go\Aop\Intercept\Joinpoint; |
17
|
|
|
use Go\Aop\Intercept\MethodInvocation; |
18
|
|
|
use Go\Aop\PointFilter; |
19
|
|
|
use ReflectionClass; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Dynamic invocation matcher combines a pointcut and interceptor. |
23
|
|
|
* |
24
|
|
|
* For each invocation interceptor asks the pointcut if it matches the invocation. |
25
|
|
|
* Matcher will receive reflection point, object instance and invocation arguments to make a decision |
26
|
|
|
*/ |
27
|
|
|
class DynamicInvocationMatcherInterceptor implements Interceptor |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* Instance of pointcut to dynamically match joinpoints with args |
31
|
|
|
*/ |
32
|
|
|
protected PointFilter $pointFilter; |
|
|
|
|
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Instance of interceptor to invoke |
36
|
|
|
*/ |
37
|
|
|
protected Interceptor $interceptor; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Dynamic matcher constructor |
41
|
|
|
*/ |
42
|
|
|
public function __construct(PointFilter $pointFilter, Interceptor $interceptor) |
43
|
|
|
{ |
44
|
|
|
$this->pointFilter = $pointFilter; |
45
|
|
|
$this->interceptor = $interceptor; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @inheritdoc |
50
|
|
|
*/ |
51
|
|
|
final public function invoke(Joinpoint $joinpoint) |
52
|
|
|
{ |
53
|
|
|
if ($joinpoint instanceof MethodInvocation) { |
54
|
|
|
$method = $joinpoint->getMethod(); |
55
|
|
|
$context = $joinpoint->getThis() ?? $joinpoint->getScope(); |
56
|
|
|
$contextClass = new ReflectionClass($context); |
57
|
|
|
if ($this->pointFilter->matches($method, $contextClass, $context, $joinpoint->getArguments())) { |
58
|
|
|
return $this->interceptor->invoke($joinpoint); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return $joinpoint->proceed(); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|