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