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
|
19 |
|
public function __construct(Result $result) |
33
|
|
|
{ |
34
|
19 |
|
parent::__construct(); |
35
|
|
|
|
36
|
19 |
|
$this->result = $result->setOutput($this); |
37
|
19 |
|
} |
38
|
|
|
|
39
|
18 |
|
protected function locales(): array |
40
|
|
|
{ |
41
|
18 |
|
return (array) $this->argument('locales'); |
42
|
|
|
} |
43
|
|
|
|
44
|
14 |
|
protected function available(): array |
45
|
|
|
{ |
46
|
14 |
|
return Locale::available(); |
47
|
|
|
} |
48
|
|
|
|
49
|
5 |
|
protected function installed(): array |
50
|
|
|
{ |
51
|
5 |
|
return Locale::installed($this->wantsJson()); |
52
|
|
|
} |
53
|
|
|
|
54
|
11 |
|
protected function select(array $locales): array |
55
|
|
|
{ |
56
|
11 |
|
$question = sprintf($this->select_all_template, $this->action); |
57
|
|
|
|
58
|
11 |
|
return $this->confirm($question, $this->action_default) |
59
|
4 |
|
? ['*'] |
60
|
11 |
|
: $this->wrapSelectedValues($locales, $this->choiceLocales($locales)); |
61
|
|
|
} |
62
|
|
|
|
63
|
18 |
|
protected function isForce(): bool |
64
|
|
|
{ |
65
|
18 |
|
return $this->hasOption('force') && (bool) $this->option('force'); |
66
|
|
|
} |
67
|
|
|
|
68
|
18 |
|
protected function isFull(): bool |
69
|
|
|
{ |
70
|
18 |
|
return $this->hasOption('full') && (bool) $this->option('full'); |
71
|
|
|
} |
72
|
|
|
|
73
|
18 |
|
protected function wantsJson(): bool |
74
|
|
|
{ |
75
|
18 |
|
return (bool) $this->option('json') |
76
|
13 |
|
|| (bool) $this->option('jet') |
77
|
11 |
|
|| (bool) $this->option('fortify') |
78
|
9 |
|
|| (bool) $this->option('cashier') |
79
|
18 |
|
|| (bool) $this->option('nova'); |
80
|
|
|
} |
81
|
|
|
|
82
|
18 |
|
protected function setProcessor(string $php, string $json): void |
83
|
|
|
{ |
84
|
18 |
|
$this->processor = $this->wantsJson() ? $json : $php; |
85
|
18 |
|
} |
86
|
|
|
|
87
|
18 |
|
protected function exec(array $locales): void |
88
|
|
|
{ |
89
|
18 |
|
foreach ($this->getLocales($locales) as $locale) { |
90
|
18 |
|
$this->result->merge( |
91
|
18 |
|
$this->localization() |
92
|
18 |
|
->processor($this->getProcessor()) |
93
|
18 |
|
->force($this->isForce()) |
94
|
18 |
|
->full($this->isFull()) |
95
|
18 |
|
->run($locale) |
96
|
|
|
); |
97
|
|
|
} |
98
|
12 |
|
} |
99
|
|
|
|
100
|
18 |
|
protected function getLocales(array $locales): array |
101
|
|
|
{ |
102
|
18 |
|
$items = $this->locales() ?: $this->select($locales); |
103
|
|
|
|
104
|
18 |
|
return $items === ['*'] ? $locales : $items; |
105
|
|
|
} |
106
|
|
|
|
107
|
18 |
|
protected function localization(): Localizationable |
108
|
|
|
{ |
109
|
18 |
|
return app(Localization::class); |
110
|
|
|
} |
111
|
|
|
|
112
|
7 |
|
protected function wrapSelectedValues(array $available, $selected): array |
113
|
|
|
{ |
114
|
7 |
|
return Arr::wrap( |
115
|
7 |
|
is_numeric($selected) |
116
|
|
|
? Arr::get($available, (int) $selected) |
117
|
7 |
|
: $selected |
118
|
|
|
); |
119
|
|
|
} |
120
|
|
|
|
121
|
7 |
|
protected function choiceLocales(array $locales) |
122
|
|
|
{ |
123
|
7 |
|
return $this->choice( |
124
|
7 |
|
sprintf($this->select_template, $this->action), |
125
|
|
|
$locales, |
126
|
7 |
|
null, |
127
|
7 |
|
null, |
128
|
7 |
|
true |
129
|
|
|
); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|