procyonweb /
laravel-translation-generator
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace ProcyonWeb\TranslationGenerator; |
||
| 6 | |||
| 7 | use Illuminate\Support\Collection; |
||
| 8 | |||
| 9 | class SearchService |
||
| 10 | { |
||
| 11 | public function getTranslatableStrings(array $patterns): array |
||
| 12 | { |
||
| 13 | $translations = []; |
||
| 14 | $files = $this->getFiles($patterns); |
||
| 15 | |||
| 16 | foreach ($files as $file) { |
||
| 17 | $handle = fopen($file, 'rb'); |
||
| 18 | $text = fread($handle, filesize($file)); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 19 | |||
| 20 | $re = '/(?:\_\_|\$t)\(\\\'((?:.(?!(?<![\\\\])\\\'))*.?)/m'; |
||
| 21 | preg_match_all($re, $text, $matches, PREG_SET_ORDER, 0); |
||
| 22 | |||
| 23 | foreach ($matches as $match) { |
||
| 24 | $translations[] = $match[1]; |
||
| 25 | } |
||
| 26 | } |
||
| 27 | |||
| 28 | return array_unique($translations); |
||
| 29 | } |
||
| 30 | |||
| 31 | public function getFiles(array $patterns): Collection |
||
| 32 | { |
||
| 33 | $files = new Collection(); |
||
| 34 | |||
| 35 | foreach ($patterns as $path => $pattern) { |
||
| 36 | $directory = new \RecursiveDirectoryIterator($path); |
||
| 37 | $iterator = new \RecursiveIteratorIterator($directory); |
||
| 38 | $regexIterator = new \RegexIterator($iterator, $pattern, \RecursiveRegexIterator::GET_MATCH); |
||
| 39 | $fileArray = new Collection(iterator_to_array($regexIterator)); |
||
| 40 | $files = $files->merge($fileArray->flatten()); |
||
| 41 | } |
||
| 42 | |||
| 43 | return $files; |
||
| 44 | } |
||
| 45 | } |
||
| 46 |