|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace ReachDigital\BlankClassicFrontend\Plugin; |
| 4 | + |
| 5 | +use Magento\Framework\App\Config\ScopeConfigInterface; |
| 6 | +use Magento\Framework\App\Request\Http as RequestHttp; |
| 7 | +use Magento\Framework\View\Result\Page; |
| 8 | +use Magento\Store\Model\ScopeInterface; |
| 9 | + |
| 10 | +class PreventLayoutRendering |
| 11 | +{ |
| 12 | + protected ScopeConfigInterface $scopeConfig; |
| 13 | + |
| 14 | + const allowedRoutes = [ |
| 15 | + 'swagger', |
| 16 | + ]; |
| 17 | + private RequestHttp $request; |
| 18 | + |
| 19 | + public function __construct ( |
| 20 | + ScopeConfigInterface $scopeConfig, |
| 21 | + RequestHttp $request |
| 22 | + ) { |
| 23 | + $this->scopeConfig = $scopeConfig; |
| 24 | + $this->request = $request; |
| 25 | + } |
| 26 | + |
| 27 | + public function afterRenderResult( |
| 28 | + Page $subject, |
| 29 | + $result, |
| 30 | + $response |
| 31 | + ) { |
| 32 | + $shouldBlankFrontend = $this->scopeConfig->isSetFlag("blank_classic_frontend/general/enabled", ScopeInterface::SCOPE_STORE); |
| 33 | + $shouldRedirect = $this->scopeConfig->isSetFlag("blank_classic_frontend/general/should_redirect", ScopeInterface::SCOPE_STORE); |
| 34 | + |
| 35 | + if ($shouldBlankFrontend) { |
| 36 | + if ($this->routeIsAllowed()) { |
| 37 | + return $result; |
| 38 | + } |
| 39 | + /** @var \Magento\Framework\App\Response\Http $response */ |
| 40 | + $response->clearBody(); |
| 41 | + if ($shouldRedirect) { |
| 42 | + $customUrl = $this->scopeConfig->getValue('blank_classic_frontend/general/custom_redirect', ScopeInterface::SCOPE_STORE) ?? ''; |
| 43 | + $baseUrl = $this->scopeConfig->getValue('web/secure/base_link_url', ScopeInterface::SCOPE_STORE); |
| 44 | + $response->setRedirect(strlen(trim($customUrl)) > 0 ? $customUrl : $baseUrl); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + return $result; |
| 49 | + } |
| 50 | + |
| 51 | + private function routeIsAllowed(): bool |
| 52 | + { |
| 53 | + return in_array($this->request->getRouteName(), self::allowedRoutes); |
| 54 | + } |
| 55 | +} |
0 commit comments