4
4
5
5
use Composer \InstalledVersions ;
6
6
use Illuminate \Auth \Access \AuthorizationException ;
7
+ use Illuminate \Contracts \Filesystem \Filesystem ;
7
8
use Illuminate \Support \Collection ;
8
9
use Illuminate \Support \Facades \Gate ;
10
+ use Illuminate \Support \Facades \Storage ;
9
11
use Illuminate \Support \Str ;
12
+ use Symfony \Component \Finder \Glob ;
10
13
11
14
class LogViewerService
12
15
{
@@ -20,30 +23,20 @@ class LogViewerService
20
23
21
24
protected function getFilePaths (): array
22
25
{
23
- // Because we'll use the base path as a parameter for `glob`, we should escape any
24
- // glob's special characters and treat those as actual characters of the path.
25
- // We can assume this, because it's the actual path of the Laravel app, not a user-defined
26
- // search pattern.
27
- if (PHP_OS_FAMILY === 'Windows ' ) {
28
- $ baseDir = str_replace (
29
- ['[ ' , '] ' ],
30
- ['{LEFTBRACKET} ' , '{RIGHTBRACKET} ' ],
31
- str_replace ('\\' , '/ ' , $ this ->basePathForLogs ())
32
- );
33
- $ baseDir = str_replace (
34
- ['{LEFTBRACKET} ' , '{RIGHTBRACKET} ' ],
35
- ['[[] ' , '[]] ' ],
36
- $ baseDir
37
- );
38
- } else {
39
- $ baseDir = str_replace (
40
- ['* ' , '? ' , '\\' , '[ ' , '] ' ],
41
- ['\* ' , '\? ' , '\\\\' , '\[ ' , '\] ' ],
42
- $ this ->basePathForLogs ()
43
- );
44
- }
45
26
$ files = [];
46
27
28
+ // Because we use the Glob::toRegex function we have to escape the brackets
29
+ $ baseDir = str_replace (
30
+ ['[ ' , '] ' ],
31
+ ['{LEFTBRACKET} ' , '{RIGHTBRACKET} ' ],
32
+ str_replace ('\\' , '/ ' , $ this ->basePathForLogs ())
33
+ );
34
+ $ baseDir = str_replace (
35
+ ['{LEFTBRACKET} ' , '{RIGHTBRACKET} ' ],
36
+ ['[[] ' , '[]] ' ],
37
+ $ baseDir
38
+ );
39
+
47
40
foreach (config ('log-viewer.include_files ' , []) as $ pattern ) {
48
41
if (! str_starts_with ($ pattern , DIRECTORY_SEPARATOR )) {
49
42
$ pattern = $ baseDir .$ pattern ;
@@ -60,23 +53,29 @@ protected function getFilePaths(): array
60
53
$ files = array_diff ($ files , $ this ->getFilePathsMatchingPattern ($ pattern ));
61
54
}
62
55
63
- $ files = array_map ('realpath ' , $ files );
64
-
65
- $ files = array_filter ($ files , 'is_file ' );
66
-
67
56
return array_values (array_reverse ($ files ));
68
57
}
69
58
70
59
protected function getFilePathsMatchingPattern ($ pattern )
71
60
{
72
- // The GLOB_BRACE flag is not available on some non GNU systems, like Solaris or Alpine Linux.
61
+ $ files = [];
73
62
74
- return glob ($ pattern );
63
+ foreach ($ this ->getFilesystem ()->allFiles ($ this ->basePathForLogs ()) as $ file )
64
+ {
65
+ if (preg_match (pattern: Glob::toRegex (glob: $ pattern ), subject: $ file )) {
66
+ $ files [] = $ file ;
67
+ }
68
+ }
69
+
70
+ return $ files ;
75
71
}
76
72
77
73
public function basePathForLogs (): string
78
74
{
79
- return Str::finish (realpath (storage_path ('logs ' )), DIRECTORY_SEPARATOR );
75
+ $ rootFolder = Str::of (config ('log-viewer.filesystem.root ' ));
76
+ return empty ($ rootFolder )
77
+ ? $ rootFolder ->finish ('/ ' )
78
+ : $ rootFolder ;
80
79
}
81
80
82
81
/**
@@ -154,6 +153,11 @@ public function getRouteMiddleware(): array
154
153
return config ('log-viewer.middleware ' , []) ?: ['web ' ];
155
154
}
156
155
156
+ public function getFilesystem (): Filesystem
157
+ {
158
+ return Storage::disk (config ('log-viewer.filesystem.disk ' ));
159
+ }
160
+
157
161
public function auth ($ callback = null ): void
158
162
{
159
163
if (is_null ($ callback ) && isset ($ this ->authCallback )) {
0 commit comments