|
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
|
|
|
use function array_merge; |
|
18
|
|
|
use function array_pop; |
|
19
|
|
|
use function count; |
|
20
|
|
|
use function is_string; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Abstract method invocation implementation |
|
24
|
|
|
*/ |
|
25
|
|
|
abstract class AbstractMethodInvocation extends AbstractInvocation implements MethodInvocation |
|
26
|
|
|
{ |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Instance of object for invoking |
|
30
|
|
|
* |
|
31
|
|
|
* @var object|null |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $instance; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Instance of reflection method for class |
|
37
|
|
|
* |
|
38
|
|
|
* @var ReflectionMethod |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $reflectionMethod; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Class name scope for static invocation |
|
44
|
|
|
* |
|
45
|
|
|
* @var string |
|
46
|
|
|
*/ |
|
47
|
|
|
protected $scope; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Constructor for method invocation |
|
51
|
24 |
|
* |
|
52
|
|
|
* @param array $advices List of advices for this invocation |
|
53
|
24 |
|
*/ |
|
54
|
24 |
|
public function __construct(string $className, string $methodName, array $advices) |
|
55
|
24 |
|
{ |
|
56
|
|
|
parent::__construct($advices); |
|
57
|
|
|
$this->reflectionMethod = $method = new AnnotatedReflectionMethod($className, $methodName); |
|
58
|
24 |
|
|
|
59
|
4 |
|
// Give an access to call protected method |
|
60
|
|
|
if ($method->isProtected()) { |
|
61
|
24 |
|
$method->setAccessible(true); |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Invokes current method invocation with all interceptors |
|
67
|
|
|
* |
|
68
|
|
|
* @param null|object|string $instance Invocation instance (class name for static methods) |
|
69
|
|
|
* @param array $arguments List of arguments for method invocation |
|
70
|
|
|
* @param array $variadicArguments Additional list of variadic arguments |
|
71
|
|
|
* |
|
72
|
20 |
|
* @return mixed Result of invocation |
|
73
|
|
|
*/ |
|
74
|
20 |
|
final public function __invoke($instance = null, array $arguments = [], array $variadicArguments = []) |
|
75
|
2 |
|
{ |
|
76
|
|
|
if ($this->level > 0) { |
|
77
|
|
|
$this->stackFrames[] = [$this->arguments, $this->scope, $this->instance, $this->current]; |
|
78
|
20 |
|
} |
|
79
|
|
|
|
|
80
|
|
|
if (count($variadicArguments) > 0) { |
|
81
|
|
|
$arguments = array_merge($arguments, $variadicArguments); |
|
82
|
|
|
} |
|
83
|
20 |
|
|
|
84
|
|
|
try { |
|
85
|
20 |
|
++$this->level; |
|
86
|
20 |
|
|
|
87
|
20 |
|
$this->current = 0; |
|
88
|
|
|
$this->arguments = $arguments; |
|
89
|
20 |
|
|
|
90
|
|
|
if (is_string($instance)) { |
|
91
|
20 |
|
$this->scope = $instance; |
|
92
|
|
|
} else { |
|
93
|
20 |
|
// If it's not a string, then it should be object or null |
|
94
|
20 |
|
// We do not fill the $this->scope variable here to avoid performance lost |
|
95
|
|
|
$this->instance = $instance; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
return $this->proceed(); |
|
99
|
|
|
} finally { |
|
100
|
|
|
--$this->level; |
|
101
|
|
|
|
|
102
|
|
|
if ($this->level > 0) { |
|
103
|
|
|
[$this->arguments, $this->scope, $this->instance, $this->current] = array_pop($this->stackFrames); |
|
104
|
3 |
|
} else { |
|
105
|
|
|
$this->instance = null; |
|
106
|
3 |
|
$this->arguments = []; |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Gets the method being called. |
|
113
|
|
|
* |
|
114
|
|
|
* @return ReflectionMethod|AnnotatedReflectionMethod the method being called. |
|
115
|
|
|
*/ |
|
116
|
|
|
public function getMethod(): ReflectionMethod |
|
117
|
|
|
{ |
|
118
|
|
|
return $this->reflectionMethod; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Returns friendly description of this joinpoint |
|
123
|
|
|
*/ |
|
124
|
|
|
final public function __toString(): string |
|
125
|
1 |
|
{ |
|
126
|
|
|
return sprintf( |
|
127
|
1 |
|
'execution(%s%s%s())', |
|
128
|
|
|
$this->getScope(), |
|
129
|
|
|
$this->reflectionMethod->isStatic() ? '::' : '->', |
|
130
|
|
|
$this->reflectionMethod->name |
|
131
|
|
|
); |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|