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
|
22 |
|
public function __construct($className, $methodName, array $advices) |
52
|
|
|
{ |
53
|
22 |
|
parent::__construct($advices); |
54
|
22 |
|
$this->className = $className; |
55
|
22 |
|
$this->reflectionMethod = $method = new AnnotatedReflectionMethod($this->className, $methodName); |
56
|
|
|
|
57
|
|
|
// Give an access to call protected method |
58
|
22 |
|
if ($method->isProtected()) { |
59
|
4 |
|
$method->setAccessible(true); |
60
|
|
|
} |
61
|
22 |
|
} |
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
|
|
|
* @param array $variadicArguments Additional list of variadic arguments |
69
|
|
|
* |
70
|
|
|
* @return mixed Result of invocation |
71
|
|
|
*/ |
72
|
20 |
|
final public function __invoke($instance = null, array $arguments = [], array $variadicArguments = []) |
73
|
|
|
{ |
74
|
20 |
|
if ($this->level) { |
75
|
2 |
|
$this->stackFrames[] = [$this->arguments, $this->instance, $this->current]; |
76
|
|
|
} |
77
|
|
|
|
78
|
20 |
|
if (!empty($variadicArguments)) { |
79
|
|
|
$arguments = \array_merge($arguments, $variadicArguments); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
try { |
83
|
20 |
|
++$this->level; |
84
|
|
|
|
85
|
20 |
|
$this->current = 0; |
86
|
20 |
|
$this->instance = $instance; |
87
|
20 |
|
$this->arguments = $arguments; |
88
|
|
|
|
89
|
20 |
|
return $this->proceed(); |
90
|
|
|
} finally { |
91
|
20 |
|
--$this->level; |
92
|
|
|
|
93
|
20 |
|
if ($this->level > 0) { |
94
|
20 |
|
list($this->arguments, $this->instance, $this->current) = \array_pop($this->stackFrames); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Gets the method being called. |
101
|
|
|
* |
102
|
|
|
* @return AnnotatedReflectionMethod the method being called. |
103
|
|
|
*/ |
104
|
2 |
|
public function getMethod() |
105
|
|
|
{ |
106
|
2 |
|
return $this->reflectionMethod; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Returns the object that holds the current joinpoint's static |
111
|
|
|
* part. |
112
|
|
|
* |
113
|
|
|
* @return object|string the object for dynamic call or string with name of scope |
114
|
|
|
*/ |
115
|
|
|
public function getThis() |
116
|
|
|
{ |
117
|
|
|
return $this->instance; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Returns the static part of this joinpoint. |
122
|
|
|
* |
123
|
|
|
* @return object |
124
|
|
|
*/ |
125
|
1 |
|
public function getStaticPart() |
126
|
|
|
{ |
127
|
1 |
|
return $this->getMethod(); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Returns friendly description of this joinpoint |
132
|
|
|
* |
133
|
|
|
* @return string |
134
|
|
|
*/ |
135
|
|
|
final public function __toString() |
136
|
|
|
{ |
137
|
|
|
return sprintf( |
138
|
|
|
'execution(%s%s%s())', |
139
|
|
|
is_object($this->instance) ? get_class($this->instance) : $this->instance, |
140
|
|
|
$this->reflectionMethod->isStatic() ? '::' : '->', |
141
|
|
|
$this->reflectionMethod->name |
142
|
|
|
); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|