14
14
15
15
final class EioFile implements File
16
16
{
17
- private Internal \EioPoll $ poll ;
17
+ private readonly Internal \EioPoll $ poll ;
18
18
19
19
/** @var resource eio file handle. */
20
20
private $ fh ;
@@ -35,12 +35,10 @@ final class EioFile implements File
35
35
36
36
private ?Future $ closing = null ;
37
37
38
+ private readonly DeferredFuture $ onClose ;
39
+
38
40
/**
39
- * @param Internal\EioPoll $poll
40
41
* @param resource $fh
41
- * @param string $path
42
- * @param string $mode
43
- * @param int $size
44
42
*/
45
43
public function __construct (Internal \EioPoll $ poll , $ fh , string $ path , string $ mode , int $ size )
46
44
{
@@ -52,6 +50,14 @@ public function __construct(Internal\EioPoll $poll, $fh, string $path, string $m
52
50
$ this ->position = ($ mode [0 ] === "a " ) ? $ size : 0 ;
53
51
54
52
$ this ->queue = new \SplQueue ;
53
+ $ this ->onClose = new DeferredFuture ;
54
+ }
55
+
56
+ public function __destruct ()
57
+ {
58
+ if (!$ this ->onClose ->isComplete ()) {
59
+ $ this ->onClose ->complete ();
60
+ }
55
61
}
56
62
57
63
public function read (?Cancellation $ cancellation = null , int $ length = self ::DEFAULT_READ_LENGTH ): ?string
@@ -63,7 +69,7 @@ public function read(?Cancellation $cancellation = null, int $length = self::DEF
63
69
$ this ->isActive = true ;
64
70
65
71
$ remaining = $ this ->size - $ this ->position ;
66
- $ length = $ length > $ remaining ? $ remaining : $ length ;
72
+ $ length = \min ( $ length, $ remaining) ;
67
73
68
74
$ deferred = new DeferredFuture ;
69
75
$ this ->poll ->listen ();
@@ -152,14 +158,13 @@ public function close(): void
152
158
return ;
153
159
}
154
160
155
- $ deferred = new DeferredFuture ;
156
- $ this ->closing = $ deferred ->getFuture ();
161
+ $ this ->closing = $ this ->onClose ->getFuture ();
157
162
$ this ->poll ->listen ();
158
163
159
164
\eio_close ($ this ->fh , \EIO_PRI_DEFAULT , static function (DeferredFuture $ deferred ): void {
160
165
// Ignore errors when closing file, as the handle will become invalid anyway.
161
- $ deferred ->complete (null );
162
- }, $ deferred );
166
+ $ deferred ->complete ();
167
+ }, $ this -> onClose );
163
168
164
169
try {
165
170
$ this ->closing ->await ();
@@ -173,6 +178,11 @@ public function isClosed(): bool
173
178
return $ this ->closing !== null ;
174
179
}
175
180
181
+ public function onClose (\Closure $ onClose ): void
182
+ {
183
+ $ this ->onClose ->getFuture ()->finally ($ onClose );
184
+ }
185
+
176
186
public function truncate (int $ size ): void
177
187
{
178
188
if ($ this ->isActive && $ this ->queue ->isEmpty ()) {
0 commit comments