Resolves the middleware.

@param  string            $middleware middleware
@throws \RuntimeException
@return array
323631
protected function resolveMiddleware(string $middleware): array
	{
		[$name, $parameters] = $this->parseFunction($middleware);

		if(!isset($this->middleware[$name]))
		{
			throw new RuntimeException(vsprintf('No middleware named [ %s ] has been registered.', [$middleware]));
		}

		return ['name' => $name, 'middleware' => $this->middleware[$name], 'parameters' => $parameters];
	}
531587
def get_middleware_resolvers(middlewares: Tuple[Any, ...]) -> Iterator[Callable]:
    """"""
    for middleware in middlewares:
        if isfunction(middleware):
            yield middleware
        else:  # middleware provided as object with 'resolve' method
            resolver_func = getattr(middleware, "resolve", None)
            if resolver_func is not None:
                yield resolver_func
236460
public static function resolve($name, $nameMiddleware, $middlewareGroups)
    {
        if ($name instanceof Closure) {
            return $name;
        }

        if (isset($nameMiddleware[$name]) && $nameMiddleware[$name] instanceof Closure) {
            return $nameMiddleware[$name];
        }

        if (isset($middlewareGroups[$name])) {
            return static::parseMiddlewareGroup($name, $nameMiddleware, $middlewareGroups);
        }

        [$middleware, $parameters] = array_pad(explode(':', $name, 2), 2, null);

        if (isset($nameMiddleware[$middleware])) {
            $middleware = $nameMiddleware[$middleware];
        }

        return $middleware.($parameters ? ':'.$parameters : '');
    }
221079
public function applyMiddleware($path, array $middleware)
    {
        foreach ($middleware as $name) {
            if (!$this->hasMiddleware($name) && !$this->hasMiddlewareGroup($name)) {
                $message = "Cannot apply '$name' middleware or middleware group to path '$path'. It has not been registered.";
                throw new RuntimeException($message);
            }
        }
        // Matches route element pattern in Cake\Routing\Route
        $path = '#^' . preg_quote($path, '#') . '#';
        $path = preg_replace('/\\\\:([a-z0-9-_]+(?<![-_]))/i', '[^/]+', $path);

        if (!isset($this->_middlewarePaths[$path])) {
            $this->_middlewarePaths[$path] = [];
        }
        $this->_middlewarePaths[$path] = array_merge($this->_middlewarePaths[$path], $middleware);

        return $this;
    }
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}]");
            }
        }
    }
554281
protected static function middleware()
    {
        foreach (self::fetch('http/middleware') as $file) {
            Bus::need($file);
        }

        Middleware::ini();
    }
897373
public function middleware($middleware = null)
    {
        if (is_null($middleware)) {
            return (array) ($this->action['middleware'] ?? []);
        }

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

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

        return $this;
    }
462715
public function compile($middleware)
    {
        if ($middleware instanceof MiddlewareFactory) {
            $subMiddlewares = $middleware->getSubMiddlewares();

            // Recursive compilation in sub-middlewares
            $subMiddlewares = array_map([$this, 'compile'], $subMiddlewares);

            $middleware = $middleware->create($this->container, $subMiddlewares);
        }

        return $middleware;
    }
1277435
protected function resolveMiddleware($callback)
    {
        $callbackResolver = $this->callbackResolver;

        return function (Request $request) use ($callback, $callbackResolver) {
            if (!is_string($callback) || substr($callback, 0, 2) !== '::') {
                return $callbackResolver->resolveCallback($callback);
            }

            $controller = $callbackResolver->resolveCallback($request->attributes->get('_controller'));
            if (is_array($controller)) {
                list($cls, $_) = $controller;
            } elseif (is_string($controller)) {
                if (strpos($controller, '::') !== false) {
                    list($cls, $_) = explode('::', $controller);
                } else {
                    $cls = $controller;
                }
            } else {
                return null;
            }
            $callback = [$cls, substr($callback, 2)];

            return $callback;
        };
    }
1054973
private function getMiddleware() :?MiddlewareInterface
    {
        if (array_key_exists($this->middlewaresIndex, $this->middlewares)) {
            $middleware = $this->middlewares[$this->middlewaresIndex];
            if (is_object($middleware) &&
                $middleware instanceof MiddlewareInterface
            ) {
                $this->middlewaresIndex++;
                return $middleware;
            } else {
                if (class_exists($middleware)) {
                    $reflexionClass = new \ReflectionClass($middleware);
                    if ($reflexionClass->implementsInterface(MiddlewareInterface::class)) {
                        $resultMiddleware = new $middleware();
                        $this->middlewaresIndex++;
                        return $resultMiddleware;
                    } else {
                        return null;
                    }
                } else {
                    return null;
                }
            }
        }
        return null;
    }
760309
private function composeMiddleware(array $middlewares)
	{
		$curryedMiddlewares = array_map(function($middleware) {
			return function ($next) use ($middleware) {
				return function (...$args) use ($middleware, $next) {
					return call_user_func($middleware, $next, ...$args);
				};
			};
		}, array_reverse($middlewares));

		return function ($handler) use ($curryedMiddlewares) {
			$handler = function(...$args) use ($handler) {
				return call_user_func_array($handler, $args);
			};
			return array_reduce($curryedMiddlewares, function($state, $middleware){
				return function(...$args) use($state, $middleware) {
					return $middleware($state)(...$args);
				};
			}, $handler);
		};
	}
1022681
protected function resolve($middleware): callable
    {
        if (null === $middleware) {
            return function (RequestInterface $request, ResponseInterface $response, callable $next) {
                return $response;
            };
        }

        return $this->resolveMiddleware($middleware);
    }
100780
public static function removeMiddleware($middleware)
    {
        $kernelFile = self::read();
        if (str_contains($kernelFile, $middleware)) {
            $kernelFile = preg_replace('^.*' . $middleware . '.*$', '', $kernelFile);
            self::save($kernelFile);
        }
    }
91285
protected function orderMiddlewareByPriority(array $middleware): array
	{
		if(empty($this->middlewarePriority))
		{
			return $middleware;
		}

		$priority = array_intersect_key($this->middlewarePriority, $middleware) + array_fill_keys(array_keys(array_diff_key($middleware, $this->middlewarePriority)), static::MIDDLEWARE_DEFAULT_PRIORITY);

		// Sort the priority map using stable sorting

		$position = 0;

		foreach($priority as $key => $value)
		{
			$priority[$key] = [$position++, $value];
		}

		uasort($priority, function($a, $b)
		{
			return $a[1] === $b[1] ? ($a[0] > $b[0] ? 1 : -1) : ($a[1] > $b[1] ? 1 : -1);
		});

		foreach($priority as $key => $value)
		{
			$priority[$key] = $value[1];
		}

		// Return sorted middleware list

		return array_merge($priority, $middleware);
	}
256316
public static function addMiddleware($middleware)
    {
        $kernelFile = self::read();
        if (!str_contains($kernelFile, $middleware)) {
            $kernelFile = self::insertBetween($kernelFile, 'protected $middleware = [', "]", $middleware);
            self::save($kernelFile);
        }
    }
498598
public function addMiddlewares(...$middlewares): array
    {
        $stack = $this->getMiddlewareStack(true);
        foreach ($middlewares as $middleware) {
            $stack->append($middleware);
        }
        return $middlewares;
    }
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;
    }