1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* Go! AOP framework |
4
|
|
|
* |
5
|
|
|
* @copyright Copyright 2013, Lisachenko Alexander <[email protected]> |
6
|
|
|
* |
7
|
|
|
* This source file is subject to the license that is bundled |
8
|
|
|
* with this source code in the file LICENSE. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Go\Aop\Framework; |
12
|
|
|
|
13
|
|
|
use Go\Aop\PointFilter; |
14
|
|
|
use Go\Aop\Intercept\Interceptor; |
15
|
|
|
use Go\Aop\Intercept\Invocation; |
16
|
|
|
use Go\Aop\Intercept\Joinpoint; |
17
|
|
|
use Serializable; |
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, Serializable |
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
|
|
|
* Serializes an interceptor into string representation |
55
|
|
|
* |
56
|
|
|
* @return string the string representation of the object or null |
57
|
|
|
*/ |
58
|
|
|
public function serialize() |
59
|
|
|
{ |
60
|
|
|
return serialize([$this->pointFilter, $this->interceptor]); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Unserialize an interceptor from the string |
65
|
|
|
* |
66
|
|
|
* @param string $serialized The string representation of the object. |
67
|
|
|
* @return void |
68
|
|
|
*/ |
69
|
|
|
public function unserialize($serialized) |
70
|
|
|
{ |
71
|
|
|
list($this->pointFilter, $this->interceptor) = unserialize($serialized); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Method invoker |
76
|
|
|
* |
77
|
|
|
* @param Joinpoint $joinpoint the method invocation joinpoint |
78
|
|
|
* |
79
|
|
|
* @return mixed the result of the call to {@see Joinpoint->proceed()} |
80
|
|
|
*/ |
81
|
|
|
final public function invoke(Joinpoint $joinpoint) |
82
|
|
|
{ |
83
|
|
|
if ($joinpoint instanceof Invocation) { |
84
|
|
|
$point = $joinpoint->getStaticPart(); |
85
|
|
|
$instance = $joinpoint->getThis(); |
86
|
|
|
$context = new \ReflectionClass($instance); |
87
|
|
|
if ($this->pointFilter->matches($point, $context, $instance, $joinpoint->getArguments())) { |
88
|
|
|
return $this->interceptor->invoke($joinpoint); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return $joinpoint->proceed(); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|