|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Hyde\Facades; |
|
6
|
|
|
|
|
7
|
|
|
use Hyde\Foundation\HydeKernel; |
|
8
|
|
|
use Hyde\Framework\Concerns\Internal\ForwardsIlluminateFilesystem; |
|
9
|
|
|
use Illuminate\Support\Collection; |
|
10
|
|
|
|
|
11
|
|
|
use function app; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Proxies the Laravel File facade with extra features and helpers tailored for HydePHP. |
|
15
|
|
|
* |
|
16
|
|
|
* For maximum compatability and interoperability, all path references in HydePHP are relative to the root of the project. |
|
17
|
|
|
* The helpers here will then prepend the project root to the path before actually interacting with the filesystem. |
|
18
|
|
|
* |
|
19
|
|
|
* @see \Hyde\Foundation\Kernel\Filesystem |
|
20
|
|
|
* @see \Illuminate\Filesystem\Filesystem |
|
21
|
|
|
*/ |
|
22
|
|
|
class Filesystem |
|
23
|
|
|
{ |
|
24
|
|
|
use ForwardsIlluminateFilesystem; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Get the base path of the HydePHP project. |
|
28
|
|
|
* |
|
29
|
|
|
* @return string |
|
30
|
|
|
*/ |
|
31
|
|
|
public static function basePath(): string |
|
32
|
|
|
{ |
|
33
|
|
|
return self::kernel()->path(); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Format the given project path to be absolute. Already absolute paths are normalized. |
|
38
|
|
|
* |
|
39
|
|
|
* @param string $path |
|
40
|
|
|
* @return string |
|
41
|
|
|
*/ |
|
42
|
|
|
public static function absolutePath(string $path): string |
|
43
|
|
|
{ |
|
44
|
|
|
return self::kernel()->pathToAbsolute(self::relativePath($path)); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Remove the absolute path from the given project path so that it becomes relative. |
|
49
|
|
|
* |
|
50
|
|
|
* @param string $path |
|
51
|
|
|
* @return string |
|
52
|
|
|
*/ |
|
53
|
|
|
public static function relativePath(string $path): string |
|
54
|
|
|
{ |
|
55
|
|
|
return self::kernel()->pathToRelative($path); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* A smarter glob function that will run the specified glob pattern a bit more intelligently. |
|
60
|
|
|
* While this method will use the absolute path when interacting with the filesystem, |
|
61
|
|
|
* the returned collection will only contain relative paths. |
|
62
|
|
|
* |
|
63
|
|
|
* @param string $pattern |
|
64
|
|
|
* @param int $flags |
|
65
|
|
|
* @return \Illuminate\Support\Collection<int, string> |
|
66
|
|
|
*/ |
|
67
|
|
|
public static function smartGlob(string $pattern, int $flags = 0): Collection |
|
68
|
|
|
{ |
|
69
|
|
|
return self::kernel()->filesystem()->smartGlob($pattern, $flags); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Find files in the project's directory, with optional filtering by extension and recursion. |
|
74
|
|
|
* |
|
75
|
|
|
* The returned collection will be a list of paths relative to the project root. |
|
76
|
|
|
* |
|
77
|
|
|
* @param string $directory |
|
78
|
|
|
* @param string|array<string>|false $matchExtensions The file extension(s) to match, or false to match all files. |
|
79
|
|
|
* @param bool $recursive Whether to search recursively or not. |
|
80
|
|
|
* @return \Illuminate\Support\Collection<int, string> |
|
81
|
|
|
*/ |
|
82
|
|
|
public static function findFiles(string $directory, string|array|false $matchExtensions = false, bool $recursive = false): Collection |
|
83
|
|
|
{ |
|
84
|
|
|
return self::kernel()->filesystem()->findFiles($directory, $matchExtensions, $recursive); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Touch one or more files in the project's directory. |
|
89
|
|
|
* |
|
90
|
|
|
* @param string|array $path |
|
91
|
|
|
* @return bool |
|
92
|
|
|
*/ |
|
93
|
|
|
public static function touch(string|array $path): bool |
|
94
|
|
|
{ |
|
95
|
|
|
return self::kernel()->filesystem()->touch($path); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Unlink one or more files in the project's directory. |
|
100
|
|
|
* |
|
101
|
|
|
* @param string|array $path |
|
102
|
|
|
* @return bool |
|
103
|
|
|
*/ |
|
104
|
|
|
public static function unlink(string|array $path): bool |
|
105
|
|
|
{ |
|
106
|
|
|
return self::kernel()->filesystem()->unlink($path); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Unlink a file in the project's directory, but only if it exists. |
|
111
|
|
|
* |
|
112
|
|
|
* @param string $path |
|
113
|
|
|
* @return bool True if the file was unlinked, false if it did not exist or failed to unlink. |
|
114
|
|
|
*/ |
|
115
|
|
|
public static function unlinkIfExists(string $path): bool |
|
116
|
|
|
{ |
|
117
|
|
|
return self::kernel()->filesystem()->unlinkIfExists($path); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Ensure that the parent directory of the given file path exists. |
|
122
|
|
|
* |
|
123
|
|
|
* @param string $path |
|
124
|
|
|
* @param int $mode |
|
125
|
|
|
* @param bool $recursive |
|
126
|
|
|
* @return void |
|
127
|
|
|
*/ |
|
128
|
|
|
public static function ensureParentDirectoryExists(string $path, int $mode = 0755, bool $recursive = true): void |
|
129
|
|
|
{ |
|
130
|
|
|
self::ensureDirectoryExists(self::dirname($path), $mode, $recursive); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Get the contents of a file. |
|
135
|
|
|
* |
|
136
|
|
|
* @param string $path |
|
137
|
|
|
* @param bool $lock |
|
138
|
|
|
* @return string |
|
139
|
|
|
* |
|
140
|
|
|
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException |
|
141
|
|
|
*/ |
|
142
|
|
|
public static function getContents(string $path, bool $lock = false): string |
|
143
|
|
|
{ |
|
144
|
|
|
return self::get(...func_get_args()); |
|
|
|
|
|
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* Write the contents of a file. |
|
149
|
|
|
* |
|
150
|
|
|
* @param string $path |
|
151
|
|
|
* @param string $contents |
|
152
|
|
|
* @param bool $lock |
|
153
|
|
|
* @return int|bool |
|
154
|
|
|
*/ |
|
155
|
|
|
public static function putContents(string $path, string $contents, bool $lock = false): bool|int |
|
156
|
|
|
{ |
|
157
|
|
|
return self::put(...func_get_args()); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* Improved mime type detection for the given path that can be relative to the project root, |
|
162
|
|
|
* or a remote URL where this will try to guess the mime type based on the file extension. |
|
163
|
|
|
* |
|
164
|
|
|
* @param string $path The path to the file, relative to the project root or a remote URL. |
|
165
|
|
|
*/ |
|
166
|
|
|
public static function findMimeType(string $path): string |
|
167
|
|
|
{ |
|
168
|
|
|
$extension = self::extension($path); |
|
169
|
|
|
|
|
170
|
|
|
$lookup = [ |
|
171
|
|
|
'txt' => 'text/plain', |
|
172
|
|
|
'md' => 'text/markdown', |
|
173
|
|
|
'html' => 'text/html', |
|
174
|
|
|
'css' => 'text/css', |
|
175
|
|
|
'svg' => 'image/svg+xml', |
|
176
|
|
|
'png' => 'image/png', |
|
177
|
|
|
'jpg' => 'image/jpeg', |
|
178
|
|
|
'jpeg' => 'image/jpeg', |
|
179
|
|
|
'gif' => 'image/gif', |
|
180
|
|
|
'json' => 'application/json', |
|
181
|
|
|
'js' => 'application/javascript', |
|
182
|
|
|
'xml' => 'application/xml', |
|
183
|
|
|
]; |
|
184
|
|
|
|
|
185
|
|
|
if (isset($lookup[$extension])) { |
|
186
|
|
|
return $lookup[$extension]; |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
if (extension_loaded('fileinfo') && self::exists($path)) { |
|
190
|
|
|
return self::mimeType($path); |
|
|
|
|
|
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
return 'text/plain'; |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
protected static function filesystem(): \Illuminate\Filesystem\Filesystem |
|
197
|
|
|
{ |
|
198
|
|
|
return app(\Illuminate\Filesystem\Filesystem::class); |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
protected static function kernel(): HydeKernel |
|
202
|
|
|
{ |
|
203
|
|
|
return HydeKernel::getInstance(); |
|
204
|
|
|
} |
|
205
|
|
|
} |
|
206
|
|
|
|