1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Helldar\LaravelLangPublisher\Console; |
4
|
|
|
|
5
|
|
|
use Helldar\LaravelLangPublisher\Concerns\Containable; |
6
|
|
|
use Helldar\LaravelLangPublisher\Concerns\Logger; |
7
|
|
|
use Helldar\LaravelLangPublisher\Concerns\Pathable; |
8
|
|
|
use Helldar\LaravelLangPublisher\Constants\Locales as LocalesList; |
9
|
|
|
use Helldar\LaravelLangPublisher\Contracts\Actionable; |
10
|
|
|
use Helldar\LaravelLangPublisher\Contracts\Processor; |
11
|
|
|
use Helldar\LaravelLangPublisher\Facades\Config; |
12
|
|
|
use Helldar\LaravelLangPublisher\Facades\Info; |
13
|
|
|
use Helldar\LaravelLangPublisher\Facades\Locales; |
14
|
|
|
use Helldar\LaravelLangPublisher\Facades\Packages; |
15
|
|
|
use Helldar\LaravelLangPublisher\Facades\Validator; |
16
|
|
|
use Helldar\LaravelLangPublisher\Services\Command\Locales as LocalesSupport; |
17
|
|
|
use Helldar\LaravelLangPublisher\Support\Info as InfoSupport; |
18
|
|
|
use Helldar\Support\Facades\Helpers\Arr; |
19
|
|
|
use Helldar\Support\Facades\Helpers\Filesystem\File; |
20
|
|
|
use Helldar\Support\Facades\Helpers\Str; |
21
|
|
|
use Illuminate\Console\Command; |
22
|
|
|
|
23
|
|
|
abstract class BaseCommand extends Command |
24
|
|
|
{ |
25
|
|
|
use Containable; |
26
|
|
|
use Logger; |
27
|
|
|
use Pathable; |
28
|
|
|
|
29
|
|
|
protected $action; |
30
|
|
|
|
31
|
|
|
protected $locales_length = 0; |
32
|
|
|
|
33
|
|
|
protected $files_length = 0; |
34
|
|
|
|
35
|
|
|
protected $files; |
36
|
|
|
|
37
|
|
|
protected $locales; |
38
|
|
|
|
39
|
15 |
|
public function handle() |
40
|
|
|
{ |
41
|
15 |
|
$this->setLogger(); |
42
|
15 |
|
$this->start(); |
43
|
15 |
|
$this->clean(); |
44
|
15 |
|
$this->ran(); |
45
|
12 |
|
$this->end(); |
46
|
12 |
|
} |
47
|
|
|
|
48
|
|
|
abstract protected function processor(): Processor; |
49
|
|
|
|
50
|
14 |
|
protected function ran(): void |
51
|
|
|
{ |
52
|
14 |
|
$this->log('Starting processing of the package list...'); |
53
|
|
|
|
54
|
14 |
|
foreach ($this->packages() as $package) { |
55
|
14 |
|
$this->log('Packages handling:', $package); |
56
|
|
|
|
57
|
14 |
|
$this->validatePackage($package); |
58
|
|
|
|
59
|
12 |
|
$this->ranLocales($package); |
60
|
|
|
} |
61
|
11 |
|
} |
62
|
|
|
|
63
|
12 |
|
protected function ranLocales(string $package): void |
64
|
|
|
{ |
65
|
12 |
|
$this->log('Starting processing of the locales list for the', $package, 'package...'); |
66
|
|
|
|
67
|
12 |
|
foreach ($this->locales() as $locale) { |
68
|
12 |
|
$this->log('Localization handling:', $locale); |
69
|
|
|
|
70
|
12 |
|
$this->validateLocale($locale); |
71
|
|
|
|
72
|
11 |
|
$this->ranFiles($package, $locale); |
73
|
|
|
} |
74
|
11 |
|
} |
75
|
|
|
|
76
|
11 |
|
protected function ranFiles(string $package, string $locale): void |
77
|
|
|
{ |
78
|
11 |
|
$this->log('Starting processing of the files for the', $package, 'package and', $locale, 'localization...'); |
79
|
|
|
|
80
|
11 |
|
foreach ($this->files($package) as $filename) { |
81
|
11 |
|
$this->log('Processing the localization file:', $filename); |
82
|
|
|
|
83
|
11 |
|
$this->processing($locale, $filename, $package); |
84
|
|
|
|
85
|
11 |
|
$status = $this->process($package, $locale, $filename); |
86
|
|
|
|
87
|
11 |
|
$this->processed($locale, $filename, $status, $package); |
88
|
|
|
} |
89
|
11 |
|
} |
90
|
|
|
|
91
|
11 |
|
protected function process(string $package, string $locale, string $filename): string |
92
|
|
|
{ |
93
|
11 |
|
$this->log('Launching the processor for localization:', $locale, ',', $filename); |
94
|
|
|
|
95
|
11 |
|
return $this->processor() |
96
|
11 |
|
->force($this->hasForce()) |
97
|
11 |
|
->package($package) |
98
|
11 |
|
->locale($locale) |
99
|
11 |
|
->filename($filename, $this->hasInline()) |
100
|
11 |
|
->run(); |
101
|
|
|
} |
102
|
|
|
|
103
|
12 |
|
protected function locales(): array |
104
|
|
|
{ |
105
|
12 |
|
$this->log('Getting a list of localizations...'); |
106
|
|
|
|
107
|
12 |
|
if (! empty($this->locales)) { |
108
|
11 |
|
return $this->locales; |
109
|
|
|
} |
110
|
|
|
|
111
|
12 |
|
return $this->locales = LocalesSupport::make($this->input, $this->output, $this->action(), $this->targetLocales())->get(); |
112
|
|
|
} |
113
|
|
|
|
114
|
7 |
|
protected function targetLocales(): array |
115
|
|
|
{ |
116
|
7 |
|
$this->log('Getting a list of installed localizations...'); |
117
|
|
|
|
118
|
7 |
|
return Locales::installed(); |
119
|
|
|
} |
120
|
|
|
|
121
|
15 |
|
protected function packages(): array |
122
|
|
|
{ |
123
|
15 |
|
$this->log('Getting a list of packages available for processing...'); |
124
|
|
|
|
125
|
15 |
|
return Packages::get(); |
126
|
|
|
} |
127
|
|
|
|
128
|
12 |
|
protected function files(string $package): array |
129
|
|
|
{ |
130
|
12 |
|
$this->log('Getting a list of files for the ', $package, 'package...'); |
131
|
|
|
|
132
|
12 |
|
if ($this->files[$package] ?? false) { |
133
|
11 |
|
return $this->files[$package]; |
134
|
|
|
} |
135
|
|
|
|
136
|
12 |
|
$path = $this->pathSource($package, LocalesList::ENGLISH); |
137
|
|
|
|
138
|
12 |
|
return $this->files[$package] = File::names($path, static function ($filename) { |
139
|
12 |
|
return ! Str::contains($filename, 'inline'); |
140
|
12 |
|
}); |
141
|
|
|
} |
142
|
|
|
|
143
|
15 |
|
protected function start(): void |
144
|
|
|
{ |
145
|
15 |
|
$this->log('Running the console command:', parent::class); |
146
|
|
|
|
147
|
15 |
|
$action = $this->action()->present(true); |
148
|
|
|
|
149
|
15 |
|
$this->info($action . ' localizations...'); |
150
|
15 |
|
} |
151
|
|
|
|
152
|
12 |
|
protected function end(): void |
153
|
|
|
{ |
154
|
12 |
|
$this->log('Completing the execution of the console command...'); |
155
|
|
|
|
156
|
12 |
|
$action = $this->action()->past(); |
157
|
|
|
|
158
|
12 |
|
$this->info('Localizations have ben successfully ' . $action . '.'); |
159
|
12 |
|
} |
160
|
|
|
|
161
|
12 |
|
protected function processing(string $locale, string $filename, string $package = null): void |
162
|
|
|
{ |
163
|
12 |
|
$this->log('Displaying a message about the start of file processing: locale is', $locale, ', filename is', $filename, ', package is', $package . '...'); |
164
|
|
|
|
165
|
12 |
|
$message = $this->message($locale, $filename, $package)->start(); |
166
|
|
|
|
167
|
12 |
|
$this->output->write($message); |
168
|
12 |
|
} |
169
|
|
|
|
170
|
12 |
|
protected function processed(string $locale, string $filename, string $status, string $package = null): void |
171
|
|
|
{ |
172
|
12 |
|
$this->log('Displaying a message about the finish of file processing: locale is', $locale, ', filename is', $filename, ', package is', $package . '...'); |
173
|
|
|
|
174
|
12 |
|
$message = $this->message($locale, $filename, $package)->finish($status); |
175
|
|
|
|
176
|
12 |
|
$this->output->writeln($message); |
177
|
12 |
|
} |
178
|
|
|
|
179
|
12 |
|
protected function message(string $locale, string $filename, string $package = null): InfoSupport |
180
|
|
|
{ |
181
|
12 |
|
$this->log('Preparing an object for displaying a message: locale is', $locale, ', filename is', $filename, ', package is', $package . '...'); |
182
|
|
|
|
183
|
12 |
|
return Info::same() |
184
|
12 |
|
->package($package) |
185
|
12 |
|
->locale($locale, $this->localesLength()) |
186
|
12 |
|
->filename($filename, $this->filesLength()); |
187
|
|
|
} |
188
|
|
|
|
189
|
12 |
|
protected function localesLength(): int |
190
|
|
|
{ |
191
|
12 |
|
$this->log('Getting the maximum length of a localization string...'); |
192
|
|
|
|
193
|
12 |
|
if ($this->locales_length > 0) { |
194
|
12 |
|
return $this->locales_length; |
195
|
|
|
} |
196
|
|
|
|
197
|
12 |
|
$this->log('Calculating the maximum length of a localization string...'); |
198
|
|
|
|
199
|
12 |
|
return $this->locales_length = Arr::longestStringLength($this->locales()); |
200
|
|
|
} |
201
|
|
|
|
202
|
12 |
|
protected function filesLength(): int |
203
|
|
|
{ |
204
|
12 |
|
$this->log('Getting the maximum length of a filenames...'); |
205
|
|
|
|
206
|
12 |
|
if ($this->files_length > 0) { |
207
|
12 |
|
return $this->files_length; |
208
|
|
|
} |
209
|
|
|
|
210
|
12 |
|
$this->log('Calculating the maximum length of a filenames...'); |
211
|
|
|
|
212
|
12 |
|
$files = []; |
213
|
|
|
|
214
|
12 |
|
foreach ($this->packages() as $package) { |
215
|
12 |
|
$files = array_merge($files, $this->files($package)); |
216
|
|
|
} |
217
|
|
|
|
218
|
12 |
|
return $this->files_length = Arr::longestStringLength(array_unique($files)); |
219
|
|
|
} |
220
|
|
|
|
221
|
11 |
|
protected function hasInline(): bool |
222
|
|
|
{ |
223
|
11 |
|
$this->log('Getting a use case for a validation file.'); |
224
|
|
|
|
225
|
11 |
|
return Config::hasInline(); |
226
|
|
|
} |
227
|
|
|
|
228
|
15 |
|
protected function action(): Actionable |
229
|
|
|
{ |
230
|
15 |
|
$this->log('Getting the action...'); |
231
|
|
|
|
232
|
15 |
|
return $this->container($this->action); |
233
|
|
|
} |
234
|
|
|
|
235
|
10 |
|
protected function hasForce(): bool |
236
|
|
|
{ |
237
|
10 |
|
$this->log('Getting the value of the "force" option...'); |
238
|
|
|
|
239
|
10 |
|
return $this->boolOption('force'); |
240
|
|
|
} |
241
|
|
|
|
242
|
2 |
|
protected function hasFull(): bool |
243
|
|
|
{ |
244
|
2 |
|
$this->log('Getting the value of the "full" option...'); |
245
|
|
|
|
246
|
2 |
|
return $this->boolOption('full'); |
247
|
|
|
} |
248
|
|
|
|
249
|
10 |
|
protected function boolOption(string $key): bool |
250
|
|
|
{ |
251
|
10 |
|
$this->log('Getting the value of the "', $key, '" option...'); |
252
|
|
|
|
253
|
10 |
|
return $this->hasOption($key) && $this->option($key); |
254
|
|
|
} |
255
|
|
|
|
256
|
13 |
|
protected function validateLocale(string $locale): void |
257
|
|
|
{ |
258
|
13 |
|
$this->log('Calling the localization validation method: ', $locale, '...'); |
259
|
|
|
|
260
|
13 |
|
Validator::locale($locale); |
261
|
12 |
|
} |
262
|
|
|
|
263
|
14 |
|
protected function validatePackage(string $package): void |
264
|
|
|
{ |
265
|
14 |
|
$this->log('Calling the package validation method: ', $package, '...'); |
266
|
|
|
|
267
|
14 |
|
Validator::package($package); |
268
|
12 |
|
} |
269
|
|
|
|
270
|
4 |
|
protected function doesntProtect(string $locale): bool |
271
|
|
|
{ |
272
|
4 |
|
$this->log('Check if the localization is among the protected:', $locale); |
273
|
|
|
|
274
|
4 |
|
return ! Locales::isProtected($locale); |
275
|
|
|
} |
276
|
|
|
|
277
|
15 |
|
protected function clean(): void |
278
|
|
|
{ |
279
|
15 |
|
$this->log('Clear the variable from the saved localizations...'); |
280
|
|
|
|
281
|
15 |
|
$this->locales = null; |
282
|
15 |
|
} |
283
|
|
|
} |
284
|
|
|
|