1 | <?php |
||
2 | |||
3 | namespace Fomvasss\UrlAliases; |
||
4 | |||
5 | use Fomvasss\UrlAliases\Traits\Localization; |
||
6 | |||
7 | class UrlAliasLocalization |
||
8 | { |
||
9 | use Localization; |
||
10 | |||
11 | protected $app; |
||
12 | |||
13 | protected $config; |
||
14 | |||
15 | protected $defaultLocale; |
||
16 | |||
17 | protected $supportedLocales; |
||
18 | |||
19 | protected $currentLocale; |
||
20 | |||
21 | /** |
||
22 | * Localization constructor. |
||
23 | * @param $app |
||
24 | */ |
||
25 | public function __construct($app) |
||
26 | { |
||
27 | if (!$app) { |
||
28 | $app = app(); //Fallback when $app is not given |
||
29 | } |
||
30 | $this->app = $app; |
||
31 | |||
32 | $this->config = $this->app['config']; |
||
33 | |||
34 | $this->defaultLocale = $this->getDefaultLocale(); |
||
35 | |||
36 | $this->config->set('url-aliases-laravellocalization.origin_default_locale', $this->defaultLocale); // virtual temporary config |
||
37 | |||
38 | $this->currentLocale = $this->defaultLocale; |
||
39 | |||
40 | $this->supportedLocales = $this->getSupportedLocales(); |
||
41 | |||
42 | if (empty($this->supportedLocales[$this->defaultLocale])) { |
||
43 | throw new \Exception('Laravel default locale is not in the supportedLocales array.'); |
||
44 | } |
||
45 | } |
||
46 | |||
47 | /** |
||
48 | * TODO: remove $segment1 |
||
49 | * @param $path |
||
50 | * @param $segment1 |
||
51 | * @return \Illuminate\Http\RedirectResponse|string |
||
52 | */ |
||
53 | public function prepareLocalizePath(string $path): array |
||
54 | { |
||
55 | // session()->put('locale', $this->defaultLocale); |
||
56 | $segment1 = url_path_segments($path, 1); |
||
57 | |||
58 | if (key_exists($segment1, $this->supportedLocales)) { |
||
59 | // session()->put('locale', $segment1); |
||
60 | |||
61 | $path = preg_replace("/^$segment1\\//", "", "$path"); |
||
62 | |||
63 | if ($this->hideDefaultLocaleInURL() && $segment1 === $this->defaultLocale) { |
||
64 | $path = ($path == $segment1) ? '/' : $path; |
||
65 | return ['redirect' => $path]; |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||
66 | } |
||
67 | |||
68 | if ($key = $this->config->get('url-aliases.use_key_get_param_localization')) { |
||
69 | $this->currentLocale = request($key, $segment1); |
||
70 | } else { |
||
71 | $this->currentLocale = $segment1; |
||
72 | } |
||
73 | $this->app->setLocale($segment1); |
||
74 | } |
||
75 | |||
76 | // Regional locale such as de_DE, so formatLocalized works in Carbon |
||
77 | $regional = $this->getCurrentLocaleRegional(); |
||
78 | $suffix = $this->config->get('url-aliases.laravellocalization.utf8suffix'); |
||
79 | if ($regional) { |
||
80 | setlocale(LC_TIME, $regional . $suffix); |
||
81 | setlocale(LC_MONETARY, $regional . $suffix); |
||
82 | } |
||
83 | |||
84 | return ['path' => $path]; |
||
0 ignored issues
–
show
|
|||
85 | } |
||
86 | |||
87 | /** |
||
88 | * @return bool |
||
89 | */ |
||
90 | protected function hideDefaultLocaleInURL() |
||
91 | { |
||
92 | return $this->config->get('url-aliases-laravellocalization.hideDefaultLocaleInURL'); |
||
93 | } |
||
94 | |||
95 | /** |
||
96 | * @return \Illuminate\Contracts\Routing\UrlGenerator|string |
||
97 | */ |
||
98 | public function getRoot() |
||
99 | { |
||
100 | return url($this->currentLocale); |
||
101 | } |
||
102 | |||
103 | /** |
||
104 | * @param string $default |
||
105 | * @param bool $absolute |
||
106 | * @return array |
||
107 | */ |
||
108 | public function getCurrentBound(string $default = '', $absolute = true) |
||
109 | { |
||
110 | $bound = request()->server('ALIAS_LOCALE_BOUND'); |
||
111 | |||
112 | $modelClass = $this->config->get('url-aliases.model', \Fomvasss\UrlAliases\Models\UrlAlias::class); |
||
113 | $aliasModels = $modelClass::whereNotNull('locale_bound')->where('locale_bound', $bound)->get(); |
||
114 | |||
115 | $res = $this->supportedLocales; |
||
116 | foreach ($this->supportedLocales as $key => $item) { |
||
117 | if ($modelClass = $aliasModels->where('locale', $key)->first()) { |
||
118 | $link = $modelClass->localeAlias; |
||
119 | } elseif($default) { |
||
120 | $link = $default; |
||
121 | } else { |
||
122 | $link = $key; |
||
123 | } |
||
124 | $res[$key]['url'] = $absolute ? url($link) : $link; |
||
125 | } |
||
126 | |||
127 | return $res; |
||
128 | } |
||
129 | |||
130 | /** |
||
131 | * @param null $bound |
||
132 | * @param bool $absolute |
||
133 | * @return array |
||
134 | */ |
||
135 | public function getLocalesModelsBound($bound = null, $absolute = true) |
||
136 | { |
||
137 | $modelClass = $this->config->get('url-aliases.model', \Fomvasss\UrlAliases\Models\UrlAlias::class); |
||
138 | $aliasModels = $modelClass::whereNotNull('locale_bound')->where('locale_bound', $bound)->get(); |
||
139 | |||
140 | $res = $this->supportedLocales; |
||
141 | foreach ($this->supportedLocales as $key => $item) { |
||
142 | $res[$key]['model'] = null; |
||
143 | if ($model = $aliasModels->where('locale', $key)->first()) { |
||
144 | $link = $model->localeAlias; |
||
145 | $res[$key]['model'] = $model->aliasable; |
||
146 | } else { |
||
147 | $link = $key; |
||
148 | } |
||
149 | $res[$key]['url'] = $absolute ? url($link) : $link; |
||
150 | } |
||
151 | |||
152 | return $res; |
||
153 | } |
||
154 | |||
155 | /** |
||
156 | * @param string $localeKey |
||
157 | * @param string|null $bound |
||
158 | * @return mixed|null |
||
159 | */ |
||
160 | public function getLocaleModelBound(string $localeKey, ?string $bound = null) |
||
161 | { |
||
162 | if ($bound) { |
||
163 | $modelClass = $this->config->get('url-aliases.model', \Fomvasss\UrlAliases\Models\UrlAlias::class); |
||
164 | |||
165 | return optional($modelClass::whereNotNull('locale_bound') |
||
166 | ->where('locale_bound', $bound) |
||
167 | ->where('locale', $localeKey) |
||
168 | ->first())->aliasable; |
||
169 | } |
||
170 | |||
171 | return null; |
||
172 | } |
||
173 | |||
174 | |||
175 | /** |
||
176 | * @param string $valueField [key, name, native] |
||
177 | * @return array |
||
178 | */ |
||
179 | public function getSupportedLocalesForSelect(string $valueField = 'key') |
||
180 | { |
||
181 | $res = []; |
||
182 | foreach ($this->supportedLocales as $key => $value) { |
||
183 | $res[$key] = $valueField === 'key' ? $key : ($value[$valueField] ?? $key); |
||
184 | } |
||
185 | |||
186 | return $res; |
||
187 | } |
||
188 | } |