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