-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTranslAnalyseCommand.php
192 lines (153 loc) · 8.44 KB
/
TranslAnalyseCommand.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
<?php
declare(strict_types=1);
namespace Transl\Commands;
use NumberFormatter;
use Illuminate\Support\Number;
use Illuminate\Console\Command;
use Transl\Commands\Concerns\UsesBranch;
use Transl\Commands\Concerns\UsesConfig;
use Transl\Support\Contracts\Driverable;
use Transl\Commands\Concerns\UsesProject;
use Transl\Commands\Concerns\NeedsHelpers;
use Transl\Support\Analysis\ProjectAnalysis;
use Transl\Commands\Concerns\OutputsRecapToConsole;
use Transl\Config\ProjectConfigurationDriverCollection;
class TranslAnalyseCommand extends Command
{
use UsesConfig;
use UsesProject;
use UsesBranch;
use OutputsRecapToConsole;
use NeedsHelpers;
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = '
transl:analyse
{--project= : The configured project to target (should be the `auth_key` or the `name` of a project defined in `config(transl.projects)`)}
{--branch= : The branch on the defined project to target}
';
/**
* The console command description.
*
* @var string
*/
protected $description = "Analyses the defined project's translation lines";
/**
* Execute the console command.
*/
public function handle(): int
{
$this->hydrateProperties();
$this->outputRecap();
$this->handler();
return $this->handled();
}
/* Hydration
------------------------------------------------*/
protected function hydrateProperties(): void
{
$this->hydrateConfigProperty();
$this->hydrateProjectProperty($this->optionAsNullableString('project'));
$this->hydrateBranchProperty($this->optionAsNullableString('branch'));
}
/* Actions
------------------------------------------------*/
protected function drivers(): ProjectConfigurationDriverCollection
{
return $this->project->drivers;
}
protected function handler(): void
{
foreach ($this->drivers() as $driverClass => $driverParams) {
/** @var Driverable $driver */
$driver = app($driverClass, $driverParams);
$this->newLine();
$this->outputAnalysis(
$driver,
ProjectAnalysis::fromTranslationSets($driver->getTranslationSets($this->project, $this->branch)),
);
}
}
protected function handled(): int
{
$this->newLine(2);
return self::SUCCESS;
}
/* Console outputs
------------------------------------------------*/
protected function outputAnalysis(Driverable $driver, ProjectAnalysis $analysis): void
{
$this->components->bulletList([
'Using driver: ' . $driver::class,
]);
$isVerbose = $this->getOutput()->isVerbose();
$isVeryVerbose = $this->getOutput()->isVeryVerbose();
$num = fn (int $value): string => $this->formatNumber($value);
foreach ($analysis->locales as $locale => $analysedLocale) {
$this->components->twoColumnDetail("• <fg=cyan;options=bold>{$locale}</>");
// Output Translation sets
if ($isVerbose) {
$this->components->twoColumnDetail("·· <fg=yellow;options=bold>Translation sets</>");
foreach ($analysedLocale->translation_sets as $translationSetKey => $analysedSet) {
$this->components->twoColumnDetail("···· {$translationSetKey}");
// Output Translation Lines
if ($isVeryVerbose) {
$this->components->twoColumnDetail("······ <fg=yellow;options=bold>Lines</>");
foreach ($analysedSet->lines as $translationLineKey => $analysedLine) {
$words = $num($analysedLine->word_count) . ($analysedLine->word_count > 1 ? ' words' : ' word');
$characters = $num($analysedLine->character_count) . ($analysedLine->character_count > 1 ? ' characters' : ' character');
$this->components->twoColumnDetail("········ <fg=gray;options=bold>{$translationLineKey}</>", "<fg=gray;options=bold>{$words} / {$characters}</>");
}
$this->components->twoColumnDetail("······ <fg=yellow;options=bold>Summary</>");
$this->components->twoColumnDetail("········ Translation lines", "{$num($analysedSet->summary->translation_line_count)}");
$this->components->twoColumnDetail("········ Translation line words", "{$num($analysedSet->summary->translation_line_word_count)}");
$this->components->twoColumnDetail("········ Translation line characters", "{$num($analysedSet->summary->translation_line_character_count)}");
} else {
$this->components->twoColumnDetail("······ <fg=gray;options=bold>Translation lines</>", "<fg=gray;options=bold>{$num($analysedSet->summary->translation_line_count)}</>");
$this->components->twoColumnDetail("······ <fg=gray;options=bold>Translation line words</>", "<fg=gray;options=bold>{$num($analysedSet->summary->translation_line_word_count)}</>");
$this->components->twoColumnDetail("······ <fg=gray;options=bold>Translation line characters</>", "<fg=gray;options=bold>{$num($analysedSet->summary->translation_line_character_count)}</>");
}
}
$this->components->twoColumnDetail("·· <fg=yellow;options=bold>`{$locale}` summary</>");
$this->components->twoColumnDetail("···· Translation sets", "{$num($analysedLocale->summary->translation_set_count)}");
$this->components->twoColumnDetail("···· Translation lines", "{$num($analysedLocale->summary->translation_line_count)}");
$this->components->twoColumnDetail("···· Translation line words", "{$num($analysedLocale->summary->translation_line_word_count)}");
$this->components->twoColumnDetail("···· Translation line characters", "{$num($analysedLocale->summary->translation_line_character_count)}");
} else {
$this->components->twoColumnDetail("·· Translation sets", "{$num($analysedLocale->summary->translation_set_count)}");
$this->components->twoColumnDetail("·· Translation lines", "{$num($analysedLocale->summary->translation_line_count)}");
$this->components->twoColumnDetail("·· Translation line words", "{$num($analysedLocale->summary->translation_line_word_count)}");
$this->components->twoColumnDetail("·· Translation line characters", "{$num($analysedLocale->summary->translation_line_character_count)}");
}
}
$this->components->twoColumnDetail("• <fg=yellow;options=bold>Project summary</>");
$this->components->twoColumnDetail("·· Unique translation keys", "{$num($analysis->summary->unique_translation_key_count)}");
$this->components->twoColumnDetail("·· Unique translation sets", "{$num($analysis->summary->unique_translation_set_count)}");
$this->components->twoColumnDetail("·· Translation keys", "{$num($analysis->summary->translation_key_count)}");
$this->components->twoColumnDetail("·· Translation sets", "{$num($analysis->summary->translation_set_count)}");
$this->components->twoColumnDetail("·· Translation lines", "{$num($analysis->summary->translation_line_count)}");
$this->components->twoColumnDetail("·· Translation line words", "{$num($analysis->summary->translation_line_word_count)}");
$this->components->twoColumnDetail("·· Translation line characters", "{$num($analysis->summary->translation_line_character_count)}");
}
/* Helpers
------------------------------------------------*/
protected function formatNumber(int $value): string
{
if (!extension_loaded('intl')) {
return (string) $value;
}
$formatted = class_exists(Number::class)
? Number::format($value, locale: app()->getLocale())
: (function () use ($value): string|bool {
$formatter = new NumberFormatter(app()->getLocale(), NumberFormatter::DECIMAL);
return $formatter->format($value);
})();
if (is_bool($formatted)) {
return (string) $value;
}
return str($formatted)->ascii()->value();
}
}