From ecc5f546b7749780eb655582d8d1de0f0fdf83a5 Mon Sep 17 00:00:00 2001 From: Mate Sitchinava Date: Mon, 13 Feb 2023 17:51:52 +0400 Subject: [PATCH 1/4] emulate graphql area code for cms page in action --- src/Controller/Pwa.php | 5 ++++ src/Controller/Router.php | 57 ++++++++++++++++++++------------------- 2 files changed, 35 insertions(+), 27 deletions(-) diff --git a/src/Controller/Pwa.php b/src/Controller/Pwa.php index ced18ed..4967dfd 100644 --- a/src/Controller/Pwa.php +++ b/src/Controller/Pwa.php @@ -65,6 +65,11 @@ class Pwa extends Action implements HttpGetActionInterface, HttpPostActionInterf */ protected $cmsPage; + /** + * @var array|null + */ + protected $slider; + /** * @var string */ diff --git a/src/Controller/Router.php b/src/Controller/Router.php index 2dc1a41..cdc3872 100644 --- a/src/Controller/Router.php +++ b/src/Controller/Router.php @@ -9,6 +9,8 @@ namespace ScandiPWA\Router\Controller; +use Magento\Framework\App\Area; +use Magento\Framework\App\State as AppState; use Magento\Framework\App\ActionFactory; use Magento\Framework\App\ActionInterface; use Magento\Framework\App\Config\ScopeConfigInterface; @@ -34,9 +36,7 @@ use Magento\Catalog\Model\ProductRepository; use Magento\Catalog\Api\CategoryRepositoryInterface; use Magento\Catalog\Model\Product\Attribute\Source\Status; -use Magento\Cms\Api\PageRepositoryInterface; use ScandiPWA\CmsGraphQl\Model\Resolver\DataProvider\Page as PageProvider; -use Magento\Cms\Api\GetPageByIdentifierInterface; use Magento\Framework\Filter\Template as WidgetTemplate; use Magento\Framework\Filter\Template\Tokenizer\Parameter as TokenizerParameter; use ScandiPWA\SliderGraphQl\Model\Resolver\Slider as SliderResolver; @@ -56,6 +56,8 @@ class Router extends BaseRouter 'catalog_default_sort_by' => self::XML_PATH_CATALOG_DEFAULT_SORT_BY ]; + protected AppState $appState; + /** * @var ValidationManagerInterface */ @@ -116,16 +118,6 @@ class Router extends BaseRouter */ protected PageProvider $pageProvider; - /** - * @var PageRepositoryInterface - */ - protected $pageRepository; - - /** - * @var GetPageByIdentifierInterface - */ - protected $pageByIdentifier; - /** * @var TokenizerParameter */ @@ -143,6 +135,7 @@ class Router extends BaseRouter /** * Router constructor. + * @param AppState $appState * @param ActionList $actionList * @param ActionFactory $actionFactory * @param DefaultPathInterface $defaultPath @@ -160,8 +153,11 @@ class Router extends BaseRouter * @param ProductRepository $productRepository * @param CategoryRepositoryInterface $categoryRepository * @param PageProvider $pageProvider + * @param TokenizerParameter $tokenizerParameter + * @param SliderResolver $sliderResolver */ public function __construct( + AppState $appState, ActionList $actionList, ActionFactory $actionFactory, DefaultPathInterface $defaultPath, @@ -179,13 +175,11 @@ public function __construct( ProductRepository $productRepository, CategoryRepositoryInterface $categoryRepository, PageProvider $pageProvider, - PageRepositoryInterface $pageRepository, - GetPageByIdentifierInterface $pageByIdentifier, TokenizerParameter $tokenizerParameter, SliderResolver $sliderResolver, - array $ignoredURLs = [], - ) - { + array $ignoredURLs = [] + ) { + $this->appState = $appState; $this->scopeConfig = $scopeConfig; $this->themeProvider = $themeProvider; $this->validationManager = $validationManager; @@ -194,8 +188,6 @@ 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(); @@ -326,13 +318,15 @@ protected function setResponseHomePage(ActionInterface $action) ); try { - $page = $this->pageByIdentifier->execute($homePageIdentifier, (int)$this->storeId); - - $action->setId($page['page_id'] ?? ''); + $page = $this->appState->emulateAreaCode( + Area::AREA_GRAPHQL, + [$this->pageProvider, 'getDataByPageIdentifier'], + [$homePageIdentifier, (int) $this->storeId] + ); $action->setType(self::PAGE_TYPE_CMS_PAGE); $action->setId($page['page_id'] ?? ''); - $action->setCmsPage($this->pageProvider->convertPageData($page)); + $action->setCmsPage($page); $action->setSlider($this->getSliderInformation($page['content'])); } catch (\Throwable $th) { $action->setType('PWA_ROUTER'); @@ -349,7 +343,7 @@ protected function getSliderInformation($content) { preg_match('/{{(.*?)}}/m', $content, $match); - $this->tokenizerParameter->setString($match[0]); + $this->tokenizerParameter->setString($match[0] ?? ''); $params = $this->tokenizerParameter->tokenize(); foreach ($params as $key => $value) { @@ -359,7 +353,11 @@ protected function getSliderInformation($content) } if (isset($params['slider_id'])) { - return $this->sliderResolver->getSlider($params['slider_id']); + return $this->appState->emulateAreaCode( + Area::AREA_GRAPHQL, + [$this->sliderResolver, 'getSlider'], + [$params['slider_id']] + ); } } @@ -374,9 +372,14 @@ protected function getSliderInformation($content) protected function setResponseCmsPage($id, ActionInterface $action) { try { - $page = $this->pageRepository->getById($id); + $page = $this->appState->emulateAreaCode( + Area::AREA_GRAPHQL, + [$this->pageProvider, 'getDataByPageId'], + [(int) $id, (int) $this->storeId] + ); + $action->setId($page['page_id'] ?? ''); - $action->setCmsPage($this->pageProvider->convertPageData($page)); + $action->setCmsPage($page); $action->setSlider($this->getSliderInformation($page['content'])); } catch (\Throwable $th) { $this->setNotFound($action); From 9c9dfe048e87d96200dd2b19e9de4c490924bc56 Mon Sep 17 00:00:00 2001 From: Mate Sitchinava Date: Tue, 14 Feb 2023 15:56:02 +0400 Subject: [PATCH 2/4] replaced emulation with ObjectManager --- src/Controller/Router.php | 56 ++++++++++++++++++++++++++++----------- 1 file changed, 40 insertions(+), 16 deletions(-) diff --git a/src/Controller/Router.php b/src/Controller/Router.php index cdc3872..01da5c3 100644 --- a/src/Controller/Router.php +++ b/src/Controller/Router.php @@ -1,4 +1,5 @@ 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']; } } @@ -318,16 +321,12 @@ protected function setResponseHomePage(ActionInterface $action) ); try { - $page = $this->appState->emulateAreaCode( - Area::AREA_GRAPHQL, - [$this->pageProvider, 'getDataByPageIdentifier'], - [$homePageIdentifier, (int) $this->storeId] - ); + $page = $this->getCmsPageFromGraphqlArea(null, $homePageIdentifier); $action->setType(self::PAGE_TYPE_CMS_PAGE); $action->setId($page['page_id'] ?? ''); $action->setCmsPage($page); - $action->setSlider($this->getSliderInformation($page['content'])); + $action->setSlider($this->getSliderInformation($page['content'] ?? '')); } catch (\Throwable $th) { $action->setType('PWA_ROUTER'); } @@ -372,15 +371,11 @@ protected function getSliderInformation($content) protected function setResponseCmsPage($id, ActionInterface $action) { try { - $page = $this->appState->emulateAreaCode( - Area::AREA_GRAPHQL, - [$this->pageProvider, 'getDataByPageId'], - [(int) $id, (int) $this->storeId] - ); + $page = $this->getCmsPageFromGraphqlArea((int) $id); $action->setId($page['page_id'] ?? ''); $action->setCmsPage($page); - $action->setSlider($this->getSliderInformation($page['content'])); + $action->setSlider($this->getSliderInformation($page['content'] ?? '')); } catch (\Throwable $th) { $this->setNotFound($action); } @@ -458,6 +453,35 @@ 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(AppState::class)->getAreaCode(); + $objectManager->configure($configLoader->load(Area::AREA_GRAPHQL)); + + $pageProvider = $objectManager->create(PageProvider::class); + $page = null; + + if ($id) { + $page = $pageProvider->getDataByPageId($id, (int)$this->storeId); + } + + if ($identifier) { + $page = $pageProvider->getDataByPageIdentifier($identifier, (int)$this->storeId); + } + + // vvv reset config area back to initial + $objectManager->configure($configLoader->load($currentConfigArea)); + + return $page; + } catch (\Exception $e) { + return null; + } + } + /** * Set "404 Not Found" response * @@ -584,7 +608,7 @@ protected function isHomePage(RequestInterface $request): bool { $requestPath = $request->getPathInfo(); - if(!$requestPath || $requestPath === '/') { + if (!$requestPath || $requestPath === '/') { return true; } @@ -597,7 +621,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 cbc9592449b727d4b46c53bf2e9886fb02bef29e Mon Sep 17 00:00:00 2001 From: Mate Sitchinava Date: Tue, 14 Feb 2023 15:57:59 +0400 Subject: [PATCH 3/4] cleanup --- src/Controller/Router.php | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/src/Controller/Router.php b/src/Controller/Router.php index 01da5c3..df8c810 100644 --- a/src/Controller/Router.php +++ b/src/Controller/Router.php @@ -11,7 +11,7 @@ namespace ScandiPWA\Router\Controller; use Magento\Framework\App\Area; -use Magento\Framework\App\State as AppState; +use Magento\Framework\App\State; use Magento\Framework\App\ObjectManager; use Magento\Framework\App\ActionFactory; use Magento\Framework\App\ActionInterface; @@ -59,8 +59,6 @@ class Router extends BaseRouter 'catalog_default_sort_by' => self::XML_PATH_CATALOG_DEFAULT_SORT_BY ]; - protected AppState $appState; - /** * @var ValidationManagerInterface */ @@ -138,7 +136,6 @@ class Router extends BaseRouter /** * Router constructor. - * @param AppState $appState * @param ActionList $actionList * @param ActionFactory $actionFactory * @param DefaultPathInterface $defaultPath @@ -160,7 +157,6 @@ class Router extends BaseRouter * @param SliderResolver $sliderResolver */ public function __construct( - AppState $appState, ActionList $actionList, ActionFactory $actionFactory, DefaultPathInterface $defaultPath, @@ -182,7 +178,6 @@ public function __construct( SliderResolver $sliderResolver, array $ignoredURLs = [] ) { - $this->appState = $appState; $this->scopeConfig = $scopeConfig; $this->themeProvider = $themeProvider; $this->validationManager = $validationManager; @@ -459,7 +454,7 @@ protected function getCmsPageFromGraphqlArea(int $id = null, string $identifier // vvv construct PageProvider with grahql area $objectManager = ObjectManager::getInstance(); $configLoader = $objectManager->get(ConfigLoaderInterface::class); - $currentConfigArea = $objectManager->get(AppState::class)->getAreaCode(); + $currentConfigArea = $objectManager->get(State::class)->getAreaCode(); $objectManager->configure($configLoader->load(Area::AREA_GRAPHQL)); $pageProvider = $objectManager->create(PageProvider::class); From d9fe2bea7824638ea62ee5538b5cbc2821b1a1ad Mon Sep 17 00:00:00 2001 From: Mate Sitchinava Date: Tue, 14 Feb 2023 16:00:30 +0400 Subject: [PATCH 4/4] cleanup --- src/Controller/Router.php | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/Controller/Router.php b/src/Controller/Router.php index df8c810..2e9248a 100644 --- a/src/Controller/Router.php +++ b/src/Controller/Router.php @@ -347,11 +347,7 @@ protected function getSliderInformation($content) } if (isset($params['slider_id'])) { - return $this->appState->emulateAreaCode( - Area::AREA_GRAPHQL, - [$this->sliderResolver, 'getSlider'], - [$params['slider_id']] - ); + return $this->sliderResolver->getSlider($params['slider_id']); } }