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
{
24 * Inject router interface and locales
26 * @param RouterInterface $router The router instance
27 * @param array $locales The supported locales
29 public function __construct(protected RouterInterface
$router, protected array $locales) {
33 * Change locale for request with ?fb_locale=xx
35 * @param RequestEvent The request event
37 public function onKernelRequest(RequestEvent
$event): void {
38 //Without main request
39 if (!$event->isMainRequest()) {
44 $request = $event->getRequest();
46 //Check for facebook locale
48 $request->query
->has('fb_locale') &&
49 in_array($locale = $request->query
->get('fb_locale'), $this->locales
)
52 $request->setLocale($locale);
55 $request->setDefaultLocale($locale);
58 $context = $this->router
->getContext();
61 $context->setParameter('_locale', $locale);
63 //Set back router context
64 $this->router
->setContext($context);
69 * Get subscribed events
71 * @return array The subscribed events
73 public static function getSubscribedEvents(): array {
75 // must be registered before the default locale listener
76 KernelEvents
::REQUEST
=> [['onKernelRequest', 10]]