-
-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #276 from saloonphp/feature/last-middleware
Feature | V3 - Allow Middleware To Be Reordered
- Loading branch information
Showing
10 changed files
with
220 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Saloon\Data; | ||
|
||
use Saloon\Enums\Order; | ||
|
||
class PipeOrder | ||
{ | ||
/** | ||
* Constructor | ||
*/ | ||
public function __construct( | ||
public readonly Order $type, | ||
) { | ||
// | ||
} | ||
|
||
/** | ||
* Run the middleware first | ||
*/ | ||
public static function first(): self | ||
{ | ||
return new self(Order::FIRST); | ||
} | ||
|
||
/** | ||
* Run the middleware last | ||
*/ | ||
public static function last(): self | ||
{ | ||
return new self(Order::LAST); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Saloon\Enums; | ||
|
||
enum Order: string | ||
{ | ||
case FIRST = 'first'; | ||
case LAST = 'last'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,8 @@ | |
namespace Saloon\Helpers; | ||
|
||
use Saloon\Data\Pipe; | ||
use Saloon\Enums\Order; | ||
use Saloon\Data\PipeOrder; | ||
use Saloon\Exceptions\DuplicatePipeNameException; | ||
use Saloon\Contracts\Pipeline as PipelineContract; | ||
|
||
|
@@ -24,17 +26,15 @@ class Pipeline implements PipelineContract | |
* @return $this | ||
* @throws \Saloon\Exceptions\DuplicatePipeNameException | ||
*/ | ||
public function pipe(callable $callable, bool $prepend = false, ?string $name = null): static | ||
public function pipe(callable $callable, ?string $name = null, ?PipeOrder $order = null): static | ||
{ | ||
$pipe = new Pipe($callable, $name); | ||
$pipe = new Pipe($callable, $name, $order); | ||
|
||
if (is_string($name) && $this->pipeExists($name)) { | ||
throw new DuplicatePipeNameException($name); | ||
} | ||
|
||
$prepend === true | ||
? array_unshift($this->pipes, $pipe) | ||
: $this->pipes[] = $pipe; | ||
$this->pipes[] = $pipe; | ||
|
||
return $this; | ||
} | ||
|
@@ -44,13 +44,46 @@ public function pipe(callable $callable, bool $prepend = false, ?string $name = | |
*/ | ||
public function process(mixed $payload): mixed | ||
{ | ||
foreach ($this->pipes as $pipe) { | ||
foreach ($this->sortPipes() as $pipe) { | ||
$payload = call_user_func($pipe->callable, $payload); | ||
} | ||
|
||
return $payload; | ||
} | ||
|
||
/** | ||
* Sort the pipes based on the "order" classes | ||
*/ | ||
protected function sortPipes(): array | ||
{ | ||
$pipes = $this->pipes; | ||
|
||
/** @var array<\Saloon\Data\PipeOrder> $pipeNames */ | ||
$pipeOrders = array_map(static fn (Pipe $pipe) => $pipe->order, $pipes); | ||
Check failure on line 62 in src/Helpers/Pipeline.php GitHub Actions / phpstan
|
||
|
||
// Now we'll iterate through the pipe orders and if a specific pipe | ||
// requests to be placed at the top - we will move the pipe to the | ||
// top of the array. If it wants to be at the bottom we can put it | ||
// there too. | ||
|
||
foreach ($pipeOrders as $index => $order) { | ||
if (is_null($order)) { | ||
continue; | ||
} | ||
|
||
$pipe = $pipes[$index]; | ||
|
||
unset($pipes[$index]); | ||
|
||
match (true) { | ||
$order->type === Order::FIRST => array_unshift($pipes, $pipe), | ||
$order->type === Order::LAST => $pipes[] = $pipe, | ||
}; | ||
} | ||
|
||
return $pipes; | ||
} | ||
|
||
/** | ||
* Set the pipes on the pipeline. | ||
* | ||
|
@@ -66,7 +99,7 @@ public function setPipes(array $pipes): static | |
// so we can check if the name already exists. | ||
|
||
foreach ($pipes as $pipe) { | ||
$this->pipe($pipe->callable, false, $pipe->name); | ||
$this->pipe($pipe->callable, $pipe->name, $pipe->order); | ||
} | ||
|
||
return $this; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.