1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Bone\Server; |
4
|
|
|
|
5
|
|
|
use Bone\Mvc\Router\NotFoundException; |
6
|
|
|
use Locale; |
7
|
|
|
use Psr\Http\Message\ResponseInterface; |
8
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
9
|
|
|
use Psr\Http\Server\MiddlewareInterface; |
10
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
11
|
|
|
use Zend\I18n\Translator\Translator; |
12
|
|
|
|
13
|
|
|
class I18nHandler |
14
|
|
|
{ |
15
|
|
|
const REGEX_LOCALE = '#^/(?P<locale>[a-z]{2}[-_][a-zA-Z]{2})(?:/|$)#'; |
16
|
|
|
|
17
|
|
|
/** @var Translator$translator */ |
18
|
|
|
private $translator; |
19
|
|
|
|
20
|
|
|
/** @var array $supportedLocales */ |
21
|
|
|
private $supportedLocales; |
22
|
|
|
|
23
|
|
|
/** @var string $defaultLocale */ |
24
|
|
|
private $defaultLocale; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* InternationalisationMiddleware constructor. |
28
|
|
|
* @param $helper |
29
|
|
|
* @param string|null $defaultLocale |
30
|
|
|
*/ |
31
|
3 |
|
public function __construct(Translator $translator, array $supportedLocales, string $defaultLocale) |
32
|
|
|
{ |
33
|
3 |
|
$this->translator = $translator; |
34
|
3 |
|
$this->supportedLocales = $supportedLocales; |
35
|
3 |
|
$this->defaultLocale = $defaultLocale; |
36
|
3 |
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param ServerRequestInterface $request |
40
|
|
|
* @return ServerRequestInterface |
41
|
|
|
* @throws NotFoundException |
42
|
|
|
*/ |
43
|
2 |
|
public function handleI18n(ServerRequestInterface $request): ServerRequestInterface |
44
|
|
|
{ |
45
|
2 |
|
$uri = $request->getUri(); |
46
|
2 |
|
$path = $uri->getPath(); |
47
|
|
|
|
48
|
2 |
|
if (! preg_match(self::REGEX_LOCALE, $path, $matches)) { |
49
|
|
|
$locale = Locale::canonicalize($this->defaultLocale); |
|
|
|
|
50
|
|
|
Locale::setDefault($this->defaultLocale); |
51
|
|
|
|
52
|
|
|
return $request; |
53
|
|
|
} |
54
|
|
|
|
55
|
2 |
|
$locale = $matches['locale']; |
56
|
|
|
|
57
|
2 |
|
if (in_array($locale, $this->supportedLocales)) { |
58
|
1 |
|
$locale = Locale::canonicalize($locale); |
59
|
1 |
|
Locale::setDefault($locale); |
60
|
1 |
|
$this->translator->setLocale($locale); |
61
|
1 |
|
$path = substr($path, strlen($locale) + 1); |
62
|
1 |
|
$uri = $uri->withPath($path); |
63
|
1 |
|
$request = $request->withUri($uri); |
64
|
|
|
} |
65
|
|
|
|
66
|
2 |
|
return $request; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param ServerRequestInterface $request |
71
|
|
|
* @return ServerRequestInterface |
72
|
|
|
*/ |
73
|
1 |
|
public function removeI18n(ServerRequestInterface $request): ServerRequestInterface |
74
|
|
|
{ |
75
|
1 |
|
$uri = $request->getUri(); |
76
|
1 |
|
$path = $uri->getPath(); |
77
|
|
|
|
78
|
1 |
|
if (! preg_match(self::REGEX_LOCALE, $path, $matches)) { |
79
|
|
|
return $request; |
80
|
|
|
} |
81
|
|
|
|
82
|
1 |
|
$locale = $matches['locale']; |
83
|
1 |
|
$path = substr($path, strlen($locale) + 1); |
84
|
1 |
|
$uri = $uri->withPath($path); |
85
|
1 |
|
$request = $request->withUri($uri); |
86
|
|
|
|
87
|
1 |
|
return $request; |
88
|
|
|
} |
89
|
|
|
} |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.