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