alexpts/php-routing

View on GitHub
src/PTS/Routing/Traits/MiddlewaresTrait.php

Summary

Maintainability
A
0 mins
Test Coverage
<?php
declare(strict_types = 1);

namespace PTS\Routing\Traits;

use Psr\Http\Message\RequestInterface;

trait MiddlewaresTrait
{
    /** @var callable[] */
    protected $middlewares = [];


    public function pushMiddleware(callable $middleware): self
    {
        $this->middlewares[] = $middleware;
        return $this;
    }

    /**
     * @return \callable[]
     */
    public function getMiddlewares(): array
    {
        return $this->middlewares;
    }

    protected function invoke(RequestInterface $request)
    {
        $middleware = array_shift($this->middlewares);
        return $middleware(... [$request, $this]);
    }

}