src/Valkyrja/Http/Middleware/MiddlewareAwareTrait.php
public function requestMiddleware(Request $request, array $middleware = null)
    {
        // Set the middleware to any middleware passed or the base middleware
        $middleware = $middleware ?? self::$middleware;

        $modifiedRequest = $request;

        // Get the application
        $app = $this->getApplication();

        // Iterate through the middleware
        foreach ($middleware as $item) {
            // If the middleware is a group
            if ($this->isMiddlewareGroup($item)) {
                // Recurse into that middleware group
                $this->requestMiddleware(
                    $request,
                    $this->getMiddlewareGroup($item)
                );

                continue;
            }

            /* @var \Valkyrja\Http\Middleware\Middleware $item */
            $modifiedRequest = $item::before($request);

            if ($modifiedRequest instanceof Response) {
                return $modifiedRequest;
            }

            // Set the returned request in the container
            $app->container()->singleton(Request::class, $modifiedRequest);
        }

        return $modifiedRequest;
    }
public function requestMiddleware(Request $request, array $middleware = null)
    {
        // Set the middleware to any middleware passed or the base middleware
        $middleware = $middleware ?? self::$middleware;

        $modifiedRequest = $request;

        // Get the application
        $app = $this->getApplication();

        // Iterate through the middleware
        foreach ($middleware as $item) {
            // If the middleware is a group
            if ($this->isMiddlewareGroup($item)) {
                // Recurse into that middleware group
                $this->requestMiddleware(
                    $request,
                    $this->getMiddlewareGroup($item)
                );

                continue;
            }

            /* @var \Valkyrja\Http\Middleware\Middleware $item */
            $modifiedRequest = $item::before($request);

            if ($modifiedRequest instanceof Response) {
                return $modifiedRequest;
            }

            // Set the returned request in the container
            $app->container()->singleton(Request::class, $modifiedRequest);
        }

        return $modifiedRequest;
    }
Dispatch middleware.

@param Request $request    The request
@param array   $middleware [optional] The middleware to dispatch

@return mixed