1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Aitor24\Localizer\Middlewares; |
4
|
|
|
|
5
|
|
|
use Aitor24\Localizer\Facades\LocalizerFacade as Localizer; |
6
|
|
|
use Carbon\Carbon; |
7
|
|
|
use Closure; |
8
|
|
|
use Illuminate\Support\Facades\App; |
9
|
|
|
use Illuminate\Support\Facades\Auth; |
10
|
|
|
use Unicodeveloper\Identify\Facades\IdentityFacade as Identify; |
11
|
|
|
|
12
|
|
|
class LocalizerMiddleware |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* This function checks if language to set is an allowed lang of config. |
16
|
|
|
* |
17
|
|
|
* @param string $lang |
18
|
|
|
**/ |
19
|
|
|
private function setIfAllowed($lang) |
20
|
|
|
{ |
21
|
|
|
$allowedLangs = array_keys(Localizer::allowedLanguages()); |
22
|
|
|
if (config('localizer.block_unallowed_langs') && !in_array($lang, $allowedLangs)) { |
23
|
|
|
$lang = $allowedLangs[0]; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
App::setLocale($lang); |
27
|
|
|
if (config('localizer.carbon')) { |
28
|
|
|
Carbon::setLocale($lang); |
29
|
|
|
} |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* This function checks if user is logged in, and loads the prefered language. |
34
|
|
|
* |
35
|
|
|
* @param string $lang |
|
|
|
|
36
|
|
|
**/ |
37
|
|
|
public function setLang($request) |
38
|
|
|
{ |
39
|
|
|
if (Auth::check()) { |
40
|
|
|
$user = Auth::user(); |
41
|
|
|
|
42
|
|
|
// Set user locale |
43
|
|
|
if ($user->locale) { |
|
|
|
|
44
|
|
|
$this->setIfAllowed($user->locale); |
|
|
|
|
45
|
|
View Code Duplication |
} else { |
|
|
|
|
46
|
|
|
if (config('localizer.set_auto_lang')) { |
47
|
|
|
$this->setIfAllowed(Identify::lang()->getLanguage()); |
48
|
|
|
} else { |
49
|
|
|
$this->setIfAllowed(config('localizer.default_lang')); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
} else { |
53
|
|
|
if ($request->session()->has('locale')) { |
54
|
|
|
$this->setIfAllowed(session('locale')); |
55
|
|
View Code Duplication |
} else { |
|
|
|
|
56
|
|
|
if (config('localizer.set_auto_lang')) { |
57
|
|
|
$this->setIfAllowed(Identify::lang()->getLanguage()); |
58
|
|
|
} else { |
59
|
|
|
$this->setIfAllowed(config('localizer.default_lang')); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Handle an incoming request. |
67
|
|
|
* |
68
|
|
|
* @param \Illuminate\Http\Request $request |
69
|
|
|
* @param \Closure $next |
70
|
|
|
* |
71
|
|
|
* @return mixed |
72
|
|
|
*/ |
73
|
|
|
public function handle($request, Closure $next) |
74
|
|
|
{ |
75
|
|
|
$this->setLang($request); |
76
|
|
|
|
77
|
|
|
return $next($request); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.