Skip to content

Commit

Permalink
Merge pull request #2766 from dpfaffenbauer/issue/admin-store-context
Browse files Browse the repository at this point in the history
[Store] optimize Store Context resolving with fallback and admin/non-admin
  • Loading branch information
dpfaffenbauer authored Dec 19, 2024
2 parents 7164bfa + d556fdd commit ebd5ee2
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 21 deletions.
9 changes: 7 additions & 2 deletions src/CoreShop/Bundle/StoreBundle/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,19 @@ services:
tags:
- { name: coreshop.context.store, priority: 2 }

CoreShop\Component\Store\Context\SiteBasedResolverInterface: '@CoreShop\Component\Store\Context\SiteBasedResolver'
CoreShop\Component\Store\Context\SiteBasedResolver:
arguments:
- '@coreshop.repository.store'

CoreShop\Component\Store\Context\RequestBased\RequestResolverInterface: '@CoreShop\Component\Store\Context\RequestBased\CompositeRequestResolver'
CoreShop\Component\Store\Context\RequestBased\CompositeRequestResolver:
public: false

CoreShop\Component\Store\Context\RequestBased\PimcoreAdminSiteBasedRequestResolver:
public: false
arguments:
- '@coreshop.repository.store'
- '@CoreShop\Component\Store\Context\SiteBasedResolverInterface'
- '@Pimcore\Http\RequestHelper'
- '@Pimcore\Model\Document\Service'
tags:
Expand All @@ -52,7 +57,7 @@ services:
CoreShop\Component\Store\Context\RequestBased\SiteBasedRequestResolver:
public: false
arguments:
- '@coreshop.repository.store'
- '@CoreShop\Component\Store\Context\SiteBasedResolverInterface'
tags:
- { name: coreshop.context.store.request_based.resolver, priority: 100 }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@

namespace CoreShop\Component\Store\Context\RequestBased;

use CoreShop\Component\Store\Context\SiteBasedResolverInterface;
use CoreShop\Component\Store\Context\StoreNotFoundException;
use CoreShop\Component\Store\Model\StoreInterface;
use CoreShop\Component\Store\Repository\StoreRepositoryInterface;
use Pimcore\Http\RequestHelper;
use Pimcore\Model\Document;
use Pimcore\Model\Document\Service;
Expand All @@ -30,7 +30,7 @@
final class PimcoreAdminSiteBasedRequestResolver implements RequestResolverInterface
{
public function __construct(
private StoreRepositoryInterface $storeRepository,
private SiteBasedResolverInterface $siteBasedResolver,
private RequestHelper $requestHelper,
private Service $documentService,
) {
Expand All @@ -47,20 +47,26 @@ public function findStore(Request $request): ?StoreInterface
$document = Document::getById((int) $id);
}
}

if ($this->requestHelper->isFrontendRequestByAdmin($request)) {
else if ($this->requestHelper->isFrontendRequestByAdmin($request)) {
/** @psalm-suppress InternalMethod */
$document = $this->documentService->getNearestDocumentByPath($request->getPathInfo());
}
else {
throw new StoreNotFoundException();
}

if ($document instanceof Document) {
do {
try {
$site = Site::getByRootId($document->getId());

if ($site instanceof Site) {
return $this->storeRepository->findOneBySite($site->getId());
$store = $this->siteBasedResolver->resolveSiteWithDefaultForStore($site);

if (null === $store) {
throw new StoreNotFoundException();
}

return $store;
} catch (\Exception) {
//Ignore Exception and continue
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,33 +18,25 @@

namespace CoreShop\Component\Store\Context\RequestBased;

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

final class SiteBasedRequestResolver implements RequestResolverInterface
{
public function __construct(
private StoreRepositoryInterface $storeRepository,
private SiteBasedResolverInterface $siteBasedResolver,
) {
}

public function findStore(Request $request): ?StoreInterface
{
if (Site::isSiteRequest()) {
$store = $this->storeRepository->findOneBySite(Site::getCurrentSite()->getId());
$store = $this->siteBasedResolver->resolveSiteWithDefaultForStore(Site::isSiteRequest() ? Site::getCurrentSite() : null);

if ($store !== null) {
return $store;
}
}

$defaultStore = $this->storeRepository->findStandard();

if ($defaultStore) {
return $defaultStore;
if ($store) {
return $store;
}

throw new StoreNotFoundException();
Expand Down
50 changes: 50 additions & 0 deletions src/CoreShop/Component/Store/Context/SiteBasedResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);

/*
* CoreShop
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - CoreShop Commercial License (CCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) CoreShop GmbH (https://www.coreshop.org)
* @license https://www.coreshop.org/license GPLv3 and CCL
*
*/

namespace CoreShop\Component\Store\Context;

use CoreShop\Component\Store\Model\StoreInterface;
use CoreShop\Component\Store\Repository\StoreRepositoryInterface;
use Pimcore\Model\Site;

class SiteBasedResolver implements SiteBasedResolverInterface
{
public function __construct(
private StoreRepositoryInterface $storeRepository,
) {
}

public function resolveSiteWithDefaultForStore(?Site $site): ?StoreInterface
{
if (null !== $site) {
$store = $this->storeRepository->findOneBySite($site->getId());

if ($store !== null) {
return $store;
}
}

$defaultStore = $this->storeRepository->findStandard();

if ($defaultStore) {
return $defaultStore;
}

return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

/*
* CoreShop
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - CoreShop Commercial License (CCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) CoreShop GmbH (https://www.coreshop.org)
* @license https://www.coreshop.org/license GPLv3 and CCL
*
*/

namespace CoreShop\Component\Store\Context;

use CoreShop\Component\Store\Model\StoreInterface;
use Pimcore\Model\Site;

interface SiteBasedResolverInterface
{
public function resolveSiteWithDefaultForStore(?Site $site): ?StoreInterface;
}

0 comments on commit ebd5ee2

Please sign in to comment.