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