|
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
|
4 |
|
* @return array |
|
24
|
|
|
*/ |
|
25
|
4 |
|
public function names(string $path, callable $callback = null, bool $recursive = false): array |
|
26
|
|
|
{ |
|
27
|
|
|
$items = []; |
|
28
|
4 |
|
|
|
29
|
4 |
|
/** @var \DirectoryIterator $item */ |
|
30
|
4 |
|
foreach (DirectoryHelper::all($path) as $item) { |
|
31
|
|
|
if ($item->isFile()) { |
|
32
|
4 |
|
$name = $item->getFilename(); |
|
33
|
4 |
|
|
|
34
|
|
|
if (! is_callable($callback) || $callback($name)) { |
|
35
|
|
|
$items[] = $name; |
|
36
|
|
|
} |
|
37
|
|
|
} |
|
38
|
4 |
|
|
|
39
|
|
|
if ($recursive && $item->isDir() && ! $item->isDot()) { |
|
40
|
4 |
|
$prefix = (string) Str::of($item->getRealPath()) |
|
41
|
|
|
->after(realpath($path)) |
|
42
|
|
|
->trim('\\/'); |
|
43
|
|
|
|
|
44
|
|
|
foreach ($this->names($item->getRealPath(), $callback, $recursive) as $value) { |
|
45
|
|
|
$items[] = $prefix . '/' . $value; |
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
sort($items); |
|
51
|
|
|
|
|
52
|
28 |
|
return array_values($items); |
|
53
|
|
|
} |
|
54
|
28 |
|
|
|
55
|
|
|
/** |
|
56
|
28 |
|
* Save content to file. |
|
57
|
|
|
* |
|
58
|
28 |
|
* @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
|
|
|
public function store(string $path, string $content, int $mode = 0755): string |
|
65
|
|
|
{ |
|
66
|
|
|
DirectoryHelper::make(pathinfo($path, PATHINFO_DIRNAME), $mode); |
|
|
|
|
|
|
67
|
|
|
|
|
68
|
10 |
|
file_put_contents($path, $content); |
|
69
|
|
|
|
|
70
|
10 |
|
return realpath($path); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Checks if the file exists. |
|
75
|
|
|
* |
|
76
|
|
|
* @param string $path |
|
77
|
|
|
* |
|
78
|
|
|
* @return bool |
|
79
|
|
|
*/ |
|
80
|
28 |
|
public function exists(string $path): bool |
|
81
|
|
|
{ |
|
82
|
28 |
|
return file_exists($path) && is_file($path); |
|
83
|
|
|
} |
|
84
|
28 |
|
|
|
85
|
|
|
/** |
|
86
|
28 |
|
* Deletes files in the specified paths. |
|
87
|
|
|
* |
|
88
|
28 |
|
* @param string|string[] $paths |
|
89
|
28 |
|
* |
|
90
|
|
|
* @return bool |
|
91
|
|
|
*/ |
|
92
|
|
|
public function delete($paths): bool |
|
93
|
|
|
{ |
|
94
|
|
|
$paths = Arr::wrap($paths); |
|
95
|
|
|
|
|
96
|
28 |
|
$success = true; |
|
97
|
|
|
|
|
98
|
|
|
foreach ($paths as $path) { |
|
99
|
|
|
try { |
|
100
|
|
|
if (! @unlink($path)) { |
|
101
|
|
|
$success = false; |
|
102
|
|
|
} |
|
103
|
|
|
} catch (Throwable $e) { |
|
104
|
|
|
$success = false; |
|
105
|
|
|
} |
|
106
|
14 |
|
} |
|
107
|
|
|
|
|
108
|
14 |
|
return $success; |
|
109
|
4 |
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
12 |
|
* Checks if an object or link is a file at the specified path. |
|
113
|
|
|
* |
|
114
|
|
|
* @param \DirectoryIterator|\SplFileInfo|string $value |
|
115
|
|
|
* |
|
116
|
|
|
* @return bool |
|
117
|
|
|
*/ |
|
118
|
|
|
public function isFile($value): bool |
|
119
|
|
|
{ |
|
120
|
|
|
if (Instance::of($value, [SplFileInfo::class, DirectoryIterator::class])) { |
|
121
|
|
|
return $value->isFile(); |
|
122
|
8 |
|
} |
|
123
|
|
|
|
|
124
|
8 |
|
return is_file($value); |
|
125
|
4 |
|
} |
|
126
|
|
|
|
|
127
|
4 |
|
/** |
|
128
|
|
|
* Checks the existence of a file. |
|
129
|
|
|
* |
|
130
|
|
|
* @param \DirectoryIterator|\SplFileInfo|string $path |
|
131
|
|
|
* |
|
132
|
|
|
* @throws \Helldar\Support\Exceptions\FileNotFoundException |
|
133
|
|
|
*/ |
|
134
|
|
|
public function validate($path): void |
|
135
|
|
|
{ |
|
136
|
|
|
if (! $this->isFile($path)) { |
|
137
|
|
|
throw new FileNotFoundException($path); |
|
138
|
4 |
|
} |
|
139
|
|
|
} |
|
140
|
4 |
|
|
|
141
|
|
|
/** |
|
142
|
2 |
|
* Checks the existence of a file and return full path if exist. |
|
143
|
|
|
* |
|
144
|
|
|
* @param \DirectoryIterator|\SplFileInfo|string $path |
|
145
|
|
|
* |
|
146
|
|
|
* @throws \Helldar\Support\Exceptions\FileNotFoundException |
|
147
|
|
|
* |
|
148
|
|
|
* @return string |
|
149
|
|
|
*/ |
|
150
|
|
|
public function validated($path): string |
|
151
|
|
|
{ |
|
152
|
|
|
$this->validate($path); |
|
153
|
|
|
|
|
154
|
|
|
return realpath($path); |
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
|
|
|