1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Containers\Localization\Middlewares; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use App; |
7
|
|
|
use Config; |
8
|
|
|
|
9
|
|
|
use App\Ship\Parents\Middlewares\Middleware; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class LocalizationMiddleware |
13
|
|
|
* |
14
|
|
|
* @author Mahmoud Zalt <[email protected]> |
15
|
|
|
*/ |
16
|
|
|
class LocalizationMiddleware extends Middleware |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Handle an incoming request. |
20
|
|
|
* |
21
|
|
|
* @param \Illuminate\Http\Request $request |
22
|
|
|
* @param \Closure $next |
23
|
|
|
* |
24
|
|
|
* @return mixed |
25
|
|
|
*/ |
26
|
|
|
public function handle($request, Closure $next) |
27
|
|
|
{ |
28
|
|
|
// find and validate the lang on that request |
29
|
|
|
$lang = $this->validateLanguage($this->findLanguage($request)); |
30
|
|
|
|
31
|
|
|
// set the local language |
32
|
|
|
App::setLocale($lang); |
33
|
|
|
|
34
|
|
|
// get the response after the request is done |
35
|
|
|
$response = $next($request); |
36
|
|
|
|
37
|
|
|
// set Content Languages header in the response |
38
|
|
|
$response->headers->set('Content-Language', $lang); |
39
|
|
|
|
40
|
|
|
// return the response |
41
|
|
|
return $response; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param $lang |
46
|
|
|
* |
47
|
|
|
* @return string|Exception |
48
|
|
|
*/ |
49
|
|
|
private function validateLanguage($lang) |
50
|
|
|
{ |
51
|
|
|
// check the languages defined is supported |
52
|
|
|
if (!array_key_exists($lang, $this->getSupportedLanguages())) { |
53
|
|
|
// respond with error |
54
|
|
|
$lang = abort(403, 'Language not supported.'); |
|
|
|
|
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return $lang; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param $request |
62
|
|
|
* |
63
|
|
|
* @return string |
64
|
|
|
*/ |
65
|
|
|
private function findLanguage($request) |
66
|
|
|
{ |
67
|
|
|
// read the language from the request header, if the header is missed, take the default local language |
68
|
|
|
return $request->header('Content-Language') ? : Config::get('app.locale'); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @return array |
73
|
|
|
*/ |
74
|
|
|
private function getSupportedLanguages() |
75
|
|
|
{ |
76
|
|
|
return Config::get('localization.supported_languages'); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
} |
80
|
|
|
|
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()
can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.