1
|
|
|
<?php declare (strict_types = 1); |
2
|
|
|
|
3
|
|
|
namespace Limoncello\l10n\Messages; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Copyright 2015-2019 [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\Contracts\L10n\MessageStorageInterface; |
22
|
|
|
use Limoncello\l10n\Contracts\Messages\ResourceBundleInterface; |
23
|
|
|
use function assert; |
24
|
|
|
use function class_exists; |
25
|
|
|
use function class_implements; |
26
|
|
|
use function glob; |
27
|
|
|
use function in_array; |
28
|
|
|
use function is_dir; |
29
|
|
|
use function is_string; |
30
|
|
|
use function pathinfo; |
31
|
|
|
use function realpath; |
32
|
|
|
use function scandir; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @package Limoncello\l10n |
36
|
|
|
*/ |
37
|
|
|
class FileBundleEncoder extends BundleEncoder |
38
|
|
|
{ |
39
|
|
|
/** |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
private $globMessagePatterns; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param iterable|null $messageDescriptions |
46
|
|
|
* @param string $localesDir |
47
|
|
|
* @param string $globMessagePatterns |
48
|
|
|
*/ |
49
|
6 |
|
public function __construct( |
50
|
|
|
?iterable $messageDescriptions, |
51
|
|
|
string $localesDir, |
52
|
|
|
string $globMessagePatterns = '*.php' |
53
|
|
|
) { |
54
|
|
|
$this |
55
|
6 |
|
->setGlobMessagePatterns($globMessagePatterns) |
56
|
6 |
|
->loadDescriptions($messageDescriptions) |
57
|
6 |
|
->loadBundles($localesDir); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Method is used for loading resources from packages. |
62
|
|
|
* |
63
|
|
|
* @see ProvidesMessageResourcesInterface |
64
|
|
|
* |
65
|
|
|
* @param iterable|null $messageDescriptions |
66
|
|
|
* |
67
|
|
|
* @return FileBundleEncoder |
68
|
|
|
*/ |
69
|
6 |
|
protected function loadDescriptions(?iterable $messageDescriptions): self |
70
|
|
|
{ |
71
|
6 |
|
if ($messageDescriptions !== null) { |
72
|
2 |
|
foreach ($messageDescriptions as list($locale, $namespace, $messageStorage)) { |
73
|
2 |
|
assert(is_string($locale) === true && empty($locale) === false); |
74
|
2 |
|
assert(is_string($namespace) === true && empty($namespace) === false); |
75
|
2 |
|
assert(is_string($messageStorage) === true && empty($messageStorage) === false); |
76
|
2 |
|
assert(class_exists($messageStorage) === true); |
77
|
2 |
|
assert(in_array(MessageStorageInterface::class, class_implements($messageStorage)) === true); |
78
|
|
|
|
79
|
|
|
/** @var MessageStorageInterface $messageStorage */ |
80
|
|
|
|
81
|
2 |
|
$properties = $messageStorage::getMessages(); |
82
|
2 |
|
$bundle = new ResourceBundle($locale, $namespace, $properties); |
83
|
2 |
|
$this->addBundle($bundle); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
6 |
|
return $this; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param string $localesDir |
92
|
|
|
* |
93
|
|
|
* @return self |
94
|
|
|
*/ |
95
|
6 |
|
protected function loadBundles(string $localesDir): self |
96
|
|
|
{ |
97
|
6 |
|
assert(empty($localesDir) === false); |
98
|
|
|
|
99
|
6 |
|
$localesDir = realpath($localesDir); |
100
|
6 |
|
assert($localesDir !== false); |
101
|
|
|
|
102
|
6 |
|
foreach (scandir($localesDir) as $fileOrDir) { |
103
|
6 |
|
if ($fileOrDir !== '.' && $fileOrDir !== '..' && |
104
|
6 |
|
is_dir($localeDirFullPath = $localesDir . DIRECTORY_SEPARATOR . $fileOrDir . DIRECTORY_SEPARATOR) |
105
|
|
|
) { |
106
|
5 |
|
$localeDir = $fileOrDir; |
107
|
5 |
|
foreach (glob($localeDirFullPath . $this->getGlobMessagePatterns()) as $messageFile) { |
108
|
5 |
|
$namespace = pathinfo($messageFile, PATHINFO_FILENAME); |
109
|
5 |
|
$bundle = $this->loadBundleFromFile($messageFile, $localeDir, $namespace); |
110
|
6 |
|
$this->addBundle($bundle); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
115
|
6 |
|
return $this; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @return string |
120
|
|
|
*/ |
121
|
5 |
|
protected function getGlobMessagePatterns(): string |
122
|
|
|
{ |
123
|
5 |
|
return $this->globMessagePatterns; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @param string $globMessagePatterns |
128
|
|
|
* |
129
|
|
|
* @return self |
130
|
|
|
*/ |
131
|
6 |
|
protected function setGlobMessagePatterns(string $globMessagePatterns): self |
132
|
|
|
{ |
133
|
6 |
|
assert(is_string($globMessagePatterns) === true && empty($globMessagePatterns) === false); |
134
|
6 |
|
$this->globMessagePatterns = $globMessagePatterns; |
135
|
|
|
|
136
|
6 |
|
return $this; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @param string $fileFullPath |
141
|
|
|
* @param string $localeDir |
142
|
|
|
* @param string $messageFile |
143
|
|
|
* |
144
|
|
|
* @return ResourceBundleInterface |
145
|
|
|
*/ |
146
|
5 |
|
protected function loadBundleFromFile( |
147
|
|
|
string $fileFullPath, |
148
|
|
|
string $localeDir, |
149
|
|
|
string $messageFile |
150
|
|
|
): ResourceBundleInterface { |
151
|
|
|
/** @noinspection PhpIncludeInspection */ |
152
|
5 |
|
$properties = require $fileFullPath; |
153
|
5 |
|
$bundle = new ResourceBundle($localeDir, $messageFile, $properties); |
154
|
|
|
|
155
|
5 |
|
return $bundle; |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|