3 namespace Rapsys\AirBundle\EventSubscriber
;
5 use Symfony\Component\EventDispatcher\EventSubscriberInterface
;
6 use Symfony\Component\HttpKernel\Event\ControllerEvent
;
7 use Symfony\Component\HttpKernel\Event\RequestEvent
;
8 use Symfony\Component\HttpKernel\Event\ResponseEvent
;
9 use Symfony\Component\HttpKernel\KernelEvents
;
10 use Symfony\Component\Routing\RouterInterface
;
12 class FacebookSubscriber
implements EventSubscriberInterface
{
13 ///Supported locales array
20 * Inject router interface and locales
22 * @param RouterInterface $router The router instance
23 * @param array $locales The supported locales
25 public function __construct(RouterInterface
$router, array $locales) {
27 $this->locales
= $locales;
30 $this->router
= $router;
34 * Change locale for request with ?fb_locale=xx
36 * @param RequestEvent The request event
38 public function onKernelRequest(RequestEvent
$event) {
40 $request = $event->getRequest();
42 //Check for facebook locale
44 $request->query
->has('fb_locale') &&
45 in_array($preferred = $request->query
->get('fb_locale'), $this->locales
)
48 $request->setLocale($preferred);
51 $request->setDefaultLocale($preferred);
54 $context = $this->router
->getContext();
57 $context->setParameter('_locale', $preferred);
59 //Set back router context
60 $this->router
->setContext($context);
65 * Get subscribed events
67 * @return array The subscribed events
69 public static function getSubscribedEvents() {
71 // must be registered before the default locale listener
72 KernelEvents
::REQUEST
=> [['onKernelRequest', 10]]