-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHasPipes.php
122 lines (100 loc) · 3.29 KB
/
HasPipes.php
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
<?php
declare(strict_types=1);
/**
* Copyright (c) 2024-2025 guanguans<ityaozm@gmail.com>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*
* @see https://github.com/guanguans/laravel-api-response
*/
namespace Guanguans\LaravelApiResponse\Concerns;
use Guanguans\LaravelApiResponse\Exceptions\InvalidArgumentException;
use Illuminate\Support\Collection;
/**
* @see https://github.com/guzzle/guzzle/blob/8.0/src/HandlerStack.php
* @see \GuzzleHttp\HandlerStack
*
* @mixin \Guanguans\LaravelApiResponse\ApiResponse
*/
trait HasPipes
{
protected Collection $pipes;
public function unshiftPipes(mixed ...$pipes): self
{
return $this->tapPipes(static function (Collection $originalPipes) use ($pipes): void {
$originalPipes->unshift(...$pipes);
});
}
public function pushPipes(mixed ...$pipes): self
{
return $this->tapPipes(static function (Collection $originalPipes) use ($pipes): void {
$originalPipes->push(...$pipes);
});
}
public function beforePipes(string $findPipe, mixed ...$pipes): self
{
return $this->splicePipes($findPipe, $pipes, true);
}
public function afterPipes(string $findPipe, mixed ...$pipes): self
{
return $this->splicePipes($findPipe, $pipes, false);
}
public function removePipes(string ...$findPipes): self
{
return $this->extendPipes(
static fn (Collection $pipes): Collection => $pipes
->reject(static function ($pipe) use ($findPipes): bool {
if (\is_object($pipe) && !$pipe instanceof \Closure) {
$pipe = $pipe::class;
}
if (!\is_string($pipe)) {
return false;
}
return str($pipe)->startsWith($findPipes);
})
->values()
);
}
public function extendPipes(callable $callback): self
{
$this->pipes = $this->pipes->pipe($callback);
return $this;
}
public function tapPipes(callable $callback): self
{
tap($this->pipes, $callback);
return $this;
}
/**
* @param list<mixed> $pipes
*/
private function splicePipes(string $findPipe, array $pipes, bool $before): self
{
$idx = $this->findByPipe($findPipe);
if ($before) {
if (0 === $idx) {
$this->pipes->unshift(...$pipes);
} else {
$this->pipes->splice($idx, 1, [...$pipes, $this->pipes[$idx]]);
}
} elseif ($this->pipes->count() - 1 === $idx) {
$this->pipes->push(...$pipes);
} else {
$this->pipes->splice($idx, 1, [$this->pipes[$idx], ...$pipes]);
}
return $this;
}
private function findByPipe(string $findPipe): int
{
foreach ($this->pipes as $idx => $pipe) {
if (\is_object($pipe) && !$pipe instanceof \Closure) {
$pipe = $pipe::class;
}
if (\is_string($pipe) && str($pipe)->startsWith($findPipe)) {
return $idx;
}
}
throw new InvalidArgumentException("Pipe not found: $findPipe");
}
}