-
Notifications
You must be signed in to change notification settings - Fork 0
/
File.php
197 lines (180 loc) · 5.29 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
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
<?php
namespace Aternos\IO\System\File;
use Aternos\IO\Exception\CreateDirectoryException;
use Aternos\IO\Exception\CreateFileException;
use Aternos\IO\Exception\DeleteException;
use Aternos\IO\Exception\IOException;
use Aternos\IO\Exception\MissingPermissionsException;
use Aternos\IO\Exception\ReadException;
use Aternos\IO\Exception\StatException;
use Aternos\IO\Exception\WriteException;
use Aternos\IO\Interfaces\Types\FileInterface;
use Aternos\IO\System\Directory\Directory;
use Aternos\IO\System\FilesystemElement;
use Aternos\IO\System\Socket\Traits\CloseSocketTrait;
use Aternos\IO\System\Socket\Traits\GetSocketPositionTrait;
use Aternos\IO\System\Socket\Traits\IsEndOfFileSocketTrait;
use Aternos\IO\System\Socket\Traits\OpenSocketTrait;
use Aternos\IO\System\Socket\Traits\ReadSocketTrait;
use Aternos\IO\System\Socket\Traits\SetSocketPositionTrait;
use Aternos\IO\System\Socket\Traits\TruncateSocketTrait;
use Aternos\IO\System\Socket\Traits\WriteSocketTrait;
/**
* Class File
*
* Filesystem file
*
* @package Aternos\IO\System\File
*/
class File extends FilesystemElement implements FileInterface
{
use OpenSocketTrait,
CloseSocketTrait,
GetSocketPositionTrait,
IsEndOfFileSocketTrait,
ReadSocketTrait,
SetSocketPositionTrait,
TruncateSocketTrait,
WriteSocketTrait {
write as traitWrite;
read as traitRead;
}
/**
* @inheritDoc
* @throws IOException
* @noinspection PhpMixedReturnTypeCanBeReducedInspection
*/
protected function openSocketResource(): mixed
{
$resource = @fopen($this->path, $this->getMode());
if (!$resource) {
$this->throwException("Could not open {type}");
}
return $resource;
}
/**
* Get the best mode for the file based on the permissions
*
* @return string
* @throws MissingPermissionsException|CreateDirectoryException|IOException
*/
protected function getMode(): string
{
if (!file_exists($this->path)) {
$parentDirectory = new Directory(dirname($this->path));
if (!$parentDirectory->exists()) {
$parentDirectory->create();
}
if (is_writable($parentDirectory->getPath())) {
return "c+b";
}
$this->throwException("Could not open {type} due to missing write permissions in parent directory", MissingPermissionsException::class);
}
if (is_readable($this->path) && is_writable($this->path)) {
return "c+b";
}
if (is_readable($this->path)) {
return "rb";
}
if (is_writable($this->path)) {
return "wb";
}
$this->throwException("Could not open {type} due to missing read and write permissions", MissingPermissionsException::class);
}
/**
* @inheritDoc
* @throws StatException
* @throws IOException
*/
public function getSize(): int
{
$size = @filesize($this->path);
if ($size === false) {
$this->throwException("Could not get {type} size", StatException::class);
}
return $size;
}
/**
* @inheritDoc
*/
public function getName(): string
{
return basename($this->path);
}
/**
* @inheritDoc
* @throws IOException
* @throws ReadException
*/
public function read(int $length): string
{
try {
return $this->traitRead($length);
} catch (ReadException $exception) {
if (!is_readable($this->path)) {
$this->throwException("Could not read from {type} due to missing read permissions", MissingPermissionsException::class);
}
throw $exception;
}
}
/**
* @inheritDoc
* @throws IOException
* @throws WriteException
*/
public function write(string $buffer): static
{
try {
$this->traitWrite($buffer);
} catch (WriteException $exception) {
if (!is_writable($this->path)) {
$this->throwException("Could not write to {type} due to missing write permissions", MissingPermissionsException::class);
}
throw $exception;
}
return $this;
}
/**
* @inheritDoc
* @throws CreateFileException|CreateDirectoryException|IOException
*/
public function create(): static
{
$parentDirectory = new Directory(dirname($this->path));
if (!$parentDirectory->exists()) {
$parentDirectory->create();
}
if (!@touch($this->path)) {
$this->throwException("Could not create {type}", CreateFileException::class);
}
return $this;
}
/**
* @inheritDoc
* @throws DeleteException|IOException
*/
public function delete(): static
{
if (!$this->exists()) {
return $this;
}
if (!@unlink($this->path)) {
$this->throwException("Could not delete {type}", DeleteException::class);
}
return $this;
}
/**
* @inheritDoc
*/
protected function getTypeForErrors(): string
{
return "file";
}
/**
* @inheritDoc
*/
protected function getErrorContext(): ?string
{
return $this->path;
}
}