]> Raphaƫl G. Git Repositories - airbundle/blob - EventSubscriber/FacebookSubscriber.php
Add findByUserId function
[airbundle] / EventSubscriber / FacebookSubscriber.php
1 <?php
2
3 namespace Rapsys\AirBundle\EventSubscriber;
4
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;
11
12 class FacebookSubscriber implements EventSubscriberInterface {
13 ///Supported locales array
14 private $locales;
15
16 ///Router instance
17 private $router;
18
19 /*
20 * Inject router interface and locales
21 *
22 * @param RouterInterface $router The router instance
23 * @param array $locales The supported locales
24 */
25 public function __construct(RouterInterface $router, array $locales) {
26 //Set locales
27 $this->locales = $locales;
28
29 //Set router
30 $this->router = $router;
31 }
32
33 /**
34 * Change locale for request with ?fb_locale=xx
35 *
36 * @param RequestEvent The request event
37 */
38 public function onKernelRequest(RequestEvent $event) {
39 //Retrieve request
40 $request = $event->getRequest();
41
42 //Check for facebook locale
43 if (
44 $request->query->has('fb_locale') &&
45 in_array($preferred = $request->query->get('fb_locale'), $this->locales)
46 ) {
47 //Set locale
48 $request->setLocale($preferred);
49
50 //Set default locale
51 $request->setDefaultLocale($preferred);
52
53 //Get router context
54 $context = $this->router->getContext();
55
56 //Set context locale
57 $context->setParameter('_locale', $preferred);
58
59 //Set back router context
60 $this->router->setContext($context);
61 }
62 }
63
64 /**
65 * Get subscribed events
66 *
67 * @return array The subscribed events
68 */
69 public static function getSubscribedEvents() {
70 return [
71 // must be registered before the default locale listener
72 KernelEvents::REQUEST => [['onKernelRequest', 10]]
73 ];
74 }
75 }