1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Spiral\Core; |
6
|
|
|
|
7
|
|
|
use Psr\EventDispatcher\EventDispatcherInterface; |
8
|
|
|
use Spiral\Core\Event\InterceptorCalling; |
9
|
|
|
use Spiral\Core\Exception\InterceptorException; |
10
|
|
|
use Spiral\Interceptors\Context\CallContext; |
11
|
|
|
use Spiral\Interceptors\Context\Target; |
12
|
|
|
use Spiral\Interceptors\HandlerInterface; |
13
|
|
|
use Spiral\Interceptors\InterceptorInterface; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Provides the ability to modify the call to the domain core on it's way to the action. |
17
|
|
|
* |
18
|
|
|
* @deprecated use {@see \Spiral\Interceptors\Handler\InterceptorPipeline} instead |
19
|
|
|
*/ |
20
|
|
|
final class InterceptorPipeline implements CoreInterface, HandlerInterface |
|
|
|
|
21
|
|
|
{ |
22
|
|
|
private ?CoreInterface $core = null; |
23
|
|
|
private ?HandlerInterface $handler = null; |
24
|
|
|
|
25
|
|
|
/** @var list<CoreInterceptorInterface|InterceptorInterface> */ |
|
|
|
|
26
|
|
|
private array $interceptors = []; |
27
|
|
|
|
28
|
|
|
private int $position = 0; |
29
|
|
|
private ?CallContext $context = null; |
30
|
|
|
|
31
|
445 |
|
public function __construct( |
32
|
|
|
private readonly ?EventDispatcherInterface $dispatcher = null |
33
|
|
|
) { |
34
|
445 |
|
} |
35
|
|
|
|
36
|
436 |
|
public function addInterceptor(CoreInterceptorInterface|InterceptorInterface $interceptor): void |
37
|
|
|
{ |
38
|
436 |
|
$this->interceptors[] = $interceptor; |
39
|
|
|
} |
40
|
|
|
|
41
|
436 |
|
public function withCore(CoreInterface $core): self |
42
|
|
|
{ |
43
|
436 |
|
$pipeline = clone $this; |
44
|
436 |
|
$pipeline->core = $core; |
45
|
436 |
|
$pipeline->handler = null; |
46
|
436 |
|
return $pipeline; |
47
|
|
|
} |
48
|
|
|
|
49
|
34 |
|
public function withHandler(HandlerInterface $handler): self |
50
|
|
|
{ |
51
|
34 |
|
$pipeline = clone $this; |
52
|
34 |
|
$pipeline->handler = $handler; |
53
|
34 |
|
$pipeline->core = null; |
54
|
34 |
|
return $pipeline; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @throws \Throwable |
59
|
|
|
*/ |
60
|
170 |
|
public function callAction(string $controller, string $action, array $parameters = []): mixed |
61
|
|
|
{ |
62
|
170 |
|
if ($this->context === null) { |
63
|
26 |
|
return $this->handle( |
64
|
26 |
|
new CallContext(Target::fromPathArray([$controller, $action]), $parameters), |
65
|
26 |
|
); |
66
|
|
|
} |
67
|
|
|
|
68
|
154 |
|
if ($this->context->getTarget()->getPath() === [$controller, $action]) { |
69
|
153 |
|
return $this->handle($this->context->withArguments($parameters)); |
70
|
|
|
} |
71
|
|
|
|
72
|
1 |
|
return $this->handle( |
73
|
1 |
|
$this->context->withTarget( |
74
|
1 |
|
Target::fromPathArray([$controller, $action]), |
75
|
1 |
|
)->withArguments($parameters) |
76
|
1 |
|
); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @throws \Throwable |
81
|
|
|
*/ |
82
|
171 |
|
public function handle(CallContext $context): mixed |
83
|
|
|
{ |
84
|
171 |
|
if ($this->core === null && $this->handler === null) { |
85
|
3 |
|
throw new InterceptorException('Unable to invoke pipeline without last handler.'); |
|
|
|
|
86
|
|
|
} |
87
|
|
|
|
88
|
168 |
|
$path = $context->getTarget()->getPath(); |
89
|
|
|
|
90
|
168 |
|
if (isset($this->interceptors[$this->position])) { |
91
|
165 |
|
$interceptor = $this->interceptors[$this->position]; |
92
|
165 |
|
$handler = $this->nextWithContext($context); |
93
|
|
|
|
94
|
165 |
|
$this->dispatcher?->dispatch( |
95
|
165 |
|
new InterceptorCalling( |
|
|
|
|
96
|
165 |
|
controller: $path[0] ?? '', |
97
|
165 |
|
action: $path[1] ?? '', |
98
|
165 |
|
parameters: $context->getArguments(), |
99
|
165 |
|
interceptor: $interceptor, |
100
|
165 |
|
) |
101
|
165 |
|
); |
102
|
|
|
|
103
|
165 |
|
return $interceptor instanceof CoreInterceptorInterface |
104
|
164 |
|
? $interceptor->process($path[0] ?? '', $path[1] ?? '', $context->getArguments(), $handler) |
105
|
139 |
|
: $interceptor->intercept($context, $handler); |
106
|
|
|
} |
107
|
|
|
|
108
|
156 |
|
return $this->core === null |
109
|
26 |
|
? $this->handler->handle($context) |
|
|
|
|
110
|
156 |
|
: $this->core->callAction($path[0] ?? '', $path[1] ?? '', $context->getArguments()); |
111
|
|
|
} |
112
|
|
|
|
113
|
165 |
|
private function nextWithContext(CallContext $context): self |
114
|
|
|
{ |
115
|
165 |
|
$pipeline = clone $this; |
116
|
165 |
|
$pipeline->context = $context; |
117
|
165 |
|
++$pipeline->position; |
118
|
165 |
|
return $pipeline; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
This interface has been deprecated. The supplier of the interface has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the interface will be removed and what other interface to use instead.