Passed
Pull Request — master (#10)
by Rustam
13:02
created

Locale::detectLocale()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 6
ccs 0
cts 4
cp 0
crap 6
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Middleware;
6
7
use DateInterval;
8
use Psr\Http\Message\ResponseFactoryInterface;
9
use Psr\Http\Message\ResponseInterface;
10
use Psr\Http\Message\ServerRequestInterface;
11
use Psr\Http\Server\MiddlewareInterface;
12
use Psr\Http\Server\RequestHandlerInterface;
13
use Psr\Log\LoggerInterface;
14
use Yiisoft\Cookies\Cookie;
15
use Yiisoft\Http\Header;
16
use Yiisoft\Http\Status;
17
use Yiisoft\Router\UrlGeneratorInterface;
18
use Yiisoft\Session\SessionInterface;
19
use Yiisoft\Translator\TranslatorInterface;
20
21
final class Locale implements MiddlewareInterface
22
{
23
    private const DEFAULT_LOCALE = 'en';
24
    private const DEFAULT_LOCALE_NAME = '_language';
25
26
    private TranslatorInterface $translator;
27
    private UrlGeneratorInterface $urlGenerator;
28
    private SessionInterface $session;
29
    private ResponseFactoryInterface $responseFactory;
30
    private LoggerInterface $logger;
31
    private array $locales;
32
    private bool $enableSaveLocale = true;
33
    private bool $enableDetectLocale = false;
34
    private string $defaultLocale = self::DEFAULT_LOCALE;
35
    private string $queryParameterName = self::DEFAULT_LOCALE_NAME;
36
    private string $sessionName = self::DEFAULT_LOCALE_NAME;
37
    private ?DateInterval $cookieDuration;
38
39
    public function __construct(
40
        TranslatorInterface $translator,
41
        UrlGeneratorInterface $urlGenerator,
42
        SessionInterface $session,
43
        LoggerInterface $logger,
44
        ResponseFactoryInterface $responseFactory,
45
        array $locales = []
46
    ) {
47
        $this->translator = $translator;
48
        $this->urlGenerator = $urlGenerator;
49
        $this->session = $session;
50
        $this->logger = $logger;
51
        $this->responseFactory = $responseFactory;
52
        $this->locales = $locales;
53
        $this->cookieDuration = new DateInterval('P30D');
54
    }
55
56
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
57
    {
58
        if ($this->locales === []) {
59
            return $handler->handle($request);
60
        }
61
62
        $uri = $request->getUri();
63
        $path = $uri->getPath();
64
        [$locale, $country] = $this->getLocaleFromPath($path);
65
66
        if ($locale !== null) {
67
            $length = strlen($locale);
68
            $newPath = substr($path, $length + 1);
69
            if ($newPath === '') {
70
                $newPath = '/';
71
            }
72
            $this->translator->setLocale($locale);
73
            $this->urlGenerator->setDefaultArgument($this->queryParameterName, $locale);
74
75
            $response = $handler->handle($request);
76
            if ($this->isDefaultLocale($locale, $country) && $request->getMethod() === 'GET') {
77
                $response = $this->responseFactory
78
                    ->createResponse(Status::FOUND)
79
                    ->withHeader(Header::LOCATION, $newPath);
80
            }
81
            if ($this->enableSaveLocale) {
82
                $response = $this->saveLocale($locale, $response);
83
            }
84
            return $response;
85
        }
86
        if ($this->enableSaveLocale) {
87
            [$locale, $country] = $this->getLocaleFromRequest($request);
88
        }
89
        if ($locale === null && $this->enableDetectLocale) {
90
            [$locale, $country] = $this->detectLocale($request);
91
        }
92
        if ($locale === null || $this->isDefaultLocale($locale, $country)) {
93
            $this->urlGenerator->setDefaultArgument($this->queryParameterName, null);
94
            $request = $request->withUri($uri->withPath('/' . $this->defaultLocale . $path));
95
            return $handler->handle($request);
96
        }
97
        $this->urlGenerator->setDefaultArgument($this->queryParameterName, $locale);
98
99
        if ($request->getMethod() === 'GET') {
100
            return $this->responseFactory
101
                ->createResponse(Status::FOUND)
102
                ->withHeader(Header::LOCATION, '/' . $locale . $path);
103
        }
104
105
        return $handler->handle($request);
106
    }
107
108
    private function getLocaleFromPath(string $path): array
109
    {
110
        $parts = [];
111
        foreach ($this->locales as $code => $locale) {
112
            $lang = is_string($code) ? $code : $locale;
113
            $parts[] = $lang;
114
        }
115
116
        $pattern = implode('|', $parts);
117
        if (preg_match("#^/($pattern)\b(/?)#i", $path, $matches)) {
118
            $locale = $matches[1];
119
            [$locale, $country] = $this->parseLocale($locale);
120
            if (isset($this->locales[$locale])) {
121
                $this->logger->info(sprintf("Locale '%s' found in URL", $locale));
122
                return [$locale, $country];
123
            }
124
        }
125
        return [null, null];
126
    }
127
128
    private function getLocaleFromRequest(ServerRequestInterface $request): array
129
    {
130
        $cookies = $request->getCookieParams();
131
        $queryParameters = $request->getQueryParams();
132
        if (isset($cookies[$this->sessionName])) {
133
            $this->logger->info(sprintf("Locale '%s' found in cookies", $cookies[$this->sessionName]));
134
            return $this->parseLocale($cookies[$this->sessionName]);
135
        }
136
        if (isset($queryParameters[$this->queryParameterName])) {
137
            $this->logger->info(
138
                sprintf("Locale '%s' found in query string", $queryParameters[$this->queryParameterName])
139
            );
140
            return $this->parseLocale($queryParameters[$this->queryParameterName]);
141
        }
142
        return [null, null];
143
    }
144
145
    private function isDefaultLocale(string $locale, ?string $country): bool
146
    {
147
        return $locale === $this->defaultLocale || ($country !== null && $this->defaultLocale === "$locale-$country");
148
    }
149
150
    private function detectLocale(ServerRequestInterface $request): array
151
    {
152
        foreach ($request->getHeader(Header::ACCEPT_LANGUAGE) as $language) {
153
            return $this->parseLocale($language);
154
        }
155
        return [null, null];
156
    }
157
158
    private function saveLocale(string $locale, ResponseInterface $response): ResponseInterface
159
    {
160
        $this->logger->info('Saving found locale to cookies');
161
        $this->session->set($this->sessionName, $locale);
162
        $cookie = (new Cookie($this->sessionName, $locale));
163
        if ($this->cookieDuration !== null) {
164
            $cookie = $cookie->withMaxAge($this->cookieDuration);
165
        }
166
        return $cookie->addToResponse($response);
167
    }
168
169
    private function parseLocale(string $locale): array
170
    {
171
        if (str_contains($locale, '-')) {
172
            return explode('-', $locale, 2);
173
        }
174
        if (isset($this->locales[$locale]) && str_contains($this->locales[$locale], '-')) {
175
            return explode('-', $this->locales[$locale], 2);
176
        }
177
        return [$locale, null];
178
    }
179
180
    public function withLocales(array $locales): self
181
    {
182
        $new = clone $this;
183
        $new->locales = $locales;
184
        return $new;
185
    }
186
187
    public function withDefaultLocale(string $defaultLocale): self
188
    {
189
        $new = clone $this;
190
        $new->defaultLocale = $defaultLocale;
191
        return $new;
192
    }
193
194
    public function withQueryParameterName(string $queryParameterName): self
195
    {
196
        $new = clone $this;
197
        $new->queryParameterName = $queryParameterName;
198
        return $new;
199
    }
200
201
    public function withSessionName(string $sessionName): self
202
    {
203
        $new = clone $this;
204
        $new->sessionName = $sessionName;
205
        return $new;
206
    }
207
208
    public function withEnableSaveLocale(bool $enableSaveLocale): self
209
    {
210
        $new = clone $this;
211
        $new->enableDetectLocale = $enableSaveLocale;
212
        return $new;
213
    }
214
215
    public function withEnableDetectLocale(bool $enableDetectLocale): self
216
    {
217
        $new = clone $this;
218
        $new->enableDetectLocale = $enableDetectLocale;
219
        return $new;
220
    }
221
}
222