The core middleware pipeline implementation that manages the execution order and delegation of middleware components.
MiddlewarePipe is the central component that orchestrates middleware execution in the cmd-bus system. It maintains a queue of middleware components and ensures they execute in the correct order (FIFO - First In, First Out).
final class MiddlewarePipe implements MiddlewarePipelineInterface
{
/** @var SplQueue<MiddlewareInterface> */
private SplQueue $pipeline;
public function __construct();
public function __clone();
public function handle(CommandInterface $command): mixed;
public function process(CommandInterface $command, CommandHandlerInterface $handler): mixed;
public function pipe(MiddlewareInterface $middleware): void;
}Creates a new middleware pipeline with an empty queue.
public function __construct()
{
$this->pipeline = new SplQueue();
}Processes a command through the entire pipeline, using an EmptyPipelineHandler as the fallback handler.
Parameters:
$command- The command to process
Returns:
mixed- The result from the pipeline execution
Processes a command through the pipeline with a specific final handler.
Parameters:
$command- The command to process$handler- The handler to use when the pipeline is exhausted
Returns:
CommandResultInterface- The result from the pipeline execution
Adds middleware to the end of the pipeline queue.
Parameters:
$middleware- The middleware to add
Performs a deep clone of the pipeline queue to ensure cloned instances have independent pipelines.
use Webware\CommandBus\MiddlewarePipe;
use App\Middleware\LoggingMiddleware;
use App\Middleware\ValidationMiddleware;
use Webware\CommandBus\Middleware\CommandHandlerMiddleware;
$pipeline = new MiddlewarePipe();
// Add middleware in execution order
$pipeline->pipe(new LoggingMiddleware($logger));
$pipeline->pipe(new CommandHandlerMiddleware($handlerFactory));
// Process a command
$result = $pipeline->handle($command);The middleware pipeline executes in FIFO order:
- First Middleware - Executes first (highest priority)
- Second Middleware - Called by first middleware
- Third Middleware - Called by second middleware
- Final Handler - Called when pipeline is exhausted
- MiddlewarePipelineInterface - Interface this class implements
- MiddlewareInterface - Interface for middleware components
- Next - Helper class for pipeline delegation
- MiddlewarePipeFactory - Factory for creating instances