1 <?php
declare(strict_types
=1);
4 * This file is part of the Rapsys PackBundle package.
6 * (c) Raphaël Gertz <symfony@rapsys.eu>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Rapsys\PackBundle\Subscriber
;
14 use Symfony\Component\EventDispatcher\EventSubscriberInterface
;
15 use Symfony\Component\HttpKernel\Event\RequestEvent
;
16 use Symfony\Component\HttpKernel\KernelEvents
;
17 use Symfony\Component\Routing\RouterInterface
;
22 class FacebookSubscriber
implements EventSubscriberInterface
{
23 ///Supported locales array
30 * Inject router interface and locales
32 * @param RouterInterface $router The router instance
33 * @param array $locales The supported locales
35 public function __construct(RouterInterface
$router, array $locales) {
37 $this->locales
= $locales;
40 $this->router
= $router;
44 * Change locale for request with ?fb_locale=xx
46 * @param RequestEvent The request event
48 public function onKernelRequest(RequestEvent
$event): void {
49 //Without main request
50 if (!$event->isMainRequest()) {
55 $request = $event->getRequest();
57 //Check for facebook locale
59 $request->query
->has('fb_locale') &&
60 in_array($locale = $request->query
->get('fb_locale'), $this->locales
)
63 $request->setLocale($locale);
66 $request->setDefaultLocale($locale);
69 $context = $this->router
->getContext();
72 $context->setParameter('_locale', $locale);
74 //Set back router context
75 $this->router
->setContext($context);
80 * Get subscribed events
82 * @return array The subscribed events
84 public static function getSubscribedEvents(): array {
86 // must be registered before the default locale listener
87 KernelEvents
::REQUEST
=> [['onKernelRequest', 10]]