vendor/jms/i18n-routing-bundle/JMS/I18nRoutingBundle/EventListener/LocaleChoosingListener.php line 51

Open in your IDE?
  1. <?php
  2. /*
  3.  * Copyright 2012 Johannes M. Schmitt <schmittjoh@gmail.com>
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  * http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. namespace JMS\I18nRoutingBundle\EventListener;
  18. use JMS\I18nRoutingBundle\Router\LocaleResolverInterface;
  19. use Symfony\Component\HttpFoundation\RedirectResponse;
  20. use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
  21. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  22. use Symfony\Component\HttpKernel\HttpKernelInterface;
  23. use Symfony\Component\Routing\Exception\ResourceNotFoundException;
  24. /**
  25.  * Chooses the default locale.
  26.  *
  27.  * This listener chooses the default locale to use on the first request of a
  28.  * user to the application.
  29.  *
  30.  * This listener is only active if the strategy is "prefix".
  31.  *
  32.  * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  33.  */
  34. class LocaleChoosingListener
  35. {
  36.     private $defaultLocale;
  37.     private $locales;
  38.     private $localeResolver;
  39.     public function __construct($defaultLocale, array $localesLocaleResolverInterface $localeResolver)
  40.     {
  41.         $this->defaultLocale $defaultLocale;
  42.         $this->locales $locales;
  43.         $this->localeResolver $localeResolver;
  44.     }
  45.     public function onKernelException(GetResponseForExceptionEvent $event)
  46.     {
  47.         if (HttpKernelInterface::MASTER_REQUEST !== $event->getRequestType()) {
  48.             return;
  49.         }
  50.         $request $event->getRequest();
  51.         if ('' !== rtrim($request->getPathInfo(), '/')) {
  52.             return;
  53.         }
  54.         $ex $event->getException();
  55.         if (!$ex instanceof NotFoundHttpException || !$ex->getPrevious() instanceof ResourceNotFoundException) {
  56.             return;
  57.         }
  58.         $locale $this->localeResolver->resolveLocale($request$this->locales) ?: $this->defaultLocale;
  59.         $request->setLocale($locale);
  60.         $params $request->query->all();
  61.         unset($params['hl']);
  62.         $event->setResponse(new RedirectResponse($request->getBaseUrl().'/'.$locale.'/'.($params '?'.http_build_query($params) : '')));
  63.     }
  64. }