Dispatch the middleware queue.

@param ServerRequestInterface $request
@param ResponseInterface $response
198673
public function dispatch(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface
        {
            reset($this->middleware);
            $this->response = $response;
            return $this->handle($request);
        }
4421
public function onRequest($server, $serverSocket, $request, $response)
    {
        try {
            $serializer = $this->getSerializer($serverSocket);

            $message = $serializer->deserialize($request->rawcontent());

            $result = $this->dispatcher->handleMessage($message);

            $body = $serializer->serialize($result);

            $this->respond($response, 200, $body);
        } catch (Throwable $e) {
            Log::channel('homer')->warning('Handle invoking failed, cause: '.$e->getMessage(), [
                'exception' => $e
            ]);

            $this->respond($response, 500, $e->getMessage());
        }
    }
201152
public function dispatch(Request $request, Response $response) {
		$module = ucfirst($request->module);
		$controller = ucfirst($request->controller);
		$action = ucfirst($request->action);
		$response->setTemplate($controller . '/' . $action);
		$response->setTemplatePath(APP_PATH . 'Module/' . $module . '/View/');
		if (!empty($request->extension)) {
			$response->mimeType($request->extension);
		}
		try {
			$code = self::isValid($module, $controller, $action);
			if ($code === self::ROUTE_VALID) {
				$className = EntryUtil::controller($module, $controller);
				$clazz = Container::getInstance()->get($className);
				$actionName = $action . 'Action';
				$result = $clazz->$actionName($request, $response);
				if ($result !== null) {
					$response->result = $result;
				}
			} else {
				// Not found
				$request->status = $code;
			}
		} catch (\Throwable $e) {
			$request->status = $e;
		}
		$this->executeInterceptor('after', $request, $response);
		$request->end();
		$response->end();
		unset($request, $response);
	}
343653
public function terminate($request, $response)
    {
        $middlewares = array_merge(
            $this->gatherRouteMiddlewares($request),
            $this->middleware
        );

        foreach ($middlewares as $middleware) {
            list($name, $parameters) = $this->parseMiddleware($middleware);

            $instance = $this->app->make($name);

            if (method_exists($instance, 'terminate')) {
                $instance->terminate($request, $response);
            }
        }

        //$this->app->terminate();
    }
845434
public function handle(ServerRequestInterface $request): ResponseInterface
    {
        return $this->dispatchRoute($this->resolve($request), $request);
    }
455427
public function run(RequestInterface $request, ResponseInterface $response)
    {
        // Unused parameter
        unset($request);

        // Lock script, to ensure it can not be run twice  at the same time (before previous instance is done).
        $this->startLock();

        $climate = $this->climate();

        $processedCallback = function($success, $failures, $skipped) use ($climate) {
            if (!empty($success)) {
                $climate->green()->out(sprintf('%s emails were successfully sent.', count($success)));
            }

            if (!empty($failures)) {
                $climate->red()->out(sprintf('%s emails were not successfully sent', count($failures)));
            }

            if (!empty($skipped)) {
                $climate->dim()->out(sprintf('%s emails were skipped.', count($skipped)));
            }
        };

        $queueManager = new EmailQueueManager([
            'logger' => $this->logger,
            'queue_item_factory' => $this->queueItemFactory
        ]);
        $queueManager->setProcessedCallback($processedCallback);
        $queueManager->processQueue();

        $this->stopLock();

        return $response;
    }
1344252
public function dispatch(Request $request, Response $response, $next = null)
    {
        $controller = $this->currentController;

        $controller->setRequest($request)->setResponse($response);

        $this->prepareExecute();

        // @event: onPackageBeforeExecute
        $this->getDispatcher()->triggerEvent('onPackageBeforeExecute', [
            'package' => $this,
            'controller' => &$controller,
            'task' => $controller,
        ]);

        $result = $controller->execute();

        $result = $this->postExecute($result);

        // @event: onPackageAfterExecute
        $this->getDispatcher()->triggerEvent('onPackageAfterExecute', [
            'package' => $this,
            'controller' => $controller,
            'result' => &$result
        ]);

        $response = $controller->getResponse();

        if ($result !== null) {
            // Render view if return value is a view object,
            // don't use (string) keyword to make sure we can get Exception when error occurred.
            // @see  https://bugs.php.net/bug.php?id=53648
            if ($result instanceof AbstractView) {
                $result = $result->render();
            } elseif (is_stringable($result)) {
                $result = (string) $result;
            } elseif (is_array($result) || is_object($result)) {
                $result = json_encode($result);
            }

            $response->getBody()->write((string) $result);
        }

        return $response;
    }
426507
public function run(RequestInterface $request = null, ResponseInterface $response = null)
    {

        if (is_null($request)) {
            if (!$this->getContainer()->has('request')) {
                if ($this->getContainer()->has('Psr\Http\Message\ServerRequestInterface')) {
                    $this->getContainer()->add('request', 'Psr\Http\Message\ServerRequestInterface');
                } else if ($this->getContainer()->has('Psr\Http\Message\RequestInterface')) {
                    $this->getContainer()->add('request', 'Psr\Http\Message\RequestInterface');
                }
            }
            $request = $this->getContainer()->get('request');
        }
        if (is_null($response)) {
            if (!$this->getContainer()->has('response') && $this->getContainer()->has('Psr\Http\Message\ResponseInterface')) {
                $this->getContainer()->add('response', 'Psr\Http\Message\ResponseInterface');
            }
            $response = $this->getContainer()->get('response');
        }

        $this->getContainer()->add('request', $request);
        $this->getContainer()->add('response', $response);

        $this->setErrorHandler();
        $this->getKernel()->run($request, $response);

        if ($this->getLogger()) {
            if ($request instanceof ServerRequestInterface) {
                $this->getLogger()->debug('Script execution time: '.number_format(microtime(true) - $request->getServerParams()['REQUEST_TIME_FLOAT'], 3).' s');
            }
            $this->getLogger()->debug('Memory usage: '.number_format(memory_get_usage()/1024/1024, 3).' MB.');
        }
    }
128064
public function terminate(ServerRequestInterface $request, ResponseInterface $response)
    {
        $applicationEvent = $this->getApplicationEvent();
        $applicationEvent->setRequest($request);
        $applicationEvent->setResponse($response);
        $applicationEvent->setName(self::EVENT_LIFECYCLE_COMPLETE);
        $this->emit($applicationEvent);

    }
1293086
public function handle(ServerRequestInterface $request): ResponseInterface
    {
        if (!isset($this->middlewaresInPipe[$this->currentMiddlewareInPipeIndex])) {
            return $this->generate404($request);
        }

        $middleware = $this->getMiddlewareInPipe();
        if (\is_callable($middleware)) {
            return \call_user_func_array($middleware, [$request, [$this, 'handle']]);
        } elseif ($middleware instanceof MiddlewareInterface) {
            return $middleware->process($request, $this);
        } elseif (\is_string($middleware)) {
            $middlewareInstance = (new $middleware());
            if (\method_exists($middlewareInstance, 'process')) {
                return $middlewareInstance->process($request, $this);
            }
        } elseif ($middleware instanceof self) {
            if ($middleware->findRouteRequest($request)) {
                return $middleware->dispatch($request);
            }

            return $this->handle($request);
        }
        throw new RouterException(\sprintf('Middleware is invalid: %s', \gettype($middleware)));
    }
685419
private function doRun(RequestInterface $request, ResponseInterface $response): void
    {
        $throwable = null;

        foreach ($this->plugins as $plugin) {
            if ($plugin->onPreRequest($this, $request, $response)) {
                return;
            }
        }

        try {
            $this->handleRequest($request, $response);
        } catch (\Throwable $throwable) {
            $this->throwableToResponse($throwable, $response);
        }

        if ($response->getStatusCode()->isError()) {
            $this->handleError($request, $response, $throwable);
        }

        foreach ($this->plugins as $plugin) {
            if ($plugin->onPostRequest($this, $request, $response)) {
                return;
            }
        }
    }
463200
public function executeFilters(Request $request, Response $response)
    {
        $middlewares = $this->filterMiddleware(Filter::class);

        foreach ($middlewares as $middleware) {
            if (($returnResponse = $middleware->doFilter($request, $response)) !== null) {
                return $returnResponse;
            }
            unset($returnResponse);
        }

        unset($middlewares);
    }
871908
public function run(ServerRequestInterface $request, ResponseInterface $response, $next = null)
    {
        if (!$request->getAttribute('route') instanceof Route) {
            $route = $this->routes->getRoute($request);

            if (!$route) {
                return $this->notFound($request, $response);
            }

            $request = $request->withAttribute('route', $route);
        }
        
        $runner = $this->getRunner();

        return $runner($request, $response, $next);
    }
330094
public function delete( ServerRequestInterface $request, ResponseInterface $response )
	{
		return $this->client->delete( $request, $response );
	}
1101623
public function terminate($request, $response)
    {
        $routeMiddlewares = $this->gatherRouteMiddlewares($request);
        foreach (array_merge($routeMiddlewares, $this->middleware) as $middleware) {
            $instance = $this->app->make($middleware);
            if ($instance instanceof TerminableMiddleware) {
                $instance->terminate($request, $response);
            }
        }
        $this->app->terminate();
    }
876774
public function dispatch(RequestInterface $request, ResponseInterface $response = null)
    {
        if (!($request instanceof ConsoleRequest)) {
            throw new \RuntimeException('You can use this controller only from a console!');
        }

        return parent::dispatch($request, $response);
    }
574136
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
    {
        $isExecutingNextHandler = false;
        $next = new class($isExecutingNextHandler, $handler) implements RequestHandlerInterface {
            /**
             * @var bool
             */
            private $isExecutingNextHandler;
            /**
             * @var RequestHandlerInterface
             */
            private $nextHandler;

            public function __construct(bool &$isExecutingNextHandler, RequestHandlerInterface $realHandler)
            {
                $this->isExecutingNextHandler = &$isExecutingNextHandler;
                $this->nextHandler = $realHandler;
            }

            /**
             * Handle the request and return a response.
             */
            public function handle(ServerRequestInterface $request): ResponseInterface
            {
                $this->isExecutingNextHandler = true;
                $result = $this->nextHandler->handle($request);
                $this->isExecutingNextHandler = false;

                return $result;
            }
        };

        try {
            return $this->middleware->process($request, $next);
        } catch (\Throwable $throwable) {
            if ($isExecutingNextHandler) {
                throw $throwable;
            }

            return $handler->handle($request);
        }
    }