1
|
|
|
<?php namespace Limoncello\Application\Packages\L10n; |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright 2015-2017 [email protected] |
5
|
|
|
* |
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
7
|
|
|
* you may not use this file except in compliance with the License. |
8
|
|
|
* You may obtain a copy of the License at |
9
|
|
|
* |
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
11
|
|
|
* |
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15
|
|
|
* See the License for the specific language governing permissions and |
16
|
|
|
* limitations under the License. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
use Limoncello\Contracts\Application\ApplicationConfigurationInterface as A; |
20
|
|
|
use Limoncello\Contracts\Provider\ProvidesMessageResourcesInterface; |
21
|
|
|
use Limoncello\Contracts\Settings\SettingsInterface; |
22
|
|
|
use Limoncello\l10n\Messages\FileBundleEncoder; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @package Limoncello\Application |
26
|
|
|
*/ |
27
|
|
|
abstract class L10nSettings implements SettingsInterface |
28
|
|
|
{ |
29
|
|
|
/** Settings key */ |
30
|
|
|
const KEY_DEFAULT_LOCALE = 0; |
31
|
|
|
|
32
|
|
|
/** Settings key */ |
33
|
|
|
const KEY_LOCALES_FOLDER = self::KEY_DEFAULT_LOCALE + 1; |
34
|
|
|
|
35
|
|
|
/** Settings key */ |
36
|
|
|
const KEY_LOCALES_DATA = self::KEY_LOCALES_FOLDER + 1; |
37
|
|
|
|
38
|
|
|
/** Settings key */ |
39
|
|
|
protected const KEY_LAST = self::KEY_LOCALES_DATA; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var array |
43
|
|
|
*/ |
44
|
|
|
private $appConfig; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @inheritdoc |
48
|
|
|
*/ |
49
|
1 |
|
final public function get(array $appConfig): array |
50
|
|
|
{ |
51
|
1 |
|
$this->appConfig = $appConfig; |
52
|
|
|
|
53
|
1 |
|
$defaults = $this->getSettings(); |
54
|
|
|
|
55
|
1 |
|
$defaultLocale = $defaults[static::KEY_DEFAULT_LOCALE] ?? null; |
56
|
1 |
|
assert(empty($defaultLocale) === false, "Invalid default locale `$defaultLocale`."); |
57
|
|
|
|
58
|
1 |
|
$localesFolder = $defaults[static::KEY_LOCALES_FOLDER] ?? null; |
59
|
1 |
|
assert( |
60
|
1 |
|
$localesFolder !== null && empty(glob($localesFolder)) === false, |
61
|
1 |
|
"Invalid Locales folder `$localesFolder`." |
62
|
|
|
); |
63
|
|
|
|
64
|
1 |
|
$bundleEncoder = new FileBundleEncoder($this->getMessageDescriptionsFromProviders(), $localesFolder); |
|
|
|
|
65
|
|
|
|
66
|
|
|
return $defaults + [ |
67
|
1 |
|
static::KEY_LOCALES_DATA => $bundleEncoder->getStorageData($defaultLocale), |
68
|
|
|
]; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @return array |
73
|
|
|
*/ |
74
|
1 |
|
protected function getSettings(): array |
75
|
|
|
{ |
76
|
|
|
return [ |
77
|
1 |
|
static::KEY_DEFAULT_LOCALE => 'en', |
78
|
|
|
]; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @return mixed |
83
|
|
|
*/ |
84
|
1 |
|
protected function getAppConfig() |
85
|
|
|
{ |
86
|
1 |
|
return $this->appConfig; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* |
91
|
|
|
* @return iterable |
|
|
|
|
92
|
|
|
*/ |
93
|
1 |
|
private function getMessageDescriptionsFromProviders(): iterable |
94
|
|
|
{ |
95
|
1 |
|
$providerClasses = $this->getAppConfig()[A::KEY_PROVIDER_CLASSES] ?? []; |
96
|
1 |
|
foreach ($providerClasses as $class) { |
97
|
1 |
|
if (in_array(ProvidesMessageResourcesInterface::class, class_implements($class)) === true) { |
98
|
|
|
/** @var ProvidesMessageResourcesInterface $class */ |
99
|
1 |
|
foreach ($class::getMessageDescriptions() as $messageDescription) { |
100
|
1 |
|
yield $messageDescription; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: