-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathFilesystem.php
205 lines (170 loc) · 5.4 KB
/
Filesystem.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
<?php
declare(strict_types=1);
namespace Hyde\Foundation\Kernel;
use Hyde\Hyde;
use Hyde\Foundation\HydeKernel;
use Hyde\Foundation\PharSupport;
use Illuminate\Support\Collection;
use function Hyde\normalize_slashes;
use function Hyde\path_join;
use function file_exists;
use function str_replace;
use function array_map;
use function is_string;
use function is_array;
use function str_starts_with;
use function unslash;
use function unlink;
use function touch;
/**
* File helper methods, bound to the HydeKernel instance, and is an integral part of the framework.
*
* All paths arguments are relative to the root of the application,
* and will be automatically resolved to absolute paths.
*/
class Filesystem
{
protected HydeKernel $kernel;
public function __construct(HydeKernel $kernel)
{
$this->kernel = $kernel;
}
public function getBasePath(): string
{
return $this->kernel->getBasePath();
}
/**
* Get an absolute file path from a supplied relative path.
*
* The function returns the fully qualified path to your site's root directory.
*
* You may also use the function to generate a fully qualified path to a given file
* relative to the project root directory when supplying the path argument.
*/
public function path(string $path = ''): string
{
if (empty($path)) {
return $this->getBasePath();
}
if (str_starts_with($path, 'phar://')) {
return $path;
}
$path = unslash($this->pathToRelative($path));
return path_join($this->getBasePath(), $path);
}
/**
* Get an absolute file path from a supplied relative path.
*
* Input types are matched, meaning that if the input is a string so will the output be.
*
* @param string|array<string> $path
*/
public function pathToAbsolute(string|array $path): string|array
{
if (is_array($path)) {
return array_map(fn (string $path): string => $this->pathToAbsolute($path), $path);
}
return $this->path($path);
}
/**
* Decode an absolute path created with a Hyde::path() helper into its relative counterpart.
*/
public function pathToRelative(string $path): string
{
return normalize_slashes(str_starts_with($path, $this->path())
? unslash(str_replace($this->path(), '', $path))
: $path);
}
/**
* Get the absolute path to the media source directory, or a file within it.
*/
public function mediaPath(string $path = ''): string
{
if (empty($path)) {
return $this->path(Hyde::getMediaDirectory());
}
$path = unslash($path);
return $this->path(Hyde::getMediaDirectory()."/$path");
}
/**
* Get the absolute path to the compiled site directory, or a file within it.
*/
public function sitePath(string $path = ''): string
{
if (empty($path)) {
return $this->path(Hyde::getOutputDirectory());
}
$path = unslash($path);
return $this->path(Hyde::getOutputDirectory()."/$path");
}
/**
* Get the absolute path to the compiled site's media directory, or a file within it.
*/
public function siteMediaPath(string $path = ''): string
{
if (empty($path)) {
return $this->sitePath(Hyde::getMediaOutputDirectory());
}
$path = unslash($path);
return $this->sitePath(Hyde::getMediaOutputDirectory()."/$path");
}
/**
* Works similarly to the path() function, but returns a file in the Framework package.
*
* @internal This is not intended to be used outside the HydePHP framework.
*/
public function vendorPath(string $path = '', string $package = 'framework'): string
{
if (PharSupport::running() && ! PharSupport::hasVendorDirectory()) {
return PharSupport::vendorPath($path, $package);
}
return $this->path("vendor/hyde/$package/".unslash($path));
}
/**
* Touch one or more files in the project's directory.
*
* @param string|array<string> $path
*/
public function touch(string|array $path): bool
{
if (is_string($path)) {
return touch($this->path($path));
}
foreach ($path as $p) {
touch($this->path($p));
}
return true;
}
/**
* Unlink one or more files in the project's directory.
*
* @param string|array<string> $path
*/
public function unlink(string|array $path): bool
{
if (is_string($path)) {
return unlink($this->path($path));
}
foreach ($path as $p) {
unlink($this->path($p));
}
return true;
}
/**
* Unlink a file in the project's directory, but only if it exists.
*/
public function unlinkIfExists(string $path): bool
{
if (file_exists($this->path($path))) {
return unlink($this->path($path));
}
return false;
}
/** @return \Illuminate\Support\Collection<int, string> */
public function smartGlob(string $pattern, int $flags = 0): Collection
{
/** @var \Illuminate\Support\Collection<int, string> $files */
$files = collect(\Hyde\Facades\Filesystem::glob($pattern, $flags));
return $files->map(fn (string $path): string => $this->pathToRelative($path));
}
}