22
33namespace Elegantly \Translator \Services \SearchCode ;
44
5- use Closure ;
65use Elegantly \Translator \Caches \SearchCodeCache ;
76use Exception ;
87use Illuminate \Contracts \Filesystem \Filesystem ;
2120use Symfony \Component \Finder \Finder ;
2221use Symfony \Component \Finder \SplFileInfo ;
2322
23+ use function Orchestra \Testbench \package_path ;
24+
2425class PhpParserService implements SearchCodeServiceInterface
2526{
2627 public ?SearchCodeCache $ cache = null ;
@@ -71,13 +72,8 @@ public function finder(): Finder
7172 ->files ();
7273 }
7374
74- public static function filterTranslationsKeys (? string $ key ): bool
75+ public static function isTranslationKeyFromPackage ( string $ key ): bool
7576 {
76-
77- if (blank ($ key )) {
78- return false ;
79- }
80-
8177 preg_match (
8278 '/^(?<package>[a-zA-Z0-9-_]+)::(?<key>.+)$/ ' ,
8379 $ key ,
@@ -87,6 +83,16 @@ public static function filterTranslationsKeys(?string $key): bool
8783 return empty ($ matches );
8884 }
8985
86+ public static function filterTranslationsKeys (?string $ key ): bool
87+ {
88+
89+ if (blank ($ key )) {
90+ return false ;
91+ }
92+
93+ return static ::isTranslationKeyFromPackage ($ key );
94+ }
95+
9096 public static function isFunCallTo (
9197 FuncCall $ node ,
9298 string $ function ,
@@ -173,17 +179,13 @@ public static function scanCode(string $code): array
173179 ->toArray ();
174180 }
175181
176- /**
177- * @param null|(Closure(string $file, string[] $translations):void) $progress
178- */
179- public function translationsByFiles (
180- ?Closure $ progress = null ,
181- ): array {
182+ public function translationsByFiles (): array
183+ {
182184 return collect ($ this ->finder ())
183- ->map (function (SplFileInfo $ file , string $ key ) use ( $ progress ) {
185+ ->mapWithKeys (function (SplFileInfo $ file , string $ path ) {
184186
185187 $ lastModified = $ file ->getMTime ();
186- $ cachedResult = $ this ->cache ?->get($ key );
188+ $ cachedResult = $ this ->cache ?->get($ path );
187189
188190 if (
189191 $ lastModified && $ cachedResult &&
@@ -204,27 +206,26 @@ public function translationsByFiles(
204206 previous: $ th
205207 );
206208 }
207- $ this ->cache ?->put($ key , $ translations );
209+ $ this ->cache ?->put($ path , $ translations );
208210 }
209211
210- if ($ progress ) {
211- $ progress ($ file , $ translations );
212- }
212+ $ relativePath = str ($ path )
213+ ->after (package_path ())
214+ ->after (base_path ())
215+ ->value ();
213216
214- return $ translations ;
217+ return [
218+ $ relativePath => $ translations ,
219+ ];
215220 })
216221 ->filter ()
217222 ->sortKeys (SORT_NATURAL )
218223 ->toArray ();
219224 }
220225
221- /**
222- * @param null|(Closure(string $file, string[] $translations):void) $progress
223- */
224- public function filesByTranslations (
225- ?Closure $ progress = null ,
226- ): array {
227- $ translations = $ this ->translationsByFiles ($ progress );
226+ public function filesByTranslations (): array
227+ {
228+ $ translations = $ this ->translationsByFiles ();
228229
229230 $ results = [];
230231
0 commit comments