1 | <?php |
||
2 | |||
3 | namespace Hokan22\LaravelTranslator\Commands; |
||
4 | |||
5 | use Hokan22\LaravelTranslator\Models\TranslationIdentifier; |
||
6 | use Hokan22\LaravelTranslator\Models\Translations; |
||
7 | use Hokan22\LaravelTranslator\TranslatorFacade; |
||
8 | use Illuminate\Support\Facades\File; |
||
9 | use Illuminate\Console\Command; |
||
10 | |||
11 | class ClearUnusedTranslationsCommand extends Command |
||
12 | { |
||
13 | |||
14 | /** |
||
15 | * The name and signature of the console command. |
||
16 | * |
||
17 | * @var string |
||
18 | */ |
||
19 | protected $signature = 'translator:clear-unused'; |
||
20 | |||
21 | /** |
||
22 | * The console command description. |
||
23 | * |
||
24 | * @var string |
||
25 | */ |
||
26 | protected $description = ''; |
||
27 | |||
28 | protected $cache = []; |
||
29 | |||
30 | protected $found_identifier = 0; |
||
31 | |||
32 | protected $folders; |
||
33 | protected $extensions; |
||
34 | |||
35 | |||
36 | /** |
||
37 | * Create a new command instance. |
||
38 | * |
||
39 | */ |
||
40 | public function __construct() { |
||
41 | parent::__construct(); |
||
42 | } |
||
43 | |||
44 | public function handle() { |
||
45 | // Get start time |
||
46 | $start = microtime(true); |
||
47 | $not_used = 0; |
||
48 | $found_plain = 0; |
||
49 | $removed = 0; |
||
50 | |||
51 | $this->folders = TranslatorFacade::getConfigValue('search_folders'); |
||
52 | $this->extensions = TranslatorFacade::getConfigValue('search_extensions'); |
||
53 | |||
54 | $aFiles = $this->getAllIdentifier(); |
||
55 | |||
56 | $aDB = $this->loadFromDB(); |
||
57 | |||
58 | foreach ($aDB as $identifier) { |
||
59 | |||
60 | if (!in_array($identifier->identifier, $aFiles)) { |
||
61 | |||
62 | $found_as_plain = $this->verifyMissing($identifier->identifier); |
||
63 | |||
64 | $this->line(''); |
||
65 | |||
66 | if ($found_as_plain) { |
||
67 | $this->warn('\'' . $identifier->identifier . '\' was not found withing Translator directives'); |
||
68 | $found_plain++; |
||
69 | } else { |
||
70 | $this->line('\'' . $identifier->identifier . '\' seems to be not used anymore'); |
||
71 | $not_used++; |
||
72 | } |
||
73 | |||
74 | $task = $this->choice('What do you want me to do?', ['Nothing', 'Remove'], 0); |
||
75 | |||
76 | if ($task === 'Remove') { |
||
77 | $identifier->delete(); |
||
78 | Translations::where('translation_identifier_id', $identifier->id)->delete(); |
||
79 | $removed++; |
||
80 | } |
||
81 | } |
||
82 | } |
||
83 | |||
84 | $this->table(['Num', 'Identifier'], [ |
||
85 | [$this->found_identifier, "In DB"], |
||
86 | [$not_used, "Not Found"], |
||
87 | [$found_plain, "Found Plain"], |
||
88 | [$removed, "Removed"], |
||
89 | ]); |
||
90 | |||
91 | $this->info($not_used . ' Translations no longer used.'); |
||
92 | $this->line(''); |
||
93 | |||
94 | $this->info('Finished in: ' . number_format(microtime(true) - $start, 2) . 'sec'); |
||
95 | } |
||
96 | |||
97 | /** |
||
98 | * @param $identifier string The Identifier to search |
||
99 | * @return boolean boolean True if Identifier was found false if it was not |
||
100 | */ |
||
101 | public function verifyMissing($identifier) { |
||
102 | $aFiles = []; |
||
103 | $found = false; |
||
104 | |||
105 | $valid_extensions = ['php', 'html', 'js']; |
||
106 | $folders = [ |
||
107 | 'app', |
||
108 | 'config', |
||
109 | 'resources/views', |
||
110 | 'resources/assets/js', |
||
111 | ]; |
||
112 | |||
113 | foreach ($folders as $folder) { |
||
114 | $aFiles = array_merge($aFiles, File::allFiles(base_path() . '/' . $folder)); |
||
115 | } |
||
116 | |||
117 | $num_files = count($aFiles); |
||
118 | |||
119 | $this->bar = $this->output->createProgressBar($num_files); |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||
120 | $this->bar->setMessage('Analyzing ' . $num_files . ' files'); |
||
121 | $this->bar->setFormat('very_verbose'); |
||
122 | |||
123 | $pattern = preg_quote($identifier, '/'); |
||
124 | $pattern = "/^.*$pattern.*\$/m"; |
||
125 | |||
126 | /** @var File $file */ |
||
127 | foreach ($aFiles as $file) { |
||
128 | |||
129 | $extension = $file->getExtension(); |
||
130 | |||
131 | if (in_array($extension, $valid_extensions)) { |
||
132 | |||
133 | $content = file_get_contents($file); |
||
134 | if (preg_match_all($pattern, $content, $matches)) { |
||
135 | $this->bar->clear(); |
||
136 | $this->warn('\'' . $identifier . '\' is used in: \'' . $file->getPath() . '\'!'); |
||
137 | |||
138 | foreach ($matches[0] as $match) { |
||
139 | $this->warn($match); |
||
140 | } |
||
141 | $found = true; |
||
142 | $this->bar->display(); |
||
143 | } |
||
144 | } |
||
145 | $this->bar->advance(); |
||
146 | } |
||
147 | $this->bar->finish(); |
||
148 | |||
149 | return $found; |
||
150 | } |
||
151 | |||
152 | /** |
||
153 | * @return array |
||
154 | */ |
||
155 | public function getAllIdentifier() { |
||
156 | $aFiles = []; |
||
157 | $return = []; |
||
158 | |||
159 | $regexes = [ |
||
160 | 'default' => '/(?:[\@|\_]t\()["\'](?\'identifier\'.*?)["\'](?:\)|(?:, (?\'parameters\'\[.*\]))(?:\)|, \'(?\'locale\'\w*?)\'))/', |
||
161 | 'js' => '/\$filter\(\'translate\'\)\(\'(?\'identifier\'.*?)\'\)/' |
||
162 | ]; |
||
163 | |||
164 | foreach ($this->folders as $folder) { |
||
165 | $aFiles = array_merge($aFiles, File::allFiles(base_path() . '/' . $folder)); |
||
166 | } |
||
167 | |||
168 | //TranslatorFacade::setLocale('de_DE'); |
||
169 | |||
170 | $num_files = count($aFiles); |
||
171 | |||
172 | $this->bar = $this->output->createProgressBar($num_files); |
||
0 ignored issues
–
show
|
|||
173 | $this->bar->setMessage('Analyzing ' . $num_files . ' files'); |
||
174 | $this->bar->setFormat('very_verbose'); |
||
175 | |||
176 | foreach ($aFiles as $file) { |
||
177 | |||
178 | $extension = $file->getExtension(); |
||
179 | |||
180 | if (in_array($extension, $this->extensions)) { |
||
181 | $content = file_get_contents($file); |
||
182 | |||
183 | foreach ($regexes as $key => $regex) { |
||
184 | preg_match_all($regex, $content, $result, PREG_SET_ORDER); |
||
185 | |||
186 | if (!empty($result[0])) { |
||
187 | foreach ($result as $item) { |
||
188 | $this->found_identifier++; |
||
189 | $return[] = $item['identifier']; |
||
190 | } |
||
191 | } |
||
192 | } |
||
193 | } |
||
194 | $this->bar->advance(); |
||
195 | } |
||
196 | |||
197 | $this->bar->finish(); |
||
198 | $this->line(''); |
||
199 | |||
200 | $this->info($this->found_identifier . ' Translations found.'); |
||
201 | |||
202 | return $return; |
||
203 | } |
||
204 | |||
205 | /** |
||
206 | * Get all translation identifier with translation from the given locale |
||
207 | * |
||
208 | * @return TranslationIdentifier|\Illuminate\Database\Eloquent\Collection|static[] |
||
209 | */ |
||
210 | private function loadFromDB() { |
||
211 | // Get all Texts with translations for the given locale |
||
212 | $trans_identifier = TranslationIdentifier::select(['id', 'identifier'])->get(); |
||
213 | |||
214 | return $trans_identifier; |
||
215 | } |
||
216 | } |
||
217 |