22
33namespace Amp \File \Driver ;
44
5- use Amp \File \File ;
65use Amp \File \FilesystemDriver ;
76use Amp \File \FilesystemException ;
87
98final class BlockingFilesystemDriver implements FilesystemDriver
109{
11- public function openFile (string $ path , string $ mode ): File
10+ private readonly \Closure $ errorHandler ;
11+
12+ public function __construct ()
13+ {
14+ $ this ->errorHandler = static fn () => true ;
15+ }
16+
17+ public function openFile (string $ path , string $ mode ): BlockingFile
1218 {
1319 $ mode = \str_replace (['b ' , 't ' , 'e ' ], '' , $ mode );
1420
@@ -30,7 +36,7 @@ public function openFile(string $path, string $mode): File
3036 }
3137
3238 try {
33- \set_error_handler (static function ($ type , $ message ) use ($ path , $ mode ) {
39+ \set_error_handler (static function (int $ type , string $ message ) use ($ path , $ mode ): never {
3440 throw new FilesystemException ("Failed to open ' {$ path }' in mode ' {$ mode }': {$ message }" );
3541 });
3642
@@ -47,21 +53,31 @@ public function openFile(string $path, string $mode): File
4753 public function getStatus (string $ path ): ?array
4854 {
4955 \clearstatcache (true , $ path );
56+ \set_error_handler ($ this ->errorHandler );
5057
51- return @\stat ($ path ) ?: null ;
58+ try {
59+ return \stat ($ path ) ?: null ;
60+ } finally {
61+ \restore_error_handler ();
62+ }
5263 }
5364
5465 public function getLinkStatus (string $ path ): ?array
5566 {
5667 \clearstatcache (true , $ path );
68+ \set_error_handler ($ this ->errorHandler );
5769
58- return @\lstat ($ path ) ?: null ;
70+ try {
71+ return \lstat ($ path ) ?: null ;
72+ } finally {
73+ \restore_error_handler ();
74+ }
5975 }
6076
6177 public function createSymlink (string $ target , string $ link ): void
6278 {
6379 try {
64- \set_error_handler (static function ($ type , $ message ) use ($ target , $ link ) {
80+ \set_error_handler (static function (int $ type , string $ message ) use ($ target , $ link ): never {
6581 throw new FilesystemException ("Could not create symbolic link ' {$ link }' to ' {$ target }': {$ message }" );
6682 });
6783
@@ -76,7 +92,7 @@ public function createSymlink(string $target, string $link): void
7692 public function createHardlink (string $ target , string $ link ): void
7793 {
7894 try {
79- \set_error_handler (static function ($ type , $ message ) use ($ target , $ link ) {
95+ \set_error_handler (static function (int $ type , string $ message ) use ($ target , $ link ): never {
8096 throw new FilesystemException ("Could not create hard link ' {$ link }' to ' {$ target }': {$ message }" );
8197 });
8298
@@ -91,7 +107,7 @@ public function createHardlink(string $target, string $link): void
91107 public function resolveSymlink (string $ target ): string
92108 {
93109 try {
94- \set_error_handler (static function ($ type , $ message ) use ($ target ) {
110+ \set_error_handler (static function (int $ type , string $ message ) use ($ target ): never {
95111 throw new FilesystemException ("Could not resolve symbolic link ' {$ target }': {$ message }" );
96112 });
97113
@@ -108,7 +124,7 @@ public function resolveSymlink(string $target): string
108124 public function move (string $ from , string $ to ): void
109125 {
110126 try {
111- \set_error_handler (static function ($ type , $ message ) use ($ from , $ to ) {
127+ \set_error_handler (static function (int $ type , string $ message ) use ($ from , $ to ): never {
112128 throw new FilesystemException ("Could not move file from ' {$ from }' to ' {$ to }': {$ message }" );
113129 });
114130
@@ -123,7 +139,7 @@ public function move(string $from, string $to): void
123139 public function deleteFile (string $ path ): void
124140 {
125141 try {
126- \set_error_handler (static function ($ type , $ message ) use ($ path ) {
142+ \set_error_handler (static function (int $ type , string $ message ) use ($ path ): never {
127143 throw new FilesystemException ("Could not delete file ' {$ path }': {$ message }" );
128144 });
129145
@@ -138,7 +154,7 @@ public function deleteFile(string $path): void
138154 public function createDirectory (string $ path , int $ mode = 0777 ): void
139155 {
140156 try {
141- \set_error_handler (static function ($ type , $ message ) use ($ path ) {
157+ \set_error_handler (static function (int $ type , string $ message ) use ($ path ): never {
142158 throw new FilesystemException ("Could not create directory ' {$ path }': {$ message }" );
143159 });
144160
@@ -154,7 +170,7 @@ public function createDirectory(string $path, int $mode = 0777): void
154170 public function createDirectoryRecursively (string $ path , int $ mode = 0777 ): void
155171 {
156172 try {
157- \set_error_handler (static function ($ type , $ message ) use ($ path ) {
173+ \set_error_handler (static function (int $ type , string $ message ) use ($ path ): bool {
158174 if (!\is_dir ($ path )) {
159175 throw new FilesystemException ("Could not create directory ' {$ path }': {$ message }" );
160176 }
@@ -182,7 +198,7 @@ public function createDirectoryRecursively(string $path, int $mode = 0777): void
182198 public function deleteDirectory (string $ path ): void
183199 {
184200 try {
185- \set_error_handler (static function ($ type , $ message ) use ($ path ) {
201+ \set_error_handler (static function (int $ type , string $ message ) use ($ path ): never {
186202 throw new FilesystemException ("Could not remove directory ' {$ path }': {$ message }" );
187203 });
188204
@@ -197,7 +213,7 @@ public function deleteDirectory(string $path): void
197213 public function listFiles (string $ path ): array
198214 {
199215 try {
200- \set_error_handler (static function ($ type , $ message ) use ($ path ) {
216+ \set_error_handler (static function (int $ type , string $ message ) use ($ path ): never {
201217 throw new FilesystemException ("Failed to list files in ' {$ path }': {$ message }" );
202218 });
203219
@@ -208,7 +224,7 @@ public function listFiles(string $path): array
208224 if ($ arr = \scandir ($ path )) {
209225 \clearstatcache (true , $ path );
210226
211- return \array_values (\array_filter ($ arr , static function ($ el ) {
227+ return \array_values (\array_filter ($ arr , static function ($ el ): bool {
212228 return $ el !== ". " && $ el !== ".. " ;
213229 }));
214230 }
@@ -222,7 +238,7 @@ public function listFiles(string $path): array
222238 public function changePermissions (string $ path , int $ mode ): void
223239 {
224240 try {
225- \set_error_handler (static function ($ type , $ message ) use ($ path ) {
241+ \set_error_handler (static function (int $ type , string $ message ) use ($ path ): never {
226242 throw new FilesystemException ("Failed to change permissions for ' {$ path }': {$ message }" );
227243 });
228244
@@ -237,7 +253,7 @@ public function changePermissions(string $path, int $mode): void
237253 public function changeOwner (string $ path , ?int $ uid , ?int $ gid ): void
238254 {
239255 try {
240- \set_error_handler (static function ($ type , $ message ) use ($ path ) {
256+ \set_error_handler (static function (int $ type , string $ message ) use ($ path ): never {
241257 throw new FilesystemException ("Failed to change owner for ' {$ path }': {$ message }" );
242258 });
243259
@@ -259,7 +275,7 @@ public function changeOwner(string $path, ?int $uid, ?int $gid): void
259275 public function touch (string $ path , ?int $ modificationTime , ?int $ accessTime ): void
260276 {
261277 try {
262- \set_error_handler (static function ($ type , $ message ) use ($ path ) {
278+ \set_error_handler (static function (int $ type , string $ message ) use ($ path ): never {
263279 throw new FilesystemException ("Failed to touch ' {$ path }': {$ message }" );
264280 });
265281
@@ -277,7 +293,7 @@ public function touch(string $path, ?int $modificationTime, ?int $accessTime): v
277293 public function read (string $ path ): string
278294 {
279295 try {
280- \set_error_handler (static function ($ type , $ message ) use ($ path ) {
296+ \set_error_handler (static function (int $ type , string $ message ) use ($ path ): never {
281297 throw new FilesystemException ("Failed to read ' {$ path }': {$ message }" );
282298 });
283299
@@ -294,7 +310,7 @@ public function read(string $path): string
294310 public function write (string $ path , string $ contents ): void
295311 {
296312 try {
297- \set_error_handler (static function ($ type , $ message ) use ($ path ) {
313+ \set_error_handler (static function (int $ type , string $ message ) use ($ path ): never {
298314 throw new FilesystemException ("Failed to write to ' {$ path }': {$ message }" );
299315 });
300316
0 commit comments