1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
/* |
4
|
|
|
* Go! AOP framework |
5
|
|
|
* |
6
|
|
|
* @copyright Copyright 2013, 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\Aop\Framework; |
13
|
|
|
|
14
|
|
|
use Go\Aop\Intercept\Interceptor; |
15
|
|
|
use Go\Aop\Intercept\Invocation; |
16
|
|
|
use Go\Aop\Intercept\Joinpoint; |
17
|
|
|
use Go\Aop\PointFilter; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Dynamic invocation matcher combines a pointcut and interceptor. |
21
|
|
|
* |
22
|
|
|
* For each invocation interceptor asks the pointcut if it matches the invocation. |
23
|
|
|
* Matcher will receive reflection point, object instance and invocation arguments to make a decision |
24
|
|
|
*/ |
25
|
|
|
class DynamicInvocationMatcherInterceptor implements Interceptor |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* Instance of pointcut to dynamically match joinpoints with args |
29
|
|
|
* |
30
|
|
|
* @var PointFilter |
31
|
|
|
*/ |
32
|
|
|
protected $pointFilter; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Overloaded property for storing instance of Interceptor |
36
|
|
|
* |
37
|
|
|
* @var Interceptor |
38
|
|
|
*/ |
39
|
|
|
protected $interceptor; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Dynamic matcher constructor |
43
|
|
|
* |
44
|
|
|
* @param PointFilter $pointFilter Instance of dynamic matcher |
45
|
|
|
* @param Interceptor $interceptor Instance of interceptor to invoke |
46
|
|
|
*/ |
47
|
|
|
public function __construct(PointFilter $pointFilter, Interceptor $interceptor) |
48
|
|
|
{ |
49
|
|
|
$this->pointFilter = $pointFilter; |
50
|
|
|
$this->interceptor = $interceptor; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Method invoker |
55
|
|
|
* |
56
|
|
|
* @param Joinpoint $joinpoint the method invocation joinpoint |
57
|
|
|
* |
58
|
|
|
* @return mixed the result of the call to {@see Joinpoint->proceed()} |
59
|
|
|
*/ |
60
|
|
|
final public function invoke(Joinpoint $joinpoint) |
61
|
|
|
{ |
62
|
|
|
if ($joinpoint instanceof Invocation) { |
63
|
|
|
$point = $joinpoint->getStaticPart(); |
64
|
|
|
$instance = $joinpoint->getThis(); |
65
|
|
|
$context = new \ReflectionClass($instance); |
66
|
|
|
if ($this->pointFilter->matches($point, $context, $instance, $joinpoint->getArguments())) { |
67
|
|
|
return $this->interceptor->invoke($joinpoint); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return $joinpoint->proceed(); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|