Skip to content

Commit

Permalink
Merge
Browse files Browse the repository at this point in the history
  • Loading branch information
danog committed May 31, 2024
2 parents c37be07 + be1e245 commit 600acf5
Show file tree
Hide file tree
Showing 18 changed files with 415 additions and 138 deletions.
16 changes: 14 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,22 @@ jobs:
include:
- operating-system: 'ubuntu-latest'
php-version: '8.1'
extensions: uv, eio

- operating-system: 'ubuntu-latest'
php-version: '8.2'
extensions: uv, eio

- operating-system: 'ubuntu-latest'
php-version: '8.3'
extensions: uv
style-fix: none
static-analysis: none

- operating-system: 'ubuntu-latest'
php-version: '8.4'
extensions: uv
style-fix: none
static-analysis: none

- operating-system: 'windows-latest'
Expand All @@ -26,7 +36,9 @@ jobs:

- operating-system: 'macos-latest'
php-version: '8.3'
extensions: uv
job-description: 'on macOS'
style-fix: none
static-analysis: none


Expand All @@ -53,7 +65,7 @@ jobs:
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-version }}
extensions: eio-beta, uv-amphp/ext-uv@master
extensions: ${{ matrix.extensions }}

- name: Get Composer cache directory
id: composer-cache
Expand Down Expand Up @@ -91,7 +103,7 @@ jobs:
env:
PHP_CS_FIXER_IGNORE_ENV: 1
run: vendor/bin/php-cs-fixer --diff --dry-run -v fix
if: runner.os != 'Windows'
if: runner.os != 'Windows' && matrix.style-fix != 'none'

- name: Install composer-require-checker
run: php -r 'file_put_contents("composer-require-checker.phar", file_get_contents("https://github.com/maglnet/ComposerRequireChecker/releases/download/3.7.0/composer-require-checker.phar"));'
Expand Down
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ This package can be installed as a [Composer](https://getcomposer.org/) dependen
composer require amphp/file
```

## Requirements

- PHP 8.1+

`amphp/file` works out of the box without any PHP extensions.
It uses multiple processes by default, but also comes with a blocking driver that uses PHP's blocking functions in the current process.

Expand Down Expand Up @@ -163,7 +167,7 @@ array(13) {

## Security

If you discover any security related issues, please email [`[email protected]`](mailto:[email protected]) instead of using the issue tracker.
If you discover any security related issues, please use the private security issue reporter instead of using the public issue tracker.

## License

Expand Down
1 change: 1 addition & 0 deletions composer-require-check.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
"EIO_S_IWUSR",
"EIO_S_IXUSR",
"UV",
"UVLoop",
"uv_fs_chmod",
"uv_fs_chown",
"uv_fs_fstat",
Expand Down
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"require-dev": {
"amphp/phpunit-util": "^3",
"phpunit/phpunit": "^9",
"psalm/phar": "^5.4",
"psalm/phar": "5.22.2",
"amphp/php-cs-fixer-config": "^2"
},
"suggest": {
Expand All @@ -59,11 +59,13 @@
"autoload-dev": {
"psr-4": {
"Amp\\File\\Test\\": "test",
"Amp\\Cache\\Test\\": "vendor/amphp/cache/test",
"Amp\\Sync\\": "vendor/amphp/sync/test"
}
},
"config": {
"preferred-install": {
"amphp/cache": "source",
"amphp/sync": "source"
}
},
Expand Down
7 changes: 7 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,12 @@
<directory name="src"/>
</errorLevel>
</MissingClosureReturnType>

<RiskyTruthyFalsyComparison>
<errorLevel type="suppress">
<directory name="examples"/>
<directory name="src"/>
</errorLevel>
</RiskyTruthyFalsyComparison>
</issueHandlers>
</psalm>
13 changes: 8 additions & 5 deletions src/Driver/ParallelFilesystemDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ final class ParallelFilesystemDriver implements FilesystemDriver
/** @var int Maximum number of workers to use for open files. */
private int $workerLimit;

/** @var \SplObjectStorage Worker storage. */
/** @var \SplObjectStorage<Worker, int> Worker storage. */
private \SplObjectStorage $workerStorage;

/** @var Future Pending worker request */
Expand All @@ -31,11 +31,11 @@ final class ParallelFilesystemDriver implements FilesystemDriver
/**
* @param int $workerLimit Maximum number of workers to use from the pool for open files.
*/
public function __construct(WorkerPool $pool = null, int $workerLimit = self::DEFAULT_WORKER_LIMIT)
public function __construct(?WorkerPool $pool = null, int $workerLimit = self::DEFAULT_WORKER_LIMIT)
{
$this->pool = $pool ?? workerPool();
$this->workerLimit = $workerLimit;
$this->workerStorage = new \SplObjectStorage;
$this->workerStorage = new \SplObjectStorage();
$this->pendingWorker = Future::complete();
}

Expand All @@ -45,8 +45,11 @@ public function openFile(string $path, string $mode): ParallelFile

$workerStorage = $this->workerStorage;
$worker = new Internal\FileWorker($worker, static function (Worker $worker) use ($workerStorage): void {
\assert($workerStorage->contains($worker));
if (($workerStorage[$worker] -=1) === 0 || !$worker->isRunning()) {
if (!$workerStorage->contains($worker)) {
return;
}

if (($workerStorage[$worker] -= 1) === 0 || !$worker->isRunning()) {
$workerStorage->detach($worker);
}
});
Expand Down
23 changes: 3 additions & 20 deletions src/Driver/UvFile.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?php declare(strict_types=1);
/** @noinspection PhpComposerExtensionStubsInspection */

namespace Amp\File\Driver;

Expand All @@ -10,31 +9,27 @@
use Amp\File\Internal;
use Amp\File\PendingOperationError;
use Amp\Future;
use Revolt\EventLoop\Driver\UvDriver as UvLoopDriver;
use Revolt\EventLoop\Driver as EventLoopDriver;

final class UvFile extends Internal\QueuedWritesFile
{
private readonly Internal\UvPoll $poll;

/** @var \UVLoop|resource */
private $eventLoopHandle;
private readonly \UVLoop $eventLoopHandle;

/** @var resource */
private $fh;

private ?Future $closing = null;

/** @var bool True if ext-uv version is < 0.3.0. */
private readonly bool $priorVersion;

private readonly DeferredFuture $onClose;

/**
* @param Internal\UvPoll $poll Poll for keeping the loop active.
* @param resource $fh File handle.
*/
public function __construct(
UvLoopDriver $driver,
EventLoopDriver $driver,
Internal\UvPoll $poll,
$fh,
string $path,
Expand All @@ -49,8 +44,6 @@ public function __construct(
/** @psalm-suppress PropertyTypeCoercion */
$this->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
Expand Down Expand Up @@ -86,16 +79,6 @@ public function read(?Cancellation $cancellation = null, int $length = self::DEF
$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 {
Expand Down
Loading

0 comments on commit 600acf5

Please sign in to comment.