From 4359c2159a133527cc2aedf9ae1cdf573d49ca72 Mon Sep 17 00:00:00 2001 From: Julio Montoya Date: Fri, 29 Jan 2021 15:09:27 +0100 Subject: [PATCH] Update LocaleSubscriber.php --- .../LocaleSubscriber.php} | 31 ++++++++----------- .../UserLocaleSubscriber.php} | 22 +++++++------ src/CoreBundle/Resources/config/services.yml | 10 +++--- 3 files changed, 30 insertions(+), 33 deletions(-) rename src/CoreBundle/{EventListener/LocaleListener.php => EventSubscriber/LocaleSubscriber.php} (88%) rename src/CoreBundle/{EventListener/UserLocaleListener.php => EventSubscriber/UserLocaleSubscriber.php} (65%) diff --git a/src/CoreBundle/EventListener/LocaleListener.php b/src/CoreBundle/EventSubscriber/LocaleSubscriber.php similarity index 88% rename from src/CoreBundle/EventListener/LocaleListener.php rename to src/CoreBundle/EventSubscriber/LocaleSubscriber.php index 53f93a3573..1e439ea7c0 100644 --- a/src/CoreBundle/EventListener/LocaleListener.php +++ b/src/CoreBundle/EventSubscriber/LocaleSubscriber.php @@ -2,7 +2,7 @@ /* For licensing terms, see /license.txt */ -namespace Chamilo\CoreBundle\EventListener; +namespace Chamilo\CoreBundle\EventSubscriber; use Chamilo\CoreBundle\Entity\Course; use Chamilo\CoreBundle\Manager\SettingsManager; @@ -12,26 +12,20 @@ use Symfony\Component\HttpKernel\Event\RequestEvent; use Symfony\Component\HttpKernel\KernelEvents; /** - * Class LocaleListener * Checks the portal listener depending of different settings: * platform, user, course. */ -class LocaleListener implements EventSubscriberInterface +class LocaleSubscriber implements EventSubscriberInterface { - protected $settingsManager; protected $defaultLocale; - protected $paremeterBag; - - /** - * LocaleListener constructor. - * - * @param string $defaultLocale - */ - public function __construct($defaultLocale, SettingsManager $settingsManager, ParameterBagInterface $paremeterBag) + protected $parameterBag; + protected $settingsManager; + + public function __construct(string $defaultLocale, SettingsManager $settingsManager, ParameterBagInterface $parameterBag) { $this->defaultLocale = $defaultLocale; $this->settingsManager = $settingsManager; - $this->paremeterBag = $paremeterBag; + $this->parameterBag = $parameterBag; } public function onKernelRequest(RequestEvent $event) @@ -40,12 +34,16 @@ class LocaleListener implements EventSubscriberInterface if (!$request->hasPreviousSession()) { return; } - $installed = 1 === (int) $this->paremeterBag->get('installed'); + $installed = 1 === (int) $this->parameterBag->get('installed'); if (!$installed) { return; } + if (false === $request->hasSession()) { + return; + } + $loadFromDb = $request->getSession()->get('check_locale_from_db', true); if (false === $loadFromDb && @@ -142,14 +140,11 @@ class LocaleListener implements EventSubscriberInterface } } - /** - * @return array - */ public static function getSubscribedEvents() { return [ // must be registered before the default Locale listener - KernelEvents::REQUEST => [['onKernelRequest', 15]], + KernelEvents::REQUEST => [['onKernelRequest', 20]], ]; } } diff --git a/src/CoreBundle/EventListener/UserLocaleListener.php b/src/CoreBundle/EventSubscriber/UserLocaleSubscriber.php similarity index 65% rename from src/CoreBundle/EventListener/UserLocaleListener.php rename to src/CoreBundle/EventSubscriber/UserLocaleSubscriber.php index 3a523ce272..9887bce32e 100644 --- a/src/CoreBundle/EventListener/UserLocaleListener.php +++ b/src/CoreBundle/EventSubscriber/UserLocaleSubscriber.php @@ -2,28 +2,24 @@ /* For licensing terms, see /license.txt */ -namespace Chamilo\CoreBundle\EventListener; +namespace Chamilo\CoreBundle\EventSubscriber; +use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\HttpFoundation\Session\SessionInterface; use Symfony\Component\Security\Http\Event\InteractiveLoginEvent; +use Symfony\Component\Security\Http\SecurityEvents; /** - * Class UserLocaleListener. - * * Stores the locale of the user in the session after the - * login. This can be used by the LocaleListener afterwards. + * login. This can be used by the LocaleSubscriber afterwards. * * Priority order: platform -> user * Priority order: platform -> user -> course */ -class UserLocaleListener +class UserLocaleSubscriber implements EventSubscriberInterface { - /** @var SessionInterface */ private $session; - /** - * UserLocaleListener constructor. - */ public function __construct(SessionInterface $session) { $this->session = $session; @@ -35,9 +31,17 @@ class UserLocaleListener public function onInteractiveLogin(InteractiveLoginEvent $event) { $user = $event->getAuthenticationToken()->getUser(); + if (null !== $user->getLocale()) { $this->session->set('_locale', $user->getLocale()); $this->session->set('_locale_user', $user->getLocale()); } } + + public static function getSubscribedEvents() + { + return [ + SecurityEvents::INTERACTIVE_LOGIN => 'onInteractiveLogin', + ]; + } } diff --git a/src/CoreBundle/Resources/config/services.yml b/src/CoreBundle/Resources/config/services.yml index 70ccdaced9..3589ac3827 100644 --- a/src/CoreBundle/Resources/config/services.yml +++ b/src/CoreBundle/Resources/config/services.yml @@ -152,15 +152,13 @@ services: - {name: kernel.event_listener, event: kernel.controller, method: onKernelController} # Locale listener - Chamilo\CoreBundle\EventListener\LocaleListener: + Chamilo\CoreBundle\EventSubscriber\LocaleSubscriber: arguments: ['%kernel.default_locale%', '@chamilo.settings.manager', '@parameter_bag'] - tags: - - {name: kernel.event_subscriber, priority: 15} # User locale listener - Chamilo\CoreBundle\EventListener\UserLocaleListener: - tags: - - {name: kernel.event_listener, event: security.interactive_login, method: onInteractiveLogin, priority: 15} +# Chamilo\CoreBundle\EventListener\UserLocaleListener: +# tags: +# - {name: kernel.event_listener, event: security.interactive_login, method: onInteractiveLogin, priority: 15} # Settings listener Chamilo\CoreBundle\EventListener\SettingListener: