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
|
|
|
* This static variable holds the name of field to use to avoid extra "if" section in the __invoke method |
51
|
24 |
|
* |
52
|
|
|
* Overridden in children classes and initialized via LSB |
53
|
24 |
|
* |
54
|
24 |
|
* @var string |
55
|
24 |
|
*/ |
56
|
|
|
protected static $propertyName; |
57
|
|
|
|
58
|
24 |
|
/** |
59
|
4 |
|
* Constructor for method invocation |
60
|
|
|
* |
61
|
24 |
|
* @param array $advices List of advices for this invocation |
62
|
|
|
*/ |
63
|
|
|
public function __construct(string $className, string $methodName, array $advices) |
64
|
|
|
{ |
65
|
|
|
parent::__construct($advices); |
66
|
|
|
$this->reflectionMethod = $method = new AnnotatedReflectionMethod($className, $methodName); |
67
|
|
|
|
68
|
|
|
// Give an access to call protected method |
69
|
|
|
if ($method->isProtected()) { |
70
|
|
|
$method->setAccessible(true); |
71
|
|
|
} |
72
|
20 |
|
} |
73
|
|
|
|
74
|
20 |
|
/** |
75
|
2 |
|
* Invokes current method invocation with all interceptors |
76
|
|
|
* |
77
|
|
|
* @param null|object|string $instance Invocation instance (class name for static methods) |
78
|
20 |
|
* @param array $arguments List of arguments for method invocation |
79
|
|
|
* @param array $variadicArguments Additional list of variadic arguments |
80
|
|
|
* |
81
|
|
|
* @return mixed Result of invocation |
82
|
|
|
*/ |
83
|
20 |
|
final public function __invoke($instance = null, array $arguments = [], array $variadicArguments = []) |
84
|
|
|
{ |
85
|
20 |
|
if ($this->level > 0) { |
86
|
20 |
|
$this->stackFrames[] = [$this->arguments, $this->scope, $this->instance, $this->current]; |
87
|
20 |
|
} |
88
|
|
|
|
89
|
20 |
|
if (count($variadicArguments) > 0) { |
90
|
|
|
$arguments = array_merge($arguments, $variadicArguments); |
91
|
20 |
|
} |
92
|
|
|
|
93
|
20 |
|
try { |
94
|
20 |
|
++$this->level; |
95
|
|
|
|
96
|
|
|
$this->current = 0; |
97
|
|
|
$this->arguments = $arguments; |
98
|
|
|
|
99
|
|
|
$this->{static::$propertyName} = $instance; |
100
|
|
|
|
101
|
|
|
return $this->proceed(); |
102
|
|
|
} finally { |
103
|
|
|
--$this->level; |
104
|
3 |
|
|
105
|
|
|
if ($this->level > 0) { |
106
|
3 |
|
[$this->arguments, $this->scope, $this->instance, $this->current] = array_pop($this->stackFrames); |
107
|
|
|
} else { |
108
|
|
|
$this->instance = null; |
109
|
|
|
$this->arguments = []; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Gets the method being called. |
116
|
|
|
* |
117
|
|
|
* @return ReflectionMethod|AnnotatedReflectionMethod the method being called. |
118
|
|
|
*/ |
119
|
|
|
public function getMethod(): ReflectionMethod |
120
|
|
|
{ |
121
|
|
|
return $this->reflectionMethod; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
1 |
|
* Returns friendly description of this joinpoint |
126
|
|
|
*/ |
127
|
1 |
|
final public function __toString(): string |
128
|
|
|
{ |
129
|
|
|
return sprintf( |
130
|
|
|
'execution(%s%s%s())', |
131
|
|
|
$this->getScope(), |
132
|
|
|
$this->reflectionMethod->isStatic() ? '::' : '->', |
133
|
|
|
$this->reflectionMethod->name |
134
|
|
|
); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|