custom/plugins/ScopPlatformRedirecter/src/Subscriber/RequestSubscriber.php line 68

Open in your IDE?
  1. <?php
  2. /**
  3.  * Implemented by scope01 GmbH team https://scope01.com
  4.  *
  5.  * @copyright scope01 GmbH https://scope01.com
  6.  * @license MIT License
  7.  * @link https://scope01.com
  8.  */
  9. declare(strict_types=1);
  10. /**
  11.  * Implemented by scope01 GmbH team https://scope01.com
  12.  *
  13.  * @copyright scope01 GmbH https://scope01.com
  14.  * @license MIT
  15.  * @link https://scope01.com
  16.  */
  17. namespace Scop\PlatformRedirecter\Subscriber;
  18. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  19. use Shopware\Core\Framework\Event\BeforeSendResponseEvent;
  20. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  21. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  22. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
  23. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  24. use Shopware\Core\Framework\Context;
  25. use Symfony\Component\HttpFoundation\RedirectResponse;
  26. class RequestSubscriber implements EventSubscriberInterface
  27. {
  28.     /**
  29.      * @var EntityRepositoryInterface
  30.      */
  31.     private $repository;
  32.     /**
  33.      * @var EntityRepositoryInterface
  34.      */
  35.     private $seoUrlRepository;
  36.     /**
  37.      * @param EntityRepositoryInterface $redirectRepository
  38.      */
  39.     public function __construct(EntityRepositoryInterface $redirectRepositoryEntityRepositoryInterface $seoUrlRepository)
  40.     {
  41.         /** @var EntityRepositoryInterface $repository */
  42.         $this->repository $redirectRepository;
  43.         $this->seoUrlRepository $seoUrlRepository;
  44.     }
  45.     /**
  46.      *
  47.      * @return array
  48.      */
  49.     public static function getSubscribedEvents(): array
  50.     {
  51.         return [
  52.             BeforeSendResponseEvent::class => 'redirectBeforeSendResponse'
  53.         ];
  54.     }
  55.     /**
  56.      * Redirect to the new url if found in redirects
  57.      * Otherwise do nothing
  58.      * Modules like admin, api or widgets are excluded from redirects
  59.      *
  60.      * @param BeforeSendResponseEvent $event
  61.      */
  62.     public function redirectBeforeSendResponse(BeforeSendResponseEvent $event): void
  63.     {
  64.         $requestUri = (string)$event->getRequest()->get('resolved-uri');
  65.         $storefrontUri $event->getRequest()->get('sw-storefront-url');
  66.         $requestBase $event->getRequest()->getPathInfo();
  67.         $requestBaseUrl $event->getRequest()->getBaseUrl();
  68.         $queryString = (string)$event->getRequest()->getQueryString();
  69.         $search = [];
  70.         // Block overriding /admin and /api and widgets, so you can't lock out of the administration.
  71.         if (\strpos($requestBase"/admin") === 0) {
  72.             return;
  73.         }
  74.         if (\strpos($requestBase"/api") === 0) {
  75.             return;
  76.         }
  77.         if (\strpos($requestBase"/widgets") === 0) {
  78.             return;
  79.         }
  80.         if (\strpos($requestBase"/store-api") === 0) {
  81.             return;
  82.         }
  83.         if ($queryString !== '') {
  84.             $queryString urldecode($queryString);
  85.             $requestUri .= '?' $queryString;
  86.         }
  87.         // try to load the seo route
  88.         $context Context::createDefaultContext();
  89.         $redirects $this->seoUrlRepository->search((new Criteria())
  90.             ->addFilter(new EqualsAnyFilter('pathInfo', [$requestUri])), $context);
  91.         // if found overwrite search term with the seo route
  92.         if ($redirects->count() !== 0) {
  93.             foreach ($redirects as $redirect) {
  94.                 $requestBase $redirect->getSeoPathInfo();
  95.                 // Search for Redirect
  96.                 $search[] = [
  97.                     $requestBaseUrl '/' $requestBase// relative url with shopware 6 in sub folder: /public/Ergonomic-Concrete-Cough-Machine/48314803f1244f609a2ce907bfb48f53
  98.                     $requestBaseUrl $requestBase// relative url with shopware 6 in sub folder url is not shopware seo url: /public/test
  99.                     $storefrontUri $requestBase// absolute url with shopware 6 in sub folder, full url with host: http://shopware-platform.local/public/test1
  100.                     $storefrontUri '/' $requestBase// absolute url with shopware 6 in sub folder, full url with host and slash at the end: http://shopware-platform.local/public/Freizeit-Elektro/Telefone/
  101.                     $requestBase// relative url domain configured in public folder: /Ergonomic-Concrete-Cough-Machine/48314803f1244f609a2ce907bfb48f53 or /test4
  102.                     '/' $requestBase// absolute url domain configured in public folder: http://shopware-platform.local/Shoes-Baby/
  103.                     \substr($requestBase1), // e.g. "test"
  104.                 ];
  105.             }
  106.         }
  107.         if (!empty($search)) {
  108.             $search array_merge(...$search);
  109.         } else {
  110.             $search = [
  111.                 $requestBaseUrl '/' $requestUri// relative url with shopware 6 in sub folder: /public/Ergonomic-Concrete-Cough-Machine/48314803f1244f609a2ce907bfb48f53
  112.                 $requestBaseUrl $requestUri// relative url with shopware 6 in sub folder url is not shopware seo url: /public/test
  113.                 $storefrontUri $requestUri// absolute url with shopware 6 in sub folder, full url with host: http://shopware-platform.local/public/test1
  114.                 $storefrontUri '/' $requestUri// absolute url with shopware 6 in sub folder, full url with host and slash at the end: http://shopware-platform.local/public/Freizeit-Elektro/Telefone/
  115.                 $requestUri// relative url domain configured in public folder: /Ergonomic-Concrete-Cough-Machine/48314803f1244f609a2ce907bfb48f53 or /test4
  116.                 '/' $requestUri// absolute url domain configured in public folder: http://shopware-platform.local/Shoes-Baby/
  117.                 \substr($requestUri1), // e.g. "test"
  118.             ];
  119.         }
  120.         // search for the redirect in the database
  121.         $redirects $this->repository->search((new Criteria())->addFilter(new EqualsAnyFilter('sourceURL'$search))->addFilter(new EqualsFilter('enabled'true))
  122.             ->setLimit(1), $context);
  123.         // No Redirect found for this URL, do nothing
  124.         if ($redirects->count() === 0) {
  125.             return;
  126.         }
  127.         $redirect $redirects->first();
  128.         $targetURL $redirect->getTargetURL();
  129.         $code $redirect->getHttpCode();
  130.         /*
  131.          *  checks if $targetURL is a full url or path and redirects accordingly
  132.          */
  133.         if (!(\strpos($targetURL"http:") === || \strpos($targetURL"https:") === 0)) {
  134.             if (\strpos($targetURL"www.") === 0) {
  135.                 $targetURL "http://" $targetURL;
  136.             } else {
  137.                 if (\strpos($targetURL"/") !== 0) {
  138.                     $targetURL "/" $targetURL;
  139.                 }
  140.             }
  141.         }
  142.         $event->setResponse(new RedirectResponse($targetURL$code));
  143.     }
  144. }