Repository navigation
Expand file tree
/
Copy pathAdviceChain.php
More file actions
156 lines (134 loc) · 4.87 KB
/
Copy pathAdviceChain.php
File metadata and controls
156 lines (134 loc) · 4.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php
/** @noinspection PhpPropertyOnlyWrittenInspection */
namespace Okapi\Aop\Core\Invocation;
use Closure;
use DI\Attribute\Inject;
use Okapi\Aop\Attributes\After;
use Okapi\Aop\Core\Container\AdviceType\MethodAdviceContainer;
use Okapi\Aop\Core\Factory\InvocationFactory;
use Okapi\Aop\Invocation\MethodInvocation;
/**
* # Advice Chain
*
* This class is responsible for executing the advice chain.
*/
class AdviceChain
{
// region DI
#[Inject]
private InvocationFactory $invocationFactory;
// endregion
private int $currentInterceptorIndex = 0;
private readonly ?Closure $originalMethod;
/**
* Stores the result of the target method.
*
* @var mixed|null
*/
private mixed $result = null;
/**
* Whether the result has been manually set.
*
* @var bool
*/
private bool $resultHasBeenSet = false;
/**
* AroundAdviceChain constructor.
*
* @param MethodAdviceContainer[] $interceptors
* @param object|null $subject
* @param string $className
* @param string $methodName
* @param array $arguments
* @param callable|null $originalMethod
* @param mixed $resultFromOriginalMethod
*
* @noinspection PhpMissingParamTypeInspection
*/
public function __construct(
private readonly array $interceptors,
private readonly ?object $subject,
private readonly string $className,
private readonly string $methodName,
private array &$arguments,
?callable $originalMethod = null,
private readonly mixed $resultFromOriginalMethod = null,
) {
$this->originalMethod = $originalMethod === null ? null : Closure::fromCallable($originalMethod);
if ($this->interceptors[0]->adviceAttributeInstance instanceof After) {
$this->setResult($this->resultFromOriginalMethod);
}
}
/**
* Proceed to next interceptor or target method.
*
* @param bool $allowRepeatedCalls
*
* @return mixed
*
* @noinspection PhpUnhandledExceptionInspection
* @noinspection PhpDocMissingThrowsInspection
*/
public function proceed(bool $allowRepeatedCalls = false): mixed
{
if ($this->currentInterceptorIndex < count($this->interceptors)) {
$interceptor = $this->interceptors[$this->currentInterceptorIndex];
$this->currentInterceptorIndex++;
$aspectInstance = $interceptor->aspectInstance;
$adviceRefMethod = $interceptor->adviceRefMethod;
// Get invocation
/** @var MethodInvocation&AdviceChainAwareTrait $invocation */
$invocation = $this->invocationFactory->getInvocation(
adviceContainer: $interceptor,
subject: $this->subject,
className: $this->className,
methodName: $this->methodName,
result: null,
arguments: $this->arguments,
);
// Set advice chain to invocation
$invocation->setAdviceChain($this);
// Call the advice method
/** @var mixed $result */
$result = $adviceRefMethod->invoke($aspectInstance, $invocation);
// Check if the advice method will return a value
$hasReturnValue = $adviceRefMethod->getReturnType()?->__toString() !== 'void';
// 1. Accept return value of advice method OR
// 2. Check if the advice method used "setResult()" OR
// 3. Proceed with the next advice in the chain
if ($hasReturnValue && $result !== null) {
// If the advice returns null instead of using "setResult()"
// and does not have a return type we ignore the return value,
// because it's not possible to distinguish between a null
// return value and a void return value.
$this->setResult($result);
}
return $this->proceed($allowRepeatedCalls);
}
// 1. Check if the result has been set AND if repeated calls are allowed OR
// 2. Check if the original method is available OR
// 3. Return the result from the original method
if ($this->resultHasBeenSet && !$allowRepeatedCalls) {
return $this->result;
}
if ($this->originalMethod) {
/** @var mixed $result */
$result = ($this->originalMethod)(...array_values($this->arguments));
$this->setResult($result);
return $result;
}
return $this->resultFromOriginalMethod;
}
/**
* Set result.
*
* @param mixed $result
*
* @return void
*/
public function setResult(mixed $result): void
{
$this->result = $result;
$this->resultHasBeenSet = true;
}
}