/** * @inheritdoc */ public function run() { if ($this->sapi === null) { throw new LogicException('SAPI not set.'); } $userContainer = $this->createContainer(); $this->setUpExceptionHandler($this->sapi, $userContainer); list($matchCode, $allowedMethods, $handlerParams, $handler, $routeMiddleware, $configurators, $requestFactory) = $this->getRouter()->match($this->sapi->getMethod(), $this->sapi->getUri()->getPath()); if (empty($configurators) === false) { $this->configureUserContainer($userContainer, $configurators); } switch ($matchCode) { case RouterInterface::MATCH_FOUND: $handler = $this->createOrdinaryTerminalHandler($handler, $handlerParams, $userContainer); break; case RouterInterface::MATCH_METHOD_NOT_ALLOWED: $handler = $this->createMethodNotAllowedTerminalHandler($allowedMethods); break; default: $handler = $this->createNotFoundTerminalHandler(); break; } $globalMiddleware = $this->getGlobalMiddleware(); $handler = $this->createMiddlewareChain($handler, $userContainer, $globalMiddleware, $routeMiddleware); // if handler consists only from Controller and user didn't asked for Request creation $requestNotNeeded = $requestFactory === null && empty($globalMiddleware) === true && empty($routeMiddleware) && $matchCode === RouterInterface::MATCH_FOUND; $request = $requestNotNeeded === true ? null : $this->createRequest($this->sapi, $userContainer, $requestFactory); // send `Request` down all middleware (global then route's then terminal handler in `Controller` and back) and // then send `Response` to SAPI $this->sapi->handleResponse($this->handleRequest($handler, $request)); }
/** * @param Exception|Throwable $exception * @param SapiInterface $sapi * @param ContainerInterface $container * * @SuppressWarnings(PHPMD.ElseExpression) */ private function handle($exception, SapiInterface $sapi, ContainerInterface $container) { $appConfig = $container->get(ConfigInterface::class)->getConfig(C::class); $message = 'Internal Server Error'; $this->logException($exception, $container, $message); if ($appConfig[C::KEY_IS_LOG_ENABLED] === true) { $run = new Run(); $handler = new PrettyPageHandler(); // You can add app specific detailed data here $appSpecificDetails = []; $appName = $appConfig[C::KEY_NAME]; if (empty($appSpecificDetails) === false) { $handler->addDataTable("{$appName} Details", $appSpecificDetails); } $handler->setPageTitle("Whoops! There was a problem with '{$appName}'."); $run->pushHandler($handler); $htmlMessage = $run->handleException($exception); $response = new HtmlResponse($htmlMessage, 500); } else { $response = new TextResponse($message, 500); } $sapi->handleResponse($response); }