-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFindMissing.php
217 lines (189 loc) · 5.84 KB
/
FindMissing.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
<?php
namespace clentfort\LaravelFindJsLocalizations;
use Illuminate\Console\Command;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Collection;
use Symfony\Component\Console\Output\OutputInterface;
class FindMissing extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'js-localization:missing';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Scans the JavaScript files for translation-keys '.
'that are missing from the translation-files.';
/**
* The config for the command.
*
* @var array
*/
protected $config;
/**
* @var Filesystem
*/
private $filesystem;
/**
* Create a new command instance.
*
* @param array $config The configuration for this command
*/
public function __construct(Filesystem $filesystem, array $config)
{
parent::__construct();
$this->filesystem = $filesystem;
$this->config = $config;
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$this->line(
"Analysing JavaScript-files in {$this->config['directory']}, this ".
'might take a while.'
);
$this->line('');
$keyList = $this->getKeyList();
$keySets = $this->getAllKeySets();
$keySets->each(function ($keySet, $path) use ($keyList) {
list(
$missingKeys,
$missingKeyCount
) = $this->getKeysMissingFromSet($keySet, $keyList);
if ($missingKeyCount == 0) {
return;
}
if (!$this->confirm(
"Found ${missingKeyCount} keys missing in ${path}, do you ".
'want to add these?'
)) {
return;
}
$this->info("Adding keys in ${path}");
$this->addMissingKeysToKeySet($keySet, $missingKeys);
});
}
/**
* Gets a list of all translation-keys used in the JavaScript-files.
*
* @return Collection
*/
private function getKeyList()
{
list(
$fileKeyList,
$errors
) = (new KeyFinder(
$this->filesystem,
base_path($this->config['directory']),
$this->config['node_executable'],
$this->config['extension']
))->findKeys();
if (!$errors->isEmpty()) {
$this->warn(
'Some files could not be parsed, the following errors were '.
'reported:'
);
$errors->each(function ($error) {
$this->warn(" * ${error}");
});
}
return static::fileKeyListToKeyList($fileKeyList);
}
/**
* Finds keys that are in $keyList but not in $keySet.
*
* @param KeySet $keySet
* @param Collection $keyList
*
* @return array a collection of keys, grouped by their common preifx, that
* are missing from $keySet and the total number of foudn
* keys
*/
private function getKeysMissingFromSet(
KeySet $keySet,
Collection $keyList
) {
$missingKeys = KeySetDiffer::diffKeys($keySet, $keyList);
$missingKeyCount = $missingKeys->reduce(function ($total, $group) {
return $total + $group->count();
}, 0);
return [$missingKeys, $missingKeyCount];
}
private function addMissingKeysToKeySet(
KeySet $keySet,
Collection $missingKeys
) {
return $missingKeys->each(function ($keys, $prefix) use ($keySet) {
$currentKeys = $keySet->getKeysWithPrefix($prefix);
// $group contains a list of keys not yet in the keys of the
// key-set, since we want to use the keys as the indices of an
// associative array we need to flip them first.
$newKeys = [];
foreach ($keys as $key) {
$newKeys[$key] = sprintf(
$this->config['lemma'],
"${prefix}.${key}"
);
}
$allKeys = $currentKeys->merge($newKeys);
if ($this->isVerbose()) {
$this->info(" * Writing \"${prefix}.php\".");
}
$keySet->setKeysWithPrefix($prefix, $allKeys);
});
}
/**
* Determines the languages used by the application by looking at the
* directories in resources/lang.
*
* @return Collection A collection of [$dir => KeySet]
*/
private function getAllKeySets()
{
$languageDirsPath = base_path('resources/lang');
$languageDirs = $this->filesystem->directories($languageDirsPath);
$languages = [];
foreach ($languageDirs as $languageDir) {
$languages[$languageDir] = new KeySet(
$this->filesystem,
$languageDir
);
}
return new Collection($languages);
}
/**
* Checks if the command is run with the verbose-flag.
*
* @return bool
*/
private function isVerbose()
{
$verbosityLevel = $this->getOutput()->getVerbosity();
return $verbosityLevel >= OutputInterface::VERBOSITY_VERBOSE;
}
/**
* Extracts all keys from a file-key-list generated by KeyFinder#findKeys.
*
* @param Collection $fileKeyList
*
* @return Collection a collection of translation-keys
*/
private static function fileKeyListToKeyList($fileKeyList)
{
return $fileKeyList->reduce(function ($keys, $file) {
return $keys->merge(array_map(function ($key) {
return $key->value;
}, $file->keys));
}, new Collection());
}
}