Skip to content

Commit

Permalink
Merge pull request #29 from scandipwa/slider-prealod
Browse files Browse the repository at this point in the history
Preload slider for CMS Pages
  • Loading branch information
AleksandrsKondratjevs authored Feb 10, 2023
2 parents 22bb87c + c49c1e8 commit ef9583d
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 38 deletions.
8 changes: 8 additions & 0 deletions src/Controller/Pwa.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,12 @@ public function setCmsPage($cmsPage): self
return $this;
}

public function setSlider($slider): self
{
$this->slider = $slider;
return $this;
}

/**
* @param string $description
* @return Pwa
Expand Down Expand Up @@ -208,6 +214,7 @@ public function __construct(
$this->name = '';
$this->display_mode = '';
$this->cmsPage = null;
$this->slider = null;
$this->description = '';
$this->catalogDefaultSortBy = '';
$this->storeConfig = null;
Expand All @@ -229,6 +236,7 @@ public function execute()
$resultLayout->setSku($this->sku);
$resultLayout->setName($this->name);
$resultLayout->setCmsPage($this->cmsPage);
$resultLayout->setSlider($this->slider);
$resultLayout->setDisplayMode($this->display_mode);
$resultLayout->setDescription($this->description);
$resultLayout->setCatalogDefaultSortBy($this->catalogDefaultSortBy);
Expand Down
140 changes: 103 additions & 37 deletions src/Controller/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,12 @@
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;

class Router extends BaseRouter
{
Expand Down Expand Up @@ -111,46 +116,76 @@ class Router extends BaseRouter
*/
protected PageProvider $pageProvider;

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

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

/**
* @var TokenizerParameter
*/
protected $tokenizerParameter;

/**
* @var SliderResolver
*/
protected $sliderResolver;

/**
* @var WidgetTemplate
*/
protected $widgetTemplate;

/**
* Router constructor.
* @param ActionList $actionList
* @param ActionFactory $actionFactory
* @param DefaultPathInterface $defaultPath
* @param ResponseFactory $responseFactory
* @param ConfigInterface $routeConfig
* @param UrlInterface $url
* @param NameBuilder $nameBuilder
* @param PathConfigInterface $pathConfig
* @param ActionList $actionList
* @param ActionFactory $actionFactory
* @param DefaultPathInterface $defaultPath
* @param ResponseFactory $responseFactory
* @param ConfigInterface $routeConfig
* @param UrlInterface $url
* @param NameBuilder $nameBuilder
* @param PathConfigInterface $pathConfig
* @param ValidationManagerInterface $validationManager
* @param UrlFinderInterface $urlFinder
* @param StoreManagerInterface $storeManager
* @param ScopeConfigInterface $scopeConfig
* @param ThemeProviderInterface $themeProvider
* @param UrlFinderInterface $urlFinder
* @param StoreManagerInterface $storeManager
* @param ScopeConfigInterface $scopeConfig
* @param ThemeProviderInterface $themeProvider
* @param PageFactory $pageFactory
* @param ProductRepository $productRepository
* @param CategoryRepositoryInterface $categoryRepository
* @param PageProvider $pageProvider
*/
public function __construct(
ActionList $actionList,
ActionFactory $actionFactory,
DefaultPathInterface $defaultPath,
ResponseFactory $responseFactory,
ConfigInterface $routeConfig,
UrlInterface $url,
NameBuilder $nameBuilder,
PathConfigInterface $pathConfig,
ValidationManagerInterface $validationManager,
UrlFinderInterface $urlFinder,
StoreManagerInterface $storeManager,
ScopeConfigInterface $scopeConfig,
ThemeProviderInterface $themeProvider,
PageFactory $pageFactory,
ProductRepository $productRepository,
CategoryRepositoryInterface $categoryRepository,
PageProvider $pageProvider,
array $ignoredURLs = []
) {
ActionList $actionList,
ActionFactory $actionFactory,
DefaultPathInterface $defaultPath,
ResponseFactory $responseFactory,
ConfigInterface $routeConfig,
UrlInterface $url,
NameBuilder $nameBuilder,
PathConfigInterface $pathConfig,
ValidationManagerInterface $validationManager,
UrlFinderInterface $urlFinder,
StoreManagerInterface $storeManager,
ScopeConfigInterface $scopeConfig,
ThemeProviderInterface $themeProvider,
PageFactory $pageFactory,
ProductRepository $productRepository,
CategoryRepositoryInterface $categoryRepository,
PageProvider $pageProvider,
PageRepositoryInterface $pageRepository,
GetPageByIdentifierInterface $pageByIdentifier,
TokenizerParameter $tokenizerParameter,
SliderResolver $sliderResolver,
array $ignoredURLs = [],
)
{
$this->scopeConfig = $scopeConfig;
$this->themeProvider = $themeProvider;
$this->validationManager = $validationManager;
Expand All @@ -159,9 +194,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;

parent::__construct(
$actionList,
Expand Down Expand Up @@ -243,7 +282,7 @@ public function match(RequestInterface $request)
$action->setCode(404)->setPhrase('Not Found');
}

if ($this->isHomePage($request, $action)) {
if ($this->isHomePage($request)) {
$this->setResponseHomePage($action);
}

Expand Down Expand Up @@ -287,16 +326,43 @@ protected function setResponseHomePage(ActionInterface $action)
);

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

$action->setId($page['page_id'] ?? '');

$action->setType(self::PAGE_TYPE_CMS_PAGE);
$action->setId($page['page_id'] ?? '');
$action->setCmsPage($page);
$action->setCmsPage($this->pageProvider->convertPageData($page));
$action->setSlider($this->getSliderInformation($page['content']));
} catch (\Throwable $th) {
$action->setType('PWA_ROUTER');
}
}

/**
* @param string $content
*
* This function gets first widget from page content
* In case if it is slider, then it gets slider it.
*/
protected function getSliderInformation($content)
{
preg_match('/{{(.*?)}}/m', $content, $match);

$this->tokenizerParameter->setString($match[0]);
$params = $this->tokenizerParameter->tokenize();

foreach ($params as $key => $value) {
if (substr($value, 0, 1) === '$') {
$params[$key] = $this->widgetTemplate->getVariable(substr($value, 1), null);
}
}

if (isset($params['slider_id'])) {
return $this->sliderResolver->getSlider($params['slider_id']);
}
}

/**
* Validate that CMS page is assigned to current store and is enabled and return 404 if not
*
Expand All @@ -308,10 +374,10 @@ protected function setResponseHomePage(ActionInterface $action)
protected function setResponseCmsPage($id, ActionInterface $action)
{
try {
$page = $this->pageProvider->getDataByPageId((int) $id, (int) $this->storeId);

$page = $this->pageRepository->getById($id);
$action->setId($page['page_id'] ?? '');
$action->setCmsPage($page);
$action->setCmsPage($this->pageProvider->convertPageData($page));
$action->setSlider($this->getSliderInformation($page['content']));
} catch (\Throwable $th) {
$this->setNotFound($action);
}
Expand Down
23 changes: 22 additions & 1 deletion src/View/Result/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ public function __construct(
$this->name = '';
$this->display_mode = '';
$this->cmsPage = null;
$this->slider = null;
$this->description = '';
$this->catalogDefaultSortBy = '';
$this->storeConfig = null;
Expand Down Expand Up @@ -324,14 +325,34 @@ public function getCatalogDefaultSortBy()

public function setStoreConfig($storeConfig)
{
if($this->storeConfig === null) {
if ($this->storeConfig === null) {
$this->storeConfig = $storeConfig;
return $this;
}

return null;
}

public function getStoreConfig()
{
return $this->storeConfig;
}

public function setSlider($slider)
{
if ($this->slider === null) {
$this->slider = $slider;
return $this;
}

return '';
}

public function getSlider()
{
return $this->slider;
}

public function getCatalogDefaultSortByConfig()
{
return $this->storeConfig;
Expand Down

0 comments on commit ef9583d

Please sign in to comment.