diff --git a/src/Adapter/LegacyContext.php b/src/Adapter/LegacyContext.php index 538a47291b32f..7160aaeabbaeb 100644 --- a/src/Adapter/LegacyContext.php +++ b/src/Adapter/LegacyContext.php @@ -79,13 +79,7 @@ public function __construct( public function getContext() { if (null === static::$instance) { - $legacyContext = Context::getContext(); - if ($legacyContext && !empty($legacyContext->shop) && !isset($legacyContext->controller) && isset($legacyContext->employee)) { - //init real legacy shop context - //$adminController = new AdminController(); - //$adminController->initShopContext(); - } - static::$instance = $legacyContext; + static::$instance = Context::getContext(); } return static::$instance; diff --git a/src/Core/Context/CurrencyContextBuilder.php b/src/Core/Context/CurrencyContextBuilder.php index b4a6680bf5f3a..e5f43f4d2cf40 100644 --- a/src/Core/Context/CurrencyContextBuilder.php +++ b/src/Core/Context/CurrencyContextBuilder.php @@ -93,5 +93,7 @@ public function setCurrencyId(int $currencyId) $legacyCurrency = $this->currencyRepository->get(new CurrencyId($this->currencyId)); $this->contextStateManager->setCurrency($legacyCurrency); } + + return $this; } } diff --git a/src/Core/Context/ShopContextBuilder.php b/src/Core/Context/ShopContextBuilder.php index b074a59056b4e..2769db41e27dd 100644 --- a/src/Core/Context/ShopContextBuilder.php +++ b/src/Core/Context/ShopContextBuilder.php @@ -28,10 +28,12 @@ namespace PrestaShop\PrestaShop\Core\Context; +use PrestaShop\PrestaShop\Adapter\ContextStateManager; use PrestaShop\PrestaShop\Adapter\Shop\Repository\ShopRepository; use PrestaShop\PrestaShop\Core\Domain\Shop\ValueObject\ShopConstraint; use PrestaShop\PrestaShop\Core\Domain\Shop\ValueObject\ShopId; use PrestaShop\PrestaShop\Core\Exception\InvalidArgumentException; +use Shop as LegacyShop; /** * @experimental Depends on ADR https://github.com/PrestaShop/ADR/pull/36 @@ -42,7 +44,8 @@ class ShopContextBuilder private ?int $shopId = null; public function __construct( - private readonly ShopRepository $shopRepository + private readonly ShopRepository $shopRepository, + private readonly ContextStateManager $contextStateManager ) { } @@ -82,6 +85,19 @@ public function build(): ShopContext ); } + public function buildLegacyContext(ShopConstraint $shopConstraint): self + { + if ($shopConstraint->forAllShops()) { + $this->contextStateManager->setShopContext(LegacyShop::CONTEXT_ALL); + } else if (empty($shopConstraint->getShopId())) { + $this->contextStateManager->setShopContext(LegacyShop::CONTEXT_GROUP, $shopConstraint->getShopGroupId()->getValue()); + } else { + $this->contextStateManager->setShopContext(LegacyShop::CONTEXT_SHOP, $shopConstraint->getShopId()->getValue()); + } + + return $this; + } + public function setShopId(int $shopId): self { $this->shopId = $shopId; @@ -95,4 +111,8 @@ public function setShopConstraint(ShopConstraint $shopConstraint): self return $this; } + + + + } diff --git a/src/PrestaShopBundle/EventListener/Context/Admin/ShopContextListener.php b/src/PrestaShopBundle/EventListener/Context/Admin/ShopContextListener.php index aea56d24f740f..b028fc723fb39 100644 --- a/src/PrestaShopBundle/EventListener/Context/Admin/ShopContextListener.php +++ b/src/PrestaShopBundle/EventListener/Context/Admin/ShopContextListener.php @@ -30,7 +30,6 @@ use PrestaShop\PrestaShop\Adapter\Feature\MultistoreFeature; use PrestaShop\PrestaShop\Adapter\LegacyContext; -use PrestaShop\PrestaShop\Adapter\Shop\Context as ShopContextAdapter; use PrestaShop\PrestaShop\Core\Context\EmployeeContext; use PrestaShop\PrestaShop\Core\Context\ShopContextBuilder; use PrestaShop\PrestaShop\Core\Domain\Configuration\ShopConfigurationInterface; @@ -48,7 +47,6 @@ public function __construct( private readonly ShopConfigurationInterface $configuration, private readonly LegacyContext $legacyContext, private readonly MultistoreFeature $multistoreFeature, - private readonly ShopContextAdapter $shopContextAdapter ) { } @@ -65,9 +63,9 @@ public function onKernelRequest(RequestEvent $event): void $shopConstraint = ShopConstraint::allShops(); $cookieShopConstraint = $this->getShopConstraintFromCookie(); - if (!empty($event->getRequest()->get('setShopContext'))) { + /*if (!empty($event->getRequest()->get('setShopContext'))) { $this->changeShopContext($event->getRequest()->get('setShopContext')); - } + }*/ if ($cookieShopConstraint && $cookieShopConstraint->getShopGroupId()) { // Check if the employee has permission on selected group if not fallback on single shop context with employee's default shop @@ -93,13 +91,7 @@ public function onKernelRequest(RequestEvent $event): void $this->shopContextBuilder->setShopId($shopConstraint->getShopId()->getValue()); } - /*if ($shopConstraint->forAllShops()) { - $this->shopContextAdapter->setAllContext((int) $this->configuration->get('PS_SHOP_DEFAULT', null, ShopConstraint::allShops())); - } else if (empty($shopConstraint->getShopId())) { - $this->shopContextAdapter->setShopGroupContext($shopConstraint->getShopGroupId()->getValue()); - } else { - $this->shopContextAdapter->setShopContext($shopConstraint->getShopId()->getValue()); - } */ + $this->shopContextBuilder->buildLegacyContext($shopConstraint); } /**