vendor/monsieurbiz/sylius-cms-page-plugin/src/Routing/RequestContext.php line 66

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of Monsieur Biz' Cms Page plugin for Sylius.
  4.  *
  5.  * (c) Monsieur Biz <sylius@monsieurbiz.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE.txt
  8.  * file that was distributed with this source code.
  9.  */
  10. declare(strict_types=1);
  11. namespace MonsieurBiz\SyliusCmsPagePlugin\Routing;
  12. use Exception;
  13. use Sylius\Component\Locale\Context\LocaleContextInterface;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Component\HttpFoundation\RequestStack;
  16. use Symfony\Component\Routing\RequestContext as BaseRequestContext;
  17. final class RequestContext extends BaseRequestContext
  18. {
  19.     /**
  20.      * @var BaseRequestContext
  21.      */
  22.     private $decorated;
  23.     /**
  24.      * @var PageSlugConditionChecker
  25.      */
  26.     private $pageSlugConditionChecker;
  27.     /**
  28.      * @var LocaleContextInterface
  29.      */
  30.     private $localeContext;
  31.     private RequestStack $requestStack;
  32.     /**
  33.      * RequestContext constructor.
  34.      */
  35.     public function __construct(
  36.         BaseRequestContext $decorated,
  37.         PageSlugConditionChecker $pageSlugConditionChecker,
  38.         LocaleContextInterface $localeContext,
  39.         RequestStack $requestStack
  40.     ) {
  41.         parent::__construct(
  42.             $decorated->getBaseUrl(),
  43.             $decorated->getMethod(),
  44.             $decorated->getHost(),
  45.             $decorated->getScheme(),
  46.             $decorated->getHttpPort(),
  47.             $decorated->getHttpsPort(),
  48.             $decorated->getPathInfo(),
  49.             $decorated->getQueryString()
  50.         );
  51.         $this->decorated $decorated;
  52.         $this->pageSlugConditionChecker $pageSlugConditionChecker;
  53.         $this->localeContext $localeContext;
  54.         $this->requestStack $requestStack;
  55.     }
  56.     public function checkPageSlug(Request $request): bool
  57.     {
  58.         if ($request !== $this->requestStack->getMasterRequest()) {
  59.             return false;
  60.         }
  61.         return $this->pageSlugConditionChecker->isPageSlug($this->prepareSlug($request->getPathInfo()));
  62.     }
  63.     private function prepareSlug(string $slug): string
  64.     {
  65.         $slug ltrim($slug'/');
  66.         $localeCode $this->localeContext->getLocaleCode();
  67.         if (false === strpos($slug$localeCode)) {
  68.             return $slug;
  69.         }
  70.         return str_replace(sprintf('%s/'$localeCode), ''$slug);
  71.     }
  72.     /**
  73.      * @throws Exception
  74.      *
  75.      * @return mixed
  76.      */
  77.     public function __call(string $name, array $arguments)
  78.     {
  79.         $callback = [$this->decorated$name];
  80.         if (\is_callable($callback)) {
  81.             return \call_user_func($callback$arguments);
  82.         }
  83.         throw new Exception(sprintf('Method %s not found for class "%s"'$name, \get_class($this->decorated)));
  84.     }
  85. }