diff --git a/src/Driver/UvFile.php b/src/Driver/UvFile.php index 23765bc..5b3f770 100644 --- a/src/Driver/UvFile.php +++ b/src/Driver/UvFile.php @@ -1,5 +1,4 @@ eventLoopHandle = $driver->getHandle(); $this->onClose = new DeferredFuture; - - $this->priorVersion = \version_compare(\phpversion('uv'), '0.3.0', '<'); } public function read(?Cancellation $cancellation = null, int $length = self::DEFAULT_READ_LENGTH): ?string @@ -86,16 +79,6 @@ final class UvFile extends Internal\QueuedWritesFile $deferred->complete($length ? $buffer : null); }; - if ($this->priorVersion) { - $onRead = static function ($fh, $result, $buffer) use ($onRead): void { - if ($result < 0) { - $buffer = $result; // php-uv v0.3.0 changed the callback to put an int in $buffer on error. - } - - $onRead($result, $buffer); - }; - } - \uv_fs_read($this->eventLoopHandle, $this->fh, $this->position, $length, $onRead); $id = $cancellation?->subscribe(function (\Throwable $exception) use ($deferred): void { diff --git a/src/Driver/UvFilesystemDriver.php b/src/Driver/UvFilesystemDriver.php index 695bdc3..f396c70 100644 --- a/src/Driver/UvFilesystemDriver.php +++ b/src/Driver/UvFilesystemDriver.php @@ -1,5 +1,4 @@ =') && $driver->getHandle() instanceof \UVLoop; } - /** @var \UVLoop|resource Loop resource of type uv_loop or instance of \UVLoop. */ - private $eventLoopHandle; + private readonly \UVLoop $eventLoopHandle; private readonly Internal\UvPoll $poll; - /** @var bool True if ext-uv version is < 0.3.0. */ - private readonly bool $priorVersion; - - public function __construct(private readonly UvLoopDriver $driver) + public function __construct(private readonly EventLoopDriver $driver) { + if (!self::isSupported($driver)) { + throw new \Error('Event loop did not return a compatible handle'); + } + /** @psalm-suppress PropertyTypeCoercion */ $this->eventLoopHandle = $driver->getHandle(); $this->poll = new Internal\UvPoll($driver); - $this->priorVersion = \version_compare(\phpversion('uv'), '0.3.0', '<'); } public function openFile(string $path, string $mode): UvFile @@ -83,16 +85,6 @@ final class UvFilesystemDriver implements FilesystemDriver $deferred->complete($stat); }; - if ($this->priorVersion) { - $callback = static function ($fh, $stat) use ($callback): void { - if (empty($fh)) { - $stat = 0; - } - - $callback($stat); - }; - } - \uv_fs_stat($this->eventLoopHandle, $path, $callback); try { @@ -107,17 +99,9 @@ final class UvFilesystemDriver implements FilesystemDriver $deferred = new DeferredFuture; $this->poll->listen(); - if ($this->priorVersion) { - $callback = static function ($fh, $stat) use ($deferred): void { - $deferred->complete(empty($fh) ? null : $stat); - }; - } else { - $callback = static function ($stat) use ($deferred): void { - $deferred->complete(\is_int($stat) ? null : $stat); - }; - } - - \uv_fs_lstat($this->eventLoopHandle, $path, $callback); + \uv_fs_lstat($this->eventLoopHandle, $path, static function ($stat) use ($deferred): void { + $deferred->complete(\is_int($stat) ? null : $stat); + }); try { return $deferred->getFuture()->await(); @@ -160,27 +144,14 @@ final class UvFilesystemDriver implements FilesystemDriver $deferred = new DeferredFuture; $this->poll->listen(); - if ($this->priorVersion) { - $callback = static function ($fh, $target) use ($deferred): void { - if (!(bool) $fh) { - $deferred->error(new FilesystemException("Could not read symbolic link")); - return; - } + \uv_fs_readlink($this->eventLoopHandle, $target, static function ($target) use ($deferred): void { + if (\is_int($target)) { + $deferred->error(new FilesystemException("Could not read symbolic link")); + return; + } - $deferred->complete($target); - }; - } else { - $callback = static function ($target) use ($deferred): void { - if (\is_int($target)) { - $deferred->error(new FilesystemException("Could not read symbolic link")); - return; - } - - $deferred->complete($target); - }; - } - - \uv_fs_readlink($this->eventLoopHandle, $target, $callback); + $deferred->complete($target); + }); try { return $deferred->getFuture()->await(); @@ -297,28 +268,16 @@ final class UvFilesystemDriver implements FilesystemDriver $deferred = new DeferredFuture; $this->poll->listen(); - if ($this->priorVersion) { - \uv_fs_readdir($this->eventLoopHandle, $path, 0, static function ($fh, $data) use ($deferred, $path): void { - if (empty($fh) && $data !== 0) { - $deferred->error(new FilesystemException("Failed reading contents from {$path}")); - } elseif ($data === 0) { - $deferred->complete([]); - } else { - $deferred->complete($data); - } - }); - } else { - /** @noinspection PhpUndefinedFunctionInspection */ - \uv_fs_scandir($this->eventLoopHandle, $path, static function ($data) use ($deferred, $path): void { - if (\is_int($data) && $data !== 0) { - $deferred->error(new FilesystemException("Failed reading contents from {$path}")); - } elseif ($data === 0) { - $deferred->complete([]); - } else { - $deferred->complete($data); - } - }); - } + /** @noinspection PhpUndefinedFunctionInspection */ + \uv_fs_scandir($this->eventLoopHandle, $path, static function ($data) use ($deferred, $path): void { + if (\is_int($data) && $data !== 0) { + $deferred->error(new FilesystemException("Failed reading contents from {$path}")); + } elseif ($data === 0) { + $deferred->complete([]); + } else { + $deferred->complete($data); + } + }); try { return $deferred->getFuture()->await(); @@ -528,42 +487,24 @@ final class UvFilesystemDriver implements FilesystemDriver { $deferred = new DeferredFuture; - if ($this->priorVersion) { - $callback = static function ($fileHandle, $readBytes, $buffer) use ($deferred): void { - $deferred->complete($readBytes < 0 ? null : $buffer); - }; - } else { - $callback = static function ($readBytes, $buffer) use ($deferred): void { - $deferred->complete($readBytes < 0 ? null : $buffer); - }; - } + $callback = static function ($readBytes, $buffer) use ($deferred): void { + $deferred->complete($readBytes < 0 ? null : $buffer); + }; \uv_fs_read($this->eventLoopHandle, $fileHandle, 0, $length, $callback); return $deferred->getFuture()->await(); } - private function doWrite(string $path, string $contents): void - { - } - private function createGenericCallback(DeferredFuture $deferred, string $error): \Closure { - $callback = static function (int $result) use ($deferred, $error): void { + return static function (int $result) use ($deferred, $error): void { if ($result !== 0) { $deferred->error(new FilesystemException($error)); return; } - $deferred->complete(null); + $deferred->complete(); }; - - if ($this->priorVersion) { - $callback = static function (bool $result) use ($callback): void { - $callback($result ? 0 : -1); - }; - } - - return $callback; } } diff --git a/src/Internal/UvPoll.php b/src/Internal/UvPoll.php index 5bccadf..fec197d 100644 --- a/src/Internal/UvPoll.php +++ b/src/Internal/UvPoll.php @@ -2,7 +2,7 @@ namespace Amp\File\Internal; -use Revolt\EventLoop\Driver\UvDriver as UvLoopDriver; +use Revolt\EventLoop\Driver as EventLoopDriver; /** @internal */ final class UvPoll @@ -11,7 +11,7 @@ final class UvPoll private int $requests = 0; - public function __construct(private readonly UvLoopDriver $driver) + public function __construct(private readonly EventLoopDriver $driver) { // Create dummy watcher to keep loop running while polling. diff --git a/src/functions.php b/src/functions.php index c81dd7e..80f8b3d 100644 --- a/src/functions.php +++ b/src/functions.php @@ -55,7 +55,6 @@ function createDefaultDriver(): FilesystemDriver $driver = EventLoop::getDriver(); if (UvFilesystemDriver::isSupported($driver)) { - /** @var EventLoop\Driver\UvDriver $driver */ return new UvFilesystemDriver($driver); }