44
55namespace DragonCode \LaravelDataDumper \Service ;
66
7+ use Closure ;
78use DragonCode \Support \Facades \Filesystem \File ;
89use DragonCode \Support \Facades \Helpers \Str ;
910
1011class Files
1112{
12- public static function delete (string $ path , string $ filename ): void
13+ public function delete (string $ path , string $ filename ): void
1314 {
14- if (! $ dir = static :: directory ($ path )) {
15+ if (! $ dir = $ this -> directory ($ path )) {
1516 return ;
1617 }
1718
1819 if (! file_exists ($ dir . '/ ' . $ filename )) {
19- $ filename = static :: findFile ($ dir , $ filename );
20+ $ filename = $ this -> findFile ($ dir , $ filename );
2021 }
2122
2223 File::ensureDelete ($ dir . '/ ' . $ filename );
2324 }
2425
25- protected static function directory (string $ path ): false |string
26+ protected function directory (string $ path ): false |string
2627 {
2728 if (realpath ($ path ) && is_dir ($ path )) {
2829 return rtrim ($ path , '\\/ ' );
@@ -31,15 +32,20 @@ protected static function directory(string $path): false|string
3132 return realpath (base_path ($ path ));
3233 }
3334
34- protected static function findFile (string $ path , string $ filename ): string
35+ protected function findFile (string $ path , string $ filename ): string
3536 {
36- return File::names (
37- $ path ,
38- fn (string $ name ) => Str::contains (
39- str_replace ('\\' , '/ ' , $ name ),
40- str_replace ('\\' , '/ ' , $ filename )
41- ),
42- recursive: true
43- )[0 ];
37+ return $ this ->find ($ path , function (string $ name ) use ($ filename ) {
38+ return Str::contains ($ this ->resolvePath ($ name ), $ this ->resolvePath ($ filename ));
39+ }) ?? $ filename ;
40+ }
41+
42+ protected function find (string $ path , Closure $ when ): ?string
43+ {
44+ return File::names ($ path , $ when , true )[0 ] ?? null ;
45+ }
46+
47+ protected function resolvePath (string $ path ): string
48+ {
49+ return str_replace ('\\' , '/ ' , $ path );
4450 }
4551}
0 commit comments