vendor/monsieurbiz/sylius-cms-page-plugin/src/Context/LastChanceLocaleContext.php line 35

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\Context;
  12. use Sylius\Component\Locale\Context\LocaleContextInterface;
  13. use Sylius\Component\Locale\Context\LocaleNotFoundException;
  14. use Sylius\Component\Locale\Provider\LocaleProviderInterface;
  15. use Symfony\Component\HttpFoundation\RequestStack;
  16. final class LastChanceLocaleContext implements LocaleContextInterface
  17. {
  18.     private RequestStack $requestStack;
  19.     private LocaleProviderInterface $localeProvider;
  20.     public function __construct(
  21.         RequestStack $requestStack,
  22.         LocaleProviderInterface $localeProvider
  23.     ) {
  24.         $this->requestStack $requestStack;
  25.         $this->localeProvider $localeProvider;
  26.     }
  27.     public function getLocaleCode(): string
  28.     {
  29.         $request $this->requestStack->getMasterRequest();
  30.         if (null === $request) {
  31.             throw new LocaleNotFoundException('Main request not found, therefore no locale found…');
  32.         }
  33.         $pathInfo $request->getPathInfo();
  34.         $availableLocaleCodes $this->localeProvider->getAvailableLocalesCodes();
  35.         $parts explode('/'trim($pathInfo'/'));
  36.         foreach ($parts as $part) {
  37.             if (\in_array($part$availableLocaleCodestrue)) {
  38.                 return $part;
  39.             }
  40.         }
  41.         return $this->localeProvider->getDefaultLocaleCode();
  42.     }
  43. }