Push function to middleware handler

@param HandlerStack $stack

@return HandlerStack
195256
public function push(HandlerStack $stack)
    {
        $stack->push(Middleware::mapRequest(function (RequestInterface $request) {
            $this->initEvent($request);

            return $request;
        }), 'eventDispatcher_initEvent');

        $stack->push(function (callable $handler) {
            return function (
                RequestInterface $request,
                array $options
            ) use ($handler) {
                $promise = $handler($request, $options);

                return $promise->then(
                    function (ResponseInterface $response) use ($request) {
                        $this->sendEvent($request, $response);

                        return $response;
                    },
                    function (\Exception $reason) use ($request) {
                        $this->sendErrorEvent($request, $reason);

                        throw $reason;
                    }
                );
            };
        }, 'eventDispatcher_dispatch');

        return $stack;
    }
956242
public static function handler(int $backoff = null, array $codes = null, HandlerStack $stack = null)
    {
        $stack = $stack ?: HandlerStack::create();

        $stack->push(Middleware::retry(function ($retries, RequestInterface $request, ResponseInterface $response = null, TransferException $exception = null) use ($codes) {
            return $retries < 3 && ($exception instanceof ConnectException || ($response && ($response->getStatusCode() >= 500 || in_array($response->getStatusCode(), $codes === null ? self::CODES : $codes, true))));
        }, function ($retries) use ($backoff) {
            return (int) pow(2, $retries) * ($backoff === null ? self::BACKOFF : $backoff);
        }));

        return $stack;
    }
1015856
protected function setLogHandler(\GuzzleHttp\HandlerStack $handlerStack, callable $logHandler)
    {
        try {
            $handlerStack->after('history', $logHandler, 'logger');
        } catch (\InvalidArgumentException $e) {
            try {
                $handlerStack->before('allow_redirects', $logHandler, 'logger');
            } catch (\InvalidArgumentException $e) {
                $handlerStack->push($logHandler, 'logger');
            }
        }

        return $this;
    }
972919
public function pushHandler($message = null)
    {
        $this->handle();

        if (\is_callable($message)) {
            $this->response = \call_user_func($message, $this->message);
        }

        if (\is_string($message) and class_exists($message)) {
            $this->messageHandlers[] = $message;
        }

        return $this;
    }
503097
public static function run(array $stack, ServerRequestInterface $request = null): ResponseInterface
    {
        if ($request === null) {
            $request = Factory::createServerRequest('GET', '/');
        }

        return (new static($stack))->dispatch($request);
    }
939183
public function setStack(array $stack): void
    {
        // reset
        $this->stack = [];

        foreach ($stack as $event) {
            if (!$event instanceof Event) {
                throw new EventException('Stack elements must be instanceof froq\event\Event object');
            }

            $this->stack[$this->normalizeName($event->getName())] = $event;
        }
    }
1139395
public function addStack(StackInterface $stack): ResultInterface
    {
        $this->stack->addStack($stack, false);
        return $this;
    }
604998
protected function resolve()
    {
        $callStack = new CallStack;

        $executePattern = $this->route->getProperty('execute');

        $handlers = array();

        foreach($this->route->getFullRoutes() as $route)
        {
            $group = $route->getGroup();

            // stack all the handlers
            foreach($group->getExecuteHandlers() as $name => $handler)
                $handlers[$name] = $handler;

            foreach($group->getMiddlewares() as $key => $middleware)
                $callStack->addCallable($this->resolveMiddleware($middleware[0]), $middleware[1]);

            // append all route middlewares
            foreach($route->getProperty('middleware') as $key => $middleware)
                $callStack->addCallable($this->resolveMiddleware($middleware[0]), $middleware[1]);

            foreach($route->getAttributes() as $key => $value) {
                if(is_array($value)) {
                    if(isset($this->attributes[$key]) && !is_array($this->attributes[$key]))
                        throw new Exception('Unable to push value into attribute [' . $key . '] on route ' . $route->getAbsoluteName(). '. The attribute type is not an array.');

                    foreach($value as $val) {
                        $this->attributes[$key][] = $val;
                    }
                } else {
                    $this->attributes[$key] = $value;
                }
            }

            // pass config.
            if($config = $route->getProperty('config'))
                $this->config = array_merge($this->config, $config);
        }

        foreach($handlers as $name => $class)
        {
            $handler = null;

            if(is_string($class))
            {
                $handler = new $class;
            }
            else if(is_object($class))
            {
                if($class instanceof \Closure) {
                    $class($handler = new DynamicHandler());
                } else if($class instanceof ExecuteHandler) {
                    $handler = $class;
                }
            }

            if(!$handler || !is_object($handler) || !($handler instanceof ExecuteHandler))
                throw new InvalidArgumentException('Handler must be either class name, ' . ExecuteHandler::class . ' or \Closure ');

            if($handler->validate($executePattern))
            {
                $resolve = $handler->resolve($executePattern);

                if(!is_callable($resolve))
                    throw new \Exedra\Exception\InvalidArgumentException('The resolve() method for handler [' . get_class($handler) . '] must return \Closure or callable');

                $properties = array();

                if($this->route->hasDependencies())
                    $properties['dependencies'] = $this->route->getProperty('dependencies');

                if(!$resolve)
                    throw new InvalidArgumentException('The route [' . $this->route->getAbsoluteName() . '] execute handle was not properly resolved. '.(is_string($executePattern) ? ' ['.$executePattern.']' : ''));

                $callStack->addCallable($resolve, $properties);

                return $callStack;
            }
        }

        return null;
    }
702967
public function pushHandler(HandlerInterface $handler)
    {
        array_unshift($this->handlers, $handler);
        if ($this->_startTime === null) {
            $this->_startTime = microtime(true);
            $this->_previousRecordTime = $this->_startTime;
        }
    }
475973
public function addMiddleware(MiddlewareInterface ...$middlewares) : self
    {
        foreach ($middlewares as $middleware) {
            $this->stack->push($middleware);
        }

        return $this;
    }
648625
public function init(array $stack) : void
    {
        $this->stack = $stack;
        $this->dispatcher = new MiddlemanDispatcher($this->stack);
    }
530408
public function stack($handlers)
    {
        if (is_array($handlers)) {
            $this->handlers = array_merge($this->handlers, $handlers);
        } else {
            $this->handlers[] = $handlers;
        }
        return $this;
    }
1099452
public function pushHandler($handler)
    {
        if (is_callable($handler)) {
            $handler = new CallbackHandler($handler);
        }

        if (!$handler instanceof HandlerInterface) {
            throw new InvalidArgumentException(
                  "Argument to " . __METHOD__ . " must be a callable, or instance of "
                . "Whoops\\Handler\\HandlerInterface"
            );
        }

        $this->handlerStack[] = $handler;
        return $this;
    }
51642
public function handleRequest(SymfonyRequest $request)
    {
        $stack = $this->stack;
        if ($stack instanceof MiddlewareStack) {
            $stack = $stack->withDispatcher($this->app->make(DispatcherDelegate::class, [$this->dispatcher]));
        }

        return $stack->process($request);
    }
316108
protected function pushMiddleware($middleware, $force = false)
    {
        if (PHP_SAPI !== 'cli' && $force === false && $this->app->runningUnitTests() === false) {
            return $this->getHttpKernel();
        }

        $this->getHttpKernel()->pushMiddleware($middleware);
    }
1175342
public function setProcessorsStack(array $stack)
    {
        $this->processorsStack = [];
        foreach (array_reverse($stack) as $processor) {
            $this->pushProcessor($processor);
        }

        return $this;
    }
603013
protected function getHandler() : HandlerStack
    {
        $stack = HandlerStack::create();

        $stack->push(
            Middleware::mapResponse(function (ResponseInterface $response) {
                $handler = $this->getResponseHandler();

                return new $handler($response);
            }),
            'bitcoind_response'
        );

        return $stack;
    }