1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Braunstetter\LocalizedRoutes\EventSubscriber; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface; |
7
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
8
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
9
|
|
|
use Symfony\Component\HttpKernel\Event\RequestEvent; |
10
|
|
|
use Symfony\Component\Routing\Route; |
11
|
|
|
use Symfony\Component\Routing\RouteCollection; |
12
|
|
|
use Symfony\Component\Routing\RouterInterface; |
13
|
|
|
use Symfony\Component\VarDumper\VarDumper; |
14
|
|
|
|
15
|
|
|
class LocaleRewriteSubscriber implements EventSubscriberInterface |
16
|
|
|
{ |
17
|
|
|
|
18
|
|
|
private RouteCollection $routeCollection; |
19
|
|
|
private array $supportedLocales; |
20
|
|
|
private mixed $localeRouteParam; |
21
|
|
|
|
22
|
3 |
|
public function __construct(RouterInterface $router, ParameterBagInterface $parameterBag, $localeRouteParam = '_locale') |
23
|
|
|
{ |
24
|
|
|
$this->routeCollection = $router->getRouteCollection(); |
25
|
|
|
$this->supportedLocales = explode('|', $parameterBag->get('app_locales')); |
26
|
|
|
$this->localeRouteParam = $localeRouteParam; |
27
|
|
|
} |
28
|
3 |
|
|
29
|
3 |
|
public function onKernelRequest(RequestEvent $event) |
30
|
3 |
|
{ |
31
|
|
|
$request = $event->getRequest(); |
32
|
|
|
$path = $request->getPathInfo(); |
33
|
3 |
|
|
34
|
|
|
$route_exists = false; //by default assume route does not exist. |
35
|
3 |
|
|
36
|
3 |
|
foreach ($this->routeCollection as $routeObject) { |
37
|
|
|
/** @var Route $routeObject */ |
38
|
3 |
|
$routePath = $routeObject->getPath(); |
39
|
|
|
|
40
|
3 |
|
if ($routePath == "/{" . $this->localeRouteParam . "}" . $path) { |
41
|
|
|
$route_exists = true; |
42
|
3 |
|
break; |
43
|
|
|
} |
44
|
3 |
|
} |
45
|
2 |
|
|
46
|
2 |
|
//If the route does indeed exist then lets redirect there. |
47
|
|
|
if ($route_exists) { |
48
|
|
|
|
49
|
|
|
//Get the locale from the user's browser. |
50
|
|
|
$locale = $request->getPreferredLanguage() ? explode('_', $request->getPreferredLanguage())[0] : null; |
51
|
3 |
|
|
52
|
|
|
//If no locale from browser or locale not in list of known locales supported then set to defaultLocale set in config.yml |
53
|
2 |
|
if (!$locale || $locale == "" || !$this->isLocaleSupported($locale)) { |
54
|
|
|
$locale = $request->getDefaultLocale(); |
55
|
2 |
|
} |
56
|
1 |
|
|
57
|
|
|
$event->setResponse(new RedirectResponse("/" . $locale . $path)); |
58
|
|
|
|
59
|
2 |
|
} |
60
|
|
|
|
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function isLocaleSupported($locale): bool |
64
|
2 |
|
{ |
65
|
|
|
$supportedLocale = array_filter($this->supportedLocales, function($supportedLocale) use ($locale) { |
66
|
2 |
|
return in_array($supportedLocale, explode('_', $locale)); |
67
|
2 |
|
}); |
68
|
|
|
|
69
|
|
|
return !empty($supportedLocale); |
70
|
2 |
|
} |
71
|
|
|
|
72
|
|
|
public static function getSubscribedEvents(): array |
73
|
1 |
|
{ |
74
|
|
|
return [ |
75
|
|
|
'kernel.request' => 'onKernelRequest', |
76
|
1 |
|
]; |
77
|
|
|
} |
78
|
|
|
} |