Skip to content

Commit

Permalink
Merge pull request #30 from matesich/emulate-graphql
Browse files Browse the repository at this point in the history
emulate graphql area code for cms page in action
  • Loading branch information
AleksandrsKondratjevs authored Feb 14, 2023
2 parents 6b228b9 + 214eb9a commit b052b06
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 31 deletions.
5 changes: 5 additions & 0 deletions src/Controller/Pwa.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ class Pwa extends Action implements HttpGetActionInterface, HttpPostActionInterf
*/
protected $cmsPage;

/**
* @var array|null
*/
protected $slider;

/**
* @var string
*/
Expand Down
81 changes: 50 additions & 31 deletions src/Controller/Router.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* @category ScandiPWA
* @package ScandiPWA\Router
Expand All @@ -9,6 +10,9 @@

namespace ScandiPWA\Router\Controller;

use Magento\Framework\App\Area;
use Magento\Framework\App\State;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\App\ActionFactory;
use Magento\Framework\App\ActionInterface;
use Magento\Framework\App\Config\ScopeConfigInterface;
Expand All @@ -20,6 +24,7 @@
use Magento\Framework\App\Router\PathConfigInterface;
use Magento\Framework\App\Router\Base as BaseRouter;
use Magento\Framework\Code\NameBuilder;
use Magento\Framework\Interception\ConfigLoaderInterface;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Url;
use Magento\Framework\UrlInterface;
Expand All @@ -34,7 +39,6 @@
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\Tokenizer\Parameter as TokenizerParameter;
Expand Down Expand Up @@ -115,16 +119,6 @@ class Router extends BaseRouter
*/
protected PageProvider $pageProvider;

/**
* @var PageRepositoryInterface
*/
protected $pageRepository;

/**
* @var GetPageByIdentifierInterface
*/
protected $pageByIdentifier;

/**
* @var TokenizerParameter
*/
Expand Down Expand Up @@ -154,6 +148,8 @@ class Router extends BaseRouter
* @param ProductRepository $productRepository
* @param CategoryRepositoryInterface $categoryRepository
* @param PageProvider $pageProvider
* @param TokenizerParameter $tokenizerParameter
* @param SliderResolver $sliderResolver
*/
public function __construct(
ActionList $actionList,
Expand All @@ -173,13 +169,10 @@ public function __construct(
ProductRepository $productRepository,
CategoryRepositoryInterface $categoryRepository,
PageProvider $pageProvider,
PageRepositoryInterface $pageRepository,
GetPageByIdentifierInterface $pageByIdentifier,
TokenizerParameter $tokenizerParameter,
SliderResolver $sliderResolver,
array $ignoredURLs = []
)
{
array $ignoredURLs = []
) {
$this->scopeConfig = $scopeConfig;
$this->themeProvider = $themeProvider;
$this->validationManager = $validationManager;
Expand All @@ -188,8 +181,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();
Expand Down Expand Up @@ -227,13 +218,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'];
}
}
Expand Down Expand Up @@ -320,14 +311,12 @@ protected function setResponseHomePage(ActionInterface $action)
);

try {
$page = $this->pageByIdentifier->execute($homePageIdentifier, (int)$this->storeId);

$action->setId($page['page_id'] ?? '');
$page = $this->getCmsPageFromGraphqlArea(null, $homePageIdentifier);

$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->setCmsPage($page);
$action->setSlider($this->getSliderInformation($page['content'] ?? ''));
} catch (\Throwable $th) {
$action->setType('PWA_ROUTER');
}
Expand All @@ -343,7 +332,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'])) {
Expand All @@ -362,10 +351,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);
}
Expand Down Expand Up @@ -443,6 +433,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(State::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
*
Expand Down Expand Up @@ -569,7 +588,7 @@ protected function isHomePage(RequestInterface $request): bool
{
$requestPath = $request->getPathInfo();

if(!$requestPath || $requestPath === '/') {
if (!$requestPath || $requestPath === '/') {
return true;
}

Expand All @@ -582,7 +601,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;
}

Expand Down

0 comments on commit b052b06

Please sign in to comment.