[ Index ] |
PHP Cross Reference of phpBB-3.3.14-deutsch |
[Summary view] [Print] [Text view]
1 <?php 2 3 /* 4 * This file is part of the Symfony package. 5 * 6 * (c) Fabien Potencier <fabien@symfony.com> 7 * 8 * For the full copyright and license information, please view the LICENSE 9 * file that was distributed with this source code. 10 */ 11 12 namespace Symfony\Component\HttpKernel\EventListener; 13 14 use Symfony\Component\EventDispatcher\EventSubscriberInterface; 15 use Symfony\Component\HttpFoundation\Request; 16 use Symfony\Component\HttpFoundation\RequestStack; 17 use Symfony\Component\HttpKernel\Event\FinishRequestEvent; 18 use Symfony\Component\HttpKernel\Event\GetResponseEvent; 19 use Symfony\Component\HttpKernel\KernelEvents; 20 use Symfony\Component\Routing\RequestContextAwareInterface; 21 22 /** 23 * Initializes the locale based on the current request. 24 * 25 * @author Fabien Potencier <fabien@symfony.com> 26 */ 27 class LocaleListener implements EventSubscriberInterface 28 { 29 private $router; 30 private $defaultLocale; 31 private $requestStack; 32 33 /** 34 * @param RequestStack $requestStack A RequestStack instance 35 * @param string $defaultLocale The default locale 36 * @param RequestContextAwareInterface|null $router The router 37 */ 38 public function __construct(RequestStack $requestStack, $defaultLocale = 'en', RequestContextAwareInterface $router = null) 39 { 40 $this->defaultLocale = $defaultLocale; 41 $this->requestStack = $requestStack; 42 $this->router = $router; 43 } 44 45 public function onKernelRequest(GetResponseEvent $event) 46 { 47 $request = $event->getRequest(); 48 $request->setDefaultLocale($this->defaultLocale); 49 50 $this->setLocale($request); 51 $this->setRouterContext($request); 52 } 53 54 public function onKernelFinishRequest(FinishRequestEvent $event) 55 { 56 if (null !== $parentRequest = $this->requestStack->getParentRequest()) { 57 $this->setRouterContext($parentRequest); 58 } 59 } 60 61 private function setLocale(Request $request) 62 { 63 if ($locale = $request->attributes->get('_locale')) { 64 $request->setLocale($locale); 65 } 66 } 67 68 private function setRouterContext(Request $request) 69 { 70 if (null !== $this->router) { 71 $this->router->getContext()->setParameter('_locale', $request->getLocale()); 72 } 73 } 74 75 public static function getSubscribedEvents() 76 { 77 return [ 78 // must be registered after the Router to have access to the _locale 79 KernelEvents::REQUEST => [['onKernelRequest', 16]], 80 KernelEvents::FINISH_REQUEST => [['onKernelFinishRequest', 0]], 81 ]; 82 } 83 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Mon Nov 25 19:05:08 2024 | Cross-referenced by PHPXref 0.7.1 |