Skip to content

Commit 7004fdb

Browse files
author
Anton
authored
Merge pull request #463 from AntonShevchuk/develop
Added Translator management
2 parents 3999ccd + f3d6768 commit 7004fdb

3 files changed

Lines changed: 92 additions & 46 deletions

File tree

src/Application/Application.php

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -155,15 +155,15 @@ public function init($environment = 'production'): void
155155
// init Messages
156156
Messages::getInstance();
157157

158-
// init Translator
159-
Translator::getInstance();
160-
161158
// init Request
162159
$this->initRequest();
163160

164161
// init Response
165162
$this->initResponse();
166163

164+
// init Translator
165+
$this->initTranslator();
166+
167167
// init Router
168168
$this->initRouter();
169169
} catch (\Exception $e) {
@@ -229,19 +229,6 @@ protected function initResponse(): void
229229
Response::setInstance($response);
230230
}
231231

232-
/**
233-
* Initial Router instance
234-
*
235-
* @return void
236-
*/
237-
protected function initRouter(): void
238-
{
239-
$router = new \Bluz\Router\Router();
240-
$router->setOptions(Config::get('router'));
241-
242-
Router::setInstance($router);
243-
}
244-
245232
/**
246233
* Get Response instance
247234
*
@@ -262,6 +249,34 @@ public function getRequest(): ServerRequest
262249
return Request::getInstance();
263250
}
264251

252+
/**
253+
* Initial Router instance
254+
*
255+
* @return void
256+
*/
257+
protected function initRouter(): void
258+
{
259+
$router = new \Bluz\Router\Router();
260+
$router->setOptions(Config::get('router'));
261+
262+
Router::setInstance($router);
263+
}
264+
265+
/**
266+
* Initial Translator instance
267+
*
268+
* @return void
269+
* @throws Common\Exception\ConfigurationException
270+
*/
271+
protected function initTranslator(): void
272+
{
273+
$translator = new \Bluz\Translator\Translator();
274+
$translator->setOptions(Config::get('translator'));
275+
$translator->init();
276+
277+
Translator::setInstance($translator);
278+
}
279+
265280
/**
266281
* Run application
267282
*

src/Proxy/Request.php

Lines changed: 60 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ final class Request
5252
*/
5353
static private $accept;
5454

55+
/**
56+
* @var array|null Accepted languages
57+
*/
58+
static private $language;
59+
5560
/**
5661
* Init instance
5762
*
@@ -253,41 +258,66 @@ public static function getMethod(): string
253258
public static function getAccept(): array
254259
{
255260
if (!self::$accept) {
256-
// save to static variable
257-
self::$accept = [];
261+
// get header from request
262+
self::$accept = self::parseAcceptHeader(self::getHeader('Accept'));
263+
}
264+
return self::$accept;
265+
}
258266

267+
/**
268+
* Get Accept MIME Type
269+
*
270+
* @return array
271+
*/
272+
public static function getAcceptLanguage(): array
273+
{
274+
if (!self::$language) {
259275
// get header from request
260-
$header = self::getHeader('accept');
276+
self::$language = self::parseAcceptHeader(self::getHeader('Accept-Language'));
277+
}
278+
return self::$language;
279+
}
261280

262-
// nothing ...
263-
if (!$header) {
264-
return self::$accept;
265-
}
281+
/**
282+
* parseAcceptHeader
283+
*
284+
* @param string $header
285+
*
286+
* @return array
287+
*/
288+
private static function parseAcceptHeader($header): array
289+
{
290+
// empty array
291+
$accept = [];
292+
293+
// check empty
294+
if (!$header || $header === '') {
295+
return $accept;
296+
}
266297

267-
// make array if types
268-
$header = explode(',', $header);
269-
$header = array_map('trim', $header);
270-
271-
foreach ($header as $a) {
272-
// the default quality is 1.
273-
$q = 1;
274-
// check if there is a different quality
275-
if (strpos($a, ';q=') or strpos($a, '; q=')) {
276-
// divide "mime/type;q=X" into two parts: "mime/type" i "X"
277-
[$a, $q] = preg_split('/;([ ]?)q=/', $a);
278-
}
279-
// remove other extension
280-
if (strpos($a, ';')) {
281-
$a = substr($a, 0, strpos($a, ';'));
282-
}
283-
284-
// mime-type $a is accepted with the quality $q
285-
// WARNING: $q == 0 means, that mime-type isn’t supported!
286-
self::$accept[$a] = (float)$q;
298+
// make array from header
299+
$values = explode(',', $header);
300+
$values = array_map('trim', $values);
301+
302+
foreach ($values as $a) {
303+
// the default quality is 1.
304+
$q = 1;
305+
// check if there is a different quality
306+
if (strpos($a, ';q=') or strpos($a, '; q=')) {
307+
// divide "mime/type;q=X" into two parts: "mime/type" i "X"
308+
[$a, $q] = preg_split('/;([ ]?)q=/', $a);
287309
}
288-
arsort(self::$accept);
310+
// remove other extension
311+
if (strpos($a, ';')) {
312+
$a = substr($a, 0, strpos($a, ';'));
313+
}
314+
315+
// mime-type $a is accepted with the quality $q
316+
// WARNING: $q == 0 means, that isn’t supported!
317+
$accept[$a] = (float)$q;
289318
}
290-
return self::$accept;
319+
arsort($accept);
320+
return $accept;
291321
}
292322

293323
/**
@@ -320,7 +350,7 @@ public static function checkAccept(array $allowTypes = [])
320350

321351
// let’s check our supported types:
322352
foreach ($accept as $mime => $quality) {
323-
if ($quality && \in_array($mime, $allowTypes)) {
353+
if ($quality && \in_array($mime, $allowTypes, true)) {
324354
return $mime;
325355
}
326356
}

src/Proxy/Translator.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ final class Translator
4141
* Init instance
4242
*
4343
* @return Instance
44+
* @throws \Bluz\Common\Exception\ConfigurationException
4445
*/
4546
private static function initInstance(): Instance
4647
{

0 commit comments

Comments
 (0)