1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Helldar\LaravelLangPublisher\Console; |
4
|
|
|
|
5
|
|
|
use Helldar\LaravelLangPublisher\Contracts\Localizationable; |
6
|
|
|
use Helldar\LaravelLangPublisher\Services\Localization; |
7
|
|
|
use Helldar\LaravelLangPublisher\Support\Result; |
8
|
|
|
use Helldar\LaravelLangPublisher\Traits\Containable; |
9
|
|
|
use Helldar\LaravelLangPublisher\Traits\Containers\Pathable; |
10
|
|
|
use Helldar\LaravelLangPublisher\Traits\Containers\Processable; |
11
|
|
|
use Illuminate\Console\Command; |
12
|
|
|
use Illuminate\Support\Arr; |
13
|
|
|
|
14
|
|
|
abstract class BaseCommand extends Command |
15
|
|
|
{ |
16
|
|
|
use Containable; |
17
|
|
|
use Processable; |
18
|
|
|
use Pathable; |
19
|
|
|
|
20
|
|
|
/** @var \Helldar\LaravelLangPublisher\Support\Result */ |
21
|
|
|
protected $result; |
22
|
36 |
|
|
23
|
|
|
protected $select_template = 'What languages to %s? (specify the necessary localizations separated by commas)'; |
24
|
36 |
|
|
25
|
|
|
protected $select_all_template = 'Do you want to %s all localizations?'; |
26
|
36 |
|
|
27
|
36 |
|
protected $action = 'install'; |
28
|
|
|
|
29
|
12 |
|
public function __construct(Result $result) |
30
|
|
|
{ |
31
|
12 |
|
parent::__construct(); |
32
|
|
|
|
33
|
|
|
$this->result = $result->setOutput($this); |
34
|
12 |
|
} |
35
|
|
|
|
36
|
12 |
|
protected function locales(): array |
37
|
|
|
{ |
38
|
|
|
return (array) $this->argument('locales'); |
39
|
12 |
|
} |
40
|
|
|
|
41
|
12 |
|
protected function select(array $locales): array |
42
|
|
|
{ |
43
|
|
|
$question = sprintf($this->select_all_template, $this->action); |
44
|
12 |
|
|
45
|
|
|
return $this->confirm($question, false) |
46
|
12 |
|
? ['*'] |
47
|
12 |
|
: $this->wrapSelectedValues($locales, $this->choiceLocales($locales)); |
48
|
|
|
} |
49
|
12 |
|
|
50
|
|
|
protected function isForce(): bool |
51
|
12 |
|
{ |
52
|
12 |
|
return $this->hasOption('force') && (bool) $this->option('force'); |
53
|
12 |
|
} |
54
|
12 |
|
|
55
|
12 |
|
protected function wantsJson(): bool |
56
|
12 |
|
{ |
57
|
|
|
return (bool) $this->option('json'); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
protected function setProcessor(string $php, string $json): void |
61
|
12 |
|
{ |
62
|
|
|
$this->processor = $this->wantsJson() ? $json : $php; |
63
|
12 |
|
} |
64
|
|
|
|
65
|
12 |
|
protected function exec(array $locales): void |
66
|
|
|
{ |
67
|
|
|
foreach ($this->getLocales($locales) as $locale) { |
68
|
12 |
|
$this->result->merge( |
69
|
|
|
$this->localization() |
70
|
12 |
|
->setPath($this->getPath()) |
71
|
|
|
->setProcessor($this->getProcessor()) |
72
|
|
|
->run($locale, $this->isForce()) |
73
|
|
|
); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
protected function getLocales(array $locales): array |
78
|
|
|
{ |
79
|
|
|
$items = $this->locales() ?: $this->select($locales); |
80
|
|
|
|
81
|
|
|
return $items === ['*'] ? $locales : $items; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
protected function localization(): Localizationable |
85
|
|
|
{ |
86
|
|
|
return app(Localization::class); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
protected function wrapSelectedValues(array $available, $selected): array |
90
|
|
|
{ |
91
|
|
|
return Arr::wrap( |
92
|
|
|
is_numeric($selected) |
93
|
|
|
? Arr::get($available, (int) $selected) |
94
|
|
|
: $selected |
95
|
|
|
); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
protected function choiceLocales(array $locales) |
99
|
|
|
{ |
100
|
|
|
return $this->choice( |
101
|
|
|
sprintf($this->select_template, $this->action), |
102
|
|
|
$locales, |
103
|
|
|
null, |
104
|
|
|
null, |
105
|
|
|
true |
106
|
|
|
); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|