1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sludio\HelperBundle\Translatable\Router; |
4
|
|
|
|
5
|
|
|
use JMS\I18nRoutingBundle\Router\LocaleResolverInterface; |
|
|
|
|
6
|
|
|
use Symfony\Component\HttpFoundation\Request; |
7
|
|
|
|
8
|
|
|
class DefaultLocaleResolver implements LocaleResolverInterface |
9
|
|
|
{ |
10
|
|
|
private $cookieName; |
11
|
|
|
private $hostMap; |
12
|
|
|
|
13
|
|
|
public function __construct($cookieName, array $hostMap = []) |
14
|
|
|
{ |
15
|
|
|
$this->cookieName = $cookieName; |
16
|
|
|
$this->hostMap = $hostMap; |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* {@inheritdoc} |
21
|
|
|
* @throws \Symfony\Component\HttpFoundation\Exception\SuspiciousOperationException |
22
|
|
|
*/ |
23
|
|
|
public function resolveLocale(Request $request, array $availableLocales) |
24
|
|
|
{ |
25
|
|
|
if (!empty($this->hostMap) && isset($this->hostMap[$host = $request->getHost()])) { |
26
|
|
|
return $this->hostMap[$host]; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
$functions = [ |
30
|
|
|
'returnByParameter', |
31
|
|
|
'returnByPreviousSession', |
32
|
|
|
'returnByCookie', |
33
|
|
|
'returnByLang', |
34
|
|
|
]; |
35
|
|
|
|
36
|
|
|
foreach ($functions as $function) { |
37
|
|
|
if ($result = $this->{$function}($request, $availableLocales)) { |
38
|
|
|
return $result; |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
return null; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
protected function returnByParameter(Request $request) |
46
|
|
|
{ |
47
|
|
|
if ($request->query->has('hl')) { |
48
|
|
|
$hostLanguage = $request->query->get('hl'); |
49
|
|
|
|
50
|
|
|
if (preg_match('#^[a-z]{2}(?:_[a-z]{2})?$#i', $hostLanguage)) { |
51
|
|
|
return $hostLanguage; |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
protected function returnByPreviousSession(Request $request) |
57
|
|
|
{ |
58
|
|
|
if ($request->hasPreviousSession()) { |
59
|
|
|
$session = $request->getSession(); |
60
|
|
|
if ($session->has('_locale')) { |
61
|
|
|
return $session->get('_locale'); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
protected function returnByCookie(Request $request) |
67
|
|
|
{ |
68
|
|
|
if ($request->cookies->has($this->cookieName)) { |
69
|
|
|
$hostLanguage = $request->cookies->get($this->cookieName); |
70
|
|
|
|
71
|
|
|
if (preg_match('#^[a-z]{2}(?:_[a-z]{2})?$#i', $hostLanguage)) { |
72
|
|
|
return $hostLanguage; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
protected function returnByLang(Request $request, array $availableLocales) |
78
|
|
|
{ |
79
|
|
|
$languages = []; |
80
|
|
|
foreach ($request->getLanguages() as $language) { |
81
|
|
|
if (\strlen($language) !== 2) { |
82
|
|
|
$newLang = explode('_', $language, 2); |
83
|
|
|
$languages[] = reset($newLang); |
84
|
|
|
} else { |
85
|
|
|
$languages[] = $language; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
$languages = array_unique($languages); |
89
|
|
|
if (!empty($languages)) { |
90
|
|
|
foreach ($languages as $lang) { |
91
|
|
|
if (\in_array($lang, $availableLocales, true)) { |
92
|
|
|
return $lang; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths