1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Limoncello\Application\Packages\L10n; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Copyright 2015-2020 [email protected] |
7
|
|
|
* |
8
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
9
|
|
|
* you may not use this file except in compliance with the License. |
10
|
|
|
* You may obtain a copy of the License at |
11
|
|
|
* |
12
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
13
|
|
|
* |
14
|
|
|
* Unless required by applicable law or agreed to in writing, software |
15
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
16
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
17
|
|
|
* See the License for the specific language governing permissions and |
18
|
|
|
* limitations under the License. |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
use Limoncello\Application\Packages\L10n\L10nSettings as S; |
22
|
|
|
use Limoncello\Contracts\Application\ContainerConfiguratorInterface; |
23
|
|
|
use Limoncello\Contracts\Container\ContainerInterface as LimoncelloContainerInterface; |
24
|
|
|
use Limoncello\Contracts\L10n\FormatterFactoryInterface; |
25
|
|
|
use Limoncello\Contracts\L10n\FormatterInterface; |
26
|
|
|
use Limoncello\Contracts\Settings\SettingsProviderInterface; |
27
|
|
|
use Limoncello\l10n\Format\Formatter; |
28
|
|
|
use Limoncello\l10n\Format\Translator; |
29
|
|
|
use Limoncello\l10n\Messages\BundleStorage; |
30
|
|
|
use Psr\Container\ContainerInterface as PsrContainerInterface; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @package Limoncello\Application |
34
|
|
|
*/ |
35
|
|
|
class L10nContainerConfigurator implements ContainerConfiguratorInterface |
36
|
|
|
{ |
37
|
|
|
/** @var callable */ |
38
|
|
|
const CONFIGURATOR = [self::class, self::CONTAINER_METHOD_NAME]; |
39
|
|
|
|
40
|
|
|
/** |
41
|
1 |
|
* @inheritdoc |
42
|
|
|
* |
43
|
|
|
* @SuppressWarnings(PHPMD.UndefinedVariable) |
44
|
1 |
|
*/ |
45
|
1 |
|
public static function configureContainer(LimoncelloContainerInterface $container): void |
46
|
|
|
{ |
47
|
1 |
|
$container[FormatterFactoryInterface::class] = function (PsrContainerInterface $container) { |
48
|
1 |
|
$settingsProvider = $container->get(SettingsProviderInterface::class); |
49
|
|
|
$settings = $settingsProvider->get(S::class); |
50
|
|
|
|
51
|
|
|
$defaultLocale = $settings[S::KEY_DEFAULT_LOCALE]; |
52
|
|
|
$storageData = $settings[S::KEY_LOCALES_DATA]; |
53
|
|
|
|
54
|
|
|
$factory = new class ($defaultLocale, $storageData) implements FormatterFactoryInterface |
55
|
|
|
{ |
56
|
|
|
/** |
57
|
|
|
* @var string |
58
|
|
|
*/ |
59
|
|
|
private $defaultLocale; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @var array |
63
|
|
|
*/ |
64
|
|
|
private $storageData; |
65
|
|
|
|
66
|
1 |
|
/** |
67
|
|
|
* @param string $defaultLocale |
68
|
1 |
|
* @param array $storageData |
69
|
1 |
|
*/ |
70
|
|
|
public function __construct(string $defaultLocale, array $storageData) |
71
|
|
|
{ |
72
|
|
|
$this->defaultLocale = $defaultLocale; |
73
|
|
|
$this->storageData = $storageData; |
74
|
|
|
} |
75
|
1 |
|
|
76
|
|
|
/** |
77
|
1 |
|
* @inheritdoc |
78
|
|
|
*/ |
79
|
|
|
public function createFormatter(string $namespace): FormatterInterface |
80
|
|
|
{ |
81
|
|
|
return $this->createFormatterForLocale($namespace, $this->defaultLocale); |
82
|
|
|
} |
83
|
1 |
|
|
84
|
|
|
/** |
85
|
1 |
|
* @inheritdoc |
86
|
1 |
|
*/ |
87
|
|
|
public function createFormatterForLocale(string $namespace, string $locale): FormatterInterface |
88
|
1 |
|
{ |
89
|
|
|
$translator = new Translator(new BundleStorage($this->storageData)); |
90
|
|
|
$formatter = new Formatter($locale, $namespace, $translator); |
91
|
|
|
|
92
|
1 |
|
return $formatter; |
93
|
|
|
} |
94
|
|
|
}; |
95
|
|
|
|
96
|
|
|
return $factory; |
97
|
|
|
}; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|