Skip to content

Commit

Permalink
Merge pull request #1778 from dpfaffenbauer/issues/refactor-theme-con…
Browse files Browse the repository at this point in the history
…text

[ThemeBundle] refactor theme-context to work with area-bricks
  • Loading branch information
dpfaffenbauer authored Nov 10, 2021
2 parents 95303bc + 40e780f commit 38618a8
Show file tree
Hide file tree
Showing 8 changed files with 134 additions and 16 deletions.
7 changes: 7 additions & 0 deletions src/CoreShop/Bundle/StoreBundle/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ services:
tags:
- { name: coreshop.context.store.request_based.resolver, priority: 200 }

CoreShop\Component\Store\Context\RequestBased\PimcoreAreaBrickRenderResolver:
public: false
arguments:
- '@coreshop.repository.store'
tags:
- { name: coreshop.context.store.request_based.resolver, priority: 220 }

CoreShop\Component\Store\Context\RequestBased\SiteBasedRequestResolver:
public: false
arguments:
Expand Down
13 changes: 11 additions & 2 deletions src/CoreShop/Bundle/ThemeBundle/Context/ThemeContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,22 @@

use CoreShop\Bundle\ThemeBundle\Service\ThemeNotResolvedException;
use CoreShop\Bundle\ThemeBundle\Service\ThemeResolverInterface;
use Pimcore\Http\Request\Resolver\DocumentResolver;
use Pimcore\Http\Request\Resolver\PimcoreContextResolver;
use Pimcore\Model\Document;
use Sylius\Bundle\ThemeBundle\Context\ThemeContextInterface;
use Sylius\Bundle\ThemeBundle\Model\ThemeInterface;
use Sylius\Bundle\ThemeBundle\Repository\ThemeRepositoryInterface;
use Symfony\Component\HttpFoundation\RequestStack;

final class ThemeContext implements ThemeContextInterface
{
public function __construct(private ThemeResolverInterface $resolver, private ThemeRepositoryInterface $themeRepository, private PimcoreContextResolver $pimcoreContext, private RequestStack $requestStack)
public function __construct(
private ThemeResolverInterface $resolver,
private ThemeRepositoryInterface $themeRepository,
private PimcoreContextResolver $pimcoreContext,
private RequestStack $requestStack
)
{
}

Expand All @@ -36,7 +43,9 @@ public function getTheme(): ?ThemeInterface
return null;
}

if ($this->pimcoreContext->matchesPimcoreContext($request, PimcoreContextResolver::CONTEXT_ADMIN)) {
$isAjaxBrickRendering = $request->attributes->get('_route') === 'pimcore_admin_document_page_areabrick-render-index-editmode';

if (!$isAjaxBrickRendering && $this->pimcoreContext->matchesPimcoreContext($request, PimcoreContextResolver::CONTEXT_ADMIN)) {
return null;
}

Expand Down
4 changes: 4 additions & 0 deletions src/CoreShop/Bundle/ThemeBundle/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,15 @@ services:
- '@coreshop.theme.resolver.registry'

CoreShop\Bundle\ThemeBundle\Service\PimcoreSiteThemeResolver:
arguments:
- '@Symfony\Component\HttpFoundation\RequestStack'
- '@Pimcore\Http\Request\Resolver\DocumentResolver'
tags:
- { name: coreshop.theme.resolver, type: pimcore_site, priority: 10 }

CoreShop\Bundle\ThemeBundle\Service\PimcoreDocumentPropertyResolver:
arguments:
- '@Symfony\Component\HttpFoundation\RequestStack'
- '@Pimcore\Http\Request\Resolver\DocumentResolver'
tags:
- { name: coreshop.theme.resolver, type: pimcore_document_property, priority: 15 }
Expand Down
2 changes: 0 additions & 2 deletions src/CoreShop/Bundle/ThemeBundle/Resources/pimcore/config.yml

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,41 @@
namespace CoreShop\Bundle\ThemeBundle\Service;

use Pimcore\Http\Request\Resolver\DocumentResolver;
use Pimcore\Model\Document;
use Symfony\Component\HttpFoundation\RequestStack;

final class PimcoreDocumentPropertyResolver implements ThemeResolverInterface
{
public function __construct(private DocumentResolver $documentResolver)
{
public function __construct(
private RequestStack $requestStack,
private DocumentResolver $documentResolver
) {
}

public function resolveTheme(): string
{
try {
$document = $this->documentResolver->getDocument();
$request = $this->requestStack->getMainRequest();

if (!$request) {
throw new ThemeNotResolvedException();
}

$isAjaxBrickRendering = $request->attributes->get('_route') === 'pimcore_admin_document_page_areabrick-render-index-editmode';
$document = null;

if ($isAjaxBrickRendering) {
$documentId = $request->request->get('documentId');

if ($documentId) {
$document = Document::getById((int)$documentId);
}
}
else {
$document = $this->documentResolver->getDocument($request);
}

if ($document && $document->getProperty('theme')) {
if ($document instanceof Document && $document->getProperty('theme')) {
return $document->getProperty('theme');
}
} catch (\Exception $ex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,48 @@

namespace CoreShop\Bundle\ThemeBundle\Service;

use Pimcore\Model\Site;
use Pimcore\Http\Request\Resolver\DocumentResolver;
use Pimcore\Model\Document;
use Pimcore\Tool\Frontend;
use Symfony\Component\HttpFoundation\RequestStack;

final class PimcoreSiteThemeResolver implements ThemeResolverInterface
{
public function __construct(
private RequestStack $requestStack,
private DocumentResolver $documentResolver
) {
}


public function resolveTheme(): string
{
$list = new Site\Listing();
$list->load();
$request = $this->requestStack->getMainRequest();

if (!$request) {
throw new ThemeNotResolvedException();
}

$isAjaxBrickRendering = $request->attributes->get('_route') === 'pimcore_admin_document_page_areabrick-render-index-editmode';
$document = null;

if ($isAjaxBrickRendering) {
$documentId = $request->request->get('documentId');

if ($documentId) {
$document = Document::getById((int)$documentId);
}
}
else {
$document = $this->documentResolver->getDocument($request);
}

try {
$currentSite = Site::getCurrentSite();
if ($document instanceof Document) {
$site = Frontend::getSiteForDocument($document);

if ($theme = $currentSite->getRootDocument()->getKey()) {
if ($site && $theme = $site->getRootDocument()->getKey()) {
return $theme;
}
} catch (\Exception $exception) {
throw new ThemeNotResolvedException($exception);
}

throw new ThemeNotResolvedException();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

namespace CoreShop\Bundle\ThemeBundle\Service;

use Pimcore\Model\Document;

interface ThemeResolverInterface
{
/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php
/**
* CoreShop.
*
* This source file is subject to the GNU General Public License version 3 (GPLv3)
* For the full copyright and license information, please view the LICENSE.md and gpl-3.0.txt
* files that are distributed with this source code.
*
* @copyright Copyright (c) CoreShop GmbH (https://www.coreshop.org)
* @license https://www.coreshop.org/license GNU General Public License version 3 (GPLv3)
*/

declare(strict_types=1);

namespace CoreShop\Component\Store\Context\RequestBased;

use CoreShop\Component\Store\Context\StoreNotFoundException;
use CoreShop\Component\Store\Model\StoreInterface;
use CoreShop\Component\Store\Repository\StoreRepositoryInterface;
use Pimcore\Model\Document;
use Pimcore\Model\Site;
use Pimcore\Tool\Frontend;
use Symfony\Component\HttpFoundation\Request;

final class PimcoreAreaBrickRenderResolver implements RequestResolverInterface
{
public function __construct(private StoreRepositoryInterface $storeRepository)
{
}

public function findStore(Request $request): ?StoreInterface
{
if ($request->attributes->get('_route') === 'pimcore_admin_document_page_areabrick-render-index-editmode') {
$documentId = $request->request->get('documentId');

if ($documentId) {
$document = Document::getById((int)$documentId);

if ($document) {
$site = Frontend::getSiteForDocument($document);

if ($site instanceof Site) {
return $this->storeRepository->findOneBySite($site->getId());
}
}
}
}

throw new StoreNotFoundException();
}
}

0 comments on commit 38618a8

Please sign in to comment.