Get or set the middlewares attached to the route.

@param  array|string|null $middleware
@return $this|array
979768
public function middleware( $middleware = null )
	{
		if ( is_null( $middleware ) )
			return (array) Arr::get( $this->action, 'middleware', [] );

		if ( is_string( $middleware ) )
			$middleware = [$middleware];

		$this->action['middleware'] = array_merge( (array) Arr::get( $this->action, 'middleware', [] ), $middleware );

		return $this;
	}
1022681
protected function resolve($middleware): callable
    {
        if (null === $middleware) {
            return function (RequestInterface $request, ResponseInterface $response, callable $next) {
                return $response;
            };
        }

        return $this->resolveMiddleware($middleware);
    }
1175813
public function middleware($middleware, array $options = [])
    {
        foreach ((array) $middleware as $m) {
            $this->middleware[] = [
                'middleware' => $m,
                'options' => &$options,
            ];
        }

        return new ControllerMiddlewareOptions($options);
    }
730667
protected function getParsedRoutes($middleware = null, $prefix = null)
    {
        /** @var Collection $parsedRoutes */
        $parsedRoutes = Collection::make($this->router->getRoutes()->getRoutes())
            ->map(function ($route) {
                return $this->getRouteInformation($route);
            })
            ->filter();

        if ($middleware) {
            $parsedRoutes = $parsedRoutes->filter(function($routeInfo) use ($middleware) {
                return in_array($middleware, $routeInfo['middleware']);
            });
        }

        $parsedRoutes = $parsedRoutes->map(function($routeInfo){
            unset($routeInfo['middleware']);

            return $routeInfo;
        });

        if ($prefix) {
            $parsedRoutes = $parsedRoutes->map(function ($routeInfo) use ($prefix) {
                $routeInfo['uri'] = $prefix . $routeInfo['uri'];

                return $routeInfo;
            });
        }

        return $parsedRoutes->values()->all();
    }
1361704
public function expandMiddleware( $middleware ) {
		$classes = [];

		foreach ( $middleware as $item ) {
			$classes = array_merge(
				$classes,
				$this->expandMiddlewareMolecule( $item )
			);
		}

		return $classes;
	}
747968
public function withMiddleware(string $alias, string $middleware)
    {
        if (! class_exists($middleware)) {
            throw new \Exception("Middleware not found. [{$middleware}]");
        }

        $reflection = new \ReflectionClass($middleware);
        $interfaces = $reflection->getInterfaceNames();

        $isFind = false;
        foreach ($interfaces as &$interface) {
            if ($interface === MiddlewareInterface::class) {
                $isFind = true;
                break;
            }
        }

        if (! $isFind) {
            throw new \Exception("Middleware must instanceof Weipaitang\Middleware\MiddlewareInterface. [{$middleware}]");
        }

        unset($reflection, $interfaces);

        $this->middlewares[$alias] = $middleware;

        return $this;
    }
423769
public function middleware($middleware = null)
    {
        if (is_null($middleware)) {
            return $this->middleware;
        }

        if (is_string($middleware)) {
            $middleware = func_get_args();
        }

        $this->middleware = array_unique(array_merge($this->middleware, $middleware));

        return $this;
    }
292634
public static function RoutesMiddleware($name)
    {
        if (!self::existsRoutesMiddleware($name)) {
            throw new MiddlewareNotFoundException($name);
        }
        $middleware = self::allRoutesMiddleware();

        return array_get($middleware, $name);
    }
474129
public function append(MiddlewareInterface $middleware): MiddlewarePipe
    {
        $stack = new MiddlewarePipe();

        /** @noinspection PhpParamsInspection */
        return $stack
            ->append($this)
            ->append($middleware);
    }
408028
public function validMiddleware(array &$middlewares)
    {
        if (! $middlewares) {
            return;
        }

        foreach ($middlewares as &$middleware) {
            if ($middleware instanceof \Closure) {
                continue;
            }

            if (! $this->hasMiddleware($middleware)) {
                throw new \Exception("Middleware not found. [{$middleware}]");
            }
        }
    }
221864
public function middleware($middleware): Route
	{
		$this->middleware = array_merge($this->middleware, (array) $middleware);

		return $this;
	}
431654
public function sortMiddleware( $middleware ) {
		$sorted = $middleware;

		usort( $sorted, function ( $a, $b ) use ( $middleware ) {
			$a_priority = $this->getMiddlewarePriorityForMiddleware( $a );
			$b_priority = $this->getMiddlewarePriorityForMiddleware( $b );
			$priority = $b_priority - $a_priority;

			if ( $priority !== 0 ) {
				return $priority;
			}

			// Keep relative order from original array.
			return array_search( $a, $middleware ) - array_search( $b, $middleware );
		} );

		return array_values( $sorted );
	}
674733
protected function routeMiddleware($key, $middleware = null, $force = false)
    {
        if ($this->app->runningInConsole() && $force === false) {
            return $this->getRouter();
        }
        if (is_array($key)) {
            foreach ($key as $k => $m) {
                $this->routeMiddleware($k, $m);
            }

            return $this->getRouter();
        } else {
            $this->getRouter()->middleware($key, $middleware);
        }
    }
1326345
public function middleware($middleware, $name = null)
	{
        if($name)
            $this->properties['middleware'][$name] = $middleware;
        else
            $this->properties['middleware'][] = $middleware;

		return $this;
	}
781005
public function add($middleware)
    {
        if (is_array($middleware)) {
            $this->queue = array_merge($this->queue, $middleware);

            return $this;
        }
        $this->queue[] = $middleware;

        return $this;
    }
1170997
public function insertMiddlewares(array $middlewares, $offset = null)
    {
        null === $offset && $offset = $this->offset;
        $chunkArray = array_chunk($this->middlewares, $offset);
        $after = [];
        $before = $chunkArray[0];
        if (isset($chunkArray[1])) {
            $after = $chunkArray[1];
        }
        $middlewares = array_merge((array)$before, $middlewares, (array)$after);
        $this->middlewares = $middlewares;
        return $this;
    }
1242975
protected function convertMiddlewareToPipelineStages(array $middleware) : array
    {
        $stages = [];

        foreach ($middleware as $singleMiddleware) {
            if ($singleMiddleware instanceof MiddlewareParameters) {
                /** @var MiddlewareParameters $singleMiddleware */
                /** @var ParameterizedMiddleware $tempMiddleware */
                $tempMiddleware = $this->container->resolve($singleMiddleware->getMiddlewareClassName());
                $tempMiddleware->setParameters($singleMiddleware->getParameters());
                $singleMiddleware = $tempMiddleware;
            } elseif (is_string($singleMiddleware)) {
                $singleMiddleware = $this->container->resolve($singleMiddleware);
            }

            $stages[] = $singleMiddleware;
        }

        return $stages;
    }