|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © OpenGento, All rights reserved. |
| 4 | + * See LICENSE bundled with this library for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Opengento\Application\App; |
| 9 | + |
| 10 | +use Exception; |
| 11 | +use InvalidArgumentException; |
| 12 | +use Magento\Framework\App; |
| 13 | +use Magento\Framework\App\ExceptionHandlerInterface; |
| 14 | +use Magento\Framework\App\FrontControllerInterface as FrontController; |
| 15 | +use Magento\Framework\App\HttpRequestInterface; |
| 16 | +use Magento\Framework\App\Response\Http; |
| 17 | +use Magento\Framework\App\Response\HttpInterface; |
| 18 | +use Magento\Framework\AppInterface; |
| 19 | +use Magento\Framework\Controller\ResultInterface; |
| 20 | +use Magento\Framework\Event\Manager; |
| 21 | +use Magento\Framework\ObjectManagerInterface; |
| 22 | +use Magento\Framework\Registry; |
| 23 | + |
| 24 | +class Application implements AppInterface |
| 25 | +{ |
| 26 | + public function __construct( |
| 27 | + private ObjectManagerInterface $objectManager, |
| 28 | + private Manager $eventManager, |
| 29 | + private Registry $registry, |
| 30 | + private ExceptionHandlerInterface $exceptionHandler, |
| 31 | + private Http $response, |
| 32 | + private App\Request\Http $request, |
| 33 | + ) {} |
| 34 | + |
| 35 | + /** |
| 36 | + * @inheritDoc |
| 37 | + */ |
| 38 | + public function launch(): HttpInterface |
| 39 | + { |
| 40 | + /** @var FrontController $frontController */ |
| 41 | + $frontController = $this->objectManager->create(FrontController::class); |
| 42 | + |
| 43 | + $response = $this->handleHead( |
| 44 | + $this->request, |
| 45 | + $this->handleResponse($frontController->dispatch($this->request)) |
| 46 | + ); |
| 47 | + |
| 48 | + // This event gives possibility to launch something before sending output (allow cookie setting) |
| 49 | + $this->eventManager->dispatch( |
| 50 | + 'controller_front_send_response_before', |
| 51 | + ['request' => $this->request, 'response' => $response] |
| 52 | + ); |
| 53 | + |
| 54 | + return $response; |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * @inheritdoc |
| 59 | + */ |
| 60 | + public function catchException(App\Bootstrap $bootstrap, Exception $exception): bool |
| 61 | + { |
| 62 | + return $this->exceptionHandler->handle($bootstrap, $exception, $this->response, $this->request); |
| 63 | + } |
| 64 | + |
| 65 | + private function handleResponse(ResultInterface|HttpInterface $result): ResultInterface|HttpInterface |
| 66 | + { |
| 67 | + // TODO: Temporary solution until all controllers return ResultInterface (MAGETWO-28359); |
| 68 | + return match (true) { |
| 69 | + $result instanceof ResultInterface => $this->handleLayoutResult($result), |
| 70 | + $result instanceof HttpInterface => $this->handleHttpResult($result), |
| 71 | + default => throw new InvalidArgumentException('Invalid return type.') |
| 72 | + }; |
| 73 | + } |
| 74 | + |
| 75 | + private function handleLayoutResult(ResultInterface $result): ResultInterface|HttpInterface |
| 76 | + { |
| 77 | + $this->registry->register('use_page_cache_plugin', true, true); |
| 78 | + $result->renderResult($this->response); |
| 79 | + |
| 80 | + return $this->response; |
| 81 | + } |
| 82 | + |
| 83 | + private function handleHttpResult(HttpInterface $result): ResultInterface|HttpInterface |
| 84 | + { |
| 85 | + $this->response->setContent($result->getContent()); |
| 86 | + if ($this->response !== $result) { //do not double headers |
| 87 | + $this->response->getHeaders()?->addHeaders($result->getHeaders()); |
| 88 | + } |
| 89 | + |
| 90 | + return $this->response; |
| 91 | + } |
| 92 | + |
| 93 | + private function handleHead(HttpRequestInterface $request, HttpInterface $response): HttpInterface |
| 94 | + { |
| 95 | + if ($request->isHead() && $response->getHttpResponseCode() === 200) { |
| 96 | + $contentLength = mb_strlen($response->getContent(), '8bit'); |
| 97 | + $response->clearBody(); |
| 98 | + $response->setHeader('Content-Length', $contentLength); |
| 99 | + } |
| 100 | + |
| 101 | + return $response; |
| 102 | + } |
| 103 | +} |
0 commit comments