-
-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathSupportsMiddleware.php
More file actions
125 lines (107 loc) · 3.21 KB
/
SupportsMiddleware.php
File metadata and controls
125 lines (107 loc) · 3.21 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
<?php
declare(strict_types=1);
namespace Fetch\Concerns;
use Fetch\Http\MiddlewarePipeline;
use Fetch\Interfaces\ClientHandler;
use Fetch\Interfaces\MiddlewareInterface;
/**
* Trait for adding middleware support to HTTP clients.
*
* This trait provides methods for managing and executing middleware
* that can transform requests and responses.
*/
trait SupportsMiddleware
{
/**
* The middleware pipeline instance.
*/
protected ?MiddlewarePipeline $middlewarePipeline = null;
/**
* Set multiple middleware at once.
*
* @param array<MiddlewareInterface|array{middleware: MiddlewareInterface, priority: int}> $middleware
* @return $this
*/
public function middleware(array $middleware): ClientHandler
{
$this->middlewarePipeline = new MiddlewarePipeline($middleware);
return $this;
}
/**
* Add a single middleware to the pipeline.
*
* @param MiddlewareInterface $middleware The middleware to add
* @param int $priority Higher priority middleware runs first (default: 0)
* @return $this
*/
public function addMiddleware(MiddlewareInterface $middleware, int $priority = 0): ClientHandler
{
$this->getMiddlewarePipeline()->add($middleware, $priority);
return $this;
}
/**
* Prepend middleware to run first (with highest priority).
*
* @param MiddlewareInterface $middleware The middleware to prepend
* @return $this
*/
public function prependMiddleware(MiddlewareInterface $middleware): ClientHandler
{
$this->getMiddlewarePipeline()->prepend($middleware);
return $this;
}
/**
* Get the middleware pipeline instance.
*/
public function getMiddlewarePipeline(): MiddlewarePipeline
{
if ($this->middlewarePipeline === null) {
$this->middlewarePipeline = new MiddlewarePipeline;
}
return $this->middlewarePipeline;
}
/**
* Check if any middleware is registered.
*/
public function hasMiddleware(): bool
{
return $this->middlewarePipeline !== null && ! $this->middlewarePipeline->isEmpty();
}
/**
* Clear all middleware from the pipeline.
*
* @return $this
*/
public function clearMiddleware(): ClientHandler
{
if ($this->middlewarePipeline !== null) {
$this->middlewarePipeline->clear();
}
return $this;
}
/**
* Conditionally add middleware.
*
* @param bool $condition The condition to check
* @param callable $callback Callback that receives $this and should add middleware
* @return $this
*/
public function when(bool $condition, callable $callback): ClientHandler
{
if ($condition) {
$callback($this);
}
return $this;
}
/**
* Conditionally add middleware (inverse of when).
*
* @param bool $condition The condition to check
* @param callable $callback Callback that receives $this and should add middleware
* @return $this
*/
public function unless(bool $condition, callable $callback): ClientHandler
{
return $this->when(! $condition, $callback);
}
}