vendor/store.shopware.com/swagb2bplatform/SwagB2bPlatform/Routing/ControllerRoutingExceptionHandler.php line 49

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace SwagB2bPlatform\Routing;
  3. use LogicException;
  4. use Shopware\B2B\Common\Controller\B2bControllerForwardException;
  5. use Shopware\B2B\Common\Controller\B2bControllerRedirectException;
  6. use Shopware\B2B\Common\Controller\B2bControllerRouteNameProvider;
  7. use Shopware\B2B\Common\Controller\B2bControllerRoutingException;
  8. use Shopware\Core\Framework\Routing\RequestTransformerInterface;
  9. use Shopware\Storefront\Framework\Routing\Router;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. use Symfony\Component\HttpFoundation\RedirectResponse;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\HttpKernel\Event\ExceptionEvent;
  15. use Symfony\Component\HttpKernel\HttpKernelInterface;
  16. use Symfony\Component\HttpKernel\KernelEvents;
  17. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  18. use Symfony\Component\Routing\RouterInterface;
  19. use function array_merge;
  20. use function get_class;
  21. class ControllerRoutingExceptionHandler implements EventSubscriberInterface
  22. {
  23.     private RouterInterface $router;
  24.     private HttpKernelInterface $httpKernel;
  25.     private RequestTransformerInterface $requestTransformer;
  26.     public function __construct(
  27.         RouterInterface $router,
  28.         HttpKernelInterface $httpKernel,
  29.         RequestTransformerInterface $requestTransformer
  30.     ) {
  31.         $this->router $router;
  32.         $this->httpKernel $httpKernel;
  33.         $this->requestTransformer $requestTransformer;
  34.     }
  35.     public static function getSubscribedEvents(): array
  36.     {
  37.         return [
  38.             KernelEvents::EXCEPTION => ['handleRouting'500],
  39.         ];
  40.     }
  41.     public function handleRouting(ExceptionEvent $event): void
  42.     {
  43.         $exception $event->getThrowable();
  44.         $request $event->getRequest();
  45.         if (!$exception instanceof B2bControllerRoutingException) {
  46.             return;
  47.         }
  48.         if ($exception instanceof B2bControllerForwardException) {
  49.             $this->updateEvent($event$this->handleForward($exception$request));
  50.             return;
  51.         }
  52.         if ($exception instanceof B2bControllerRedirectException) {
  53.             $this->updateEvent($event$this->handleRedirect($exception$request));
  54.             return;
  55.         }
  56.         throw new LogicException('Unable to handle ' get_class($exception));
  57.     }
  58.     /**
  59.      * @internal
  60.      */
  61.     protected function updateEvent(ExceptionEvent $eventResponse $response): void
  62.     {
  63.         $event->allowCustomResponseCode();
  64.         $event->setResponse($response);
  65.         $event->stopPropagation();
  66.     }
  67.     /**
  68.      * @internal
  69.      */
  70.     protected function handleRedirect(B2bControllerRedirectException $redirectExceptionRequest $request): Response
  71.     {
  72.         $url $this->generateUrl($redirectException$request);
  73.         return new RedirectResponse($url);
  74.     }
  75.     /**
  76.      * @internal
  77.      */
  78.     protected function handleForward(B2bControllerForwardException $forwardExceptionRequest $request): Response
  79.     {
  80.         $route $this->assembleRouteName($forwardException$request);
  81.         $url $this->router
  82.             ->generate($route, [], Router::PATH_INFO);
  83.         $subRequest $this->cloneRequest($forwardException$request$url);
  84.         return $this->httpKernel->handle($subRequestHttpKernelInterface::SUB_REQUEST);
  85.     }
  86.     /**
  87.      * @internal
  88.      */
  89.     protected function assembleRouteName(B2bControllerRoutingException $routingExceptionRequest $request): string
  90.     {
  91.         if ($routingException instanceof B2bControllerRouteNameProvider) {
  92.             return $routingException->getRouteName();
  93.         }
  94.         $controller $routingException->getController();
  95.         if ($controller === null) {
  96.             $routeParams $request->attributes->get('_route_params');
  97.             $controller $routeParams[RouteLoader::ROUTE_CONTROLLER_ROUTE_NAME];
  98.         }
  99.         $action $routingException->getAction();
  100.         return 'frontend.b2b.' $controller '.' $action;
  101.     }
  102.     /**
  103.      * @internal
  104.      */
  105.     protected function cloneRequest(B2bControllerForwardException $forwardExceptionRequest $requeststring $url): Request
  106.     {
  107.         $requestContext $this->router
  108.             ->getContext();
  109.         $currentMethod $requestContext
  110.             ->getMethod();
  111.         $requestContext
  112.             ->setMethod(Request::METHOD_GET);
  113.         $route $this->router->match($url);
  114.         $requestContext->setMethod($currentMethod);
  115.         $route['_route_params'] = $route;
  116.         $route array_merge(
  117.             $this->requestTransformer->extractInheritableAttributes($request),
  118.             $route
  119.         );
  120.         return $request->duplicate($forwardException->getParams(), null$route);
  121.     }
  122.     /**
  123.      * @internal
  124.      */
  125.     protected function generateUrl(B2bControllerRoutingException $eRequest $request): string
  126.     {
  127.         $route $this->assembleRouteName($e$request);
  128.         return $this->router
  129.             ->generate($route$e->getParams(), UrlGeneratorInterface::ABSOLUTE_PATH);
  130.     }
  131. }