src/CmsBundle/EventListener/LocaleListener.php line 20

Open in your IDE?
  1. <?php
  2. namespace App\CmsBundle\EventListener;
  3. use Symfony\Component\HttpKernel\Event\RequestEvent;
  4. use Symfony\Component\HttpKernel\KernelEvents;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. class LocaleListener implements EventSubscriberInterface
  8. {
  9.     private $defaultLocale;
  10.     private $em;
  11.     public function __construct(EntityManagerInterface $emstring $defaultLocale 'nl')
  12.     {
  13.         $this->defaultLocale $defaultLocale;
  14.         $this->em $em;
  15.     }
  16.     public function onKernelRequest(RequestEvent $event)
  17.     {
  18.         $request $event->getRequest();
  19.         if (!$request->hasPreviousSession()) {
  20.             return;
  21.         }
  22.         /*if (!preg_match('/\/admin/', $_SERVER['REQUEST_URI'])) {
  23.             $language = null;
  24.             $languages = $this->em->getRepository('CmsBundle:Language')->findAll();
  25.             $Settings = $this->em->getRepository('CmsBundle:Settings')->findOneBy(['host' => str_replace('www.', '', $request->getHttpHost())]);
  26.             if(!empty($Settings) && !preg_match('/(^admin_|^admin$)/', $request->get('_route'))){
  27.                 $Settings = $Settings;
  28.                 $language = $Settings->getLanguage();
  29.             }else{
  30.                 $locale = $request->getSession()->get('_locale');
  31.                 if($locale){
  32.                     $language = $this->em->getRepository('CmsBundle:Language')->findOneByLocale($locale);
  33.                 }else{
  34.                     $language = null;
  35.                 }
  36.             }
  37.         }
  38.         if(!preg_match('/\/admin/', $_SERVER['REQUEST_URI']) && $language){
  39.             $request->getSession()->set('_locale', $language->getLocale());
  40.             $request->setLocale($language->getLocale());
  41.         }else{*/
  42.             $isAdmin preg_match('/(^admin_|^admin$)/'$request->get('_route'));
  43.             $session_name '_locale';
  44.             if($isAdmin){
  45.                 $session_name 'admin_locale';
  46.             }
  47.             $break false;
  48.             if($isAdmin){
  49.                 // Check is custom admin locale is enabled
  50.                 if (($locale $request->getSession()->get('admin_custom_locale')) && !empty($locale)) {
  51.                     $request->setLocale($locale);
  52.                     $break true;
  53.                 }
  54.             }
  55.             // try to see if the locale has been set as a _locale routing parameter
  56.             if(!$break){
  57.                 if (($locale $request->attributes->get($session_name)) !== null) {
  58.                     $request->getSession()->set($session_name$locale);
  59.                 } elseif ($locale $request->getSession()->get($session_name)) {
  60.                     $locale $request->getSession()->get($session_name);
  61.                     $request->setLocale($locale);
  62.                 } elseif ($locale $request->attributes->get('locale')) {
  63.                     // Fallback for Trinity page loader
  64.                     $request->getSession()->set($session_name$locale);
  65.                     $request->setLocale($locale);
  66.                 } else {
  67.                     // if no explicit locale has been set on this request, use one from the session
  68.                     $request->setLocale($request->getSession()->get($session_name$this->defaultLocale));
  69.                 }
  70.             }
  71.         // }
  72.     }
  73.     public static function getSubscribedEvents()
  74.     {
  75.         return array(
  76.             // must be registered after the default Locale listener
  77.             KernelEvents::REQUEST => array(array('onKernelRequest'15)),
  78.             KernelEvents::EXCEPTION => [['onKernelRequest'10]],
  79.         );
  80.     }
  81. }