|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* Go! AOP framework |
|
4
|
|
|
* |
|
5
|
|
|
* @copyright Copyright 2011, 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\Intercept\MethodInvocation; |
|
14
|
|
|
use Go\Aop\Support\AnnotatedReflectionMethod; |
|
15
|
|
|
use ReflectionMethod; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Abstract method invocation implementation |
|
19
|
|
|
*/ |
|
20
|
|
|
abstract class AbstractMethodInvocation extends AbstractInvocation implements MethodInvocation |
|
21
|
|
|
{ |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Instance of object for invoking or class name for static call |
|
25
|
|
|
* |
|
26
|
|
|
* @var object|string |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $instance; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Instance of reflection method for class |
|
32
|
|
|
* |
|
33
|
|
|
* @var ReflectionMethod |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $reflectionMethod; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Name of the invocation class |
|
39
|
|
|
* |
|
40
|
|
|
* @var string |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $className = ''; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Constructor for method invocation |
|
46
|
|
|
* |
|
47
|
|
|
* @param string $className Class name |
|
48
|
|
|
* @param string $methodName Method to invoke |
|
49
|
|
|
* @param $advices array List of advices for this invocation |
|
50
|
|
|
*/ |
|
51
|
26 |
|
public function __construct($className, $methodName, array $advices) |
|
52
|
|
|
{ |
|
53
|
26 |
|
parent::__construct($advices); |
|
54
|
26 |
|
$this->className = $className; |
|
55
|
26 |
|
$this->reflectionMethod = $method = new AnnotatedReflectionMethod($this->className, $methodName); |
|
56
|
|
|
|
|
57
|
|
|
// Give an access to call protected method |
|
58
|
26 |
|
if ($method->isProtected()) { |
|
59
|
5 |
|
$method->setAccessible(true); |
|
60
|
|
|
} |
|
61
|
26 |
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Invokes current method invocation with all interceptors |
|
65
|
|
|
* |
|
66
|
|
|
* @param null|object|string $instance Invocation instance (class name for static methods) |
|
67
|
|
|
* @param array $arguments List of arguments for method invocation |
|
68
|
2 |
|
* @param array $variadicArguments Additional list of variadic arguments |
|
69
|
|
|
* |
|
70
|
2 |
|
* @return mixed Result of invocation |
|
71
|
|
|
*/ |
|
72
|
|
|
final public function __invoke($instance = null, array $arguments = [], array $variadicArguments = []) |
|
73
|
|
|
{ |
|
74
|
|
|
if ($this->level) { |
|
75
|
|
|
$this->stackFrames[] = [$this->arguments, $this->instance, $this->current]; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
if (!empty($variadicArguments)) { |
|
79
|
|
|
$arguments = array_merge($arguments, $variadicArguments); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
try { |
|
83
|
|
|
++$this->level; |
|
84
|
|
|
|
|
85
|
|
|
$this->current = 0; |
|
86
|
|
|
$this->instance = $instance; |
|
87
|
|
|
$this->arguments = $arguments; |
|
88
|
|
|
|
|
89
|
1 |
|
$result = $this->proceed(); |
|
90
|
|
|
} finally { |
|
91
|
1 |
|
--$this->level; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
if ($this->level) { |
|
95
|
|
|
list($this->arguments, $this->instance, $this->current) = array_pop($this->stackFrames); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
return $result; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Gets the method being called. |
|
103
|
|
|
* |
|
104
|
|
|
* @return AnnotatedReflectionMethod the method being called. |
|
105
|
|
|
*/ |
|
106
|
|
|
public function getMethod() |
|
107
|
|
|
{ |
|
108
|
|
|
return $this->reflectionMethod; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Returns the object that holds the current joinpoint's static |
|
113
|
|
|
* part. |
|
114
|
|
|
* |
|
115
|
|
|
* @return object|string the object for dynamic call or string with name of scope |
|
116
|
|
|
*/ |
|
117
|
|
|
public function getThis() |
|
118
|
|
|
{ |
|
119
|
|
|
return $this->instance; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Returns the static part of this joinpoint. |
|
124
|
|
|
* |
|
125
|
|
|
* @return object |
|
126
|
|
|
*/ |
|
127
|
|
|
public function getStaticPart() |
|
128
|
|
|
{ |
|
129
|
|
|
return $this->getMethod(); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Returns friendly description of this joinpoint |
|
134
|
|
|
* |
|
135
|
|
|
* @return string |
|
136
|
|
|
*/ |
|
137
|
|
|
final public function __toString() |
|
138
|
|
|
{ |
|
139
|
|
|
return sprintf( |
|
140
|
|
|
"execution(%s%s%s())", |
|
141
|
|
|
is_object($this->instance) ? get_class($this->instance) : $this->instance, |
|
142
|
|
|
$this->reflectionMethod->isStatic() ? '::' : '->', |
|
143
|
|
|
$this->reflectionMethod->name |
|
144
|
|
|
); |
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
|