-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathFile.php
151 lines (130 loc) · 3.63 KB
/
File.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
<?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;
/**
* Class File
*/
class File implements Stringable {
use FileOperationTrait;
public function __construct(string|Stringable $filename) {
$this->pathname = (string) $filename;
}
/**
* Reads contents from the file
*
* @throws FileException
*
* @return Text contents
*/
public function read(): Text {
if (!$this->exists()) {
throw new FileException(sprintf('File does not exist: %s', $this->getFilename()->toString()));
}
if (!$this->isReadable()) {
throw new FileException(sprintf('You don\'t have permissions to access %s file', $this->getFilename()->toString()));
}
return new Text(file_get_contents($this->pathname));
}
/**
* Writes contents to the file
*
* @param string|Stringable $contents
*
* @throws FileException
*
* @return $this
*/
public function write(Stringable|string $contents): self {
$dir = new Directory($this->getDirname());
$dir->make();
if ($this->exists() && !$this->isWritable()) {
throw new FileException(
"Impossible to write the file `{$this->getPathname()}`: do you have enough permissions?"
);
}
file_put_contents($this->pathname, (string) $contents);
return $this;
}
/**
* Append the content at the end of the file.
*
* @param Stringable|string $contents The content to append
*
* @throws FileException if the file does not exist or is not writable
*
* @return $this
*/
public function append(Stringable|string $contents): self {
if (!$this->exists() || !$this->isWritable()) {
throw new FileException(
"Impossible to write the file `{$this->getPathname()}`: do you have enough permissions?"
);
}
file_put_contents($this->pathname, (string) $contents, FILE_APPEND);
return $this;
}
/**
* Touches the file
*
* @param DateTime|int|null $created
* @param DateTime|int|null $lastAccessed
*
* @throws FileException when something goes wrong
*/
public function touch(DateTime|int|null $created = null, DateTime|int|null $lastAccessed = null): void {
$created = $created instanceof DateTime
? $created->getTimestamp()
: ($created === null ? time() : $created);
$lastAccessed = $lastAccessed instanceof DateTime
? $lastAccessed->getTimestamp()
: ($lastAccessed === null ? time() : $lastAccessed);
if (!@touch($this->pathname, $created, $lastAccessed)) {
throw new FileException(sprintf('Failed to touch file at %s', $this->pathname));
}
}
/**
* Deletes the file
*
* @throws FileException when something goes wrong
*/
public function delete(): void {
if (!@unlink($this->pathname)) {
throw new FileException(sprintf('Failed to delete file at %s', $this->pathname));
}
}
/**
* Get the size of the file in bytes.
*
* @throws FileException If the file does not exist or it's not accessible
*/
public function getSize(): int {
$size = @filesize($this->getPathname()->toString());
if ($size === false) {
$message = Text::create(error_get_last()['message'] ?? '')
->replace('filesize(): ', '')
->prepend('Impossible to get the file size: ')
->trim(' :')
->ensureEnd('.')
;
throw new FileException((string) $message);
}
return $size;
}
/**
* String representation of this file as pathname
*/
public function __toString(): string {
return $this->pathname;
}
}