1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Helldar\LaravelLangPublisher\Services\Command; |
4
|
|
|
|
5
|
|
|
use Helldar\LaravelLangPublisher\Concerns\Logger; |
6
|
|
|
use Helldar\LaravelLangPublisher\Contracts\Actionable; |
7
|
|
|
use Helldar\Support\Concerns\Makeable; |
8
|
|
|
use Illuminate\Console\OutputStyle; |
9
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @method static Locales make(InputInterface $input, OutputStyle $output, Actionable $action, array $locales) |
13
|
|
|
*/ |
14
|
|
|
final class Locales |
15
|
|
|
{ |
16
|
|
|
use Logger; |
17
|
|
|
use Makeable; |
18
|
|
|
|
19
|
|
|
protected $input; |
20
|
|
|
|
21
|
|
|
protected $output; |
22
|
|
|
|
23
|
|
|
protected $action; |
24
|
|
|
|
25
|
|
|
protected $locales = []; |
26
|
|
|
|
27
|
|
|
protected $select_template = 'What languages to %s? (specify the necessary localizations separated by commas)'; |
28
|
|
|
|
29
|
|
|
protected $select_all_template = 'Do you want to %s all localizations?'; |
30
|
|
|
|
31
|
12 |
|
public function __construct(InputInterface $input, OutputStyle $output, Actionable $action, array $locales) |
32
|
|
|
{ |
33
|
12 |
|
$this->log('Object initialization:', self::class); |
34
|
|
|
|
35
|
12 |
|
$this->input = $input; |
36
|
12 |
|
$this->output = $output; |
37
|
12 |
|
$this->action = $action; |
38
|
12 |
|
$this->locales = $locales; |
39
|
12 |
|
} |
40
|
|
|
|
41
|
12 |
|
public function get(): array |
42
|
|
|
{ |
43
|
12 |
|
$this->log('Getting a list of localizations...'); |
44
|
|
|
|
45
|
12 |
|
$input = $this->input(); |
46
|
|
|
|
47
|
12 |
|
if ($this->hasAll($input) && $this->confirm()) { |
48
|
2 |
|
$this->log('Returning a list of all localizations...'); |
49
|
|
|
|
50
|
2 |
|
return $this->locales; |
51
|
|
|
} |
52
|
|
|
|
53
|
10 |
|
if (! empty($input)) { |
54
|
8 |
|
$this->log('Returning a input list of localizations...'); |
55
|
|
|
|
56
|
8 |
|
return $this->correctLocalesList($input); |
57
|
|
|
} |
58
|
|
|
|
59
|
3 |
|
$this->log('Asking what localizations need to be done...'); |
60
|
|
|
|
61
|
3 |
|
return $this->correctLocalesList($this->select()); |
62
|
|
|
} |
63
|
|
|
|
64
|
3 |
|
protected function select(): array |
65
|
|
|
{ |
66
|
3 |
|
$this->log('Displaying an interactive question with a choice of localizations...'); |
67
|
|
|
|
68
|
3 |
|
if ($locales = $this->ask()) { |
69
|
3 |
|
return $locales; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return $this->ask(); |
73
|
|
|
} |
74
|
|
|
|
75
|
5 |
|
protected function confirm(): bool |
76
|
|
|
{ |
77
|
5 |
|
$this->log('Confirmation of processing of all localizations...'); |
78
|
|
|
|
79
|
5 |
|
return $this->output->confirm($this->confirmQuestion()); |
80
|
|
|
} |
81
|
|
|
|
82
|
3 |
|
protected function ask(): ?array |
83
|
|
|
{ |
84
|
3 |
|
$this->log('Localization selection request...'); |
85
|
|
|
|
86
|
3 |
|
return (array) $this->output->choice($this->choiceQuestion(), $this->locales); |
87
|
|
|
} |
88
|
|
|
|
89
|
12 |
|
protected function input(): array |
90
|
|
|
{ |
91
|
12 |
|
$this->log('Getting a list of localizations from arguments...'); |
92
|
|
|
|
93
|
12 |
|
return (array) $this->input->getArgument('locales'); |
94
|
|
|
} |
95
|
|
|
|
96
|
3 |
|
protected function choiceQuestion(): string |
97
|
|
|
{ |
98
|
3 |
|
return sprintf($this->select_template, $this->action->future()); |
99
|
|
|
} |
100
|
|
|
|
101
|
5 |
|
protected function confirmQuestion(): string |
102
|
|
|
{ |
103
|
5 |
|
return sprintf($this->select_all_template, $this->action->future()); |
104
|
|
|
} |
105
|
|
|
|
106
|
10 |
|
protected function correctLocalesList(array $locales): array |
107
|
|
|
{ |
108
|
10 |
|
$this->log('Correction of the array of localizations...'); |
109
|
|
|
|
110
|
10 |
|
return $this->hasAll($locales) ? $this->locales : $locales; |
111
|
|
|
} |
112
|
|
|
|
113
|
12 |
|
protected function hasAll(array $locales): bool |
114
|
|
|
{ |
115
|
12 |
|
$this->log('Checking for occurrence of the return character of all localizations...'); |
116
|
|
|
|
117
|
12 |
|
return in_array('*', $locales, true) || empty($locales); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|