vendor/sylius/sylius/src/Sylius/Bundle/ShopBundle/Controller/LocaleSwitchController.php line 35

Open in your IDE?
  1. <?php
  2. /*
  3. * This file is part of the Sylius package.
  4. *
  5. * (c) Paweł Jędrzejewski
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. declare(strict_types=1);
  11. namespace Sylius\Bundle\ShopBundle\Controller;
  12. use Sylius\Bundle\ShopBundle\Locale\LocaleSwitcherInterface;
  13. use Sylius\Component\Locale\Context\LocaleContextInterface;
  14. use Sylius\Component\Locale\Provider\LocaleProviderInterface;
  15. use Symfony\Component\HttpFoundation\Request;
  16. use Symfony\Component\HttpFoundation\Response;
  17. use Symfony\Component\HttpKernel\Exception\HttpException;
  18. use Symfony\Component\Templating\EngineInterface;
  19. use Twig\Environment;
  20. final class LocaleSwitchController
  21. {
  22. public function __construct(
  23. private EngineInterface|Environment $templatingEngine,
  24. private LocaleContextInterface $localeContext,
  25. private LocaleProviderInterface $localeProvider,
  26. private LocaleSwitcherInterface $localeSwitcher,
  27. ) {
  28. }
  29. public function renderAction(): Response
  30. {
  31. return new Response($this->templatingEngine->render('@SyliusShop/Menu/_localeSwitch.html.twig', [
  32. 'active' => $this->localeContext->getLocaleCode(),
  33. 'locales' => $this->localeProvider->getAvailableLocalesCodes(),
  34. ]));
  35. }
  36. public function switchAction(Request $request, ?string $code = null): Response
  37. {
  38. if (null === $code) {
  39. $code = $this->localeProvider->getDefaultLocaleCode();
  40. }
  41. if (!in_array($code, $this->localeProvider->getAvailableLocalesCodes(), true)) {
  42. throw new HttpException(
  43. Response::HTTP_NOT_ACCEPTABLE,
  44. sprintf('The locale code "%s" is invalid.', $code),
  45. );
  46. }
  47. return $this->localeSwitcher->handle($request, $code);
  48. }
  49. }