From d556fdd447bd6e474fc678fc521fe5ed3b860541 Mon Sep 17 00:00:00 2001 From: Dominik Pfaffenbauer Date: Wed, 18 Dec 2024 10:09:49 +0100 Subject: [PATCH] [Store] optimize Store Context resolving with fallback and admin/non-admin --- .../StoreBundle/Resources/config/services.yml | 9 +++- .../PimcoreAdminSiteBasedRequestResolver.php | 18 ++++--- .../RequestBased/SiteBasedRequestResolver.php | 18 ++----- .../Store/Context/SiteBasedResolver.php | 50 +++++++++++++++++++ .../Context/SiteBasedResolverInterface.php | 27 ++++++++++ 5 files changed, 101 insertions(+), 21 deletions(-) create mode 100644 src/CoreShop/Component/Store/Context/SiteBasedResolver.php create mode 100644 src/CoreShop/Component/Store/Context/SiteBasedResolverInterface.php diff --git a/src/CoreShop/Bundle/StoreBundle/Resources/config/services.yml b/src/CoreShop/Bundle/StoreBundle/Resources/config/services.yml index ed361e6b24..d35677ecba 100755 --- a/src/CoreShop/Bundle/StoreBundle/Resources/config/services.yml +++ b/src/CoreShop/Bundle/StoreBundle/Resources/config/services.yml @@ -29,6 +29,11 @@ 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 @@ -36,7 +41,7 @@ services: 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: @@ -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 } diff --git a/src/CoreShop/Component/Store/Context/RequestBased/PimcoreAdminSiteBasedRequestResolver.php b/src/CoreShop/Component/Store/Context/RequestBased/PimcoreAdminSiteBasedRequestResolver.php index 95d3080142..f2a7852794 100644 --- a/src/CoreShop/Component/Store/Context/RequestBased/PimcoreAdminSiteBasedRequestResolver.php +++ b/src/CoreShop/Component/Store/Context/RequestBased/PimcoreAdminSiteBasedRequestResolver.php @@ -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; @@ -30,7 +30,7 @@ final class PimcoreAdminSiteBasedRequestResolver implements RequestResolverInterface { public function __construct( - private StoreRepositoryInterface $storeRepository, + private SiteBasedResolverInterface $siteBasedResolver, private RequestHelper $requestHelper, private Service $documentService, ) { @@ -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 } diff --git a/src/CoreShop/Component/Store/Context/RequestBased/SiteBasedRequestResolver.php b/src/CoreShop/Component/Store/Context/RequestBased/SiteBasedRequestResolver.php index 7a2a1cadb8..73dd7ea49e 100644 --- a/src/CoreShop/Component/Store/Context/RequestBased/SiteBasedRequestResolver.php +++ b/src/CoreShop/Component/Store/Context/RequestBased/SiteBasedRequestResolver.php @@ -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(); diff --git a/src/CoreShop/Component/Store/Context/SiteBasedResolver.php b/src/CoreShop/Component/Store/Context/SiteBasedResolver.php new file mode 100644 index 0000000000..52668eb3fe --- /dev/null +++ b/src/CoreShop/Component/Store/Context/SiteBasedResolver.php @@ -0,0 +1,50 @@ +storeRepository->findOneBySite($site->getId()); + + if ($store !== null) { + return $store; + } + } + + $defaultStore = $this->storeRepository->findStandard(); + + if ($defaultStore) { + return $defaultStore; + } + + return null; + } +} \ No newline at end of file diff --git a/src/CoreShop/Component/Store/Context/SiteBasedResolverInterface.php b/src/CoreShop/Component/Store/Context/SiteBasedResolverInterface.php new file mode 100644 index 0000000000..80b49267bd --- /dev/null +++ b/src/CoreShop/Component/Store/Context/SiteBasedResolverInterface.php @@ -0,0 +1,27 @@ +