From f5e9cd4a0b21ef9a7acfd8a59ef431b1a127fb6b Mon Sep 17 00:00:00 2001 From: aleksandrskondratjevs Date: Tue, 14 Feb 2023 18:30:24 +0200 Subject: [PATCH 1/2] Refactore preload --- src/Controller/Router.php | 98 ++++++++++++++++++++++++++++----------- 1 file changed, 71 insertions(+), 27 deletions(-) diff --git a/src/Controller/Router.php b/src/Controller/Router.php index 2f3e6ff..dae0e15 100644 --- a/src/Controller/Router.php +++ b/src/Controller/Router.php @@ -1,4 +1,5 @@ scopeConfig = $scopeConfig; @@ -188,13 +195,13 @@ public function __construct( $this->pageFactory = $pageFactory; $this->productRepository = $productRepository; $this->categoryRepository = $categoryRepository; - $this->pageRepository = $pageRepository; - $this->pageByIdentifier = $pageByIdentifier; $this->pageProvider = $pageProvider; $this->ignoredURLs = $ignoredURLs; $this->storeId = $this->storeManager->getStore()->getId(); $this->tokenizerParameter = $tokenizerParameter; $this->sliderResolver = $sliderResolver; + $this->pageRepository = $pageRepository; + $this->pageByIdentifier = $getPageByIdentifier; parent::__construct( $actionList, @@ -227,13 +234,13 @@ public function match(RequestInterface $request) $this->storeId ); - if($expressions) { + if ($expressions) { $userAgentRules = json_decode($expressions, true); foreach ($userAgentRules as $userAgentRule) { $regexp = stripslashes($userAgentRule['regexp']); - if(preg_match($regexp, $_SERVER['HTTP_USER_AGENT'])) { + if (preg_match($regexp, $_SERVER['HTTP_USER_AGENT'])) { $themeId = $userAgentRule['value']; } } @@ -320,14 +327,17 @@ protected function setResponseHomePage(ActionInterface $action) ); try { - $page = $this->pageByIdentifier->execute($homePageIdentifier, (int)$this->storeId); + $page = $this->getCmsPageFromGraphqlArea(null, $homePageIdentifier); - $action->setId($page['page_id'] ?? ''); + if (!isset($page['cmsPage'])) { + $action->setType('PWA_ROUTER'); + return; + } $action->setType(self::PAGE_TYPE_CMS_PAGE); - $action->setId($page['page_id'] ?? ''); - $action->setCmsPage($this->pageProvider->convertPageData($page)); - $action->setSlider($this->getSliderInformation($page['content'])); + $action->setId($page['cmsPage']['page_id'] ?? ''); + $action->setCmsPage($page['cmsPage']); + $action->setSlider($this->getSliderInformation($page['content'] ?? '')); } catch (\Throwable $th) { $action->setType('PWA_ROUTER'); } @@ -343,7 +353,7 @@ protected function getSliderInformation($content) { preg_match('/{{(.*?)}}/m', $content, $match); - $this->tokenizerParameter->setString($match[0]); + $this->tokenizerParameter->setString($match[0] ?? ''); $params = $this->tokenizerParameter->tokenize(); if (isset($params['slider_id'])) { @@ -362,10 +372,11 @@ protected function getSliderInformation($content) protected function setResponseCmsPage($id, ActionInterface $action) { try { - $page = $this->pageRepository->getById($id); + $page = $this->getCmsPageFromGraphqlArea((int)$id); + $action->setId($page['page_id'] ?? ''); - $action->setCmsPage($this->pageProvider->convertPageData($page)); - $action->setSlider($this->getSliderInformation($page['content'])); + $action->setCmsPage($page); + $action->setSlider($this->getSliderInformation($page['content'] ?? '')); } catch (\Throwable $th) { $this->setNotFound($action); } @@ -443,6 +454,39 @@ protected function setStoreConfigs(ActionInterface $action) $action->setStoreConfig($storeConfig); } + protected function getCmsPageFromGraphqlArea(int $id = null, string $identifier = null) + { + try { + // vvv construct PageProvider with grahql area + $objectManager = ObjectManager::getInstance(); + $configLoader = $objectManager->get(ConfigLoaderInterface::class); + $currentConfigArea = $objectManager->get(State::class)->getAreaCode(); + $objectManager->configure($configLoader->load(Area::AREA_GRAPHQL)); + + $pageProvider = $objectManager->create(PageProvider::class); + $page = null; + $result = []; + + if ($id) { + $page = $this->pageRepository->getById($id); + } + + if ($identifier) { + $page = $this->pageByIdentifier->execute($identifier, (int)$this->storeId); + } + + $result['cmsPage'] = $pageProvider->convertPageData($page); + $result['content'] = $page->getContent(); + + // vvv reset config area back to initial + $objectManager->configure($configLoader->load($currentConfigArea)); + + return $result; + } catch (\Exception $e) { + return null; + } + } + /** * Set "404 Not Found" response * @@ -569,7 +613,7 @@ protected function isHomePage(RequestInterface $request): bool { $requestPath = $request->getPathInfo(); - if(!$requestPath || $requestPath === '/') { + if (!$requestPath || $requestPath === '/') { return true; } @@ -582,7 +626,7 @@ protected function isHomePage(RequestInterface $request): bool $routes = array_filter(explode('/', $requestPath)); $code = $routes[0] ?? ''; - if(count($routes) == 1 && $code == $storeCode) { + if (count($routes) == 1 && $code == $storeCode) { return true; } From 4cf17468746bdc25288131e651575f8f4d855928 Mon Sep 17 00:00:00 2001 From: aleksandrskondratjevs Date: Tue, 14 Feb 2023 18:33:08 +0200 Subject: [PATCH 2/2] Change preload for cms page --- src/Controller/Router.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/Controller/Router.php b/src/Controller/Router.php index dae0e15..56cf2d4 100644 --- a/src/Controller/Router.php +++ b/src/Controller/Router.php @@ -374,8 +374,13 @@ protected function setResponseCmsPage($id, ActionInterface $action) try { $page = $this->getCmsPageFromGraphqlArea((int)$id); - $action->setId($page['page_id'] ?? ''); - $action->setCmsPage($page); + if (!isset($page['cmsPage'])) { + $this->setNotFound($action); + return; + } + + $action->setId($page['cmsPage']['page_id'] ?? ''); + $action->setCmsPage($page['cmsPage']); $action->setSlider($this->getSliderInformation($page['content'] ?? '')); } catch (\Throwable $th) { $this->setNotFound($action);