1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Platine Filesystem |
5
|
|
|
* |
6
|
|
|
* Platine Filesystem is file system abstraction layer extendable by adapters |
7
|
|
|
* |
8
|
|
|
* This content is released under the MIT License (MIT) |
9
|
|
|
* |
10
|
|
|
* Copyright (c) 2020 Platine Filesystem |
11
|
|
|
* Copyright (c) 2019 Alex Sivka |
12
|
|
|
* |
13
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
14
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
15
|
|
|
* in the Software without restriction, including without limitation the rights |
16
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
17
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
18
|
|
|
* furnished to do so, subject to the following conditions: |
19
|
|
|
* |
20
|
|
|
* The above copyright notice and this permission notice shall be included in all |
21
|
|
|
* copies or substantial portions of the Software. |
22
|
|
|
* |
23
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
24
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
25
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
26
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
27
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
28
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
29
|
|
|
* SOFTWARE. |
30
|
|
|
*/ |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @file AbstractLocal.php |
34
|
|
|
* |
35
|
|
|
* The file system local base class |
36
|
|
|
* |
37
|
|
|
* @package Platine\Filesystem\Adapter\Local |
38
|
|
|
* @author Platine Developers Team |
39
|
|
|
* @copyright Copyright (c) 2020 |
40
|
|
|
* @license http://opensource.org/licenses/MIT MIT License |
41
|
|
|
* @link https://www.platine-php.com |
42
|
|
|
* @version 1.0.0 |
43
|
|
|
* @filesource |
44
|
|
|
*/ |
45
|
|
|
|
46
|
|
|
declare(strict_types=1); |
47
|
|
|
|
48
|
|
|
namespace Platine\Filesystem\Adapter\Local; |
49
|
|
|
|
50
|
|
|
use Platine\Filesystem\Adapter\AdapterInterface; |
51
|
|
|
use Platine\Filesystem\Adapter\Local\Exception\FilesystemException; |
52
|
|
|
use Platine\Filesystem\DirectoryInterface; |
53
|
|
|
use Platine\Filesystem\FileInterface; |
54
|
|
|
use Platine\Filesystem\FilesystemInterface; |
55
|
|
|
use Platine\Stdlib\Helper\Path; |
56
|
|
|
|
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @class AbstractLocal |
60
|
|
|
* @package Platine\Filesystem\Adapter\Local |
61
|
|
|
*/ |
62
|
|
|
abstract class AbstractLocal implements FilesystemInterface |
63
|
|
|
{ |
64
|
|
|
/** |
65
|
|
|
* The adapter instance |
66
|
|
|
* @var AdapterInterface |
67
|
|
|
*/ |
68
|
|
|
protected AdapterInterface $adapter; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* The path of the file system |
72
|
|
|
* @var string |
73
|
|
|
*/ |
74
|
|
|
protected string $path; |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* The original path of the file system |
78
|
|
|
* @var string |
79
|
|
|
*/ |
80
|
|
|
protected string $originalPath; |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Create new instance |
84
|
|
|
* @param string $path |
85
|
|
|
* @param AdapterInterface $adapter |
86
|
|
|
*/ |
87
|
|
|
public function __construct(string $path, AdapterInterface $adapter) |
88
|
|
|
{ |
89
|
|
|
$this->adapter = $adapter; |
90
|
|
|
$this->setPath($path); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Return the adapter instance |
95
|
|
|
* @return AdapterInterface |
96
|
|
|
*/ |
97
|
|
|
public function getAdapter(): AdapterInterface |
98
|
|
|
{ |
99
|
|
|
return $this->adapter; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Return the path of the file system |
104
|
|
|
* @return string |
105
|
|
|
*/ |
106
|
|
|
public function getPath(): string |
107
|
|
|
{ |
108
|
|
|
return $this->path; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Set the path of the file system |
113
|
|
|
* @param string $path |
114
|
|
|
* @return $this |
115
|
|
|
*/ |
116
|
|
|
public function setPath(string $path): self |
117
|
|
|
{ |
118
|
|
|
$this->originalPath = $path && $path !== DIRECTORY_SEPARATOR |
119
|
|
|
? rtrim($path, DIRECTORY_SEPARATOR) |
120
|
|
|
: DIRECTORY_SEPARATOR; |
121
|
|
|
$this->path = $this->adapter->getAbsolutePath($path); |
122
|
|
|
|
123
|
|
|
return $this; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Return the original path |
128
|
|
|
* @return string |
129
|
|
|
*/ |
130
|
|
|
public function getOriginalPath(): string |
131
|
|
|
{ |
132
|
|
|
return $this->originalPath; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* {@inheritdoc} |
137
|
|
|
*/ |
138
|
|
|
public function moveTo(string|DirectoryInterface $directory): FileInterface|DirectoryInterface |
139
|
|
|
{ |
140
|
|
|
$dest = $this->copyTo($directory); |
141
|
|
|
$dest->touch($this->getMtime()); |
142
|
|
|
$this->delete(); |
143
|
|
|
|
144
|
|
|
return $dest; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* {@inheritdoc} |
149
|
|
|
*/ |
150
|
|
|
public function chmod(int $mode): self |
151
|
|
|
{ |
152
|
|
|
chmod($this->path, $mode); |
153
|
|
|
|
154
|
|
|
return $this; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* {@inheritdoc} |
159
|
|
|
*/ |
160
|
|
|
public function exists(): bool |
161
|
|
|
{ |
162
|
|
|
return $this->isExists($this->path); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* {@inheritdoc} |
167
|
|
|
*/ |
168
|
|
|
public function getLocation(): string |
169
|
|
|
{ |
170
|
|
|
return dirname($this->path); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* {@inheritdoc} |
175
|
|
|
*/ |
176
|
|
|
public function getMtime(): int |
177
|
|
|
{ |
178
|
|
|
$time = filemtime($this->path); |
179
|
|
|
return $time !== false ? $time : -1; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* {@inheritdoc} |
184
|
|
|
*/ |
185
|
|
|
public function getName(): string |
186
|
|
|
{ |
187
|
|
|
return basename($this->path); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* {@inheritdoc} |
192
|
|
|
*/ |
193
|
|
|
public function getPermission(): string |
194
|
|
|
{ |
195
|
|
|
$permission = fileperms($this->path); |
196
|
|
|
if ($permission === false) { |
197
|
|
|
throw new FilesystemException(sprintf( |
198
|
|
|
'Can not get permission for file [%s]', |
199
|
|
|
$this->path |
200
|
|
|
)); |
201
|
|
|
} |
202
|
|
|
return substr(base_convert((string) $permission, 10, 8), -4); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* {@inheritdoc} |
207
|
|
|
*/ |
208
|
|
|
public function getSize(): int |
209
|
|
|
{ |
210
|
|
|
$size = filesize($this->path); |
211
|
|
|
return $size !== false ? $size : -1; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* {@inheritdoc} |
216
|
|
|
*/ |
217
|
|
|
public function isDir(): bool |
218
|
|
|
{ |
219
|
|
|
return $this->getType() === 'dir'; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* {@inheritdoc} |
224
|
|
|
*/ |
225
|
|
|
public function isFile(): bool |
226
|
|
|
{ |
227
|
|
|
return $this->getType() === 'file'; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* {@inheritdoc} |
232
|
|
|
*/ |
233
|
|
|
public function isReadable(): bool |
234
|
|
|
{ |
235
|
|
|
return is_readable($this->path); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* {@inheritdoc} |
240
|
|
|
*/ |
241
|
|
|
public function isWritable(): bool |
242
|
|
|
{ |
243
|
|
|
return is_writable($this->path); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* {@inheritdoc} |
248
|
|
|
*/ |
249
|
|
|
public function rename(string $newPath): self |
250
|
|
|
{ |
251
|
|
|
$normalizedNewPath = rtrim(Path::normalizePathDS($newPath), '\\/'); |
252
|
|
|
if (strpos($normalizedNewPath, DIRECTORY_SEPARATOR) === false) { |
253
|
|
|
$normalizedNewPath = dirname($this->originalPath) |
254
|
|
|
. DIRECTORY_SEPARATOR . $normalizedNewPath; |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
$newAbsolutePath = $this->adapter->getAbsolutePath($normalizedNewPath); |
258
|
|
|
if ($newAbsolutePath === $this->path) { |
259
|
|
|
return $this; |
260
|
|
|
} |
261
|
|
|
rename($this->path, $newAbsolutePath); |
262
|
|
|
|
263
|
|
|
return $this->setPath($normalizedNewPath); |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* {@inheritdoc} |
268
|
|
|
*/ |
269
|
|
|
public function touch(int $time): self |
270
|
|
|
{ |
271
|
|
|
touch($this->path, $time); |
272
|
|
|
|
273
|
|
|
return $this; |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
/** |
277
|
|
|
* Whether the given path exists |
278
|
|
|
* @param string $path |
279
|
|
|
* @return bool |
280
|
|
|
*/ |
281
|
|
|
protected function isExists(string $path): bool |
282
|
|
|
{ |
283
|
|
|
return file_exists($path); |
284
|
|
|
} |
285
|
|
|
} |
286
|
|
|
|