diff options
author | Jeremy Benoist <jeremy.benoist@gmail.com> | 2016-10-30 09:58:39 +0100 |
---|---|---|
committer | Jeremy Benoist <jeremy.benoist@gmail.com> | 2016-10-30 09:58:39 +0100 |
commit | 535bfcbe80de5d697b768c3a657214fdeff0eac3 (patch) | |
tree | ab10a21bc5358344e29ee54c17a5a404d6cd7ada /src/Wallabag/CoreBundle/Event/Listener | |
parent | 156bf62758080153668a65db611c4241d0fc8a00 (diff) | |
download | wallabag-535bfcbe80de5d697b768c3a657214fdeff0eac3.tar.gz wallabag-535bfcbe80de5d697b768c3a657214fdeff0eac3.tar.zst wallabag-535bfcbe80de5d697b768c3a657214fdeff0eac3.zip |
Move related event things in Event folder
Diffstat (limited to 'src/Wallabag/CoreBundle/Event/Listener')
-rw-r--r-- | src/Wallabag/CoreBundle/Event/Listener/LocaleListener.php | 44 | ||||
-rw-r--r-- | src/Wallabag/CoreBundle/Event/Listener/UserLocaleListener.php | 37 |
2 files changed, 81 insertions, 0 deletions
diff --git a/src/Wallabag/CoreBundle/Event/Listener/LocaleListener.php b/src/Wallabag/CoreBundle/Event/Listener/LocaleListener.php new file mode 100644 index 00000000..b435d99e --- /dev/null +++ b/src/Wallabag/CoreBundle/Event/Listener/LocaleListener.php | |||
@@ -0,0 +1,44 @@ | |||
1 | <?php | ||
2 | |||
3 | namespace Wallabag\CoreBundle\Event\Listener; | ||
4 | |||
5 | use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
6 | use Symfony\Component\HttpKernel\Event\GetResponseEvent; | ||
7 | use Symfony\Component\HttpKernel\KernelEvents; | ||
8 | |||
9 | /** | ||
10 | * @see http://symfony.com/doc/current/cookbook/session/locale_sticky_session.html | ||
11 | */ | ||
12 | class LocaleListener implements EventSubscriberInterface | ||
13 | { | ||
14 | private $defaultLocale; | ||
15 | |||
16 | public function __construct($defaultLocale = 'en') | ||
17 | { | ||
18 | $this->defaultLocale = $defaultLocale; | ||
19 | } | ||
20 | |||
21 | public function onKernelRequest(GetResponseEvent $event) | ||
22 | { | ||
23 | $request = $event->getRequest(); | ||
24 | if (!$request->hasPreviousSession()) { | ||
25 | return; | ||
26 | } | ||
27 | |||
28 | // try to see if the locale has been set as a _locale routing parameter | ||
29 | if ($locale = $request->attributes->get('_locale')) { | ||
30 | $request->getSession()->set('_locale', $locale); | ||
31 | } else { | ||
32 | // if no explicit locale has been set on this request, use one from the session | ||
33 | $request->setLocale($request->getSession()->get('_locale', $this->defaultLocale)); | ||
34 | } | ||
35 | } | ||
36 | |||
37 | public static function getSubscribedEvents() | ||
38 | { | ||
39 | return [ | ||
40 | // must be registered before the default Locale listener | ||
41 | KernelEvents::REQUEST => [['onKernelRequest', 17]], | ||
42 | ]; | ||
43 | } | ||
44 | } | ||
diff --git a/src/Wallabag/CoreBundle/Event/Listener/UserLocaleListener.php b/src/Wallabag/CoreBundle/Event/Listener/UserLocaleListener.php new file mode 100644 index 00000000..367cdfb0 --- /dev/null +++ b/src/Wallabag/CoreBundle/Event/Listener/UserLocaleListener.php | |||
@@ -0,0 +1,37 @@ | |||
1 | <?php | ||
2 | |||
3 | namespace Wallabag\CoreBundle\Event\Listener; | ||
4 | |||
5 | use Symfony\Component\HttpFoundation\Session\Session; | ||
6 | use Symfony\Component\Security\Http\Event\InteractiveLoginEvent; | ||
7 | |||
8 | /** | ||
9 | * Stores the locale of the user in the session after the | ||
10 | * login. This can be used by the LocaleListener afterwards. | ||
11 | * | ||
12 | * @see http://symfony.com/doc/master/cookbook/session/locale_sticky_session.html | ||
13 | */ | ||
14 | class UserLocaleListener | ||
15 | { | ||
16 | /** | ||
17 | * @var Session | ||
18 | */ | ||
19 | private $session; | ||
20 | |||
21 | public function __construct(Session $session) | ||
22 | { | ||
23 | $this->session = $session; | ||
24 | } | ||
25 | |||
26 | /** | ||
27 | * @param InteractiveLoginEvent $event | ||
28 | */ | ||
29 | public function onInteractiveLogin(InteractiveLoginEvent $event) | ||
30 | { | ||
31 | $user = $event->getAuthenticationToken()->getUser(); | ||
32 | |||
33 | if (null !== $user->getConfig()->getLanguage()) { | ||
34 | $this->session->set('_locale', $user->getConfig()->getLanguage()); | ||
35 | } | ||
36 | } | ||
37 | } | ||