1
|
|
|
<?php |
2
|
|
|
namespace JsLocalization\Utils; |
3
|
|
|
|
4
|
|
|
use App; |
5
|
|
|
use Event; |
6
|
|
|
use Illuminate\Support\Facades\File; |
7
|
|
|
use JsLocalization\Exceptions\FileNotFoundException; |
8
|
|
|
|
9
|
|
|
class Helper |
10
|
|
|
{ |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Array of message keys. A set of messages that are |
14
|
|
|
* supposed to be exported to the JS code in addition |
15
|
|
|
* to Config::get('js-localization.messages'). |
16
|
|
|
* |
17
|
|
|
* @var array |
18
|
|
|
*/ |
19
|
|
|
protected $messagesToExport = []; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Allows registration of additional messages to |
23
|
|
|
* export to the JS code. The additional messages |
24
|
|
|
* registered using this method extend the |
25
|
|
|
* Config::get('js-localization.messages') |
26
|
|
|
* array. |
27
|
|
|
* Don't forget to run `php artisan js-localization:refresh`! |
28
|
|
|
* |
29
|
|
|
* @param array $messageKeys Array of message keys. |
30
|
|
|
* @return void |
31
|
|
|
*/ |
32
|
2 |
|
public function addMessagesToExport(array $messageKeys) |
33
|
|
|
{ |
34
|
2 |
|
$this->messagesToExport = array_unique( |
35
|
2 |
|
array_merge( |
36
|
2 |
|
$this->messagesToExport, |
37
|
2 |
|
$this->resolveMessageKeyArray($messageKeys) |
38
|
2 |
|
) |
39
|
2 |
|
); |
40
|
2 |
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Similar to addMessagesToExport(), but does not |
44
|
|
|
* register an array of message keys, but the |
45
|
|
|
* messages of a whole language file (one of the |
46
|
|
|
* PHP files in app/lang). |
47
|
|
|
* |
48
|
|
|
* @param string $filePath Path to the message file. |
49
|
|
|
* @param string $prefix Optional. Prefix to prepend before the message keys. |
50
|
|
|
* @return void |
51
|
|
|
* @throws FileNotFoundException |
52
|
|
|
*/ |
53
|
2 |
|
public function addMessageFileToExport($filePath, $prefix="") |
54
|
|
|
{ |
55
|
2 |
|
if (!File::isFile($filePath)) { |
56
|
1 |
|
throw new FileNotFoundException("File not found: $filePath"); |
57
|
|
|
} |
58
|
|
|
|
59
|
1 |
|
$messages = require_once $filePath; |
60
|
1 |
|
$prefix = $this->prefix($prefix); |
61
|
1 |
|
$prefix .= preg_replace('/\.php$/i', '', basename($filePath)) . '.'; |
62
|
|
|
|
63
|
1 |
|
$this->messagesToExport = array_unique( |
64
|
1 |
|
array_merge( |
65
|
1 |
|
$this->messagesToExport, |
66
|
1 |
|
$this->resolveMessageArrayToMessageKeys($messages, $prefix) |
67
|
1 |
|
) |
68
|
1 |
|
); |
69
|
1 |
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Returns the message keys previously registered |
73
|
|
|
* by addMessagesToExport(). Nested arrays have |
74
|
|
|
* already been resolved to a single flat array. |
75
|
|
|
* |
76
|
|
|
* @return array |
77
|
|
|
* Array of message keys to export to the JS code. |
78
|
|
|
*/ |
79
|
10 |
|
public function getAdditionalMessages() |
80
|
|
|
{ |
81
|
10 |
|
return $this->messagesToExport; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Takes an array of message keys with nested |
86
|
|
|
* sub-arrays and returns a flat array of |
87
|
|
|
* fully qualified message keys. |
88
|
|
|
* |
89
|
|
|
* @param array $messageKeys Complex array of message keys. |
90
|
|
|
* @return array Flat array of fully qualified message keys. |
91
|
|
|
*/ |
92
|
10 |
|
public function resolveMessageKeyArray(array $messageKeys) |
93
|
|
|
{ |
94
|
10 |
|
$flatArray = []; |
95
|
|
|
|
96
|
10 |
|
foreach ($messageKeys as $index=>$key) { |
97
|
|
|
$this->resolveMessageKey($key, $index, function($qualifiedKey) use(&$flatArray) |
98
|
|
|
{ |
99
|
9 |
|
$flatArray[] = $qualifiedKey; |
100
|
9 |
|
}); |
101
|
10 |
|
} |
102
|
|
|
|
103
|
10 |
|
return $flatArray; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Resolves a message array with nested |
108
|
|
|
* sub-arrays to a flat array of fully |
109
|
|
|
* qualified message keys. |
110
|
|
|
* |
111
|
|
|
* @param array $messages Complex message array (like the ones in the app/lang/* files). |
112
|
|
|
* @return array Flat array of fully qualified message keys. |
113
|
|
|
*/ |
114
|
2 |
|
public function resolveMessageArrayToMessageKeys(array $messages, $prefix="") |
115
|
|
|
{ |
116
|
2 |
|
$flatArray = []; |
117
|
|
|
|
118
|
2 |
|
foreach ($messages as $key=>$message) { |
119
|
|
|
$this->resolveMessageToKeys($message, $key, function($qualifiedKey) use(&$flatArray) |
120
|
|
|
{ |
121
|
2 |
|
$flatArray[] = $qualifiedKey; |
122
|
2 |
|
}, $prefix); |
123
|
2 |
|
} |
124
|
|
|
|
125
|
2 |
|
return $flatArray; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Returns the concatenation of prefix and key if the key |
130
|
|
|
* is a string. If the key is an array then the function |
131
|
|
|
* will recurse. |
132
|
|
|
* |
133
|
|
|
* @param mixed $key An array item read from the configuration ('messages' array). |
134
|
|
|
* @param mixed $keyIndex The array index of $key. Is necessary if $key is an array. |
135
|
|
|
* @param callable $callback A callback function: function($fullyQualifiedKey). |
136
|
|
|
* @param string $prefix Optional key prefix. |
137
|
|
|
*/ |
138
|
9 |
|
private function resolveMessageKey($key, $keyIndex, $callback, $prefix="") |
139
|
|
|
{ |
140
|
9 |
|
if (is_array($key)) { |
141
|
3 |
|
$_prefix = $prefix ? $prefix.$keyIndex."." : $keyIndex."."; |
142
|
|
|
|
143
|
3 |
|
foreach ($key as $_index=>$_key) { |
144
|
3 |
|
$this->resolveMessageKey($_key, $_index, $callback, $_prefix); |
145
|
3 |
|
} |
146
|
|
|
|
147
|
3 |
|
} else { |
148
|
9 |
|
$callback($prefix.$key); |
149
|
|
|
} |
150
|
9 |
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Returns the concatenation of prefix and key if the value |
154
|
|
|
* is a message. If the value is an array then the function |
155
|
|
|
* will recurse. |
156
|
|
|
* |
157
|
|
|
* @param mixed $message An array item read from a message file array. |
158
|
|
|
* @param mixed $key The array key of $message. |
159
|
|
|
* @param callable $callback A callback function: function($fullyQualifiedKey). |
160
|
|
|
* @param string $prefix Optional key prefix. |
161
|
|
|
*/ |
162
|
2 |
|
private function resolveMessageToKeys($message, $key, $callback, $prefix="") |
163
|
|
|
{ |
164
|
2 |
|
if (is_array($message)) { |
165
|
2 |
|
$_prefix = $prefix ? $prefix.$key."." : $key."."; |
166
|
|
|
|
167
|
2 |
|
foreach ($message as $_key=>$_message) { |
168
|
2 |
|
$this->resolveMessageToKeys($_message, $_key, $callback, $_prefix); |
169
|
2 |
|
} |
170
|
|
|
|
171
|
2 |
|
} else { |
172
|
2 |
|
$callback($prefix.$key); |
173
|
|
|
} |
174
|
2 |
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Appends a dot to the prefix if necessary. |
178
|
|
|
* |
179
|
|
|
* @param string $prefix Prefix to validate and possibly append dot to. |
180
|
|
|
* @return string Processed prefix. |
181
|
|
|
*/ |
182
|
1 |
|
private function prefix($prefix) |
183
|
|
|
{ |
184
|
1 |
|
if ($prefix) { |
185
|
1 |
|
$prefixLastChar = substr($prefix, -1); |
186
|
|
|
|
187
|
1 |
|
if ($prefixLastChar != '.' && $prefixLastChar != ':') { |
188
|
|
|
$prefix .= '.'; |
189
|
|
|
} |
190
|
1 |
|
} |
191
|
|
|
|
192
|
1 |
|
return $prefix; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
} |