1
|
|
|
<?php namespace Arcanedev\Localization\Bases; |
2
|
|
|
|
3
|
|
|
use Arcanedev\Localization\Entities\LocaleCollection; |
4
|
|
|
use Arcanedev\Localization\Exceptions\UndefinedSupportedLocalesException; |
5
|
|
|
use Arcanedev\Support\Bases\Middleware as BaseMiddleware; |
6
|
|
|
use Illuminate\Http\RedirectResponse; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class Middleware |
10
|
|
|
* |
11
|
|
|
* @package Arcanedev\Localization\Bases |
12
|
|
|
* @author ARCANEDEV <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
abstract class Middleware extends BaseMiddleware |
15
|
|
|
{ |
16
|
|
|
/* ------------------------------------------------------------------------------------------------ |
17
|
|
|
| Properties |
18
|
|
|
| ------------------------------------------------------------------------------------------------ |
19
|
|
|
*/ |
20
|
|
|
/** |
21
|
|
|
* The URIs that should not be localized. |
22
|
|
|
* |
23
|
|
|
* @var array |
24
|
|
|
*/ |
25
|
|
|
protected $except = []; |
26
|
|
|
|
27
|
|
|
/* ------------------------------------------------------------------------------------------------ |
28
|
|
|
| Getters & Setters |
29
|
|
|
| ------------------------------------------------------------------------------------------------ |
30
|
|
|
*/ |
31
|
|
|
/** |
32
|
|
|
* Get the default locale. |
33
|
|
|
* |
34
|
|
|
* @return string |
35
|
|
|
*/ |
36
|
15 |
|
public function getDefaultLocale() |
37
|
|
|
{ |
38
|
15 |
|
return localization()->getDefaultLocale(); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Get the current locale. |
43
|
|
|
* |
44
|
|
|
* @return string |
45
|
|
|
*/ |
46
|
9 |
|
public function getCurrentLocale() |
47
|
|
|
{ |
48
|
9 |
|
return localization()->getCurrentLocale(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Return an array of all supported Locales. |
53
|
|
|
* |
54
|
|
|
* @throws UndefinedSupportedLocalesException |
55
|
|
|
* |
56
|
|
|
* @return LocaleCollection |
57
|
|
|
*/ |
58
|
9 |
|
public function getSupportedLocales() |
59
|
|
|
{ |
60
|
9 |
|
return localization()->getSupportedLocales(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Hide the default locale in URL ?? |
65
|
|
|
* |
66
|
|
|
* @return bool |
67
|
|
|
*/ |
68
|
9 |
|
protected function hideDefaultLocaleInURL() |
69
|
|
|
{ |
70
|
9 |
|
return localization()->isDefaultLocaleHiddenInUrl(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/* ------------------------------------------------------------------------------------------------ |
74
|
|
|
| Check Functions |
75
|
|
|
| ------------------------------------------------------------------------------------------------ |
76
|
|
|
*/ |
77
|
|
|
/** |
78
|
|
|
* Check is default locale hidden. |
79
|
|
|
* |
80
|
|
|
* @param string|null $locale |
81
|
|
|
* |
82
|
|
|
* @return bool |
83
|
|
|
*/ |
84
|
6 |
|
protected function isDefaultLocaleHidden($locale) |
85
|
|
|
{ |
86
|
6 |
|
return $this->getDefaultLocale() === $locale && $this->hideDefaultLocaleInURL(); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Determine if the request has a URI that should not be localized. |
91
|
|
|
* |
92
|
|
|
* @param \Illuminate\Http\Request $request |
93
|
|
|
* |
94
|
|
|
* @return bool |
95
|
|
|
*/ |
96
|
15 |
|
protected function shouldIgnore($request) |
97
|
|
|
{ |
98
|
15 |
|
foreach ($this->except as $except) { |
99
|
|
|
if ($except !== '/') |
100
|
|
|
$except = trim($except, '/'); |
101
|
|
|
|
102
|
|
|
if ($request->is($except)) |
103
|
|
|
return true; |
104
|
|
|
} |
105
|
|
|
|
106
|
15 |
|
return false; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/* ------------------------------------------------------------------------------------------------ |
110
|
|
|
| Other Functions |
111
|
|
|
| ------------------------------------------------------------------------------------------------ |
112
|
|
|
*/ |
113
|
|
|
/** |
114
|
|
|
* Get the redirection response. |
115
|
|
|
* |
116
|
|
|
* @param string $locale |
117
|
|
|
* |
118
|
|
|
* @return RedirectResponse|null |
119
|
|
|
*/ |
120
|
6 |
|
protected function getLocalizedRedirect($locale) |
121
|
|
|
{ |
122
|
6 |
|
$localizedUrl = localization()->getLocalizedURL($locale); |
123
|
|
|
|
124
|
6 |
|
if ( ! is_string($localizedUrl)) return null; |
125
|
|
|
|
126
|
6 |
|
return $this->makeRedirectResponse($localizedUrl); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Make a redirect response. |
131
|
|
|
* |
132
|
|
|
* @param string $url |
133
|
|
|
* @param int $code |
134
|
|
|
* |
135
|
|
|
* @return RedirectResponse |
136
|
|
|
*/ |
137
|
12 |
|
protected function makeRedirectResponse($url, $code = 302) |
138
|
|
|
{ |
139
|
12 |
|
return new RedirectResponse($url, $code, ['Vary' => 'Accept-Language']); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|