stack up the middleware.
converts normal Application into middleware.

@param ApplicationInterface $handler
@return $this
12733
public function push($handler)
    {
        if (!$handler) {
            return $this;
        }
        if ($this->next) {
            return $this->next->push($handler);
        }
        if (!$handler instanceof MiddlewareInterface) {
            $handler = new Middleware($handler);
        }
        $this->next = $handler;
        return $this->next;
    }
887202
public function handler(RouteHandlerInterface $handler)
    {
        $this->_method('before', [$handler, 'before']);
        $this->_method('wrap', [$handler, 'wrap']);
        $this->_method('after', [$handler, 'after']);

        return $this;
    }
224663
private function createMiddlewareChainImpl(
        Closure $handler,
        PsrContainerInterface $container,
        array $middleware = null
    ): Closure {
        if (empty($middleware) === false) {
            $start = count($middleware) - 1;
            for ($index = $start; $index >= 0; $index--) {
                $handler = $this->createMiddlewareChainLink($handler, $middleware[$index], $container);
            }
        }

        return $handler;
    }
810836
private function initHandler(SaveHandlerInterface $handler)
    {
        return session_set_save_handler(array($handler, 'open'), 
                                        array($handler, 'close'), 
                                        array($handler, 'read'), 
                                        array($handler, 'write'), 
                                        array($handler, 'destroy'), 
                                        array($handler, 'gc'));
    }
22190
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
    {
        $middlewares = $this->module_service->findByInterface(MiddlewareInterface::class);

        $pipeline = $handler;

        foreach ($middlewares as $middleware) {
            $pipeline = new class($pipeline, $middleware) implements RequestHandlerInterface {
                /** @var RequestHandlerInterface */
                private $handler;

                /** @var MiddlewareInterface */
                private $middleware;

                public function __construct(RequestHandlerInterface $handler, MiddlewareInterface $middleware)
                {
                    $this->handler    = $handler;
                    $this->middleware = $middleware;
                }

                public function handle(ServerRequestInterface $request): ResponseInterface
                {
                    return $this->middleware->process($request, $this->handler);
                }
            };
        }

        return $pipeline->handle($request);
    }
108448
public function setHandler(callable $handler = null)
    {
        $old = $this->handler;
        $this->handler = $handler;

        return $old;
    }
792224
protected function addHandler(TypedHandler $handler)
    {
        $this->addHandlerTypes($handler);
        $this->handlers[] = $handler;
        return $this;
    }
1145100
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
    {
        $httpHandler = $request->getAttribute(AttributeEnum::ROUTER_ATTRIBUTE);
        $info = $httpHandler[2];

        $actionMiddlewares = [];
        if (isset($info['handler']) && \is_string($info['handler'])) {
            // Extract action info from router handler
            $exploded             = explode('@', $info['handler']);
            $controllerClass      = $exploded[0] ?? '';
            $action               = $exploded[1] ?? '';

            $collector = MiddlewareCollector::getCollector();
            $collectedMiddlewares = $collector[$controllerClass]['middlewares']??[];

            // Get group middleware from Collector
            if ($controllerClass) {
                $collect = $collectedMiddlewares['group'] ?? [];
                $collect && $actionMiddlewares = array_merge($actionMiddlewares, $collect);
            }
            // Get the specified action middleware from Collector
            if ($action) {
                $collect = $collectedMiddlewares['actions'][$action]??[];
                $collect && $actionMiddlewares = array_merge($actionMiddlewares, $collect ?? []);
            }
        }
        if (!empty($actionMiddlewares) && $handler instanceof RequestHandler) {
            $handler->insertMiddlewares($actionMiddlewares);
        }

        return $handler->handle($request);
    }
390593
public function process(RequestInterface $request, HandlerInterface $handler)
    {
        $middleware = $this->callback;

        return $middleware($request, $handler);
    }
1254514
public function setHandler(AbstractHandler $handler)
    {
        $handler->setErrorHandler($this);
        $this->handler = $handler;

        return $this;
    }
601698
public function purge($uri, $handler, $middlewares = array())
    {
        return $this->add($this->route('PURGE', $uri, $handler, $middlewares));
    }
443672
public function put($uri, $handler, $middlewares = array())
    {
        return $this->add($this->route('PUT', $uri, $handler, $middlewares));
    }
1070269
public function applyData($name, $handler = null)
    {
        if (is_callable($name)) {
            $handler = $name;
            $name    = null;
        }

        $handler($this->getRepository($name), $this->getData());

        return $this;
    }
1225751
public function append(MiddlewareInterface $middleware){
        if($this->isAlreadyRegistered($middleware)){
            throw new Exception\MiddlewareAlreadyRegisteredException(
                static::createMsgAlreadyRegisterd($middleware)
            );
        }

        $this->registerd[$middleware]   = true;

        $this->queue->push($middleware);

        return $this;
    }
466287
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
    {
        $response = ($this->handler)($request, $handler);

        if (!($response instanceof ResponseInterface)) {
            throw new \LogicException('The middleware must return a ResponseInterface');
        }

        return $response;
    }
1037646
protected function setHandler(ServerRequestInterface $request, $handler): ServerRequestInterface
    {
        return $request->withAttribute($this->attribute, $handler);
    }
1016481
public function process(RequestInterface $request, HandlerInterface $handler)
    {
        $this->stack[] = new LastMiddleware($handler);

        $response = $this->dispatch($request);

        unset($this->stack[count($this->stack) - 1]);

        return $response;
    }