|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Quantum PHP Framework |
|
5
|
|
|
* |
|
6
|
|
|
* An open source software development framework for PHP |
|
7
|
|
|
* |
|
8
|
|
|
* @package Quantum |
|
9
|
|
|
* @author Arman Ag. <[email protected]> |
|
10
|
|
|
* @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org) |
|
11
|
|
|
* @link http://quantum.softberg.org/ |
|
12
|
|
|
* @since 2.9.5 |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace Quantum\Libraries\Storage\Adapters\Local; |
|
16
|
|
|
|
|
17
|
|
|
use Quantum\Libraries\Storage\Contracts\FilesystemAdapterInterface; |
|
18
|
|
|
use Throwable; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Class LocalFileSystemAdapter |
|
22
|
|
|
* @package Quantum\Libraries\Storage |
|
23
|
|
|
*/ |
|
24
|
|
|
class LocalFileSystemAdapter implements FilesystemAdapterInterface |
|
25
|
|
|
{ |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @param string $dirname |
|
29
|
|
|
* @param string|null $parentId |
|
30
|
|
|
* @inheritDoc |
|
31
|
|
|
*/ |
|
32
|
|
|
public function makeDirectory(string $dirname, ?string $parentId = null): bool |
|
33
|
|
|
{ |
|
34
|
|
|
return mkdir($dirname); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @inheritDoc |
|
39
|
|
|
*/ |
|
40
|
|
|
public function removeDirectory(string $dirname): bool |
|
41
|
|
|
{ |
|
42
|
|
|
if (!is_dir($dirname)) { |
|
43
|
|
|
return false; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
return rmdir($dirname); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @inheritDoc |
|
51
|
|
|
*/ |
|
52
|
|
|
public function get(string $filename) |
|
53
|
|
|
{ |
|
54
|
|
|
return file_get_contents($filename); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Reads and returns the content of a file as JSON. |
|
59
|
|
|
* @param string $filename |
|
60
|
|
|
* @return false|mixed |
|
61
|
|
|
*/ |
|
62
|
|
|
public function getJson(string $filename) |
|
63
|
|
|
{ |
|
64
|
|
|
$content = file_get_contents($filename); |
|
65
|
|
|
|
|
66
|
|
|
if (empty($content)) { |
|
67
|
|
|
return false; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
$data = json_decode($content, true); |
|
71
|
|
|
|
|
72
|
|
|
return json_last_error() === JSON_ERROR_NONE ? $data : false; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @param string $filename |
|
77
|
|
|
* @param string $content |
|
78
|
|
|
* @param string|null $parentId |
|
79
|
|
|
* @inheritDoc |
|
80
|
|
|
*/ |
|
81
|
|
|
public function put(string $filename, string $content, ?string $parentId = null) |
|
82
|
|
|
{ |
|
83
|
|
|
return file_put_contents($filename, $content, LOCK_EX); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @inheritDoc |
|
88
|
|
|
*/ |
|
89
|
|
|
public function append(string $filename, string $content) |
|
90
|
|
|
{ |
|
91
|
|
|
return file_put_contents($filename, $content, FILE_APPEND | LOCK_EX); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @inheritDoc |
|
96
|
|
|
*/ |
|
97
|
|
|
public function rename(string $oldName, string $newName): bool |
|
98
|
|
|
{ |
|
99
|
|
|
return rename($oldName, $newName); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @inheritDoc |
|
104
|
|
|
*/ |
|
105
|
|
|
public function copy(string $source, string $dest): bool |
|
106
|
|
|
{ |
|
107
|
|
|
return copy($source, $dest); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @inheritDoc |
|
112
|
|
|
*/ |
|
113
|
|
|
public function exists(string $filename): bool |
|
114
|
|
|
{ |
|
115
|
|
|
return file_exists($filename) && is_file($filename); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @inheritDoc |
|
120
|
|
|
*/ |
|
121
|
|
|
public function size(string $filename) |
|
122
|
|
|
{ |
|
123
|
|
|
return filesize($filename); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* @inheritDoc |
|
128
|
|
|
*/ |
|
129
|
|
|
public function lastModified(string $filename) |
|
130
|
|
|
{ |
|
131
|
|
|
return filemtime($filename); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* @inheritDoc |
|
136
|
|
|
*/ |
|
137
|
|
|
public function remove(string $filename): bool |
|
138
|
|
|
{ |
|
139
|
|
|
return unlink($filename); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* @inheritDoc |
|
144
|
|
|
*/ |
|
145
|
|
|
public function isFile(string $filename): bool |
|
146
|
|
|
{ |
|
147
|
|
|
return is_file($filename); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* @inheritDoc |
|
152
|
|
|
*/ |
|
153
|
|
|
public function isDirectory(string $dirname): bool |
|
154
|
|
|
{ |
|
155
|
|
|
return is_dir($dirname); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* @inheritDoc |
|
160
|
|
|
*/ |
|
161
|
|
|
public function listDirectory(string $dirname) |
|
162
|
|
|
{ |
|
163
|
|
|
$entries = []; |
|
164
|
|
|
|
|
165
|
|
|
try { |
|
166
|
|
|
foreach (scandir($dirname) as $item) { |
|
167
|
|
|
if ($item != '.' && $item != '..') { |
|
168
|
|
|
$entries[] = realpath($dirname . DS . $item); |
|
169
|
|
|
} |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
return $entries; |
|
173
|
|
|
|
|
174
|
|
|
} catch (Throwable $e) { |
|
175
|
|
|
return false; |
|
176
|
|
|
} |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* Find path names matching a pattern |
|
181
|
|
|
* @param string $pattern |
|
182
|
|
|
* @param int $flags |
|
183
|
|
|
* @return array|false |
|
184
|
|
|
*/ |
|
185
|
|
|
public function glob(string $pattern, int $flags = 0) |
|
186
|
|
|
{ |
|
187
|
|
|
return glob($pattern, $flags); |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* Is Readable |
|
192
|
|
|
* @param string $filename |
|
193
|
|
|
* @return bool |
|
194
|
|
|
*/ |
|
195
|
|
|
public function isReadable(string $filename): bool |
|
196
|
|
|
{ |
|
197
|
|
|
return is_readable($filename); |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* Is Writable |
|
202
|
|
|
* @param string $filename |
|
203
|
|
|
* @return bool |
|
204
|
|
|
*/ |
|
205
|
|
|
public function isWritable(string $filename): bool |
|
206
|
|
|
{ |
|
207
|
|
|
return is_writable($filename); |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
/** |
|
211
|
|
|
* Gets the content between given lines |
|
212
|
|
|
* @param string $filename |
|
213
|
|
|
* @param int $offset |
|
214
|
|
|
* @param int|null $length |
|
215
|
|
|
* @return array |
|
216
|
|
|
*/ |
|
217
|
|
|
public function getLines(string $filename, int $offset = 0, ?int $length = null): array |
|
218
|
|
|
{ |
|
219
|
|
|
$lines = file($filename, FILE_IGNORE_NEW_LINES); |
|
220
|
|
|
|
|
221
|
|
|
if(!$lines) { |
|
|
|
|
|
|
222
|
|
|
return []; |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
if ($offset || $length) { |
|
|
|
|
|
|
226
|
|
|
$lines = array_slice($lines, $offset, $length ?: count($lines), true); |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
return $lines; |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
/** |
|
233
|
|
|
* Gets the filename with extension |
|
234
|
|
|
* @param string $path |
|
235
|
|
|
* @return string |
|
236
|
|
|
*/ |
|
237
|
|
|
public function fileNameWithExtension(string $path): string |
|
238
|
|
|
{ |
|
239
|
|
|
return (string)pathinfo($path, PATHINFO_BASENAME); |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
/** |
|
243
|
|
|
* Gets the file name |
|
244
|
|
|
* @param string $path |
|
245
|
|
|
* @return string |
|
246
|
|
|
*/ |
|
247
|
|
|
public function fileName(string $path): string |
|
248
|
|
|
{ |
|
249
|
|
|
return (string)pathinfo($path, PATHINFO_FILENAME); |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
/** |
|
253
|
|
|
* Gets the file extension |
|
254
|
|
|
* @param string $path |
|
255
|
|
|
* @return string |
|
256
|
|
|
*/ |
|
257
|
|
|
public function extension(string $path): string |
|
258
|
|
|
{ |
|
259
|
|
|
return (string)pathinfo($path, PATHINFO_EXTENSION); |
|
260
|
|
|
} |
|
261
|
|
|
|
|
262
|
|
|
/** |
|
263
|
|
|
* Includes the required file |
|
264
|
|
|
* @param string $file |
|
265
|
|
|
* @param bool $once |
|
266
|
|
|
* @return mixed |
|
267
|
|
|
*/ |
|
268
|
|
|
public function require(string $file, bool $once = false) |
|
269
|
|
|
{ |
|
270
|
|
|
if ($once) { |
|
271
|
|
|
return require_once $file; |
|
272
|
|
|
} else { |
|
273
|
|
|
return require $file; |
|
274
|
|
|
} |
|
275
|
|
|
} |
|
276
|
|
|
|
|
277
|
|
|
/** |
|
278
|
|
|
* Includes a file |
|
279
|
|
|
* @param string $file |
|
280
|
|
|
* @param bool $once |
|
281
|
|
|
* @return mixed |
|
282
|
|
|
*/ |
|
283
|
|
|
public function include(string $file, bool $once = false) |
|
284
|
|
|
{ |
|
285
|
|
|
if ($once) { |
|
286
|
|
|
return include_once $file; |
|
287
|
|
|
} else { |
|
288
|
|
|
return include $file; |
|
289
|
|
|
} |
|
290
|
|
|
} |
|
291
|
|
|
} |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.