From d4a498dcacd9bad56e413983185fe3df397c5d31 Mon Sep 17 00:00:00 2001 From: Lorenzo Ruozzi Date: Wed, 2 Aug 2023 12:50:17 +0200 Subject: [PATCH] Do not throw if channel does not exist on current request --- composer-require-checker.json | 1 + src/EventListener/ControllerSubscriber.php | 9 ++++++++- src/EventListener/NotFoundSubscriber.php | 9 ++++++++- 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/composer-require-checker.json b/composer-require-checker.json index 90a5310..63066a0 100644 --- a/composer-require-checker.json +++ b/composer-require-checker.json @@ -21,6 +21,7 @@ "Sylius\\Bundle\\TaxonomyBundle\\Form\\Type\\TaxonTranslationType", "Sylius\\Bundle\\UiBundle\\Menu\\Event\\MenuBuilderEvent", "Sylius\\Component\\Channel\\Context\\ChannelContextInterface", + "Sylius\\Component\\Channel\\Context\\ChannelNotFoundException", "Sylius\\Component\\Channel\\Model\\ChannelAwareInterface", "Sylius\\Component\\Channel\\Model\\ChannelInterface", "Sylius\\Component\\Channel\\Model\\ChannelsAwareInterface", diff --git a/src/EventListener/ControllerSubscriber.php b/src/EventListener/ControllerSubscriber.php index 89cb908..72253ab 100644 --- a/src/EventListener/ControllerSubscriber.php +++ b/src/EventListener/ControllerSubscriber.php @@ -7,6 +7,7 @@ use Doctrine\Persistence\ObjectManager; use Setono\SyliusRedirectPlugin\Resolver\RedirectionPathResolverInterface; use Sylius\Component\Channel\Context\ChannelContextInterface; +use Sylius\Component\Channel\Context\ChannelNotFoundException; use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\HttpKernel\Event\ControllerEvent; use Symfony\Component\HttpKernel\KernelEvents; @@ -45,9 +46,15 @@ public static function getSubscribedEvents(): array public function onKernelController(ControllerEvent $event): void { $request = $event->getRequest(); + $channel = null; + + try { + $channel = $this->channelContext->getChannel(); + } catch (ChannelNotFoundException $e) { + } $redirectionPath = $this->redirectionPathResolver->resolveFromRequest( $request, - $this->channelContext->getChannel() + $channel ); if ($redirectionPath->isEmpty()) { diff --git a/src/EventListener/NotFoundSubscriber.php b/src/EventListener/NotFoundSubscriber.php index 82508e1..656ee77 100644 --- a/src/EventListener/NotFoundSubscriber.php +++ b/src/EventListener/NotFoundSubscriber.php @@ -8,6 +8,7 @@ use Setono\MainRequestTrait\MainRequestTrait; use Setono\SyliusRedirectPlugin\Resolver\RedirectionPathResolverInterface; use Sylius\Component\Channel\Context\ChannelContextInterface; +use Sylius\Component\Channel\Context\ChannelNotFoundException; use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Event\ExceptionEvent; @@ -57,11 +58,17 @@ public function onKernelException(ExceptionEvent $event): void if (!$throwable instanceof HttpException || $throwable->getStatusCode() !== Response::HTTP_NOT_FOUND) { return; } + $channel = null; + + try { + $channel = $this->channelContext->getChannel(); + } catch (ChannelNotFoundException $e) { + } $request = $event->getRequest(); $redirectionPath = $this->redirectionPathResolver->resolveFromRequest( $request, - $this->channelContext->getChannel(), + $channel, true );