@@ -30,67 +30,84 @@ public function stat(): PromiseInterface
30
30
public function getContents (int $ offset = 0 , ?int $ maxlen = null ): PromiseInterface
31
31
{
32
32
$ this ->activate ();
33
- return new Promise (function (callable $ resolve , callable $ reject ) use ($ offset , $ maxlen ): void {
34
- \eio_open (
35
- $ this ->path . DIRECTORY_SEPARATOR . $ this ->name ,
36
- \EIO_O_RDONLY ,
37
- 0 ,
38
- \EIO_PRI_DEFAULT ,
39
- function ($ _ , $ fileDescriptor ) use ($ resolve , $ reject , $ offset , $ maxlen ): void {
40
- try {
41
- \eio_fstat ($ fileDescriptor , \EIO_PRI_DEFAULT , function ($ fileDescriptor , $ stat ) use ($ resolve , $ reject , $ offset , $ maxlen ): void {
42
- try {
43
- \eio_read ($ fileDescriptor , $ maxlen ?? (int )$ stat ['size ' ], $ offset , \EIO_PRI_DEFAULT , function ($ fileDescriptor , string $ buffer ) use ($ resolve ): void {
44
- \eio_close ($ fileDescriptor , \EIO_PRI_DEFAULT , function () use ($ resolve , $ buffer ) {
45
- $ this ->deactivate ();
46
- $ resolve ($ buffer );
47
- });
48
- }, $ fileDescriptor );
49
- } catch (\Throwable $ error ) {
50
- $ this ->deactivate ();
51
- $ reject ($ error );
52
- }
33
+ return $ this ->openFile (
34
+ $ this ->path . DIRECTORY_SEPARATOR . $ this ->name ,
35
+ \EIO_O_RDONLY ,
36
+ 0 ,
37
+ )->then (
38
+ function ($ fileDescriptor ) use ($ offset , $ maxlen ): PromiseInterface {
39
+ return $ this ->statFileDescriptor ($ fileDescriptor )->then (function ($ stat ) use ($ fileDescriptor , $ offset , $ maxlen ): PromiseInterface {
40
+ return new Promise (function (callable $ resolve ) use ($ fileDescriptor , $ offset , $ maxlen , $ stat ): void {
41
+ \eio_read ($ fileDescriptor , $ maxlen ?? (int )$ stat ['size ' ], $ offset , \EIO_PRI_DEFAULT , function ($ fileDescriptor , string $ buffer ) use ($ resolve ): void {
42
+ $ resolve ($ this ->closeOpenFile ($ fileDescriptor )->then (function () use ($ buffer ): string {
43
+ return $ buffer ;
44
+ }));
53
45
}, $ fileDescriptor );
54
- } catch (\Throwable $ error ) {
55
- $ this ->deactivate ();
56
- $ reject ($ error );
57
- }
58
- }
59
- );
60
- });
46
+ });
47
+ });
48
+ }
49
+ );
61
50
}
62
51
63
52
public function putContents (string $ contents , int $ flags = 0 )
64
53
{
65
54
$ this ->activate ();
66
- return new Promise (function (callable $ resolve , callable $ reject ) use ($ contents , $ flags ): void {
67
- \eio_open (
55
+ return $ this ->openFile (
68
56
$ this ->path . DIRECTORY_SEPARATOR . $ this ->name ,
69
57
(($ flags & \FILE_APPEND ) == \FILE_APPEND ) ? \EIO_O_RDWR | \EIO_O_APPEND : \EIO_O_RDWR | \EIO_O_CREAT ,
70
- 0644 ,
58
+ 0644
59
+ )->then (
60
+ function ($ fileDescriptor ) use ($ contents , $ flags ): PromiseInterface {
61
+ return new Promise (function (callable $ resolve ) use ($ contents , $ fileDescriptor ): void {
62
+ \eio_write ($ fileDescriptor , $ contents , strlen ($ contents ), 0 , \EIO_PRI_DEFAULT , function ($ fileDescriptor , int $ bytesWritten ) use ($ resolve ): void {
63
+ $ resolve ($ this ->closeOpenFile ($ fileDescriptor )->then (function () use ($ bytesWritten ): int {
64
+ return $ bytesWritten ;
65
+ }));
66
+ }, $ fileDescriptor );
67
+ });
68
+ }
69
+ );
70
+ }
71
+
72
+ private function statFileDescriptor ($ fileDescriptor ): PromiseInterface
73
+ {
74
+ return new Promise (function (callable $ resolve , callable $ reject ) use ($ fileDescriptor ) {
75
+ \eio_fstat ($ fileDescriptor , \EIO_PRI_DEFAULT , function ($ _ , $ stat ) use ($ resolve ): void {
76
+ $ resolve ($ stat );
77
+ }, $ fileDescriptor );
78
+ });
79
+ }
80
+
81
+ private function openFile (string $ path , int $ flags , int $ mode ): PromiseInterface
82
+ {
83
+ return new Promise (function (callable $ resolve , callable $ reject ) use ($ path , $ flags , $ mode ): void {
84
+ \eio_open (
85
+ $ path ,
86
+ $ flags ,
87
+ $ mode ,
71
88
\EIO_PRI_DEFAULT ,
72
- function ($ _ , $ fileDescriptor ) use ($ resolve , $ reject , $ contents , $ flags ): void {
73
- try {
74
- \eio_write ($ fileDescriptor , $ contents , strlen ($ contents ), 0 , \EIO_PRI_DEFAULT , function ($ fileDescriptor , int $ bytesWritten ) use ($ resolve , $ reject ): void {
75
- try {
76
- \eio_close ($ fileDescriptor , \EIO_PRI_DEFAULT , function () use ($ resolve , $ bytesWritten ): void {
77
- $ this ->deactivate ();
78
- $ resolve ($ bytesWritten );
79
- });
80
- } catch (\Throwable $ error ) {
81
- $ this ->deactivate ();
82
- $ reject ($ error );
83
- }
84
- }, $ fileDescriptor );
85
- } catch (\Throwable $ error ) {
86
- $ this ->deactivate ();
87
- $ reject ($ error );
88
- }
89
+ function ($ _ , $ fileDescriptor ) use ($ resolve ): void {
90
+ $ resolve ($ fileDescriptor );
89
91
}
90
92
);
91
93
});
92
94
}
93
95
96
+ private function closeOpenFile ($ fileDescriptor ): PromiseInterface
97
+ {
98
+ return new Promise (function (callable $ resolve ) use ($ fileDescriptor ) {
99
+ try {
100
+ \eio_close ($ fileDescriptor , \EIO_PRI_DEFAULT , function () use ($ resolve ): void {
101
+ $ this ->deactivate ();
102
+ $ resolve ();
103
+ });
104
+ } catch (\Throwable $ error ) {
105
+ $ this ->deactivate ();
106
+ throw $ error ;
107
+ }
108
+ });
109
+ }
110
+
94
111
public function unlink (): PromiseInterface
95
112
{
96
113
$ this ->activate ();
@@ -121,4 +138,4 @@ protected function deactivate(): void
121
138
{
122
139
$ this ->poll ->deactivate ();
123
140
}
124
- }
141
+ }
0 commit comments