diff --git a/src/Commands/MissingCommand.php b/src/Commands/MissingCommand.php index 7f9f9b6..534d523 100644 --- a/src/Commands/MissingCommand.php +++ b/src/Commands/MissingCommand.php @@ -3,6 +3,7 @@ namespace Elegantly\Translator\Commands; use Illuminate\Contracts\Console\PromptsForMissingInput; +use Laravel\Prompts\Progress; use function Laravel\Prompts\info; use function Laravel\Prompts\intro; @@ -15,32 +16,50 @@ class MissingCommand extends TranslatorCommand implements PromptsForMissingInput public $description = 'Display all the translation keys found in the codebase but not in the driver.'; + public function formatPath(string $path): string + { + return (string) str($path)->after(base_path()); + } + public function handle(): int { $locale = $this->argument('locale'); $sync = (bool) $this->option('sync'); + /** @var null|Progress $progress */ + $progress = null; + $translator = $this->getTranslator(); - $missing = $translator->getMissingTranslations($locale); + $missing = $translator->getMissingTranslations( + $locale, + start: function (int $total) use (&$progress) { + $progress = new Progress('Scanning your codebase', $total); + $progress->start(); + }, + progress: function (string $path) use (&$progress) { + $progress?->hint($this->formatPath($path)); + $progress?->advance(); + }, + end: fn () => $progress?->finish(), + ); + + $count = count($missing); intro('Using driver: '.$translator->driver::class); - note(count($missing).' missing keys detected.'); + note("{$count} missing keys detected."); table( headers: ['Key', 'Count', 'Files'], rows: collect($missing) ->map(function ($value, $key) { return [ - str($key)->limit(20)->value(), + (string) str($key)->limit(20), (string) $value['count'], - implode("\n", - array_map( - fn ($file) => str($file)->after(base_path()), - $value['files'], - ) - ), + collect($value['files']) + ->map(fn ($file) => $this->formatPath($file)) + ->join("\n"), ]; })->values()->all() ); @@ -52,7 +71,7 @@ public function handle(): int values: array_map(fn () => null, $missing) ); - info(count($missing).' missing keys added to the driver.'); + info("{$count} missing keys added to the driver."); } diff --git a/src/Services/SearchCode/PhpParserService.php b/src/Services/SearchCode/PhpParserService.php index 877a10e..c029dee 100644 --- a/src/Services/SearchCode/PhpParserService.php +++ b/src/Services/SearchCode/PhpParserService.php @@ -193,6 +193,9 @@ public function translationsByFiles( $translations = collect($finder) ->map(function (SplFileInfo $file, string $path) use ($progress) { + if ($progress) { + $progress($path); + } $lastModified = $file->getMTime(); $cachedResult = $this->cache?->get($path); @@ -219,10 +222,6 @@ public function translationsByFiles( $this->cache?->put($path, $translations); } - if ($progress) { - $progress($path); - } - return $translations; }) ->filter() diff --git a/src/Translator.php b/src/Translator.php index 4fb2f86..d7cb31f 100755 --- a/src/Translator.php +++ b/src/Translator.php @@ -52,17 +52,28 @@ public function collect(): Translations /** * Scan the codebase to find keys not present in the driver * + * @param null|(Closure(string $path):void) $progress + * @param null|(Closure(int $total):void) $start + * @param null|(Closure():void) $end * @return array The translations keys defined in the codebase but not defined in the driver */ - public function getMissingTranslations(string $locale): array - { + public function getMissingTranslations( + string $locale, + ?Closure $progress = null, + ?Closure $start = null, + ?Closure $end = null, + ): array { if (! $this->searchcodeService) { throw TranslatorServiceException::missingSearchcodeService(); } $translations = $this->getTranslations($locale); - $keys = $this->searchcodeService->filesByTranslations(); + $keys = $this->searchcodeService->filesByTranslations( + progress: $progress, + start: $start, + end: $end + ); return collect($keys) ->filter(function ($value, $key) use ($translations) {