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