-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathFileOperationTrait.php
371 lines (329 loc) · 8.88 KB
/
FileOperationTrait.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
<?php declare(strict_types=1);
/**
* This file is part of the Phootwork package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT License
* @copyright Thomas Gossmann
*/
namespace phootwork\file;
use DateTime;
use phootwork\file\exception\FileException;
use phootwork\lang\Text;
use Stringable;
trait FileOperationTrait {
protected string $pathname;
abstract public function delete(): void;
abstract public function __construct(string|Stringable $pathname);
/**
* Static instantiator
*
* @param string|Stringable $pathname
*
* @return static
*
* @psalm-suppress UnsafeInstantiation
*/
public static function create(string|Stringable $pathname): static {
return new static($pathname);
}
/**
* Returns the file extensions
*
* @return Text the file extension
*/
public function getExtension(): Text {
return new Text(pathinfo($this->pathname, PATHINFO_EXTENSION));
}
/**
* Returns the filename
*
* @return Text the filename
*/
public function getFilename(): Text {
return new Text(basename($this->pathname));
}
/**
* Gets the path without filename
*
* @return Text
*/
public function getDirname(): Text {
return new Text(dirname($this->pathname));
}
/**
* Gets the path to the file
*
* @return Text
*/
public function getPathname(): Text {
return new Text($this->pathname);
}
/**
* Converts the path into a path object
*
* @return Path
*/
public function toPath(): Path {
return new Path($this->pathname);
}
/**
* Gets last access time.
*
* @throws FileException
*
* @return DateTime
*/
public function getLastAccessedAt(): DateTime {
try {
$timestamp = fileatime($this->pathname);
$time = new DateTime();
$time->setTimestamp($timestamp);
return $time;
} catch (\Exception $e) {
throw new FileException($e->getMessage(), (int) $e->getCode(), $e);
}
}
/**
* Gets the created time.
*
* @throws FileException
*
* @return DateTime
*/
public function getCreatedAt(): DateTime {
try {
$timestamp = filemtime($this->pathname);
$time = new DateTime();
$time->setTimestamp($timestamp);
return $time;
} catch (\Exception $e) {
throw new FileException($e->getMessage(), (int) $e->getCode(), $e);
}
}
/**
* Gets last modified time.
*
* @throws FileException
*
* @return DateTime
*/
public function getModifiedAt(): DateTime {
try {
$timestamp = filemtime($this->pathname);
$time = new DateTime();
$time->setTimestamp($timestamp);
return $time;
} catch (\Exception $e) {
throw new FileException($e->getMessage(), (int) $e->getCode(), $e);
}
}
/**
* Gets file inode
*
* @return int|null Returns the inode number of the file, or NULL on failure.
*/
public function getInode(): ?int {
$inode = fileinode($this->pathname);
return false === $inode ? null : $inode;
}
/**
* Gets file group
*
* @return int|null Returns the group ID, or NULL if an error occurs.
*/
public function getGroup(): ?int {
$group = filegroup($this->pathname);
return false === $group ? null : $group;
}
/**
* Gets file owner
*
* @return int|null Returns the user ID of the owner, or NULL on failure.
*/
public function getOwner(): ?int {
$owner = fileowner($this->pathname);
return false === $owner ? null : $owner;
}
/**
* Gets file permissions
*
* @return int Returns the file's permissions as a numeric mode. Lower bits of this
* mode are the same as the permissions expected by chmod(), however on most platforms
* the return value will also include information on the type of file given as filename.
*/
public function getPermissions(): int {
return fileperms($this->pathname);
}
/**
* Checks its existance
*
* @return bool Returns TRUE if exists; FALSE otherwise. Will return FALSE for symlinks
* pointing to non-existing files.
*/
public function exists(): bool {
return file_exists($this->pathname);
}
/**
* Tells whether is executable
*
* @return bool Returns TRUE if exists and is executable.
*/
public function isExecutable(): bool {
return is_executable($this->pathname);
}
/**
* Tells whether is readable
*
* @return bool Returns TRUE if exists and is readable.
*/
public function isReadable(): bool {
return is_readable($this->pathname);
}
/**
* Tells whether is writable
*
* @return bool Returns TRUE if exists and is writable.
*/
public function isWritable(): bool {
return is_writable($this->pathname);
}
/**
* Tells whether the filename is a symbolic link
*
* @return bool Returns TRUE if the filename exists and is a symbolic link, FALSE otherwise.
*/
public function isLink(): bool {
return is_link($this->pathname);
}
/**
* Returns the target if this is a symbolic link
*
* @see #isLink
*
* @return Path|null The target path or null if this isn't a link
*/
public function getLinkTarget(): ?Path {
if ($this->isLink()) {
return new Path(readlink($this->pathname));
}
return null;
}
/**
* Attempts to change the group.
*
* Only the superuser may change the group arbitrarily; other users may
* change the group of a file to any group of which that user is a member.
*
* @param string|int $group A group name or number.
*
* @return bool Returns TRUE on success or FALSE on failure.
*/
public function setGroup(string|int $group): bool {
return $this->isLink() ? lchgrp($this->pathname, $group) : chgrp($this->pathname, $group);
}
/**
* Attempts to change the mode.
*
* @see #setGroup
* @see #setOwner
*
* @param int $mode
* Note that mode is not automatically assumed to be an octal value, so strings
* (such as "g+w") will not work properly. To ensure the expected operation, you
* need to prefix mode with a zero (0).
*
* @return bool Returns TRUE on success or FALSE on failure.
*/
public function setMode(int $mode): bool {
return chmod($this->pathname, $mode);
}
/**
* Changes file owner
*
* Attempts to change the owner. Only the superuser may change the owner of a file.
*
* @param string|int $user A user name or number.
*
* @return bool Returns TRUE on success or FALSE on failure.
*/
public function setOwner(string|int $user): bool {
return $this->isLink() ? lchown($this->pathname, $user) : chown($this->pathname, $user);
}
/**
* Copies the file
*
* If the destination file already exists, it will be overwritten.
*
*
* @param string|Text|Path $destination The destination path.
*
* @throws FileException When an error appeared.
*/
public function copy(Path|Text|string $destination): void {
$destination = $this->getDestination($destination);
if (!@copy($this->getPathname()->toString(), $destination->toString())) {
throw new FileException(sprintf('Failed to copy %s to %s', $this->pathname, (string) $destination));
}
}
/**
* Moves the file
*
* @param string|Text|Path $destination
*
* @throws FileException When an error appeared.
*/
public function move(Path|Text|string $destination): void {
$destination = $this->getDestination($destination);
if (@rename($this->getPathname()->toString(), $destination->toString())) {
$this->pathname = (string) $destination;
} else {
throw new FileException(sprintf('Failed to move %s to %s', $this->pathname, (string) $destination));
}
}
/**
* Transforms destination into path and ensures, parent directory exists
*
* @param string|Text|Path $destination
*
* @throws FileException
*
* @return Path
*/
private function getDestination(Path|Text|string $destination): Path {
$destination = $destination instanceof Path ? $destination : new Path($destination);
$targetDir = new Directory($destination->getDirname());
$targetDir->make();
return $destination;
}
/**
* Creates a symlink to the given destination
*
* @param string|Path|Text $destination
*
* @throws FileException
*
* @psalm-suppress PossiblyNullReference If $target->isLink() is true then $target->getLinkTarget() is never null
*/
public function linkTo(Path|Text|string $destination): void {
$target = new FileDescriptor($destination);
$targetDir = new Directory($target->getDirname());
$targetDir->make();
$ok = false;
if ($target->isLink()) {
if (!$target->getLinkTarget()->equals($this->pathname)) {
$target->delete();
} else {
$ok = true;
}
}
if (!$ok && @symlink($this->pathname, $target->getPathname()->toString()) !== true) {
$report = error_get_last();
if (is_array($report) && DIRECTORY_SEPARATOR === '\\' && str_contains($report['message'], 'error code(1314)')) {
throw new FileException('Unable to create symlink due to error code 1314: \'A required privilege is not held by the client\'. Do you have the required Administrator-rights?');
}
throw new FileException(sprintf('Failed to create symbolic link from %s to %s', $this->pathname, (string) $targetDir));
}
}
}