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.0 |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace Quantum\Libraries\Storage\Adapters\Local; |
16
|
|
|
|
17
|
|
|
use Quantum\Libraries\Storage\FilesystemAdapterInterface; |
18
|
|
|
use Throwable; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Class LocalFileSystemAdapter |
22
|
|
|
* @package Quantum\Libraries\Storage |
23
|
|
|
*/ |
24
|
|
|
class LocalFileSystemAdapter implements FilesystemAdapterInterface |
25
|
|
|
{ |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var LocalFileSystemAdapter|null |
29
|
|
|
*/ |
30
|
|
|
private static $instance = null; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Get Instance |
34
|
|
|
* @return LocalFileSystemAdapter|null |
35
|
|
|
*/ |
36
|
|
|
public static function getInstance(): ?LocalFileSystemAdapter |
37
|
|
|
{ |
38
|
|
|
if (self::$instance === null) { |
39
|
|
|
self::$instance = new self(); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
return self::$instance; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @inheritDoc |
47
|
|
|
*/ |
48
|
|
|
public function makeDirectory(string $dirname): bool |
49
|
|
|
{ |
50
|
|
|
return mkdir($dirname); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @inheritDoc |
55
|
|
|
*/ |
56
|
|
|
public function removeDirectory(string $dirname): bool |
57
|
|
|
{ |
58
|
|
|
return rmdir($dirname); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @inheritDoc |
63
|
|
|
*/ |
64
|
|
|
public function get(string $filename) |
65
|
|
|
{ |
66
|
|
|
return file_get_contents($filename); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @inheritDoc |
71
|
|
|
*/ |
72
|
|
|
public function put(string $filename, string $content) |
73
|
|
|
{ |
74
|
|
|
return file_put_contents($filename, $content, LOCK_EX); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @inheritDoc |
79
|
|
|
*/ |
80
|
|
|
public function append(string $filename, string $content) |
81
|
|
|
{ |
82
|
|
|
return file_put_contents($filename, $content, FILE_APPEND | LOCK_EX); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @inheritDoc |
87
|
|
|
*/ |
88
|
|
|
public function rename(string $oldName, string $newName): bool |
89
|
|
|
{ |
90
|
|
|
return rename($oldName, $newName); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @inheritDoc |
95
|
|
|
*/ |
96
|
|
|
public function copy(string $source, string $dest): bool |
97
|
|
|
{ |
98
|
|
|
return copy($source, $dest); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @inheritDoc |
103
|
|
|
*/ |
104
|
|
|
public function exists(string $filename): bool |
105
|
|
|
{ |
106
|
|
|
return file_exists($filename) && is_file($filename); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @inheritDoc |
111
|
|
|
*/ |
112
|
|
|
public function size(string $filename) |
113
|
|
|
{ |
114
|
|
|
return filesize($filename); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @inheritDoc |
119
|
|
|
*/ |
120
|
|
|
public function lastModified(string $filename) |
121
|
|
|
{ |
122
|
|
|
return filemtime($filename); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @inheritDoc |
127
|
|
|
*/ |
128
|
|
|
public function remove(string $filename): bool |
129
|
|
|
{ |
130
|
|
|
return unlink($filename); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @inheritDoc |
135
|
|
|
*/ |
136
|
|
|
public function isFile(string $filename): bool |
137
|
|
|
{ |
138
|
|
|
return is_file($filename); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @inheritDoc |
143
|
|
|
*/ |
144
|
|
|
public function isDirectory(string $dirname): bool |
145
|
|
|
{ |
146
|
|
|
return is_dir($dirname); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @inheritDoc |
151
|
|
|
*/ |
152
|
|
|
public function listDirectory(string $dirname) |
153
|
|
|
{ |
154
|
|
|
$entries = []; |
155
|
|
|
|
156
|
|
|
try { |
157
|
|
|
foreach (scandir($dirname) as $item) { |
158
|
|
|
if ($item != '.' && $item != '..') { |
159
|
|
|
$entries[] = realpath($dirname . DS . $item); |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
return $entries; |
164
|
|
|
|
165
|
|
|
} catch (Throwable $e) { |
166
|
|
|
return false; |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Find path names matching a pattern |
172
|
|
|
* @param string $pattern |
173
|
|
|
* @param int $flags |
174
|
|
|
* @return array|false |
175
|
|
|
*/ |
176
|
|
|
public function glob(string $pattern, int $flags = 0) |
177
|
|
|
{ |
178
|
|
|
return glob($pattern, $flags); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Is Readable |
183
|
|
|
* @param string $filename |
184
|
|
|
* @return bool |
185
|
|
|
*/ |
186
|
|
|
public function isReadable(string $filename): bool |
187
|
|
|
{ |
188
|
|
|
return is_readable($filename); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Is Writable |
193
|
|
|
* @param string $filename |
194
|
|
|
* @return bool |
195
|
|
|
*/ |
196
|
|
|
public function isWritable(string $filename): bool |
197
|
|
|
{ |
198
|
|
|
return is_writable($filename); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Gets the content between given lines |
203
|
|
|
* @param string $filename |
204
|
|
|
* @param int $offset |
205
|
|
|
* @param int|null $length |
206
|
|
|
* @param int $flags |
207
|
|
|
* @return array |
208
|
|
|
*/ |
209
|
|
|
public function getLines(string $filename, int $offset, ?int $length, int $flags = 0): array |
210
|
|
|
{ |
211
|
|
|
return array_slice(file($filename, $flags), $offset, $length, true); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Gets the file name |
216
|
|
|
* @param string $path |
217
|
|
|
* @return string |
218
|
|
|
*/ |
219
|
|
|
public function fileName(string $path): string |
220
|
|
|
{ |
221
|
|
|
return (string)pathinfo($path, PATHINFO_FILENAME); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Gets the file extension |
226
|
|
|
* @param string $path |
227
|
|
|
* @return string |
228
|
|
|
*/ |
229
|
|
|
public function extension(string $path): string |
230
|
|
|
{ |
231
|
|
|
return (string)pathinfo($path, PATHINFO_EXTENSION); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* Includes the required file |
236
|
|
|
* @param string $file |
237
|
|
|
* @param bool $once |
238
|
|
|
*/ |
239
|
|
|
public function require(string $file, bool $once = false) |
240
|
|
|
{ |
241
|
|
|
if ($once) { |
242
|
|
|
require_once $file; |
243
|
|
|
} else { |
244
|
|
|
require $file; |
245
|
|
|
} |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* Includes a file |
250
|
|
|
* @param string $file |
251
|
|
|
* @param bool $once |
252
|
|
|
*/ |
253
|
|
|
public function include(string $file, bool $once = false) |
254
|
|
|
{ |
255
|
|
|
if ($once) { |
256
|
|
|
include_once $file; |
257
|
|
|
} else { |
258
|
|
|
include $file; |
259
|
|
|
} |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
} |
263
|
|
|
|