1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Helldar\Support\Helpers\Filesystem; |
4
|
|
|
|
5
|
|
|
use DirectoryIterator; |
6
|
|
|
use Helldar\Support\Exceptions\FileNotFoundException; |
7
|
|
|
use Helldar\Support\Facades\Helpers\Arr; |
8
|
|
|
use Helldar\Support\Facades\Helpers\Filesystem\Directory as DirectoryHelper; |
9
|
|
|
use Helldar\Support\Facades\Helpers\Instance; |
10
|
|
|
use Helldar\Support\Facades\Helpers\Str; |
11
|
|
|
use SplFileInfo; |
12
|
|
|
use Throwable; |
13
|
|
|
|
14
|
|
|
class File |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Get a list of filenames along a path. |
18
|
|
|
* |
19
|
|
|
* @param string $path |
20
|
|
|
* @param callable|null $callback |
21
|
|
|
* @param bool $recursive |
22
|
|
|
* |
23
|
|
|
* @return array |
24
|
|
|
*/ |
25
|
6 |
|
public function names(string $path, callable $callback = null, bool $recursive = false): array |
26
|
|
|
{ |
27
|
6 |
|
$items = []; |
28
|
|
|
|
29
|
|
|
/** @var \DirectoryIterator $item */ |
30
|
6 |
|
foreach (DirectoryHelper::all($path) as $item) { |
31
|
6 |
|
if ($item->isFile()) { |
32
|
6 |
|
$name = $item->getFilename(); |
33
|
|
|
|
34
|
6 |
|
if (! is_callable($callback) || $callback($name)) { |
35
|
6 |
|
$items[] = $name; |
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
|
39
|
6 |
|
if ($recursive && $item->isDir() && ! $item->isDot()) { |
40
|
2 |
|
$prefix = (string) Str::of($item->getRealPath()) |
41
|
2 |
|
->after(realpath($path)) |
42
|
2 |
|
->trim('\\/'); |
43
|
|
|
|
44
|
2 |
|
foreach ($this->names($item->getRealPath(), $callback, $recursive) as $value) { |
45
|
2 |
|
$items[] = $prefix . '/' . $value; |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
6 |
|
sort($items); |
51
|
|
|
|
52
|
6 |
|
return array_values($items); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Save content to file. |
57
|
|
|
* |
58
|
|
|
* @param string $path |
59
|
|
|
* @param string $content |
60
|
|
|
* @param int $mode |
61
|
|
|
* |
62
|
|
|
* @return string Returns the full path to the saved file. |
63
|
|
|
*/ |
64
|
32 |
|
public function store(string $path, string $content, int $mode = 0755): string |
65
|
|
|
{ |
66
|
32 |
|
DirectoryHelper::make(pathinfo($path, PATHINFO_DIRNAME), $mode); |
|
|
|
|
67
|
|
|
|
68
|
32 |
|
file_put_contents($path, $content); |
69
|
|
|
|
70
|
32 |
|
return realpath($path); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Checks if the file exists. |
75
|
|
|
* |
76
|
|
|
* @param string $path |
77
|
|
|
* |
78
|
|
|
* @return bool |
79
|
|
|
*/ |
80
|
14 |
|
public function exists(string $path): bool |
81
|
|
|
{ |
82
|
14 |
|
return file_exists($path) && is_file($path); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Deletes files in the specified paths. |
87
|
|
|
* |
88
|
|
|
* @param string|string[] $paths |
89
|
|
|
* |
90
|
|
|
* @return bool |
91
|
|
|
*/ |
92
|
32 |
|
public function delete($paths): bool |
93
|
|
|
{ |
94
|
32 |
|
$paths = Arr::wrap($paths); |
95
|
|
|
|
96
|
32 |
|
$success = true; |
97
|
|
|
|
98
|
32 |
|
foreach ($paths as $path) { |
99
|
|
|
try { |
100
|
32 |
|
if (! @unlink($path)) { |
101
|
32 |
|
$success = false; |
102
|
|
|
} |
103
|
|
|
} catch (Throwable $e) { |
104
|
|
|
$success = false; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
32 |
|
return $success; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Ensure the file has been deleted. |
113
|
|
|
* |
114
|
|
|
* @param array|string $paths |
115
|
|
|
* |
116
|
|
|
* @return bool |
117
|
|
|
*/ |
118
|
4 |
|
public function ensureDelete($paths): bool |
119
|
|
|
{ |
120
|
4 |
|
$paths = Arr::wrap($paths); |
121
|
|
|
|
122
|
4 |
|
$success = true; |
123
|
|
|
|
124
|
4 |
|
foreach ($paths as $path) { |
125
|
4 |
|
if ($this->exists($path) && ! $this->delete($path)) { |
126
|
|
|
$success = false; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
130
|
4 |
|
return $success; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Checks if an object or link is a file at the specified path. |
135
|
|
|
* |
136
|
|
|
* @param \DirectoryIterator|\SplFileInfo|string $value |
137
|
|
|
* |
138
|
|
|
* @return bool |
139
|
|
|
*/ |
140
|
14 |
|
public function isFile($value): bool |
141
|
|
|
{ |
142
|
14 |
|
if (Instance::of($value, [SplFileInfo::class, DirectoryIterator::class])) { |
143
|
4 |
|
return $value->isFile(); |
144
|
|
|
} |
145
|
|
|
|
146
|
12 |
|
return is_file($value); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Checks the existence of a file. |
151
|
|
|
* |
152
|
|
|
* @param \DirectoryIterator|\SplFileInfo|string $path |
153
|
|
|
* |
154
|
|
|
* @throws \Helldar\Support\Exceptions\FileNotFoundException |
155
|
|
|
*/ |
156
|
8 |
|
public function validate($path): void |
157
|
|
|
{ |
158
|
8 |
|
if (! $this->isFile($path)) { |
159
|
4 |
|
throw new FileNotFoundException($path); |
160
|
|
|
} |
161
|
4 |
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Checks the existence of a file and return full path if exist. |
165
|
|
|
* |
166
|
|
|
* @param \DirectoryIterator|\SplFileInfo|string $path |
167
|
|
|
* |
168
|
|
|
* @throws \Helldar\Support\Exceptions\FileNotFoundException |
169
|
|
|
* |
170
|
|
|
* @return string |
171
|
|
|
*/ |
172
|
4 |
|
public function validated($path): string |
173
|
|
|
{ |
174
|
4 |
|
$this->validate($path); |
175
|
|
|
|
176
|
2 |
|
return realpath($path); |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
|