_objectManager = $objectManager; $this->_eventManager = $eventManager; $this->_areaList = $areaList; $this->_request = $request; $this->_response = $response; $this->_configLoader = $configLoader; $this->_state = $state; $this->registry = $registry; $this->exceptionHandler = $exceptionHandler ?: $this->_objectManager->get(ExceptionHandlerInterface::class); } /** * Run application * * @return ResponseInterface * @throws LocalizedException|\InvalidArgumentException */ public function launch() { $areaCode = $this->_areaList->getCodeByFrontName($this->_request->getFrontName()); $this->_state->setAreaCode($areaCode); $this->_objectManager->configure($this->_configLoader->load($areaCode)); /** @var \Magento\Framework\App\FrontControllerInterface $frontController */ $frontController = $this->_objectManager->get(\Magento\Framework\App\FrontControllerInterface::class); $result = $frontController->dispatch($this->_request); // TODO: Temporary solution until all controllers return ResultInterface (MAGETWO-28359) if ($result instanceof ResultInterface) { $this->registry->register('use_page_cache_plugin', true, true); $result->renderResult($this->_response); } elseif ($result instanceof HttpInterface) { $this->_response = $result; } else { throw new \InvalidArgumentException('Invalid return type'); } if ($this->_request->isHead() && $this->_response->getHttpResponseCode() == 200) { $this->handleHeadRequest(); } // This event gives possibility to launch something before sending output (allow cookie setting) $eventParams = ['request' => $this->_request, 'response' => $this->_response]; $this->_eventManager->dispatch('controller_front_send_response_before', $eventParams); return $this->_response; } /** * Handle HEAD requests by adding the Content-Length header and removing the body from the response. * * @return void */ private function handleHeadRequest() { // It is possible that some PHP installations have overloaded strlen to use mb_strlen instead. // This means strlen might return the actual number of characters in a non-ascii string instead // of the number of bytes. Use mb_strlen explicitly with a single byte character encoding to ensure // that the content length is calculated in bytes. $contentLength = mb_strlen($this->_response->getContent(), '8bit'); $this->_response->clearBody(); $this->_response->setHeader('Content-Length', $contentLength); } /** * @inheritdoc */ public function catchException(Bootstrap $bootstrap, \Exception $exception): bool { return $this->exceptionHandler->handle($bootstrap, $exception, $this->_response, $this->_request); } }