4
4
5
5
namespace DragonCode \LaravelDataDumper \Service ;
6
6
7
+ use Closure ;
7
8
use DragonCode \Support \Facades \Filesystem \File ;
8
9
use DragonCode \Support \Facades \Helpers \Str ;
9
10
10
11
class Files
11
12
{
12
- public static function delete (string $ path , string $ filename ): void
13
+ public function delete (string $ path , string $ filename ): void
13
14
{
14
- if (! $ dir = static :: directory ($ path )) {
15
+ if (! $ dir = $ this -> directory ($ path )) {
15
16
return ;
16
17
}
17
18
18
19
if (! file_exists ($ dir . '/ ' . $ filename )) {
19
- $ filename = static :: findFile ($ dir , $ filename );
20
+ $ filename = $ this -> findFile ($ dir , $ filename );
20
21
}
21
22
22
23
File::ensureDelete ($ dir . '/ ' . $ filename );
23
24
}
24
25
25
- protected static function directory (string $ path ): false |string
26
+ protected function directory (string $ path ): false |string
26
27
{
27
28
if (realpath ($ path ) && is_dir ($ path )) {
28
29
return rtrim ($ path , '\\/ ' );
@@ -31,15 +32,20 @@ protected static function directory(string $path): false|string
31
32
return realpath (base_path ($ path ));
32
33
}
33
34
34
- protected static function findFile (string $ path , string $ filename ): string
35
+ protected function findFile (string $ path , string $ filename ): string
35
36
{
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 );
44
50
}
45
51
}
0 commit comments