Get a chain of middlewares that pre process
arguments before entering the current route handler

@param array $middlewares
The middlewares to compose

@return Closure
The middleware chain
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);
		};
	}
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;
    }
217282
public function middleware($middlewareQueue)
    {
        $middlewareQueue
            // Catch any exceptions in the lower layers,
            // and make an error page/response
            ->add(ErrorHandlerMiddleware::class)

            // Handle plugin/theme assets like CakePHP normally does.
            ->add(AssetMiddleware::class)

            // Add routing middleware.
            ->add(new RoutingMiddleware($this));

        return $middlewareQueue;
    }
1148749
public function middlewares(array $middlewares = []) {
        if (!$this->isGroupe) {
            end($this->virtualRoutes['*']);
            $lastKey = key($this->virtualRoutes['*']);
            $this->virtualRoutes['*'][$lastKey]['middlewares'] = $middlewares;
        } else {
            end($this->virtualRoutes);
            $lastKeyOfFirstRound = key($this->virtualRoutes);

            if (!$this->isGroupeMiddlewares) {
                end($this->virtualRoutes[$lastKeyOfFirstRound]);
                $lastKeyOfSecondRound = key($this->virtualRoutes[$lastKeyOfFirstRound]);
                $this->virtualRoutes[$lastKeyOfFirstRound][$lastKeyOfSecondRound]['middlewares'] = $middlewares;
            } else {
                $this->isGroupe = FALSE;

                foreach ($this->virtualRoutes[$lastKeyOfFirstRound] as &$route) {
                    $hasMiddleware = end($route);
                    if (!is_array($hasMiddleware)) {
                        $route['middlewares'] = $middlewares;
                    } else {
                        foreach ($middlewares as $middleware) {
                            $route['middlewares'][] = $middleware;
                        }
                    }
                }
            }
        }
    }
173801
public function setMiddleware($middleware, array $properties = array())
	{
		// reset on each call
		$this->properties['middleware'] = array();

		if(!is_array($middleware))
		{
			$this->properties['middleware'][] = array($middleware, $properties);
		}
		else
		{
			foreach($middleware as $m)
				$this->properties['middleware'][] = array($m);
		}

		return $this;
	}
211971
private function chainMiddleware(array $middleware): callable
    {
        $middleware = array_reverse($middleware);

        return array_reduce($middleware, function (callable $next, MiddlewareInterface $middleware): callable {
            return function (View $view) use ($middleware, $next): View {
                return $middleware->process($view, $this->request, $next);
            };
        }, function (View $view): View {
            $view->render();

            return $view;
        });
    }
296133
public function withoutMiddleware($middleware = null)
    {
        if (is_null($middleware)) {
            $this->app->instance('middleware.disable', true);

            return $this;
        }

        foreach ((array) $middleware as $abstract) {
            $this->app->instance($abstract, new class {
                public function handle($request, $next)
                {
                    return $next($request);
                }
            });
        }

        return $this;
    }
1333094
public function middleware($middlewares, bool $addToFront = false): MiddlewareAwareInterface
    {
        if (! is_array($middlewares)) {
            $middlewares = [$middlewares];
        }

        foreach ($middlewares as $middleware) {
            if ($addToFront) {
                //prepend Middleware
                array_unshift($this->middlewares, $middleware);
            } else {
                // append Middleware
                array_push($this->middlewares, $middleware);
            }
        }

        return $this;
    }
721246
public function prependMiddleware($middleware)
    {
        if (array_search($middleware, $this->middleware) === false) {
            array_unshift($this->middleware, $middleware);
        }

        return $this;
    }
1044211
public static function create(array $middlewares)
    {
        if (empty($middlewares)) {
            throw new StackException('Middlewares array cannot be empty.');
        }

        $stack = new static();

        $stack->middlewares = $middlewares;

        return $stack;
    }
637257
protected function headerMiddleware($headers)
    {
        return function (callable $handler) use ($headers) {
            return function (RequestInterface $request, array $options) use ($handler, $headers) {
                foreach ($headers as $key => $header) {
                    $request = $request->withHeader($key, $header);
                }

                return $handler($request, $options);
            };
        };
    }
1315684
public function addMiddleware($middleware, array $properties = array())
    {
        if($this->route)
        {
            $this->route->addMiddleware($middleware, $properties);
        }
        else
        {
            $this->middlewares[] = array($middleware, $properties);
        }

        return $this;
    }
871902
public function middleware($middleware)
    {
        $middleWarePath = APPPATH . '/middleware/' . $middleware . '.php';

        if (file_exists($middleWarePath)) {
            require_once($middleWarePath);

            $middleware();
        } else {
            throw new RuntimeException('Unable to load middleware file: ' . $middleWarePath);
        }
    }
758058
public function pipes(array $middlewares): App
    {
        foreach ($middlewares as $key => $middleware) {
            if (is_numeric($key)) {
                $this->pipe($middleware);
                continue;
            }
            $this->pipe($middleware, $key);
        }
        return $this;
    }
843830
public function middleware(...$middlewares): void
    {
        foreach ($middlewares as $middleware) {
            if (!$middleware instanceof MiddlewareInterface && !\is_string($middleware)) {
                $type = \is_object($middleware) ? \get_class($middleware) : \gettype($middleware);
                
                throw new \InvalidArgumentException(\sprintf('Unsupported middleware type: %s', $type));
            }
            
            $this->middlewares[] = $middleware;
        }
    }
935899
protected function getMiddleware($route)
    {
        return collect($route->gatherMiddleware())->map(function ($middleware) {
            return $middleware instanceof \Closure ? 'Closure' : $middleware;
        })->implode(',');
    }
908101
public function after($middlewares, $path)
    {
        $count = count($middlewares);
        for ($i = $count - 1; $i >= 0; $i--) {
            $middlewares[$i]->getProxy()->after_handle($path);
        }
    }