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