1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
/* |
5
|
|
|
* Go! AOP framework |
6
|
|
|
* |
7
|
|
|
* @copyright Copyright 2013, Lisachenko Alexander <[email protected]> |
8
|
|
|
* |
9
|
|
|
* This source file is subject to the license that is bundled |
10
|
|
|
* with this source code in the file LICENSE. |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace Go\Aop\Framework; |
14
|
|
|
|
15
|
|
|
use Go\Aop\Intercept\FunctionInvocation; |
16
|
|
|
use ReflectionException; |
17
|
|
|
use ReflectionFunction; |
18
|
|
|
|
19
|
|
|
use function array_merge; |
20
|
|
|
use function array_pop; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Function invocation implementation |
24
|
|
|
*/ |
25
|
|
|
class ReflectionFunctionInvocation extends AbstractInvocation implements FunctionInvocation |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* Instance of reflection function |
29
|
|
|
*/ |
30
|
|
|
protected ReflectionFunction $reflectionFunction; |
|
|
|
|
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Constructor for function invocation |
34
|
|
|
* |
35
|
|
|
* @param array $advices List of advices for this invocation |
36
|
|
|
* |
37
|
|
|
* @throws ReflectionException |
38
|
|
|
*/ |
39
|
|
|
public function __construct(array $advices, string $functionName) |
40
|
|
|
{ |
41
|
|
|
parent::__construct($advices); |
42
|
|
|
$this->reflectionFunction = new ReflectionFunction($functionName); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Invokes original function and return result from it |
47
|
|
|
* |
48
|
|
|
* @return mixed |
49
|
|
|
*/ |
50
|
|
|
public function proceed() |
51
|
|
|
{ |
52
|
|
|
if (isset($this->advices[$this->current])) { |
53
|
|
|
$currentInterceptor = $this->advices[$this->current++]; |
54
|
|
|
|
55
|
|
|
return $currentInterceptor->invoke($this); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return $this->reflectionFunction->invokeArgs($this->arguments); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Gets the function being called. |
63
|
|
|
*/ |
64
|
|
|
public function getFunction(): ReflectionFunction |
65
|
|
|
{ |
66
|
|
|
return $this->reflectionFunction; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Invokes current function invocation with all interceptors |
71
|
|
|
* |
72
|
|
|
* @param array $arguments List of arguments for function invocation |
73
|
|
|
* @param array $variadicArguments Additional list of variadic arguments |
74
|
|
|
* |
75
|
|
|
* @return mixed Result of invocation |
76
|
|
|
*/ |
77
|
|
|
final public function __invoke(array $arguments = [], array $variadicArguments = []) |
78
|
|
|
{ |
79
|
|
|
if ($this->level > 0) { |
80
|
|
|
$this->stackFrames[] = [$this->arguments, $this->current]; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
if (!empty($variadicArguments)) { |
84
|
|
|
$arguments = array_merge($arguments, $variadicArguments); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
try { |
88
|
|
|
++$this->level; |
89
|
|
|
|
90
|
|
|
$this->current = 0; |
91
|
|
|
$this->arguments = $arguments; |
92
|
|
|
|
93
|
|
|
$result = $this->proceed(); |
94
|
|
|
} finally { |
95
|
|
|
--$this->level; |
96
|
|
|
|
97
|
|
|
if ($this->level > 0) { |
98
|
|
|
[$this->arguments, $this->current] = array_pop($this->stackFrames); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return $result; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Returns a friendly description of current joinpoint |
107
|
|
|
*/ |
108
|
|
|
final public function __toString(): string |
109
|
|
|
{ |
110
|
|
|
return sprintf( |
111
|
|
|
'execution(%s())', |
112
|
|
|
$this->reflectionFunction->getName() |
113
|
|
|
); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|