Use jetpack autoloader and composer instead of namespace rewrite
This commit is contained in:
144
composer.json
144
composer.json
@ -1,57 +1,93 @@
|
||||
{
|
||||
"name": "devstone/wp-multisite-waas",
|
||||
"url": "https://wpmultisitewaas.org",
|
||||
"description": "The WordPress Multisite Website as a Service (WaaS) plugin.",
|
||||
"version": "2.3.3",
|
||||
"authors": [
|
||||
{
|
||||
"name": "Arindo Duque",
|
||||
"email": "arindo@wpultimo.com",
|
||||
"homepage": "https://wpultimo.com"
|
||||
"name": "devstone/wp-multisite-waas",
|
||||
"url": "https://wpmultisitewaas.org",
|
||||
"description": "The WordPress Multisite Website as a Service (WaaS) plugin.",
|
||||
"version": "2.3.3",
|
||||
"authors": [
|
||||
{
|
||||
"name": "Arindo Duque",
|
||||
"email": "arindo@wpultimo.com",
|
||||
"homepage": "https://wpultimo.com"
|
||||
},
|
||||
{
|
||||
"name": "David Stone",
|
||||
"homepage": "https://github.com/superdav42"
|
||||
}
|
||||
],
|
||||
"license": [
|
||||
"GPL-3.0-or-later"
|
||||
],
|
||||
"type": "wordpress-plugin",
|
||||
"require": {
|
||||
"php": ">=7.4.30",
|
||||
"automattic/jetpack-autoloader": "^5.0.0",
|
||||
"rpnzl/arrch": "dev-master#994258bbefb7722243211654c4f78813312cd5ed",
|
||||
"amphp/amp": "v2.6.2",
|
||||
"pablo-sg-pacheco/wp-namespace-autoloader": "dev-master#069163f215743c83381613749ace0c5a642720b4",
|
||||
"psr/log": "2.0.0",
|
||||
"delight-im/cookie": "v3.4.0",
|
||||
"berlindb/core": "2.0.1",
|
||||
"spatie/ssl-certificate": "1.22.1",
|
||||
"mexitek/phpcolors": "v1.0.4",
|
||||
"nesbot/carbon": "2.71.0",
|
||||
"phpdocumentor/reflection-docblock": "5.3.0",
|
||||
"stripe/stripe-php": "v10.21.0",
|
||||
"hashids/hashids": "4.1.0",
|
||||
"rakit/validation": "dev-master#ff003a35cdf5030a5f2482299f4c93f344a35b29",
|
||||
"ifsnop/mysqldump-php": "v2.12",
|
||||
"mpdf/mpdf": "v8.2.0",
|
||||
"remotelyliving/php-dns": "4.3.0",
|
||||
"jasny/sso": "v0.4.2",
|
||||
"nyholm/psr7": "1.8.0",
|
||||
"symfony/cache": "v5.4.29",
|
||||
"scssphp/scssphp": "v1.11.1",
|
||||
"cweagans/composer-patches": "^1.7"
|
||||
},
|
||||
{
|
||||
"name": "David Stone",
|
||||
"homepage": "https://github.com/superdav42"
|
||||
"require-dev": {
|
||||
"composer/installers": "1.9.0",
|
||||
"phpunit/phpunit": "^9.6.22",
|
||||
"yoast/phpunit-polyfills": "^1.1.3",
|
||||
"woocommerce/woocommerce-sniffs": "^1.0.0",
|
||||
"symplify/vendor-patches": "^11.3"
|
||||
},
|
||||
"config": {
|
||||
"allow-plugins": {
|
||||
"composer/installers": true,
|
||||
"dealerdirect/phpcodesniffer-composer-installer": true,
|
||||
"automattic/jetpack-autoloader": true,
|
||||
"cweagans/composer-patches": true
|
||||
}
|
||||
},
|
||||
"archive": {
|
||||
"exclude": [
|
||||
"tests",
|
||||
"bin",
|
||||
"phpunit.xml.dist",
|
||||
"composer.lock",
|
||||
".phpcs.xml.dist",
|
||||
".circleci",
|
||||
"vendor",
|
||||
"addons",
|
||||
".idea",
|
||||
"node_modules",
|
||||
".gitignore",
|
||||
".phpcs.xml.dist",
|
||||
".phpunit.result.cache",
|
||||
"build.sh",
|
||||
"package.json",
|
||||
"package-lock.json",
|
||||
"setuptest.sh"
|
||||
]
|
||||
},
|
||||
"extra": {
|
||||
"patches": {
|
||||
"jasny/sso": [
|
||||
"patches/jasny-sso-src-broker-cookies-php.patch"
|
||||
],
|
||||
"berlindb/core": [
|
||||
"patches/berlindb-core-src-database-query-php.patch",
|
||||
"patches/berlindb-core-src-database-column-php.patch"
|
||||
]
|
||||
}
|
||||
}
|
||||
],
|
||||
"license": [
|
||||
"GPL-3.0-or-later"
|
||||
],
|
||||
"type": "wordpress-plugin",
|
||||
"require": {
|
||||
"php": ">=7.4.30"
|
||||
},
|
||||
"require-dev": {
|
||||
"composer/installers": "1.9.0",
|
||||
"phpunit/phpunit": "^9.6.22",
|
||||
"yoast/phpunit-polyfills": "^1.1.3",
|
||||
"woocommerce/woocommerce-sniffs": "^1.0.0"
|
||||
},
|
||||
"config": {
|
||||
"allow-plugins": {
|
||||
"composer/installers": true,
|
||||
"dealerdirect/phpcodesniffer-composer-installer": true
|
||||
}
|
||||
},
|
||||
"archive": {
|
||||
"exclude": [
|
||||
"tests",
|
||||
"bin",
|
||||
"phpunit.xml.dist",
|
||||
"composer.lock",
|
||||
".phpcs.xml.dist",
|
||||
".circleci",
|
||||
"vendor",
|
||||
"addons",
|
||||
".idea",
|
||||
"node_modules",
|
||||
".gitignore",
|
||||
".phpcs.xml.dist",
|
||||
".phpunit.result.cache",
|
||||
"build.sh",
|
||||
"package.json",
|
||||
"package-lock.json",
|
||||
"setuptest.sh"
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
4130
composer.lock
generated
4130
composer.lock
generated
File diff suppressed because it is too large
Load Diff
75
dependencies/amphp/amp/lib/CallableMaker.php
vendored
75
dependencies/amphp/amp/lib/CallableMaker.php
vendored
@ -1,75 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace WP_Ultimo\Dependencies\Amp;
|
||||
|
||||
// @codeCoverageIgnoreStart
|
||||
if (\PHP_VERSION_ID < 70100) {
|
||||
/** @psalm-suppress DuplicateClass */
|
||||
trait CallableMaker
|
||||
{
|
||||
/** @var \ReflectionClass */
|
||||
private static $__reflectionClass;
|
||||
/** @var \ReflectionMethod[] */
|
||||
private static $__reflectionMethods = [];
|
||||
/**
|
||||
* Creates a callable from a protected or private instance method that may be invoked by callers requiring a
|
||||
* publicly invokable callback.
|
||||
*
|
||||
* @param string $method Instance method name.
|
||||
*
|
||||
* @return callable
|
||||
*
|
||||
* @psalm-suppress MixedInferredReturnType
|
||||
*/
|
||||
private function callableFromInstanceMethod(string $method) : callable
|
||||
{
|
||||
if (!isset(self::$__reflectionMethods[$method])) {
|
||||
if (self::$__reflectionClass === null) {
|
||||
self::$__reflectionClass = new \ReflectionClass(self::class);
|
||||
}
|
||||
self::$__reflectionMethods[$method] = self::$__reflectionClass->getMethod($method);
|
||||
}
|
||||
return self::$__reflectionMethods[$method]->getClosure($this);
|
||||
}
|
||||
/**
|
||||
* Creates a callable from a protected or private static method that may be invoked by methods requiring a
|
||||
* publicly invokable callback.
|
||||
*
|
||||
* @param string $method Static method name.
|
||||
*
|
||||
* @return callable
|
||||
*
|
||||
* @psalm-suppress MixedInferredReturnType
|
||||
*/
|
||||
private static function callableFromStaticMethod(string $method) : callable
|
||||
{
|
||||
if (!isset(self::$__reflectionMethods[$method])) {
|
||||
if (self::$__reflectionClass === null) {
|
||||
self::$__reflectionClass = new \ReflectionClass(self::class);
|
||||
}
|
||||
self::$__reflectionMethods[$method] = self::$__reflectionClass->getMethod($method);
|
||||
}
|
||||
return self::$__reflectionMethods[$method]->getClosure();
|
||||
}
|
||||
}
|
||||
} else {
|
||||
/** @psalm-suppress DuplicateClass */
|
||||
trait CallableMaker
|
||||
{
|
||||
/**
|
||||
* @deprecated Use \Closure::fromCallable() instead of this method in PHP 7.1.
|
||||
*/
|
||||
private function callableFromInstanceMethod(string $method) : callable
|
||||
{
|
||||
return \Closure::fromCallable([$this, $method]);
|
||||
}
|
||||
/**
|
||||
* @deprecated Use \Closure::fromCallable() instead of this method in PHP 7.1.
|
||||
*/
|
||||
private static function callableFromStaticMethod(string $method) : callable
|
||||
{
|
||||
return \Closure::fromCallable([self::class, $method]);
|
||||
}
|
||||
}
|
||||
}
|
||||
// @codeCoverageIgnoreEnd
|
46
dependencies/amphp/amp/lib/CancellationToken.php
vendored
46
dependencies/amphp/amp/lib/CancellationToken.php
vendored
@ -1,46 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace WP_Ultimo\Dependencies\Amp;
|
||||
|
||||
/**
|
||||
* Cancellation tokens are simple objects that allow registering handlers to subscribe to cancellation requests.
|
||||
*/
|
||||
interface CancellationToken
|
||||
{
|
||||
/**
|
||||
* Subscribes a new handler to be invoked on a cancellation request.
|
||||
*
|
||||
* This handler might be invoked immediately in case the token has already been cancelled. Returned generators will
|
||||
* automatically be run as coroutines. Any unhandled exceptions will be throw into the event loop.
|
||||
*
|
||||
* @param callable(CancelledException) $callback Callback to be invoked on a cancellation request. Will receive a
|
||||
* `CancelledException` as first argument that may be used to fail the operation's promise.
|
||||
*
|
||||
* @return string Identifier that can be used to cancel the subscription.
|
||||
*/
|
||||
public function subscribe(callable $callback) : string;
|
||||
/**
|
||||
* Unsubscribes a previously registered handler.
|
||||
*
|
||||
* The handler will no longer be called as long as this method isn't invoked from a subscribed callback.
|
||||
*
|
||||
* @param string $id
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function unsubscribe(string $id);
|
||||
/**
|
||||
* Returns whether cancellation has been requested yet.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isRequested() : bool;
|
||||
/**
|
||||
* Throws the `CancelledException` if cancellation has been requested, otherwise does nothing.
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @throws CancelledException
|
||||
*/
|
||||
public function throwIfRequested();
|
||||
}
|
@ -1,142 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace WP_Ultimo\Dependencies\Amp;
|
||||
|
||||
use WP_Ultimo\Dependencies\React\Promise\PromiseInterface as ReactPromise;
|
||||
use function WP_Ultimo\Dependencies\Amp\Promise\rethrow;
|
||||
/**
|
||||
* A cancellation token source provides a mechanism to cancel operations.
|
||||
*
|
||||
* Cancellation of operation works by creating a cancellation token source and passing the corresponding token when
|
||||
* starting the operation. To cancel the operation, invoke `CancellationTokenSource::cancel()`.
|
||||
*
|
||||
* Any operation can decide what to do on a cancellation request, it has "don't care" semantics. An operation SHOULD be
|
||||
* aborted, but MAY continue. Example: A DNS client might continue to receive and cache the response, as the query has
|
||||
* been sent anyway. An HTTP client would usually close a connection, but might not do so in case a response is close to
|
||||
* be fully received to reuse the connection.
|
||||
*
|
||||
* **Example**
|
||||
*
|
||||
* ```php
|
||||
* $tokenSource = new CancellationTokenSource;
|
||||
* $token = $tokenSource->getToken();
|
||||
*
|
||||
* $response = yield $httpClient->request("https://example.com/stream", $token);
|
||||
* $responseBody = $response->getBody();
|
||||
*
|
||||
* while (($chunk = yield $response->read()) !== null) {
|
||||
* // consume $chunk
|
||||
*
|
||||
* if ($noLongerInterested) {
|
||||
* $cancellationTokenSource->cancel();
|
||||
* break;
|
||||
* }
|
||||
* }
|
||||
* ```
|
||||
*
|
||||
* @see CancellationToken
|
||||
* @see CancelledException
|
||||
*/
|
||||
final class CancellationTokenSource
|
||||
{
|
||||
/** @var CancellationToken */
|
||||
private $token;
|
||||
/** @var callable|null */
|
||||
private $onCancel;
|
||||
public function __construct()
|
||||
{
|
||||
$onCancel = null;
|
||||
$this->token = new class($onCancel) implements CancellationToken
|
||||
{
|
||||
/** @var string */
|
||||
private $nextId = "a";
|
||||
/** @var callable[] */
|
||||
private $callbacks = [];
|
||||
/** @var \Throwable|null */
|
||||
private $exception;
|
||||
/**
|
||||
* @param mixed $onCancel
|
||||
* @param-out callable $onCancel
|
||||
*/
|
||||
public function __construct(&$onCancel)
|
||||
{
|
||||
/** @psalm-suppress MissingClosureReturnType We still support PHP 7.0 */
|
||||
$onCancel = function (\Throwable $exception) {
|
||||
$this->exception = $exception;
|
||||
$callbacks = $this->callbacks;
|
||||
$this->callbacks = [];
|
||||
foreach ($callbacks as $callback) {
|
||||
$this->invokeCallback($callback);
|
||||
}
|
||||
};
|
||||
}
|
||||
/**
|
||||
* @param callable $callback
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function invokeCallback(callable $callback)
|
||||
{
|
||||
// No type declaration to prevent exception outside the try!
|
||||
try {
|
||||
/** @var mixed $result */
|
||||
$result = $callback($this->exception);
|
||||
if ($result instanceof \Generator) {
|
||||
/** @psalm-var \Generator<mixed, Promise|ReactPromise|(Promise|ReactPromise)[], mixed, mixed> $result */
|
||||
$result = new Coroutine($result);
|
||||
}
|
||||
if ($result instanceof Promise || $result instanceof ReactPromise) {
|
||||
rethrow($result);
|
||||
}
|
||||
} catch (\Throwable $exception) {
|
||||
Loop::defer(static function () use($exception) {
|
||||
throw $exception;
|
||||
});
|
||||
}
|
||||
}
|
||||
public function subscribe(callable $callback) : string
|
||||
{
|
||||
$id = $this->nextId++;
|
||||
if ($this->exception) {
|
||||
$this->invokeCallback($callback);
|
||||
} else {
|
||||
$this->callbacks[$id] = $callback;
|
||||
}
|
||||
return $id;
|
||||
}
|
||||
public function unsubscribe(string $id)
|
||||
{
|
||||
unset($this->callbacks[$id]);
|
||||
}
|
||||
public function isRequested() : bool
|
||||
{
|
||||
return isset($this->exception);
|
||||
}
|
||||
public function throwIfRequested()
|
||||
{
|
||||
if (isset($this->exception)) {
|
||||
throw $this->exception;
|
||||
}
|
||||
}
|
||||
};
|
||||
$this->onCancel = $onCancel;
|
||||
}
|
||||
public function getToken() : CancellationToken
|
||||
{
|
||||
return $this->token;
|
||||
}
|
||||
/**
|
||||
* @param \Throwable|null $previous Exception to be used as the previous exception to CancelledException.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function cancel(\Throwable $previous = null)
|
||||
{
|
||||
if ($this->onCancel === null) {
|
||||
return;
|
||||
}
|
||||
$onCancel = $this->onCancel;
|
||||
$this->onCancel = null;
|
||||
$onCancel(new CancelledException($previous));
|
||||
}
|
||||
}
|
@ -1,17 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace WP_Ultimo\Dependencies\Amp;
|
||||
|
||||
/**
|
||||
* Will be thrown in case an operation is cancelled.
|
||||
*
|
||||
* @see CancellationToken
|
||||
* @see CancellationTokenSource
|
||||
*/
|
||||
class CancelledException extends \Exception
|
||||
{
|
||||
public function __construct(\Throwable $previous = null)
|
||||
{
|
||||
parent::__construct("The operation was cancelled", 0, $previous);
|
||||
}
|
||||
}
|
132
dependencies/amphp/amp/lib/Coroutine.php
vendored
132
dependencies/amphp/amp/lib/Coroutine.php
vendored
@ -1,132 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace WP_Ultimo\Dependencies\Amp;
|
||||
|
||||
use WP_Ultimo\Dependencies\React\Promise\PromiseInterface as ReactPromise;
|
||||
/**
|
||||
* Creates a promise from a generator function yielding promises.
|
||||
*
|
||||
* When a promise is yielded, execution of the generator is interrupted until the promise is resolved. A success
|
||||
* value is sent into the generator, while a failure reason is thrown into the generator. Using a coroutine,
|
||||
* asynchronous code can be written without callbacks and be structured like synchronous code.
|
||||
*
|
||||
* @template-covariant TReturn
|
||||
* @template-implements Promise<TReturn>
|
||||
*/
|
||||
final class Coroutine implements Promise
|
||||
{
|
||||
use Internal\Placeholder;
|
||||
/**
|
||||
* Attempts to transform the non-promise yielded from the generator into a promise, otherwise returns an instance
|
||||
* `Amp\Failure` failed with an instance of `Amp\InvalidYieldError`.
|
||||
*
|
||||
* @param mixed $yielded Non-promise yielded from generator.
|
||||
* @param \Generator $generator No type for performance, we already know the type.
|
||||
*
|
||||
* @return Promise
|
||||
*/
|
||||
private static function transform($yielded, $generator) : Promise
|
||||
{
|
||||
$exception = null;
|
||||
// initialize here, see https://github.com/vimeo/psalm/issues/2951
|
||||
try {
|
||||
if (\is_array($yielded)) {
|
||||
return Promise\all($yielded);
|
||||
}
|
||||
if ($yielded instanceof ReactPromise) {
|
||||
return Promise\adapt($yielded);
|
||||
}
|
||||
// No match, continue to returning Failure below.
|
||||
} catch (\Throwable $exception) {
|
||||
// Conversion to promise failed, fall-through to returning Failure below.
|
||||
}
|
||||
return new Failure(new InvalidYieldError($generator, \sprintf("Unexpected yield; Expected an instance of %s or %s or an array of such instances", Promise::class, ReactPromise::class), $exception));
|
||||
}
|
||||
/**
|
||||
* @param \Generator $generator
|
||||
* @psalm-param \Generator<mixed,Promise|ReactPromise|array<array-key,
|
||||
* Promise|ReactPromise>,mixed,Promise<TReturn>|ReactPromise|TReturn> $generator
|
||||
*/
|
||||
public function __construct(\Generator $generator)
|
||||
{
|
||||
try {
|
||||
$yielded = $generator->current();
|
||||
if (!$yielded instanceof Promise) {
|
||||
if (!$generator->valid()) {
|
||||
$this->resolve($generator->getReturn());
|
||||
return;
|
||||
}
|
||||
$yielded = self::transform($yielded, $generator);
|
||||
}
|
||||
} catch (\Throwable $exception) {
|
||||
$this->fail($exception);
|
||||
return;
|
||||
}
|
||||
/**
|
||||
* @param \Throwable|null $e Exception to be thrown into the generator.
|
||||
* @param mixed $v Value to be sent into the generator.
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @psalm-suppress MissingClosureParamType
|
||||
* @psalm-suppress MissingClosureReturnType
|
||||
*/
|
||||
$onResolve = function (\Throwable $e = null, $v) use($generator, &$onResolve) {
|
||||
/** @var bool $immediate Used to control iterative coroutine continuation. */
|
||||
static $immediate = \true;
|
||||
/** @var \Throwable|null $exception Promise failure reason when executing next coroutine step, null at all other times. */
|
||||
static $exception;
|
||||
/** @var mixed $value Promise success value when executing next coroutine step, null at all other times. */
|
||||
static $value;
|
||||
$exception = $e;
|
||||
/** @psalm-suppress MixedAssignment */
|
||||
$value = $v;
|
||||
if (!$immediate) {
|
||||
$immediate = \true;
|
||||
return;
|
||||
}
|
||||
try {
|
||||
try {
|
||||
do {
|
||||
if ($exception) {
|
||||
// Throw exception at current execution point.
|
||||
$yielded = $generator->throw($exception);
|
||||
} else {
|
||||
// Send the new value and execute to next yield statement.
|
||||
$yielded = $generator->send($value);
|
||||
}
|
||||
if (!$yielded instanceof Promise) {
|
||||
if (!$generator->valid()) {
|
||||
$this->resolve($generator->getReturn());
|
||||
$onResolve = null;
|
||||
return;
|
||||
}
|
||||
$yielded = self::transform($yielded, $generator);
|
||||
}
|
||||
$immediate = \false;
|
||||
$yielded->onResolve($onResolve);
|
||||
} while ($immediate);
|
||||
$immediate = \true;
|
||||
} catch (\Throwable $exception) {
|
||||
$this->fail($exception);
|
||||
$onResolve = null;
|
||||
} finally {
|
||||
$exception = null;
|
||||
$value = null;
|
||||
}
|
||||
} catch (\Throwable $e) {
|
||||
Loop::defer(static function () use($e) {
|
||||
throw $e;
|
||||
});
|
||||
}
|
||||
};
|
||||
try {
|
||||
$yielded->onResolve($onResolve);
|
||||
unset($generator, $yielded, $onResolve);
|
||||
} catch (\Throwable $e) {
|
||||
Loop::defer(static function () use($e) {
|
||||
throw $e;
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
70
dependencies/amphp/amp/lib/Deferred.php
vendored
70
dependencies/amphp/amp/lib/Deferred.php
vendored
@ -1,70 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace WP_Ultimo\Dependencies\Amp;
|
||||
|
||||
/**
|
||||
* Deferred is a container for a promise that is resolved using the resolve() and fail() methods of this object.
|
||||
* The contained promise may be accessed using the promise() method. This object should not be part of a public
|
||||
* API, but used internally to create and resolve a promise.
|
||||
*
|
||||
* @template TValue
|
||||
*/
|
||||
final class Deferred
|
||||
{
|
||||
/** @var Promise<TValue> Has public resolve and fail methods. */
|
||||
private $resolver;
|
||||
/** @var Promise<TValue> Hides placeholder methods */
|
||||
private $promise;
|
||||
public function __construct()
|
||||
{
|
||||
$this->resolver = new class implements Promise
|
||||
{
|
||||
use Internal\Placeholder {
|
||||
resolve as public;
|
||||
fail as public;
|
||||
isResolved as public;
|
||||
}
|
||||
};
|
||||
$this->promise = new Internal\PrivatePromise($this->resolver);
|
||||
}
|
||||
/**
|
||||
* @return Promise<TValue>
|
||||
*/
|
||||
public function promise() : Promise
|
||||
{
|
||||
return $this->promise;
|
||||
}
|
||||
/**
|
||||
* Fulfill the promise with the given value.
|
||||
*
|
||||
* @param mixed $value
|
||||
*
|
||||
* @psalm-param TValue|Promise<TValue> $value
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function resolve($value = null)
|
||||
{
|
||||
/** @psalm-suppress UndefinedInterfaceMethod */
|
||||
$this->resolver->resolve($value);
|
||||
}
|
||||
/**
|
||||
* Fails the promise the the given reason.
|
||||
*
|
||||
* @param \Throwable $reason
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function fail(\Throwable $reason)
|
||||
{
|
||||
/** @psalm-suppress UndefinedInterfaceMethod */
|
||||
$this->resolver->fail($reason);
|
||||
}
|
||||
/**
|
||||
* @return bool True if the promise has been resolved.
|
||||
*/
|
||||
public function isResolved() : bool
|
||||
{
|
||||
return $this->resolver->isResolved();
|
||||
}
|
||||
}
|
52
dependencies/amphp/amp/lib/Delayed.php
vendored
52
dependencies/amphp/amp/lib/Delayed.php
vendored
@ -1,52 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace WP_Ultimo\Dependencies\Amp;
|
||||
|
||||
/**
|
||||
* Creates a promise that resolves itself with a given value after a number of milliseconds.
|
||||
*
|
||||
* @template-covariant TReturn
|
||||
* @template-implements Promise<TReturn>
|
||||
*/
|
||||
final class Delayed implements Promise
|
||||
{
|
||||
use Internal\Placeholder;
|
||||
/** @var string|null Event loop watcher identifier. */
|
||||
private $watcher;
|
||||
/**
|
||||
* @param int $time Milliseconds before succeeding the promise.
|
||||
* @param TReturn $value Succeed the promise with this value.
|
||||
*/
|
||||
public function __construct(int $time, $value = null)
|
||||
{
|
||||
$this->watcher = Loop::delay($time, function () use($value) {
|
||||
$this->watcher = null;
|
||||
$this->resolve($value);
|
||||
});
|
||||
}
|
||||
/**
|
||||
* References the internal watcher in the event loop, keeping the loop running while this promise is pending.
|
||||
*
|
||||
* @return self
|
||||
*/
|
||||
public function reference() : self
|
||||
{
|
||||
if ($this->watcher !== null) {
|
||||
Loop::reference($this->watcher);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
/**
|
||||
* Unreferences the internal watcher in the event loop, allowing the loop to stop while this promise is pending if
|
||||
* no other events are pending in the loop.
|
||||
*
|
||||
* @return self
|
||||
*/
|
||||
public function unreference() : self
|
||||
{
|
||||
if ($this->watcher !== null) {
|
||||
Loop::unreference($this->watcher);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
}
|
78
dependencies/amphp/amp/lib/Emitter.php
vendored
78
dependencies/amphp/amp/lib/Emitter.php
vendored
@ -1,78 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace WP_Ultimo\Dependencies\Amp;
|
||||
|
||||
/**
|
||||
* Emitter is a container for an iterator that can emit values using the emit() method and completed using the
|
||||
* complete() and fail() methods of this object. The contained iterator may be accessed using the iterate()
|
||||
* method. This object should not be part of a public API, but used internally to create and emit values to an
|
||||
* iterator.
|
||||
*
|
||||
* @template TValue
|
||||
*/
|
||||
final class Emitter
|
||||
{
|
||||
/** @var Iterator<TValue> Has public emit, complete, and fail methods. */
|
||||
private $emitter;
|
||||
/** @var Iterator<TValue> Hides producer methods. */
|
||||
private $iterator;
|
||||
public function __construct()
|
||||
{
|
||||
$this->emitter = new class implements Iterator
|
||||
{
|
||||
use Internal\Producer {
|
||||
emit as public;
|
||||
complete as public;
|
||||
fail as public;
|
||||
}
|
||||
};
|
||||
$this->iterator = new Internal\PrivateIterator($this->emitter);
|
||||
}
|
||||
/**
|
||||
* @return Iterator
|
||||
* @psalm-return Iterator<TValue>
|
||||
*/
|
||||
public function iterate() : Iterator
|
||||
{
|
||||
return $this->iterator;
|
||||
}
|
||||
/**
|
||||
* Emits a value to the iterator.
|
||||
*
|
||||
* @param mixed $value
|
||||
*
|
||||
* @psalm-param TValue $value
|
||||
*
|
||||
* @return Promise
|
||||
* @psalm-return Promise<null>
|
||||
* @psalm-suppress MixedInferredReturnType
|
||||
* @psalm-suppress MixedReturnStatement
|
||||
*/
|
||||
public function emit($value) : Promise
|
||||
{
|
||||
/** @psalm-suppress UndefinedInterfaceMethod */
|
||||
return $this->emitter->emit($value);
|
||||
}
|
||||
/**
|
||||
* Completes the iterator.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function complete()
|
||||
{
|
||||
/** @psalm-suppress UndefinedInterfaceMethod */
|
||||
$this->emitter->complete();
|
||||
}
|
||||
/**
|
||||
* Fails the iterator with the given reason.
|
||||
*
|
||||
* @param \Throwable $reason
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function fail(\Throwable $reason)
|
||||
{
|
||||
/** @psalm-suppress UndefinedInterfaceMethod */
|
||||
$this->emitter->fail($reason);
|
||||
}
|
||||
}
|
46
dependencies/amphp/amp/lib/Failure.php
vendored
46
dependencies/amphp/amp/lib/Failure.php
vendored
@ -1,46 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace WP_Ultimo\Dependencies\Amp;
|
||||
|
||||
use WP_Ultimo\Dependencies\React\Promise\PromiseInterface as ReactPromise;
|
||||
/**
|
||||
* Creates a failed promise using the given exception.
|
||||
*
|
||||
* @template-covariant TValue
|
||||
* @template-implements Promise<TValue>
|
||||
*/
|
||||
final class Failure implements Promise
|
||||
{
|
||||
/** @var \Throwable $exception */
|
||||
private $exception;
|
||||
/**
|
||||
* @param \Throwable $exception Rejection reason.
|
||||
*/
|
||||
public function __construct(\Throwable $exception)
|
||||
{
|
||||
$this->exception = $exception;
|
||||
}
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function onResolve(callable $onResolved)
|
||||
{
|
||||
try {
|
||||
/** @var mixed $result */
|
||||
$result = $onResolved($this->exception, null);
|
||||
if ($result === null) {
|
||||
return;
|
||||
}
|
||||
if ($result instanceof \Generator) {
|
||||
$result = new Coroutine($result);
|
||||
}
|
||||
if ($result instanceof Promise || $result instanceof ReactPromise) {
|
||||
Promise\rethrow($result);
|
||||
}
|
||||
} catch (\Throwable $exception) {
|
||||
Loop::defer(static function () use($exception) {
|
||||
throw $exception;
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
159
dependencies/amphp/amp/lib/Internal/Placeholder.php
vendored
159
dependencies/amphp/amp/lib/Internal/Placeholder.php
vendored
@ -1,159 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace WP_Ultimo\Dependencies\Amp\Internal;
|
||||
|
||||
use WP_Ultimo\Dependencies\Amp\Coroutine;
|
||||
use WP_Ultimo\Dependencies\Amp\Failure;
|
||||
use WP_Ultimo\Dependencies\Amp\Loop;
|
||||
use WP_Ultimo\Dependencies\Amp\Promise;
|
||||
use WP_Ultimo\Dependencies\React\Promise\PromiseInterface as ReactPromise;
|
||||
/**
|
||||
* Trait used by Promise implementations. Do not use this trait in your code, instead compose your class from one of
|
||||
* the available classes implementing \Amp\Promise.
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
trait Placeholder
|
||||
{
|
||||
/** @var bool */
|
||||
private $resolved = \false;
|
||||
/** @var mixed */
|
||||
private $result;
|
||||
/** @var ResolutionQueue|null|callable(\Throwable|null, mixed): (Promise|\React\Promise\PromiseInterface|\Generator<mixed,
|
||||
* Promise|\React\Promise\PromiseInterface|array<array-key, Promise|\React\Promise\PromiseInterface>, mixed,
|
||||
* mixed>|null)|callable(\Throwable|null, mixed): void */
|
||||
private $onResolved;
|
||||
/** @var null|array */
|
||||
private $resolutionTrace;
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function onResolve(callable $onResolved)
|
||||
{
|
||||
if ($this->resolved) {
|
||||
if ($this->result instanceof Promise) {
|
||||
$this->result->onResolve($onResolved);
|
||||
return;
|
||||
}
|
||||
try {
|
||||
/** @var mixed $result */
|
||||
$result = $onResolved(null, $this->result);
|
||||
if ($result === null) {
|
||||
return;
|
||||
}
|
||||
if ($result instanceof \Generator) {
|
||||
$result = new Coroutine($result);
|
||||
}
|
||||
if ($result instanceof Promise || $result instanceof ReactPromise) {
|
||||
Promise\rethrow($result);
|
||||
}
|
||||
} catch (\Throwable $exception) {
|
||||
Loop::defer(static function () use($exception) {
|
||||
throw $exception;
|
||||
});
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (null === $this->onResolved) {
|
||||
$this->onResolved = $onResolved;
|
||||
return;
|
||||
}
|
||||
if (!$this->onResolved instanceof ResolutionQueue) {
|
||||
/** @psalm-suppress InternalClass */
|
||||
$this->onResolved = new ResolutionQueue($this->onResolved);
|
||||
}
|
||||
/** @psalm-suppress InternalMethod */
|
||||
$this->onResolved->push($onResolved);
|
||||
}
|
||||
public function __destruct()
|
||||
{
|
||||
try {
|
||||
$this->result = null;
|
||||
} catch (\Throwable $e) {
|
||||
Loop::defer(static function () use($e) {
|
||||
throw $e;
|
||||
});
|
||||
}
|
||||
}
|
||||
/**
|
||||
* @param mixed $value
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @throws \Error Thrown if the promise has already been resolved.
|
||||
*/
|
||||
private function resolve($value = null)
|
||||
{
|
||||
if ($this->resolved) {
|
||||
$message = "Promise has already been resolved";
|
||||
if (isset($this->resolutionTrace)) {
|
||||
$trace = formatStacktrace($this->resolutionTrace);
|
||||
$message .= ". Previous resolution trace:\n\n{$trace}\n\n";
|
||||
} else {
|
||||
// @codeCoverageIgnoreStart
|
||||
$message .= ", define environment variable AMP_DEBUG or const AMP_DEBUG = true and enable assertions " . "for a stacktrace of the previous resolution.";
|
||||
// @codeCoverageIgnoreEnd
|
||||
}
|
||||
throw new \Error($message);
|
||||
}
|
||||
\assert((function () {
|
||||
$env = \getenv("AMP_DEBUG") ?: "0";
|
||||
if ($env !== "0" && $env !== "false" || \defined("AMP_DEBUG") && \AMP_DEBUG) {
|
||||
$trace = \debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS);
|
||||
\array_shift($trace);
|
||||
// remove current closure
|
||||
$this->resolutionTrace = $trace;
|
||||
}
|
||||
return \true;
|
||||
})());
|
||||
if ($value instanceof ReactPromise) {
|
||||
$value = Promise\adapt($value);
|
||||
}
|
||||
$this->resolved = \true;
|
||||
$this->result = $value;
|
||||
if ($this->onResolved === null) {
|
||||
return;
|
||||
}
|
||||
$onResolved = $this->onResolved;
|
||||
$this->onResolved = null;
|
||||
if ($this->result instanceof Promise) {
|
||||
$this->result->onResolve($onResolved);
|
||||
return;
|
||||
}
|
||||
try {
|
||||
/** @var mixed $result */
|
||||
$result = $onResolved(null, $this->result);
|
||||
$onResolved = null;
|
||||
// allow garbage collection of $onResolved, to catch any exceptions from destructors
|
||||
if ($result === null) {
|
||||
return;
|
||||
}
|
||||
if ($result instanceof \Generator) {
|
||||
$result = new Coroutine($result);
|
||||
}
|
||||
if ($result instanceof Promise || $result instanceof ReactPromise) {
|
||||
Promise\rethrow($result);
|
||||
}
|
||||
} catch (\Throwable $exception) {
|
||||
Loop::defer(static function () use($exception) {
|
||||
throw $exception;
|
||||
});
|
||||
}
|
||||
}
|
||||
/**
|
||||
* @param \Throwable $reason Failure reason.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function fail(\Throwable $reason)
|
||||
{
|
||||
$this->resolve(new Failure($reason));
|
||||
}
|
||||
/**
|
||||
* @return bool True if the placeholder has been resolved.
|
||||
*/
|
||||
private function isResolved() : bool
|
||||
{
|
||||
return $this->resolved;
|
||||
}
|
||||
}
|
@ -1,41 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace WP_Ultimo\Dependencies\Amp\Internal;
|
||||
|
||||
use WP_Ultimo\Dependencies\Amp\Iterator;
|
||||
use WP_Ultimo\Dependencies\Amp\Promise;
|
||||
/**
|
||||
* Wraps an Iterator instance that has public methods to emit, complete, and fail into an object that only allows
|
||||
* access to the public API methods.
|
||||
*
|
||||
* @template-covariant TValue
|
||||
* @template-implements Iterator<TValue>
|
||||
*/
|
||||
final class PrivateIterator implements Iterator
|
||||
{
|
||||
/** @var Iterator<TValue> */
|
||||
private $iterator;
|
||||
/**
|
||||
* @param Iterator $iterator
|
||||
*
|
||||
* @psalm-param Iterator<TValue> $iterator
|
||||
*/
|
||||
public function __construct(Iterator $iterator)
|
||||
{
|
||||
$this->iterator = $iterator;
|
||||
}
|
||||
/**
|
||||
* @return Promise<bool>
|
||||
*/
|
||||
public function advance() : Promise
|
||||
{
|
||||
return $this->iterator->advance();
|
||||
}
|
||||
/**
|
||||
* @psalm-return TValue
|
||||
*/
|
||||
public function getCurrent()
|
||||
{
|
||||
return $this->iterator->getCurrent();
|
||||
}
|
||||
}
|
@ -1,22 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace WP_Ultimo\Dependencies\Amp\Internal;
|
||||
|
||||
use WP_Ultimo\Dependencies\Amp\Promise;
|
||||
/**
|
||||
* Wraps a Promise instance that has public methods to resolve and fail the promise into an object that only allows
|
||||
* access to the public API methods.
|
||||
*/
|
||||
final class PrivatePromise implements Promise
|
||||
{
|
||||
/** @var Promise */
|
||||
private $promise;
|
||||
public function __construct(Promise $promise)
|
||||
{
|
||||
$this->promise = $promise;
|
||||
}
|
||||
public function onResolve(callable $onResolved)
|
||||
{
|
||||
$this->promise->onResolve($onResolved);
|
||||
}
|
||||
}
|
173
dependencies/amphp/amp/lib/Internal/Producer.php
vendored
173
dependencies/amphp/amp/lib/Internal/Producer.php
vendored
@ -1,173 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace WP_Ultimo\Dependencies\Amp\Internal;
|
||||
|
||||
use WP_Ultimo\Dependencies\Amp\Deferred;
|
||||
use WP_Ultimo\Dependencies\Amp\Failure;
|
||||
use WP_Ultimo\Dependencies\Amp\Promise;
|
||||
use WP_Ultimo\Dependencies\Amp\Success;
|
||||
use WP_Ultimo\Dependencies\React\Promise\PromiseInterface as ReactPromise;
|
||||
/**
|
||||
* Trait used by Iterator implementations. Do not use this trait in your code, instead compose your class from one of
|
||||
* the available classes implementing \Amp\Iterator.
|
||||
* Note that it is the responsibility of the user of this trait to ensure that listeners have a chance to listen first
|
||||
* before emitting values.
|
||||
*
|
||||
* @internal
|
||||
* @template-covariant TValue
|
||||
*/
|
||||
trait Producer
|
||||
{
|
||||
/** @var Promise|null */
|
||||
private $complete;
|
||||
/** @var mixed[] */
|
||||
private $values = [];
|
||||
/** @var Deferred[] */
|
||||
private $backPressure = [];
|
||||
/** @var int */
|
||||
private $consumePosition = -1;
|
||||
/** @var int */
|
||||
private $emitPosition = -1;
|
||||
/** @var Deferred|null */
|
||||
private $waiting;
|
||||
/** @var null|array */
|
||||
private $resolutionTrace;
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @return Promise<bool>
|
||||
*/
|
||||
public function advance() : Promise
|
||||
{
|
||||
if ($this->waiting !== null) {
|
||||
throw new \Error("The prior promise returned must resolve before invoking this method again");
|
||||
}
|
||||
unset($this->values[$this->consumePosition]);
|
||||
$position = ++$this->consumePosition;
|
||||
if (\array_key_exists($position, $this->values)) {
|
||||
\assert(isset($this->backPressure[$position]));
|
||||
$deferred = $this->backPressure[$position];
|
||||
unset($this->backPressure[$position]);
|
||||
$deferred->resolve();
|
||||
return new Success(\true);
|
||||
}
|
||||
if ($this->complete) {
|
||||
return $this->complete;
|
||||
}
|
||||
$this->waiting = new Deferred();
|
||||
return $this->waiting->promise();
|
||||
}
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @return TValue
|
||||
*/
|
||||
public function getCurrent()
|
||||
{
|
||||
if (empty($this->values) && $this->complete) {
|
||||
throw new \Error("The iterator has completed");
|
||||
}
|
||||
if (!\array_key_exists($this->consumePosition, $this->values)) {
|
||||
throw new \Error("Promise returned from advance() must resolve before calling this method");
|
||||
}
|
||||
return $this->values[$this->consumePosition];
|
||||
}
|
||||
/**
|
||||
* Emits a value from the iterator. The returned promise is resolved once the emitted value has been consumed.
|
||||
*
|
||||
* @param mixed $value
|
||||
*
|
||||
* @return Promise
|
||||
* @psalm-return Promise<null>
|
||||
*
|
||||
* @throws \Error If the iterator has completed.
|
||||
*/
|
||||
private function emit($value) : Promise
|
||||
{
|
||||
if ($this->complete) {
|
||||
throw new \Error("Iterators cannot emit values after calling complete");
|
||||
}
|
||||
if ($value instanceof ReactPromise) {
|
||||
$value = Promise\adapt($value);
|
||||
}
|
||||
if ($value instanceof Promise) {
|
||||
$deferred = new Deferred();
|
||||
$value->onResolve(function ($e, $v) use($deferred) {
|
||||
if ($this->complete) {
|
||||
$deferred->fail(new \Error("The iterator was completed before the promise result could be emitted"));
|
||||
return;
|
||||
}
|
||||
if ($e) {
|
||||
$this->fail($e);
|
||||
$deferred->fail($e);
|
||||
return;
|
||||
}
|
||||
$deferred->resolve($this->emit($v));
|
||||
});
|
||||
return $deferred->promise();
|
||||
}
|
||||
$position = ++$this->emitPosition;
|
||||
$this->values[$position] = $value;
|
||||
if ($this->waiting !== null) {
|
||||
$waiting = $this->waiting;
|
||||
$this->waiting = null;
|
||||
$waiting->resolve(\true);
|
||||
return new Success();
|
||||
// Consumer was already waiting for a new value, so back-pressure is unnecessary.
|
||||
}
|
||||
$this->backPressure[$position] = $pressure = new Deferred();
|
||||
return $pressure->promise();
|
||||
}
|
||||
/**
|
||||
* Completes the iterator.
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @throws \Error If the iterator has already been completed.
|
||||
*/
|
||||
private function complete()
|
||||
{
|
||||
if ($this->complete) {
|
||||
$message = "Iterator has already been completed";
|
||||
if (isset($this->resolutionTrace)) {
|
||||
$trace = formatStacktrace($this->resolutionTrace);
|
||||
$message .= ". Previous completion trace:\n\n{$trace}\n\n";
|
||||
} else {
|
||||
// @codeCoverageIgnoreStart
|
||||
$message .= ", define environment variable AMP_DEBUG or const AMP_DEBUG = true and enable assertions " . "for a stacktrace of the previous resolution.";
|
||||
// @codeCoverageIgnoreEnd
|
||||
}
|
||||
throw new \Error($message);
|
||||
}
|
||||
\assert((function () {
|
||||
$env = \getenv("AMP_DEBUG") ?: "0";
|
||||
if ($env !== "0" && $env !== "false" || \defined("AMP_DEBUG") && \AMP_DEBUG) {
|
||||
$trace = \debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS);
|
||||
\array_shift($trace);
|
||||
// remove current closure
|
||||
$this->resolutionTrace = $trace;
|
||||
}
|
||||
return \true;
|
||||
})());
|
||||
$this->complete = new Success(\false);
|
||||
if ($this->waiting !== null) {
|
||||
$waiting = $this->waiting;
|
||||
$this->waiting = null;
|
||||
$waiting->resolve($this->complete);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* @param \Throwable $exception
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function fail(\Throwable $exception)
|
||||
{
|
||||
$this->complete = new Failure($exception);
|
||||
if ($this->waiting !== null) {
|
||||
$waiting = $this->waiting;
|
||||
$this->waiting = null;
|
||||
$waiting->resolve($this->complete);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,82 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace WP_Ultimo\Dependencies\Amp\Internal;
|
||||
|
||||
use WP_Ultimo\Dependencies\Amp\Coroutine;
|
||||
use WP_Ultimo\Dependencies\Amp\Loop;
|
||||
use WP_Ultimo\Dependencies\Amp\Promise;
|
||||
use WP_Ultimo\Dependencies\React\Promise\PromiseInterface as ReactPromise;
|
||||
/**
|
||||
* Stores a set of functions to be invoked when a promise is resolved.
|
||||
*
|
||||
* @internal
|
||||
* @psalm-internal Amp\Internal
|
||||
*/
|
||||
class ResolutionQueue
|
||||
{
|
||||
/** @var array<array-key, callable(\Throwable|null, mixed): (Promise|\React\Promise\PromiseInterface|\Generator<mixed,
|
||||
* Promise|\React\Promise\PromiseInterface|array<array-key, Promise|\React\Promise\PromiseInterface>, mixed,
|
||||
* mixed>|null) | callable(\Throwable|null, mixed): void> */
|
||||
private $queue = [];
|
||||
/**
|
||||
* @param callable|null $callback Initial callback to add to queue.
|
||||
*
|
||||
* @psalm-param null|callable(\Throwable|null, mixed): (Promise|\React\Promise\PromiseInterface|\Generator<mixed,
|
||||
* Promise|\React\Promise\PromiseInterface|array<array-key, Promise|\React\Promise\PromiseInterface>, mixed,
|
||||
* mixed>|null) | callable(\Throwable|null, mixed): void $callback
|
||||
*/
|
||||
public function __construct(callable $callback = null)
|
||||
{
|
||||
if ($callback !== null) {
|
||||
$this->push($callback);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Unrolls instances of self to avoid blowing up the call stack on resolution.
|
||||
*
|
||||
* @param callable $callback
|
||||
*
|
||||
* @psalm-param callable(\Throwable|null, mixed): (Promise|\React\Promise\PromiseInterface|\Generator<mixed,
|
||||
* Promise|\React\Promise\PromiseInterface|array<array-key, Promise|\React\Promise\PromiseInterface>, mixed,
|
||||
* mixed>|null) | callable(\Throwable|null, mixed): void $callback
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function push(callable $callback)
|
||||
{
|
||||
if ($callback instanceof self) {
|
||||
$this->queue = \array_merge($this->queue, $callback->queue);
|
||||
return;
|
||||
}
|
||||
$this->queue[] = $callback;
|
||||
}
|
||||
/**
|
||||
* Calls each callback in the queue, passing the provided values to the function.
|
||||
*
|
||||
* @param \Throwable|null $exception
|
||||
* @param mixed $value
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __invoke($exception, $value)
|
||||
{
|
||||
foreach ($this->queue as $callback) {
|
||||
try {
|
||||
$result = $callback($exception, $value);
|
||||
if ($result === null) {
|
||||
continue;
|
||||
}
|
||||
if ($result instanceof \Generator) {
|
||||
$result = new Coroutine($result);
|
||||
}
|
||||
if ($result instanceof Promise || $result instanceof ReactPromise) {
|
||||
Promise\rethrow($result);
|
||||
}
|
||||
} catch (\Throwable $exception) {
|
||||
Loop::defer(static function () use($exception) {
|
||||
throw $exception;
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,94 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace WP_Ultimo\Dependencies\Amp\Internal;
|
||||
|
||||
/**
|
||||
* Formats a stacktrace obtained via `debug_backtrace()`.
|
||||
*
|
||||
* @param array<array{file?: string, line: int, type?: string, class: string, function: string}> $trace Output of
|
||||
* `debug_backtrace()`.
|
||||
*
|
||||
* @return string Formatted stacktrace.
|
||||
*
|
||||
* @codeCoverageIgnore
|
||||
* @internal
|
||||
*/
|
||||
function formatStacktrace(array $trace) : string
|
||||
{
|
||||
return \implode("\n", \array_map(static function ($e, $i) {
|
||||
$line = "#{$i} ";
|
||||
if (isset($e["file"])) {
|
||||
$line .= "{$e['file']}:{$e['line']} ";
|
||||
}
|
||||
if (isset($e["type"])) {
|
||||
$line .= $e["class"] . $e["type"];
|
||||
}
|
||||
return $line . $e["function"] . "()";
|
||||
}, $trace, \array_keys($trace)));
|
||||
}
|
||||
/**
|
||||
* Creates a `TypeError` with a standardized error message.
|
||||
*
|
||||
* @param string[] $expected Expected types.
|
||||
* @param mixed $given Given value.
|
||||
*
|
||||
* @return \TypeError
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
function createTypeError(array $expected, $given) : \TypeError
|
||||
{
|
||||
$givenType = \is_object($given) ? \sprintf("instance of %s", \get_class($given)) : \gettype($given);
|
||||
if (\count($expected) === 1) {
|
||||
$expectedType = "Expected the following type: " . \array_pop($expected);
|
||||
} else {
|
||||
$expectedType = "Expected one of the following types: " . \implode(", ", $expected);
|
||||
}
|
||||
return new \TypeError("{$expectedType}; {$givenType} given");
|
||||
}
|
||||
/**
|
||||
* Returns the current time relative to an arbitrary point in time.
|
||||
*
|
||||
* @return int Time in milliseconds.
|
||||
*/
|
||||
function getCurrentTime() : int
|
||||
{
|
||||
/** @var int|null $startTime */
|
||||
static $startTime;
|
||||
/** @var int|null $nextWarning */
|
||||
static $nextWarning;
|
||||
if (\PHP_INT_SIZE === 4) {
|
||||
// @codeCoverageIgnoreStart
|
||||
if ($startTime === null) {
|
||||
$startTime = \PHP_VERSION_ID >= 70300 ? \hrtime(\false)[0] : \time();
|
||||
$nextWarning = \PHP_INT_MAX - 86400 * 7;
|
||||
}
|
||||
if (\PHP_VERSION_ID >= 70300) {
|
||||
list($seconds, $nanoseconds) = \hrtime(\false);
|
||||
$seconds -= $startTime;
|
||||
if ($seconds >= $nextWarning) {
|
||||
$timeToOverflow = (\PHP_INT_MAX - $seconds * 1000) / 1000;
|
||||
\trigger_error("getCurrentTime() will overflow in {$timeToOverflow} seconds, please restart the process before that. " . "You're using a 32 bit version of PHP, so time will overflow about every 24 days. Regular restarts are required.", \E_USER_WARNING);
|
||||
/** @psalm-suppress PossiblyNullOperand */
|
||||
$nextWarning += 600;
|
||||
// every 10 minutes
|
||||
}
|
||||
return (int) ($seconds * 1000 + $nanoseconds / 1000000);
|
||||
}
|
||||
$seconds = \microtime(\true) - $startTime;
|
||||
if ($seconds >= $nextWarning) {
|
||||
$timeToOverflow = (\PHP_INT_MAX - $seconds * 1000) / 1000;
|
||||
\trigger_error("getCurrentTime() will overflow in {$timeToOverflow} seconds, please restart the process before that. " . "You're using a 32 bit version of PHP, so time will overflow about every 24 days. Regular restarts are required.", \E_USER_WARNING);
|
||||
/** @psalm-suppress PossiblyNullOperand */
|
||||
$nextWarning += 600;
|
||||
// every 10 minutes
|
||||
}
|
||||
return (int) ($seconds * 1000);
|
||||
// @codeCoverageIgnoreEnd
|
||||
}
|
||||
if (\PHP_VERSION_ID >= 70300) {
|
||||
list($seconds, $nanoseconds) = \hrtime(\false);
|
||||
return (int) ($seconds * 1000 + $nanoseconds / 1000000);
|
||||
}
|
||||
return (int) (\microtime(\true) * 1000);
|
||||
}
|
27
dependencies/amphp/amp/lib/InvalidYieldError.php
vendored
27
dependencies/amphp/amp/lib/InvalidYieldError.php
vendored
@ -1,27 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace WP_Ultimo\Dependencies\Amp;
|
||||
|
||||
class InvalidYieldError extends \Error
|
||||
{
|
||||
/**
|
||||
* @param \Generator $generator
|
||||
* @param string $prefix
|
||||
* @param \Throwable|null $previous
|
||||
*/
|
||||
public function __construct(\Generator $generator, string $prefix, \Throwable $previous = null)
|
||||
{
|
||||
$yielded = $generator->current();
|
||||
$prefix .= \sprintf("; %s yielded at key %s", \is_object($yielded) ? \get_class($yielded) : \gettype($yielded), \var_export($generator->key(), \true));
|
||||
if (!$generator->valid()) {
|
||||
parent::__construct($prefix, 0, $previous);
|
||||
return;
|
||||
}
|
||||
$reflGen = new \ReflectionGenerator($generator);
|
||||
$exeGen = $reflGen->getExecutingGenerator();
|
||||
if ($isSubgenerator = $exeGen !== $generator) {
|
||||
$reflGen = new \ReflectionGenerator($exeGen);
|
||||
}
|
||||
parent::__construct(\sprintf("%s on line %s in %s", $prefix, $reflGen->getExecutingLine(), $reflGen->getExecutingFile()), 0, $previous);
|
||||
}
|
||||
}
|
33
dependencies/amphp/amp/lib/Iterator.php
vendored
33
dependencies/amphp/amp/lib/Iterator.php
vendored
@ -1,33 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace WP_Ultimo\Dependencies\Amp;
|
||||
|
||||
/**
|
||||
* Defines an asynchronous iterator over a set of values that is designed to be used within a coroutine.
|
||||
*
|
||||
* @template-covariant TValue
|
||||
*/
|
||||
interface Iterator
|
||||
{
|
||||
/**
|
||||
* Succeeds with true if an emitted value is available by calling getCurrent() or false if the iterator has
|
||||
* resolved. If the iterator fails, the returned promise will fail with the same exception.
|
||||
*
|
||||
* @return Promise
|
||||
* @psalm-return Promise<bool>
|
||||
*
|
||||
* @throws \Error If the prior promise returned from this method has not resolved.
|
||||
* @throws \Throwable The exception used to fail the iterator.
|
||||
*/
|
||||
public function advance() : Promise;
|
||||
/**
|
||||
* Gets the last emitted value or throws an exception if the iterator has completed.
|
||||
*
|
||||
* @return mixed Value emitted from the iterator.
|
||||
* @psalm-return TValue
|
||||
*
|
||||
* @throws \Error If the iterator has resolved or advance() was not called before calling this method.
|
||||
* @throws \Throwable The exception used to fail the iterator.
|
||||
*/
|
||||
public function getCurrent();
|
||||
}
|
38
dependencies/amphp/amp/lib/LazyPromise.php
vendored
38
dependencies/amphp/amp/lib/LazyPromise.php
vendored
@ -1,38 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace WP_Ultimo\Dependencies\Amp;
|
||||
|
||||
/**
|
||||
* Creates a promise that calls $promisor only when the result of the promise is requested (i.e. onResolve() is called
|
||||
* on the promise). $promisor can return a promise or any value. If $promisor throws an exception, the promise fails
|
||||
* with that exception. If $promisor returns a Generator, it will be run as a coroutine.
|
||||
*/
|
||||
final class LazyPromise implements Promise
|
||||
{
|
||||
/** @var callable|null */
|
||||
private $promisor;
|
||||
/** @var Promise|null */
|
||||
private $promise;
|
||||
/**
|
||||
* @param callable $promisor Function which starts an async operation, returning a Promise (or any value).
|
||||
* Generators will be run as a coroutine.
|
||||
*/
|
||||
public function __construct(callable $promisor)
|
||||
{
|
||||
$this->promisor = $promisor;
|
||||
}
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function onResolve(callable $onResolved)
|
||||
{
|
||||
if ($this->promise === null) {
|
||||
\assert($this->promisor !== null);
|
||||
$provider = $this->promisor;
|
||||
$this->promisor = null;
|
||||
$this->promise = call($provider);
|
||||
}
|
||||
\assert($this->promise !== null);
|
||||
$this->promise->onResolve($onResolved);
|
||||
}
|
||||
}
|
425
dependencies/amphp/amp/lib/Loop.php
vendored
425
dependencies/amphp/amp/lib/Loop.php
vendored
@ -1,425 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace WP_Ultimo\Dependencies\Amp;
|
||||
|
||||
use WP_Ultimo\Dependencies\Amp\Loop\Driver;
|
||||
use WP_Ultimo\Dependencies\Amp\Loop\DriverFactory;
|
||||
use WP_Ultimo\Dependencies\Amp\Loop\InvalidWatcherError;
|
||||
use WP_Ultimo\Dependencies\Amp\Loop\UnsupportedFeatureException;
|
||||
use WP_Ultimo\Dependencies\Amp\Loop\Watcher;
|
||||
/**
|
||||
* Accessor to allow global access to the event loop.
|
||||
*
|
||||
* @see \Amp\Loop\Driver
|
||||
*/
|
||||
final class Loop
|
||||
{
|
||||
/**
|
||||
* @var Driver
|
||||
*/
|
||||
private static $driver;
|
||||
/**
|
||||
* Disable construction as this is a static class.
|
||||
*/
|
||||
private function __construct()
|
||||
{
|
||||
// intentionally left blank
|
||||
}
|
||||
/**
|
||||
* Sets the driver to be used for `Loop::run()`.
|
||||
*
|
||||
* @param Driver $driver
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public static function set(Driver $driver)
|
||||
{
|
||||
try {
|
||||
self::$driver = new class extends Driver
|
||||
{
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
protected function activate(array $watchers)
|
||||
{
|
||||
throw new \Error("Can't activate watcher during garbage collection.");
|
||||
}
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
protected function dispatch(bool $blocking)
|
||||
{
|
||||
throw new \Error("Can't dispatch during garbage collection.");
|
||||
}
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
protected function deactivate(Watcher $watcher)
|
||||
{
|
||||
// do nothing
|
||||
}
|
||||
public function getHandle()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
};
|
||||
\gc_collect_cycles();
|
||||
} finally {
|
||||
self::$driver = $driver;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Run the event loop and optionally execute a callback within the scope of it.
|
||||
*
|
||||
* The loop MUST continue to run until it is either stopped explicitly, no referenced watchers exist anymore, or an
|
||||
* exception is thrown that cannot be handled. Exceptions that cannot be handled are exceptions thrown from an
|
||||
* error handler or exceptions that would be passed to an error handler but none exists to handle them.
|
||||
*
|
||||
* @param callable|null $callback The callback to execute.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public static function run(callable $callback = null)
|
||||
{
|
||||
if ($callback) {
|
||||
self::$driver->defer($callback);
|
||||
}
|
||||
self::$driver->run();
|
||||
}
|
||||
/**
|
||||
* Stop the event loop.
|
||||
*
|
||||
* When an event loop is stopped, it continues with its current tick and exits the loop afterwards. Multiple calls
|
||||
* to stop MUST be ignored and MUST NOT raise an exception.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public static function stop()
|
||||
{
|
||||
self::$driver->stop();
|
||||
}
|
||||
/**
|
||||
* Defer the execution of a callback.
|
||||
*
|
||||
* The deferred callable MUST be executed before any other type of watcher in a tick. Order of enabling MUST be
|
||||
* preserved when executing the callbacks.
|
||||
*
|
||||
* The created watcher MUST immediately be marked as enabled, but only be activated (i.e. callback can be called)
|
||||
* right before the next tick. Callbacks of watchers MUST NOT be called in the tick they were enabled.
|
||||
*
|
||||
* @param callable(string $watcherId, mixed $data) $callback The callback to defer. The `$watcherId` will be
|
||||
* invalidated before the callback call.
|
||||
* @param mixed $data Arbitrary data given to the callback function as the `$data` parameter.
|
||||
*
|
||||
* @return string An unique identifier that can be used to cancel, enable or disable the watcher.
|
||||
*/
|
||||
public static function defer(callable $callback, $data = null) : string
|
||||
{
|
||||
return self::$driver->defer($callback, $data);
|
||||
}
|
||||
/**
|
||||
* Delay the execution of a callback.
|
||||
*
|
||||
* The delay is a minimum and approximate, accuracy is not guaranteed. Order of calls MUST be determined by which
|
||||
* timers expire first, but timers with the same expiration time MAY be executed in any order.
|
||||
*
|
||||
* The created watcher MUST immediately be marked as enabled, but only be activated (i.e. callback can be called)
|
||||
* right before the next tick. Callbacks of watchers MUST NOT be called in the tick they were enabled.
|
||||
*
|
||||
* @param int $delay The amount of time, in milliseconds, to delay the execution for.
|
||||
* @param callable(string $watcherId, mixed $data) $callback The callback to delay. The `$watcherId` will be
|
||||
* invalidated before the callback call.
|
||||
* @param mixed $data Arbitrary data given to the callback function as the `$data` parameter.
|
||||
*
|
||||
* @return string An unique identifier that can be used to cancel, enable or disable the watcher.
|
||||
*/
|
||||
public static function delay(int $delay, callable $callback, $data = null) : string
|
||||
{
|
||||
return self::$driver->delay($delay, $callback, $data);
|
||||
}
|
||||
/**
|
||||
* Repeatedly execute a callback.
|
||||
*
|
||||
* The interval between executions is a minimum and approximate, accuracy is not guaranteed. Order of calls MUST be
|
||||
* determined by which timers expire first, but timers with the same expiration time MAY be executed in any order.
|
||||
* The first execution is scheduled after the first interval period.
|
||||
*
|
||||
* The created watcher MUST immediately be marked as enabled, but only be activated (i.e. callback can be called)
|
||||
* right before the next tick. Callbacks of watchers MUST NOT be called in the tick they were enabled.
|
||||
*
|
||||
* @param int $interval The time interval, in milliseconds, to wait between executions.
|
||||
* @param callable(string $watcherId, mixed $data) $callback The callback to repeat.
|
||||
* @param mixed $data Arbitrary data given to the callback function as the `$data` parameter.
|
||||
*
|
||||
* @return string An unique identifier that can be used to cancel, enable or disable the watcher.
|
||||
*/
|
||||
public static function repeat(int $interval, callable $callback, $data = null) : string
|
||||
{
|
||||
return self::$driver->repeat($interval, $callback, $data);
|
||||
}
|
||||
/**
|
||||
* Execute a callback when a stream resource becomes readable or is closed for reading.
|
||||
*
|
||||
* Warning: Closing resources locally, e.g. with `fclose`, might not invoke the callback. Be sure to `cancel` the
|
||||
* watcher when closing the resource locally. Drivers MAY choose to notify the user if there are watchers on invalid
|
||||
* resources, but are not required to, due to the high performance impact. Watchers on closed resources are
|
||||
* therefore undefined behavior.
|
||||
*
|
||||
* Multiple watchers on the same stream MAY be executed in any order.
|
||||
*
|
||||
* The created watcher MUST immediately be marked as enabled, but only be activated (i.e. callback can be called)
|
||||
* right before the next tick. Callbacks of watchers MUST NOT be called in the tick they were enabled.
|
||||
*
|
||||
* @param resource $stream The stream to monitor.
|
||||
* @param callable(string $watcherId, resource $stream, mixed $data) $callback The callback to execute.
|
||||
* @param mixed $data Arbitrary data given to the callback function as the `$data` parameter.
|
||||
*
|
||||
* @return string An unique identifier that can be used to cancel, enable or disable the watcher.
|
||||
*/
|
||||
public static function onReadable($stream, callable $callback, $data = null) : string
|
||||
{
|
||||
return self::$driver->onReadable($stream, $callback, $data);
|
||||
}
|
||||
/**
|
||||
* Execute a callback when a stream resource becomes writable or is closed for writing.
|
||||
*
|
||||
* Warning: Closing resources locally, e.g. with `fclose`, might not invoke the callback. Be sure to `cancel` the
|
||||
* watcher when closing the resource locally. Drivers MAY choose to notify the user if there are watchers on invalid
|
||||
* resources, but are not required to, due to the high performance impact. Watchers on closed resources are
|
||||
* therefore undefined behavior.
|
||||
*
|
||||
* Multiple watchers on the same stream MAY be executed in any order.
|
||||
*
|
||||
* The created watcher MUST immediately be marked as enabled, but only be activated (i.e. callback can be called)
|
||||
* right before the next tick. Callbacks of watchers MUST NOT be called in the tick they were enabled.
|
||||
*
|
||||
* @param resource $stream The stream to monitor.
|
||||
* @param callable(string $watcherId, resource $stream, mixed $data) $callback The callback to execute.
|
||||
* @param mixed $data Arbitrary data given to the callback function as the `$data` parameter.
|
||||
*
|
||||
* @return string An unique identifier that can be used to cancel, enable or disable the watcher.
|
||||
*/
|
||||
public static function onWritable($stream, callable $callback, $data = null) : string
|
||||
{
|
||||
return self::$driver->onWritable($stream, $callback, $data);
|
||||
}
|
||||
/**
|
||||
* Execute a callback when a signal is received.
|
||||
*
|
||||
* Warning: Installing the same signal on different instances of this interface is deemed undefined behavior.
|
||||
* Implementations MAY try to detect this, if possible, but are not required to. This is due to technical
|
||||
* limitations of the signals being registered globally per process.
|
||||
*
|
||||
* Multiple watchers on the same signal MAY be executed in any order.
|
||||
*
|
||||
* The created watcher MUST immediately be marked as enabled, but only be activated (i.e. callback can be called)
|
||||
* right before the next tick. Callbacks of watchers MUST NOT be called in the tick they were enabled.
|
||||
*
|
||||
* @param int $signo The signal number to monitor.
|
||||
* @param callable(string $watcherId, int $signo, mixed $data) $callback The callback to execute.
|
||||
* @param mixed $data Arbitrary data given to the callback function as the $data parameter.
|
||||
*
|
||||
* @return string An unique identifier that can be used to cancel, enable or disable the watcher.
|
||||
*
|
||||
* @throws UnsupportedFeatureException If signal handling is not supported.
|
||||
*/
|
||||
public static function onSignal(int $signo, callable $callback, $data = null) : string
|
||||
{
|
||||
return self::$driver->onSignal($signo, $callback, $data);
|
||||
}
|
||||
/**
|
||||
* Enable a watcher to be active starting in the next tick.
|
||||
*
|
||||
* Watchers MUST immediately be marked as enabled, but only be activated (i.e. callbacks can be called) right before
|
||||
* the next tick. Callbacks of watchers MUST NOT be called in the tick they were enabled.
|
||||
*
|
||||
* @param string $watcherId The watcher identifier.
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @throws InvalidWatcherError If the watcher identifier is invalid.
|
||||
*/
|
||||
public static function enable(string $watcherId)
|
||||
{
|
||||
self::$driver->enable($watcherId);
|
||||
}
|
||||
/**
|
||||
* Disable a watcher immediately.
|
||||
*
|
||||
* A watcher MUST be disabled immediately, e.g. if a defer watcher disables a later defer watcher, the second defer
|
||||
* watcher isn't executed in this tick.
|
||||
*
|
||||
* Disabling a watcher MUST NOT invalidate the watcher. Calling this function MUST NOT fail, even if passed an
|
||||
* invalid watcher.
|
||||
*
|
||||
* @param string $watcherId The watcher identifier.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public static function disable(string $watcherId)
|
||||
{
|
||||
if (\PHP_VERSION_ID < 70200 && !isset(self::$driver)) {
|
||||
// Prior to PHP 7.2, self::$driver may be unset during destruct.
|
||||
// See https://github.com/amphp/amp/issues/212.
|
||||
return;
|
||||
}
|
||||
self::$driver->disable($watcherId);
|
||||
}
|
||||
/**
|
||||
* Cancel a watcher.
|
||||
*
|
||||
* This will detatch the event loop from all resources that are associated to the watcher. After this operation the
|
||||
* watcher is permanently invalid. Calling this function MUST NOT fail, even if passed an invalid watcher.
|
||||
*
|
||||
* @param string $watcherId The watcher identifier.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public static function cancel(string $watcherId)
|
||||
{
|
||||
if (\PHP_VERSION_ID < 70200 && !isset(self::$driver)) {
|
||||
// Prior to PHP 7.2, self::$driver may be unset during destruct.
|
||||
// See https://github.com/amphp/amp/issues/212.
|
||||
return;
|
||||
}
|
||||
self::$driver->cancel($watcherId);
|
||||
}
|
||||
/**
|
||||
* Reference a watcher.
|
||||
*
|
||||
* This will keep the event loop alive whilst the watcher is still being monitored. Watchers have this state by
|
||||
* default.
|
||||
*
|
||||
* @param string $watcherId The watcher identifier.
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @throws InvalidWatcherError If the watcher identifier is invalid.
|
||||
*/
|
||||
public static function reference(string $watcherId)
|
||||
{
|
||||
self::$driver->reference($watcherId);
|
||||
}
|
||||
/**
|
||||
* Unreference a watcher.
|
||||
*
|
||||
* The event loop should exit the run method when only unreferenced watchers are still being monitored. Watchers
|
||||
* are all referenced by default.
|
||||
*
|
||||
* @param string $watcherId The watcher identifier.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public static function unreference(string $watcherId)
|
||||
{
|
||||
if (\PHP_VERSION_ID < 70200 && !isset(self::$driver)) {
|
||||
// Prior to PHP 7.2, self::$driver may be unset during destruct.
|
||||
// See https://github.com/amphp/amp/issues/212.
|
||||
return;
|
||||
}
|
||||
self::$driver->unreference($watcherId);
|
||||
}
|
||||
/**
|
||||
* Returns the current loop time in millisecond increments. Note this value does not necessarily correlate to
|
||||
* wall-clock time, rather the value returned is meant to be used in relative comparisons to prior values returned
|
||||
* by this method (intervals, expiration calculations, etc.) and is only updated once per loop tick.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public static function now() : int
|
||||
{
|
||||
return self::$driver->now();
|
||||
}
|
||||
/**
|
||||
* Stores information in the loop bound registry.
|
||||
*
|
||||
* Stored information is package private. Packages MUST NOT retrieve the stored state of other packages. Packages
|
||||
* MUST use their namespace as prefix for keys. They may do so by using `SomeClass::class` as key.
|
||||
*
|
||||
* If packages want to expose loop bound state to consumers other than the package, they SHOULD provide a dedicated
|
||||
* interface for that purpose instead of sharing the storage key.
|
||||
*
|
||||
* @param string $key The namespaced storage key.
|
||||
* @param mixed $value The value to be stored.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public static function setState(string $key, $value)
|
||||
{
|
||||
self::$driver->setState($key, $value);
|
||||
}
|
||||
/**
|
||||
* Gets information stored bound to the loop.
|
||||
*
|
||||
* Stored information is package private. Packages MUST NOT retrieve the stored state of other packages. Packages
|
||||
* MUST use their namespace as prefix for keys. They may do so by using `SomeClass::class` as key.
|
||||
*
|
||||
* If packages want to expose loop bound state to consumers other than the package, they SHOULD provide a dedicated
|
||||
* interface for that purpose instead of sharing the storage key.
|
||||
*
|
||||
* @param string $key The namespaced storage key.
|
||||
*
|
||||
* @return mixed The previously stored value or `null` if it doesn't exist.
|
||||
*/
|
||||
public static function getState(string $key)
|
||||
{
|
||||
return self::$driver->getState($key);
|
||||
}
|
||||
/**
|
||||
* Set a callback to be executed when an error occurs.
|
||||
*
|
||||
* The callback receives the error as the first and only parameter. The return value of the callback gets ignored.
|
||||
* If it can't handle the error, it MUST throw the error. Errors thrown by the callback or during its invocation
|
||||
* MUST be thrown into the `run` loop and stop the driver.
|
||||
*
|
||||
* Subsequent calls to this method will overwrite the previous handler.
|
||||
*
|
||||
* @param callable(\Throwable $error)|null $callback The callback to execute. `null` will clear the
|
||||
* current handler.
|
||||
*
|
||||
* @return callable(\Throwable $error)|null The previous handler, `null` if there was none.
|
||||
*/
|
||||
public static function setErrorHandler(callable $callback = null)
|
||||
{
|
||||
return self::$driver->setErrorHandler($callback);
|
||||
}
|
||||
/**
|
||||
* Retrieve an associative array of information about the event loop driver.
|
||||
*
|
||||
* The returned array MUST contain the following data describing the driver's currently registered watchers:
|
||||
*
|
||||
* [
|
||||
* "defer" => ["enabled" => int, "disabled" => int],
|
||||
* "delay" => ["enabled" => int, "disabled" => int],
|
||||
* "repeat" => ["enabled" => int, "disabled" => int],
|
||||
* "on_readable" => ["enabled" => int, "disabled" => int],
|
||||
* "on_writable" => ["enabled" => int, "disabled" => int],
|
||||
* "on_signal" => ["enabled" => int, "disabled" => int],
|
||||
* "enabled_watchers" => ["referenced" => int, "unreferenced" => int],
|
||||
* "running" => bool
|
||||
* ];
|
||||
*
|
||||
* Implementations MAY optionally add more information in the array but at minimum the above `key => value` format
|
||||
* MUST always be provided.
|
||||
*
|
||||
* @return array Statistics about the loop in the described format.
|
||||
*/
|
||||
public static function getInfo() : array
|
||||
{
|
||||
return self::$driver->getInfo();
|
||||
}
|
||||
/**
|
||||
* Retrieve the event loop driver that is in scope.
|
||||
*
|
||||
* @return Driver
|
||||
*/
|
||||
public static function get() : Driver
|
||||
{
|
||||
return self::$driver;
|
||||
}
|
||||
}
|
||||
// Default factory, don't move this to a file loaded by the composer "files" autoload mechanism, otherwise custom
|
||||
// implementations might have issues setting a default loop, because it's overridden by us then.
|
||||
// @codeCoverageIgnoreStart
|
||||
Loop::set((new DriverFactory())->create());
|
||||
// @codeCoverageIgnoreEnd
|
649
dependencies/amphp/amp/lib/Loop/Driver.php
vendored
649
dependencies/amphp/amp/lib/Loop/Driver.php
vendored
@ -1,649 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace WP_Ultimo\Dependencies\Amp\Loop;
|
||||
|
||||
use WP_Ultimo\Dependencies\Amp\Coroutine;
|
||||
use WP_Ultimo\Dependencies\Amp\Promise;
|
||||
use WP_Ultimo\Dependencies\React\Promise\PromiseInterface as ReactPromise;
|
||||
use function WP_Ultimo\Dependencies\Amp\Promise\rethrow;
|
||||
/**
|
||||
* Event loop driver which implements all basic operations to allow interoperability.
|
||||
*
|
||||
* Watchers (enabled or new watchers) MUST immediately be marked as enabled, but only be activated (i.e. callbacks can
|
||||
* be called) right before the next tick. Callbacks of watchers MUST NOT be called in the tick they were enabled.
|
||||
*
|
||||
* All registered callbacks MUST NOT be called from a file with strict types enabled (`declare(strict_types=1)`).
|
||||
*/
|
||||
abstract class Driver
|
||||
{
|
||||
// Don't use 1e3 / 1e6, they result in a float instead of int
|
||||
const MILLISEC_PER_SEC = 1000;
|
||||
const MICROSEC_PER_SEC = 1000000;
|
||||
/** @var string */
|
||||
private $nextId = "a";
|
||||
/** @var Watcher[] */
|
||||
private $watchers = [];
|
||||
/** @var Watcher[] */
|
||||
private $enableQueue = [];
|
||||
/** @var Watcher[] */
|
||||
private $deferQueue = [];
|
||||
/** @var Watcher[] */
|
||||
private $nextTickQueue = [];
|
||||
/** @var callable(\Throwable):void|null */
|
||||
private $errorHandler;
|
||||
/** @var bool */
|
||||
private $running = \false;
|
||||
/** @var array */
|
||||
private $registry = [];
|
||||
/**
|
||||
* Run the event loop.
|
||||
*
|
||||
* One iteration of the loop is called one "tick". A tick covers the following steps:
|
||||
*
|
||||
* 1. Activate watchers created / enabled in the last tick / before `run()`.
|
||||
* 2. Execute all enabled defer watchers.
|
||||
* 3. Execute all due timer, pending signal and actionable stream callbacks, each only once per tick.
|
||||
*
|
||||
* The loop MUST continue to run until it is either stopped explicitly, no referenced watchers exist anymore, or an
|
||||
* exception is thrown that cannot be handled. Exceptions that cannot be handled are exceptions thrown from an
|
||||
* error handler or exceptions that would be passed to an error handler but none exists to handle them.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function run()
|
||||
{
|
||||
$this->running = \true;
|
||||
try {
|
||||
while ($this->running) {
|
||||
if ($this->isEmpty()) {
|
||||
return;
|
||||
}
|
||||
$this->tick();
|
||||
}
|
||||
} finally {
|
||||
$this->stop();
|
||||
}
|
||||
}
|
||||
/**
|
||||
* @return bool True if no enabled and referenced watchers remain in the loop.
|
||||
*/
|
||||
private function isEmpty() : bool
|
||||
{
|
||||
foreach ($this->watchers as $watcher) {
|
||||
if ($watcher->enabled && $watcher->referenced) {
|
||||
return \false;
|
||||
}
|
||||
}
|
||||
return \true;
|
||||
}
|
||||
/**
|
||||
* Executes a single tick of the event loop.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function tick()
|
||||
{
|
||||
if (empty($this->deferQueue)) {
|
||||
$this->deferQueue = $this->nextTickQueue;
|
||||
} else {
|
||||
$this->deferQueue = \array_merge($this->deferQueue, $this->nextTickQueue);
|
||||
}
|
||||
$this->nextTickQueue = [];
|
||||
$this->activate($this->enableQueue);
|
||||
$this->enableQueue = [];
|
||||
foreach ($this->deferQueue as $watcher) {
|
||||
if (!isset($this->deferQueue[$watcher->id])) {
|
||||
continue;
|
||||
// Watcher disabled by another defer watcher.
|
||||
}
|
||||
unset($this->watchers[$watcher->id], $this->deferQueue[$watcher->id]);
|
||||
try {
|
||||
/** @var mixed $result */
|
||||
$result = ($watcher->callback)($watcher->id, $watcher->data);
|
||||
if ($result === null) {
|
||||
continue;
|
||||
}
|
||||
if ($result instanceof \Generator) {
|
||||
$result = new Coroutine($result);
|
||||
}
|
||||
if ($result instanceof Promise || $result instanceof ReactPromise) {
|
||||
rethrow($result);
|
||||
}
|
||||
} catch (\Throwable $exception) {
|
||||
$this->error($exception);
|
||||
}
|
||||
}
|
||||
/** @psalm-suppress RedundantCondition */
|
||||
$this->dispatch(empty($this->nextTickQueue) && empty($this->enableQueue) && $this->running && !$this->isEmpty());
|
||||
}
|
||||
/**
|
||||
* Activates (enables) all the given watchers.
|
||||
*
|
||||
* @param Watcher[] $watchers
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected abstract function activate(array $watchers);
|
||||
/**
|
||||
* Dispatches any pending read/write, timer, and signal events.
|
||||
*
|
||||
* @param bool $blocking
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected abstract function dispatch(bool $blocking);
|
||||
/**
|
||||
* Stop the event loop.
|
||||
*
|
||||
* When an event loop is stopped, it continues with its current tick and exits the loop afterwards. Multiple calls
|
||||
* to stop MUST be ignored and MUST NOT raise an exception.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function stop()
|
||||
{
|
||||
$this->running = \false;
|
||||
}
|
||||
/**
|
||||
* Defer the execution of a callback.
|
||||
*
|
||||
* The deferred callable MUST be executed before any other type of watcher in a tick. Order of enabling MUST be
|
||||
* preserved when executing the callbacks.
|
||||
*
|
||||
* The created watcher MUST immediately be marked as enabled, but only be activated (i.e. callback can be called)
|
||||
* right before the next tick. Callbacks of watchers MUST NOT be called in the tick they were enabled.
|
||||
*
|
||||
* @param callable (string $watcherId, mixed $data) $callback The callback to defer. The `$watcherId` will be
|
||||
* invalidated before the callback call.
|
||||
* @param mixed $data Arbitrary data given to the callback function as the `$data` parameter.
|
||||
*
|
||||
* @return string An unique identifier that can be used to cancel, enable or disable the watcher.
|
||||
*/
|
||||
public function defer(callable $callback, $data = null) : string
|
||||
{
|
||||
/** @psalm-var Watcher<null> $watcher */
|
||||
$watcher = new Watcher();
|
||||
$watcher->type = Watcher::DEFER;
|
||||
$watcher->id = $this->nextId++;
|
||||
$watcher->callback = $callback;
|
||||
$watcher->data = $data;
|
||||
$this->watchers[$watcher->id] = $watcher;
|
||||
$this->nextTickQueue[$watcher->id] = $watcher;
|
||||
return $watcher->id;
|
||||
}
|
||||
/**
|
||||
* Delay the execution of a callback.
|
||||
*
|
||||
* The delay is a minimum and approximate, accuracy is not guaranteed. Order of calls MUST be determined by which
|
||||
* timers expire first, but timers with the same expiration time MAY be executed in any order.
|
||||
*
|
||||
* The created watcher MUST immediately be marked as enabled, but only be activated (i.e. callback can be called)
|
||||
* right before the next tick. Callbacks of watchers MUST NOT be called in the tick they were enabled.
|
||||
*
|
||||
* @param int $delay The amount of time, in milliseconds, to delay the execution for.
|
||||
* @param callable (string $watcherId, mixed $data) $callback The callback to delay. The `$watcherId` will be
|
||||
* invalidated before the callback call.
|
||||
* @param mixed $data Arbitrary data given to the callback function as the `$data` parameter.
|
||||
*
|
||||
* @return string An unique identifier that can be used to cancel, enable or disable the watcher.
|
||||
*/
|
||||
public function delay(int $delay, callable $callback, $data = null) : string
|
||||
{
|
||||
if ($delay < 0) {
|
||||
throw new \Error("Delay must be greater than or equal to zero");
|
||||
}
|
||||
/** @psalm-var Watcher<int> $watcher */
|
||||
$watcher = new Watcher();
|
||||
$watcher->type = Watcher::DELAY;
|
||||
$watcher->id = $this->nextId++;
|
||||
$watcher->callback = $callback;
|
||||
$watcher->value = $delay;
|
||||
$watcher->expiration = $this->now() + $delay;
|
||||
$watcher->data = $data;
|
||||
$this->watchers[$watcher->id] = $watcher;
|
||||
$this->enableQueue[$watcher->id] = $watcher;
|
||||
return $watcher->id;
|
||||
}
|
||||
/**
|
||||
* Repeatedly execute a callback.
|
||||
*
|
||||
* The interval between executions is a minimum and approximate, accuracy is not guaranteed. Order of calls MUST be
|
||||
* determined by which timers expire first, but timers with the same expiration time MAY be executed in any order.
|
||||
* The first execution is scheduled after the first interval period.
|
||||
*
|
||||
* The created watcher MUST immediately be marked as enabled, but only be activated (i.e. callback can be called)
|
||||
* right before the next tick. Callbacks of watchers MUST NOT be called in the tick they were enabled.
|
||||
*
|
||||
* @param int $interval The time interval, in milliseconds, to wait between executions.
|
||||
* @param callable (string $watcherId, mixed $data) $callback The callback to repeat.
|
||||
* @param mixed $data Arbitrary data given to the callback function as the `$data` parameter.
|
||||
*
|
||||
* @return string An unique identifier that can be used to cancel, enable or disable the watcher.
|
||||
*/
|
||||
public function repeat(int $interval, callable $callback, $data = null) : string
|
||||
{
|
||||
if ($interval < 0) {
|
||||
throw new \Error("Interval must be greater than or equal to zero");
|
||||
}
|
||||
/** @psalm-var Watcher<int> $watcher */
|
||||
$watcher = new Watcher();
|
||||
$watcher->type = Watcher::REPEAT;
|
||||
$watcher->id = $this->nextId++;
|
||||
$watcher->callback = $callback;
|
||||
$watcher->value = $interval;
|
||||
$watcher->expiration = $this->now() + $interval;
|
||||
$watcher->data = $data;
|
||||
$this->watchers[$watcher->id] = $watcher;
|
||||
$this->enableQueue[$watcher->id] = $watcher;
|
||||
return $watcher->id;
|
||||
}
|
||||
/**
|
||||
* Execute a callback when a stream resource becomes readable or is closed for reading.
|
||||
*
|
||||
* Warning: Closing resources locally, e.g. with `fclose`, might not invoke the callback. Be sure to `cancel` the
|
||||
* watcher when closing the resource locally. Drivers MAY choose to notify the user if there are watchers on invalid
|
||||
* resources, but are not required to, due to the high performance impact. Watchers on closed resources are
|
||||
* therefore undefined behavior.
|
||||
*
|
||||
* Multiple watchers on the same stream MAY be executed in any order.
|
||||
*
|
||||
* The created watcher MUST immediately be marked as enabled, but only be activated (i.e. callback can be called)
|
||||
* right before the next tick. Callbacks of watchers MUST NOT be called in the tick they were enabled.
|
||||
*
|
||||
* @param resource $stream The stream to monitor.
|
||||
* @param callable (string $watcherId, resource $stream, mixed $data) $callback The callback to execute.
|
||||
* @param mixed $data Arbitrary data given to the callback function as the `$data` parameter.
|
||||
*
|
||||
* @return string An unique identifier that can be used to cancel, enable or disable the watcher.
|
||||
*/
|
||||
public function onReadable($stream, callable $callback, $data = null) : string
|
||||
{
|
||||
/** @psalm-var Watcher<resource> $watcher */
|
||||
$watcher = new Watcher();
|
||||
$watcher->type = Watcher::READABLE;
|
||||
$watcher->id = $this->nextId++;
|
||||
$watcher->callback = $callback;
|
||||
$watcher->value = $stream;
|
||||
$watcher->data = $data;
|
||||
$this->watchers[$watcher->id] = $watcher;
|
||||
$this->enableQueue[$watcher->id] = $watcher;
|
||||
return $watcher->id;
|
||||
}
|
||||
/**
|
||||
* Execute a callback when a stream resource becomes writable or is closed for writing.
|
||||
*
|
||||
* Warning: Closing resources locally, e.g. with `fclose`, might not invoke the callback. Be sure to `cancel` the
|
||||
* watcher when closing the resource locally. Drivers MAY choose to notify the user if there are watchers on invalid
|
||||
* resources, but are not required to, due to the high performance impact. Watchers on closed resources are
|
||||
* therefore undefined behavior.
|
||||
*
|
||||
* Multiple watchers on the same stream MAY be executed in any order.
|
||||
*
|
||||
* The created watcher MUST immediately be marked as enabled, but only be activated (i.e. callback can be called)
|
||||
* right before the next tick. Callbacks of watchers MUST NOT be called in the tick they were enabled.
|
||||
*
|
||||
* @param resource $stream The stream to monitor.
|
||||
* @param callable (string $watcherId, resource $stream, mixed $data) $callback The callback to execute.
|
||||
* @param mixed $data Arbitrary data given to the callback function as the `$data` parameter.
|
||||
*
|
||||
* @return string An unique identifier that can be used to cancel, enable or disable the watcher.
|
||||
*/
|
||||
public function onWritable($stream, callable $callback, $data = null) : string
|
||||
{
|
||||
/** @psalm-var Watcher<resource> $watcher */
|
||||
$watcher = new Watcher();
|
||||
$watcher->type = Watcher::WRITABLE;
|
||||
$watcher->id = $this->nextId++;
|
||||
$watcher->callback = $callback;
|
||||
$watcher->value = $stream;
|
||||
$watcher->data = $data;
|
||||
$this->watchers[$watcher->id] = $watcher;
|
||||
$this->enableQueue[$watcher->id] = $watcher;
|
||||
return $watcher->id;
|
||||
}
|
||||
/**
|
||||
* Execute a callback when a signal is received.
|
||||
*
|
||||
* Warning: Installing the same signal on different instances of this interface is deemed undefined behavior.
|
||||
* Implementations MAY try to detect this, if possible, but are not required to. This is due to technical
|
||||
* limitations of the signals being registered globally per process.
|
||||
*
|
||||
* Multiple watchers on the same signal MAY be executed in any order.
|
||||
*
|
||||
* The created watcher MUST immediately be marked as enabled, but only be activated (i.e. callback can be called)
|
||||
* right before the next tick. Callbacks of watchers MUST NOT be called in the tick they were enabled.
|
||||
*
|
||||
* @param int $signo The signal number to monitor.
|
||||
* @param callable (string $watcherId, int $signo, mixed $data) $callback The callback to execute.
|
||||
* @param mixed $data Arbitrary data given to the callback function as the $data parameter.
|
||||
*
|
||||
* @return string An unique identifier that can be used to cancel, enable or disable the watcher.
|
||||
*
|
||||
* @throws UnsupportedFeatureException If signal handling is not supported.
|
||||
*/
|
||||
public function onSignal(int $signo, callable $callback, $data = null) : string
|
||||
{
|
||||
/** @psalm-var Watcher<int> $watcher */
|
||||
$watcher = new Watcher();
|
||||
$watcher->type = Watcher::SIGNAL;
|
||||
$watcher->id = $this->nextId++;
|
||||
$watcher->callback = $callback;
|
||||
$watcher->value = $signo;
|
||||
$watcher->data = $data;
|
||||
$this->watchers[$watcher->id] = $watcher;
|
||||
$this->enableQueue[$watcher->id] = $watcher;
|
||||
return $watcher->id;
|
||||
}
|
||||
/**
|
||||
* Enable a watcher to be active starting in the next tick.
|
||||
*
|
||||
* Watchers MUST immediately be marked as enabled, but only be activated (i.e. callbacks can be called) right before
|
||||
* the next tick. Callbacks of watchers MUST NOT be called in the tick they were enabled.
|
||||
*
|
||||
* @param string $watcherId The watcher identifier.
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @throws InvalidWatcherError If the watcher identifier is invalid.
|
||||
*/
|
||||
public function enable(string $watcherId)
|
||||
{
|
||||
if (!isset($this->watchers[$watcherId])) {
|
||||
throw new InvalidWatcherError($watcherId, "Cannot enable an invalid watcher identifier: '{$watcherId}'");
|
||||
}
|
||||
$watcher = $this->watchers[$watcherId];
|
||||
if ($watcher->enabled) {
|
||||
return;
|
||||
// Watcher already enabled.
|
||||
}
|
||||
$watcher->enabled = \true;
|
||||
switch ($watcher->type) {
|
||||
case Watcher::DEFER:
|
||||
$this->nextTickQueue[$watcher->id] = $watcher;
|
||||
break;
|
||||
case Watcher::REPEAT:
|
||||
case Watcher::DELAY:
|
||||
\assert(\is_int($watcher->value));
|
||||
$watcher->expiration = $this->now() + $watcher->value;
|
||||
$this->enableQueue[$watcher->id] = $watcher;
|
||||
break;
|
||||
default:
|
||||
$this->enableQueue[$watcher->id] = $watcher;
|
||||
break;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Cancel a watcher.
|
||||
*
|
||||
* This will detach the event loop from all resources that are associated to the watcher. After this operation the
|
||||
* watcher is permanently invalid. Calling this function MUST NOT fail, even if passed an invalid watcher.
|
||||
*
|
||||
* @param string $watcherId The watcher identifier.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function cancel(string $watcherId)
|
||||
{
|
||||
$this->disable($watcherId);
|
||||
unset($this->watchers[$watcherId]);
|
||||
}
|
||||
/**
|
||||
* Disable a watcher immediately.
|
||||
*
|
||||
* A watcher MUST be disabled immediately, e.g. if a defer watcher disables a later defer watcher, the second defer
|
||||
* watcher isn't executed in this tick.
|
||||
*
|
||||
* Disabling a watcher MUST NOT invalidate the watcher. Calling this function MUST NOT fail, even if passed an
|
||||
* invalid watcher.
|
||||
*
|
||||
* @param string $watcherId The watcher identifier.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function disable(string $watcherId)
|
||||
{
|
||||
if (!isset($this->watchers[$watcherId])) {
|
||||
return;
|
||||
}
|
||||
$watcher = $this->watchers[$watcherId];
|
||||
if (!$watcher->enabled) {
|
||||
return;
|
||||
// Watcher already disabled.
|
||||
}
|
||||
$watcher->enabled = \false;
|
||||
$id = $watcher->id;
|
||||
switch ($watcher->type) {
|
||||
case Watcher::DEFER:
|
||||
if (isset($this->nextTickQueue[$id])) {
|
||||
// Watcher was only queued to be enabled.
|
||||
unset($this->nextTickQueue[$id]);
|
||||
} else {
|
||||
unset($this->deferQueue[$id]);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
if (isset($this->enableQueue[$id])) {
|
||||
// Watcher was only queued to be enabled.
|
||||
unset($this->enableQueue[$id]);
|
||||
} else {
|
||||
$this->deactivate($watcher);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Deactivates (disables) the given watcher.
|
||||
*
|
||||
* @param Watcher $watcher
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected abstract function deactivate(Watcher $watcher);
|
||||
/**
|
||||
* Reference a watcher.
|
||||
*
|
||||
* This will keep the event loop alive whilst the watcher is still being monitored. Watchers have this state by
|
||||
* default.
|
||||
*
|
||||
* @param string $watcherId The watcher identifier.
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @throws InvalidWatcherError If the watcher identifier is invalid.
|
||||
*/
|
||||
public function reference(string $watcherId)
|
||||
{
|
||||
if (!isset($this->watchers[$watcherId])) {
|
||||
throw new InvalidWatcherError($watcherId, "Cannot reference an invalid watcher identifier: '{$watcherId}'");
|
||||
}
|
||||
$this->watchers[$watcherId]->referenced = \true;
|
||||
}
|
||||
/**
|
||||
* Unreference a watcher.
|
||||
*
|
||||
* The event loop should exit the run method when only unreferenced watchers are still being monitored. Watchers
|
||||
* are all referenced by default.
|
||||
*
|
||||
* @param string $watcherId The watcher identifier.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function unreference(string $watcherId)
|
||||
{
|
||||
if (!isset($this->watchers[$watcherId])) {
|
||||
return;
|
||||
}
|
||||
$this->watchers[$watcherId]->referenced = \false;
|
||||
}
|
||||
/**
|
||||
* Stores information in the loop bound registry.
|
||||
*
|
||||
* Stored information is package private. Packages MUST NOT retrieve the stored state of other packages. Packages
|
||||
* MUST use their namespace as prefix for keys. They may do so by using `SomeClass::class` as key.
|
||||
*
|
||||
* If packages want to expose loop bound state to consumers other than the package, they SHOULD provide a dedicated
|
||||
* interface for that purpose instead of sharing the storage key.
|
||||
*
|
||||
* @param string $key The namespaced storage key.
|
||||
* @param mixed $value The value to be stored.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public final function setState(string $key, $value)
|
||||
{
|
||||
if ($value === null) {
|
||||
unset($this->registry[$key]);
|
||||
} else {
|
||||
$this->registry[$key] = $value;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Gets information stored bound to the loop.
|
||||
*
|
||||
* Stored information is package private. Packages MUST NOT retrieve the stored state of other packages. Packages
|
||||
* MUST use their namespace as prefix for keys. They may do so by using `SomeClass::class` as key.
|
||||
*
|
||||
* If packages want to expose loop bound state to consumers other than the package, they SHOULD provide a dedicated
|
||||
* interface for that purpose instead of sharing the storage key.
|
||||
*
|
||||
* @param string $key The namespaced storage key.
|
||||
*
|
||||
* @return mixed The previously stored value or `null` if it doesn't exist.
|
||||
*/
|
||||
public final function getState(string $key)
|
||||
{
|
||||
return isset($this->registry[$key]) ? $this->registry[$key] : null;
|
||||
}
|
||||
/**
|
||||
* Set a callback to be executed when an error occurs.
|
||||
*
|
||||
* The callback receives the error as the first and only parameter. The return value of the callback gets ignored.
|
||||
* If it can't handle the error, it MUST throw the error. Errors thrown by the callback or during its invocation
|
||||
* MUST be thrown into the `run` loop and stop the driver.
|
||||
*
|
||||
* Subsequent calls to this method will overwrite the previous handler.
|
||||
*
|
||||
* @param callable(\Throwable $error):void|null $callback The callback to execute. `null` will clear the
|
||||
* current handler.
|
||||
*
|
||||
* @return callable(\Throwable $error):void|null The previous handler, `null` if there was none.
|
||||
*/
|
||||
public function setErrorHandler(callable $callback = null)
|
||||
{
|
||||
$previous = $this->errorHandler;
|
||||
$this->errorHandler = $callback;
|
||||
return $previous;
|
||||
}
|
||||
/**
|
||||
* Invokes the error handler with the given exception.
|
||||
*
|
||||
* @param \Throwable $exception The exception thrown from a watcher callback.
|
||||
*
|
||||
* @return void
|
||||
* @throws \Throwable If no error handler has been set.
|
||||
*/
|
||||
protected function error(\Throwable $exception)
|
||||
{
|
||||
if ($this->errorHandler === null) {
|
||||
throw $exception;
|
||||
}
|
||||
($this->errorHandler)($exception);
|
||||
}
|
||||
/**
|
||||
* Returns the current loop time in millisecond increments. Note this value does not necessarily correlate to
|
||||
* wall-clock time, rather the value returned is meant to be used in relative comparisons to prior values returned
|
||||
* by this method (intervals, expiration calculations, etc.) and is only updated once per loop tick.
|
||||
*
|
||||
* Extending classes should override this function to return a value cached once per loop tick.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function now() : int
|
||||
{
|
||||
return (int) (\microtime(\true) * self::MILLISEC_PER_SEC);
|
||||
}
|
||||
/**
|
||||
* Get the underlying loop handle.
|
||||
*
|
||||
* Example: the `uv_loop` resource for `libuv` or the `EvLoop` object for `libev` or `null` for a native driver.
|
||||
*
|
||||
* Note: This function is *not* exposed in the `Loop` class. Users shall access it directly on the respective loop
|
||||
* instance.
|
||||
*
|
||||
* @return null|object|resource The loop handle the event loop operates on. `null` if there is none.
|
||||
*/
|
||||
public abstract function getHandle();
|
||||
/**
|
||||
* Returns the same array of data as getInfo().
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function __debugInfo()
|
||||
{
|
||||
// @codeCoverageIgnoreStart
|
||||
return $this->getInfo();
|
||||
// @codeCoverageIgnoreEnd
|
||||
}
|
||||
/**
|
||||
* Retrieve an associative array of information about the event loop driver.
|
||||
*
|
||||
* The returned array MUST contain the following data describing the driver's currently registered watchers:
|
||||
*
|
||||
* [
|
||||
* "defer" => ["enabled" => int, "disabled" => int],
|
||||
* "delay" => ["enabled" => int, "disabled" => int],
|
||||
* "repeat" => ["enabled" => int, "disabled" => int],
|
||||
* "on_readable" => ["enabled" => int, "disabled" => int],
|
||||
* "on_writable" => ["enabled" => int, "disabled" => int],
|
||||
* "on_signal" => ["enabled" => int, "disabled" => int],
|
||||
* "enabled_watchers" => ["referenced" => int, "unreferenced" => int],
|
||||
* "running" => bool
|
||||
* ];
|
||||
*
|
||||
* Implementations MAY optionally add more information in the array but at minimum the above `key => value` format
|
||||
* MUST always be provided.
|
||||
*
|
||||
* @return array Statistics about the loop in the described format.
|
||||
*/
|
||||
public function getInfo() : array
|
||||
{
|
||||
$watchers = ["referenced" => 0, "unreferenced" => 0];
|
||||
$defer = $delay = $repeat = $onReadable = $onWritable = $onSignal = ["enabled" => 0, "disabled" => 0];
|
||||
foreach ($this->watchers as $watcher) {
|
||||
switch ($watcher->type) {
|
||||
case Watcher::READABLE:
|
||||
$array =& $onReadable;
|
||||
break;
|
||||
case Watcher::WRITABLE:
|
||||
$array =& $onWritable;
|
||||
break;
|
||||
case Watcher::SIGNAL:
|
||||
$array =& $onSignal;
|
||||
break;
|
||||
case Watcher::DEFER:
|
||||
$array =& $defer;
|
||||
break;
|
||||
case Watcher::DELAY:
|
||||
$array =& $delay;
|
||||
break;
|
||||
case Watcher::REPEAT:
|
||||
$array =& $repeat;
|
||||
break;
|
||||
default:
|
||||
// @codeCoverageIgnoreStart
|
||||
throw new \Error("Unknown watcher type");
|
||||
}
|
||||
if ($watcher->enabled) {
|
||||
++$array["enabled"];
|
||||
if ($watcher->referenced) {
|
||||
++$watchers["referenced"];
|
||||
} else {
|
||||
++$watchers["unreferenced"];
|
||||
}
|
||||
} else {
|
||||
++$array["disabled"];
|
||||
}
|
||||
}
|
||||
return ["enabled_watchers" => $watchers, "defer" => $defer, "delay" => $delay, "repeat" => $repeat, "on_readable" => $onReadable, "on_writable" => $onWritable, "on_signal" => $onSignal, "running" => (bool) $this->running];
|
||||
}
|
||||
}
|
@ -1,55 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace WP_Ultimo\Dependencies\Amp\Loop;
|
||||
|
||||
// @codeCoverageIgnoreStart
|
||||
class DriverFactory
|
||||
{
|
||||
/**
|
||||
* Creates a new loop instance and chooses the best available driver.
|
||||
*
|
||||
* @return Driver
|
||||
*
|
||||
* @throws \Error If an invalid class has been specified via AMP_LOOP_DRIVER
|
||||
*/
|
||||
public function create() : Driver
|
||||
{
|
||||
$driver = (function () {
|
||||
if ($driver = $this->createDriverFromEnv()) {
|
||||
return $driver;
|
||||
}
|
||||
if (UvDriver::isSupported()) {
|
||||
return new UvDriver();
|
||||
}
|
||||
if (EvDriver::isSupported()) {
|
||||
return new EvDriver();
|
||||
}
|
||||
if (EventDriver::isSupported()) {
|
||||
return new EventDriver();
|
||||
}
|
||||
return new NativeDriver();
|
||||
})();
|
||||
if (\getenv("AMP_DEBUG_TRACE_WATCHERS")) {
|
||||
return new TracingDriver($driver);
|
||||
}
|
||||
return $driver;
|
||||
}
|
||||
/**
|
||||
* @return Driver|null
|
||||
*/
|
||||
private function createDriverFromEnv()
|
||||
{
|
||||
$driver = \getenv("AMP_LOOP_DRIVER");
|
||||
if (!$driver) {
|
||||
return null;
|
||||
}
|
||||
if (!\class_exists($driver)) {
|
||||
throw new \Error(\sprintf("Driver '%s' does not exist.", $driver));
|
||||
}
|
||||
if (!\is_subclass_of($driver, Driver::class)) {
|
||||
throw new \Error(\sprintf("Driver '%s' is not a subclass of '%s'.", $driver, Driver::class));
|
||||
}
|
||||
return new $driver();
|
||||
}
|
||||
}
|
||||
// @codeCoverageIgnoreEnd
|
258
dependencies/amphp/amp/lib/Loop/EvDriver.php
vendored
258
dependencies/amphp/amp/lib/Loop/EvDriver.php
vendored
@ -1,258 +0,0 @@
|
||||
<?php
|
||||
|
||||
/** @noinspection PhpComposerExtensionStubsInspection */
|
||||
namespace WP_Ultimo\Dependencies\Amp\Loop;
|
||||
|
||||
use WP_Ultimo\Dependencies\Amp\Coroutine;
|
||||
use WP_Ultimo\Dependencies\Amp\Promise;
|
||||
use WP_Ultimo\Dependencies\React\Promise\PromiseInterface as ReactPromise;
|
||||
use function WP_Ultimo\Dependencies\Amp\Internal\getCurrentTime;
|
||||
use function WP_Ultimo\Dependencies\Amp\Promise\rethrow;
|
||||
class EvDriver extends Driver
|
||||
{
|
||||
/** @var \EvSignal[]|null */
|
||||
private static $activeSignals;
|
||||
public static function isSupported() : bool
|
||||
{
|
||||
return \extension_loaded("ev");
|
||||
}
|
||||
/** @var \EvLoop */
|
||||
private $handle;
|
||||
/** @var \EvWatcher[] */
|
||||
private $events = [];
|
||||
/** @var callable */
|
||||
private $ioCallback;
|
||||
/** @var callable */
|
||||
private $timerCallback;
|
||||
/** @var callable */
|
||||
private $signalCallback;
|
||||
/** @var \EvSignal[] */
|
||||
private $signals = [];
|
||||
/** @var int Internal timestamp for now. */
|
||||
private $now;
|
||||
/** @var int Loop time offset */
|
||||
private $nowOffset;
|
||||
public function __construct()
|
||||
{
|
||||
$this->handle = new \EvLoop();
|
||||
$this->nowOffset = getCurrentTime();
|
||||
$this->now = \random_int(0, $this->nowOffset);
|
||||
$this->nowOffset -= $this->now;
|
||||
if (self::$activeSignals === null) {
|
||||
self::$activeSignals =& $this->signals;
|
||||
}
|
||||
/**
|
||||
* @param \EvIO $event
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
$this->ioCallback = function (\EvIO $event) {
|
||||
/** @var Watcher $watcher */
|
||||
$watcher = $event->data;
|
||||
try {
|
||||
$result = ($watcher->callback)($watcher->id, $watcher->value, $watcher->data);
|
||||
if ($result === null) {
|
||||
return;
|
||||
}
|
||||
if ($result instanceof \Generator) {
|
||||
$result = new Coroutine($result);
|
||||
}
|
||||
if ($result instanceof Promise || $result instanceof ReactPromise) {
|
||||
rethrow($result);
|
||||
}
|
||||
} catch (\Throwable $exception) {
|
||||
$this->error($exception);
|
||||
}
|
||||
};
|
||||
/**
|
||||
* @param \EvTimer $event
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
$this->timerCallback = function (\EvTimer $event) {
|
||||
/** @var Watcher $watcher */
|
||||
$watcher = $event->data;
|
||||
if ($watcher->type & Watcher::DELAY) {
|
||||
$this->cancel($watcher->id);
|
||||
} elseif ($watcher->value === 0) {
|
||||
// Disable and re-enable so it's not executed repeatedly in the same tick
|
||||
// See https://github.com/amphp/amp/issues/131
|
||||
$this->disable($watcher->id);
|
||||
$this->enable($watcher->id);
|
||||
}
|
||||
try {
|
||||
$result = ($watcher->callback)($watcher->id, $watcher->data);
|
||||
if ($result === null) {
|
||||
return;
|
||||
}
|
||||
if ($result instanceof \Generator) {
|
||||
$result = new Coroutine($result);
|
||||
}
|
||||
if ($result instanceof Promise || $result instanceof ReactPromise) {
|
||||
rethrow($result);
|
||||
}
|
||||
} catch (\Throwable $exception) {
|
||||
$this->error($exception);
|
||||
}
|
||||
};
|
||||
/**
|
||||
* @param \EvSignal $event
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
$this->signalCallback = function (\EvSignal $event) {
|
||||
/** @var Watcher $watcher */
|
||||
$watcher = $event->data;
|
||||
try {
|
||||
$result = ($watcher->callback)($watcher->id, $watcher->value, $watcher->data);
|
||||
if ($result === null) {
|
||||
return;
|
||||
}
|
||||
if ($result instanceof \Generator) {
|
||||
$result = new Coroutine($result);
|
||||
}
|
||||
if ($result instanceof Promise || $result instanceof ReactPromise) {
|
||||
rethrow($result);
|
||||
}
|
||||
} catch (\Throwable $exception) {
|
||||
$this->error($exception);
|
||||
}
|
||||
};
|
||||
}
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function cancel(string $watcherId)
|
||||
{
|
||||
parent::cancel($watcherId);
|
||||
unset($this->events[$watcherId]);
|
||||
}
|
||||
public function __destruct()
|
||||
{
|
||||
foreach ($this->events as $event) {
|
||||
/** @psalm-suppress all */
|
||||
if ($event !== null) {
|
||||
// Events may have been nulled in extension depending on destruct order.
|
||||
$event->stop();
|
||||
}
|
||||
}
|
||||
// We need to clear all references to events manually, see
|
||||
// https://bitbucket.org/osmanov/pecl-ev/issues/31/segfault-in-ev_timer_stop
|
||||
$this->events = [];
|
||||
}
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function run()
|
||||
{
|
||||
$active = self::$activeSignals;
|
||||
\assert($active !== null);
|
||||
foreach ($active as $event) {
|
||||
$event->stop();
|
||||
}
|
||||
self::$activeSignals =& $this->signals;
|
||||
foreach ($this->signals as $event) {
|
||||
$event->start();
|
||||
}
|
||||
try {
|
||||
parent::run();
|
||||
} finally {
|
||||
foreach ($this->signals as $event) {
|
||||
$event->stop();
|
||||
}
|
||||
self::$activeSignals =& $active;
|
||||
foreach ($active as $event) {
|
||||
$event->start();
|
||||
}
|
||||
}
|
||||
}
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function stop()
|
||||
{
|
||||
$this->handle->stop();
|
||||
parent::stop();
|
||||
}
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function now() : int
|
||||
{
|
||||
$this->now = getCurrentTime() - $this->nowOffset;
|
||||
return $this->now;
|
||||
}
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getHandle() : \EvLoop
|
||||
{
|
||||
return $this->handle;
|
||||
}
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function dispatch(bool $blocking)
|
||||
{
|
||||
$this->handle->run($blocking ? \Ev::RUN_ONCE : \Ev::RUN_ONCE | \Ev::RUN_NOWAIT);
|
||||
}
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function activate(array $watchers)
|
||||
{
|
||||
$this->handle->nowUpdate();
|
||||
$now = $this->now();
|
||||
foreach ($watchers as $watcher) {
|
||||
if (!isset($this->events[$id = $watcher->id])) {
|
||||
switch ($watcher->type) {
|
||||
case Watcher::READABLE:
|
||||
\assert(\is_resource($watcher->value));
|
||||
$this->events[$id] = $this->handle->io($watcher->value, \Ev::READ, $this->ioCallback, $watcher);
|
||||
break;
|
||||
case Watcher::WRITABLE:
|
||||
\assert(\is_resource($watcher->value));
|
||||
$this->events[$id] = $this->handle->io($watcher->value, \Ev::WRITE, $this->ioCallback, $watcher);
|
||||
break;
|
||||
case Watcher::DELAY:
|
||||
case Watcher::REPEAT:
|
||||
\assert(\is_int($watcher->value));
|
||||
$interval = $watcher->value / self::MILLISEC_PER_SEC;
|
||||
$this->events[$id] = $this->handle->timer(\max(0, ($watcher->expiration - $now) / self::MILLISEC_PER_SEC), $watcher->type & Watcher::REPEAT ? $interval : 0, $this->timerCallback, $watcher);
|
||||
break;
|
||||
case Watcher::SIGNAL:
|
||||
\assert(\is_int($watcher->value));
|
||||
$this->events[$id] = $this->handle->signal($watcher->value, $this->signalCallback, $watcher);
|
||||
break;
|
||||
default:
|
||||
// @codeCoverageIgnoreStart
|
||||
throw new \Error("Unknown watcher type");
|
||||
}
|
||||
} else {
|
||||
$this->events[$id]->start();
|
||||
}
|
||||
if ($watcher->type === Watcher::SIGNAL) {
|
||||
/** @psalm-suppress PropertyTypeCoercion */
|
||||
$this->signals[$id] = $this->events[$id];
|
||||
}
|
||||
}
|
||||
}
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function deactivate(Watcher $watcher)
|
||||
{
|
||||
if (isset($this->events[$id = $watcher->id])) {
|
||||
$this->events[$id]->stop();
|
||||
if ($watcher->type === Watcher::SIGNAL) {
|
||||
unset($this->signals[$id]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
281
dependencies/amphp/amp/lib/Loop/EventDriver.php
vendored
281
dependencies/amphp/amp/lib/Loop/EventDriver.php
vendored
@ -1,281 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace WP_Ultimo\Dependencies\Amp\Loop;
|
||||
|
||||
use WP_Ultimo\Dependencies\Amp\Coroutine;
|
||||
use WP_Ultimo\Dependencies\Amp\Promise;
|
||||
use WP_Ultimo\Dependencies\React\Promise\PromiseInterface as ReactPromise;
|
||||
use function WP_Ultimo\Dependencies\Amp\Internal\getCurrentTime;
|
||||
use function WP_Ultimo\Dependencies\Amp\Promise\rethrow;
|
||||
class EventDriver extends Driver
|
||||
{
|
||||
/** @var \Event[]|null */
|
||||
private static $activeSignals;
|
||||
/** @var \EventBase */
|
||||
private $handle;
|
||||
/** @var \Event[] */
|
||||
private $events = [];
|
||||
/** @var callable */
|
||||
private $ioCallback;
|
||||
/** @var callable */
|
||||
private $timerCallback;
|
||||
/** @var callable */
|
||||
private $signalCallback;
|
||||
/** @var \Event[] */
|
||||
private $signals = [];
|
||||
/** @var int Internal timestamp for now. */
|
||||
private $now;
|
||||
/** @var int Loop time offset */
|
||||
private $nowOffset;
|
||||
public function __construct()
|
||||
{
|
||||
$config = new \EventConfig();
|
||||
if (\DIRECTORY_SEPARATOR !== '\\') {
|
||||
$config->requireFeatures(\EventConfig::FEATURE_FDS);
|
||||
}
|
||||
$this->handle = new \EventBase($config);
|
||||
$this->nowOffset = getCurrentTime();
|
||||
$this->now = \random_int(0, $this->nowOffset);
|
||||
$this->nowOffset -= $this->now;
|
||||
if (self::$activeSignals === null) {
|
||||
self::$activeSignals =& $this->signals;
|
||||
}
|
||||
/**
|
||||
* @param $resource
|
||||
* @param $what
|
||||
* @param Watcher $watcher
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
$this->ioCallback = function ($resource, $what, Watcher $watcher) {
|
||||
\assert(\is_resource($watcher->value));
|
||||
try {
|
||||
$result = ($watcher->callback)($watcher->id, $watcher->value, $watcher->data);
|
||||
if ($result === null) {
|
||||
return;
|
||||
}
|
||||
if ($result instanceof \Generator) {
|
||||
$result = new Coroutine($result);
|
||||
}
|
||||
if ($result instanceof Promise || $result instanceof ReactPromise) {
|
||||
rethrow($result);
|
||||
}
|
||||
} catch (\Throwable $exception) {
|
||||
$this->error($exception);
|
||||
}
|
||||
};
|
||||
/**
|
||||
* @param $resource
|
||||
* @param $what
|
||||
* @param Watcher $watcher
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
$this->timerCallback = function ($resource, $what, Watcher $watcher) {
|
||||
\assert(\is_int($watcher->value));
|
||||
if ($watcher->type & Watcher::DELAY) {
|
||||
$this->cancel($watcher->id);
|
||||
} else {
|
||||
$this->events[$watcher->id]->add($watcher->value / self::MILLISEC_PER_SEC);
|
||||
}
|
||||
try {
|
||||
$result = ($watcher->callback)($watcher->id, $watcher->data);
|
||||
if ($result === null) {
|
||||
return;
|
||||
}
|
||||
if ($result instanceof \Generator) {
|
||||
$result = new Coroutine($result);
|
||||
}
|
||||
if ($result instanceof Promise || $result instanceof ReactPromise) {
|
||||
rethrow($result);
|
||||
}
|
||||
} catch (\Throwable $exception) {
|
||||
$this->error($exception);
|
||||
}
|
||||
};
|
||||
/**
|
||||
* @param $signum
|
||||
* @param $what
|
||||
* @param Watcher $watcher
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
$this->signalCallback = function ($signum, $what, Watcher $watcher) {
|
||||
try {
|
||||
$result = ($watcher->callback)($watcher->id, $watcher->value, $watcher->data);
|
||||
if ($result === null) {
|
||||
return;
|
||||
}
|
||||
if ($result instanceof \Generator) {
|
||||
$result = new Coroutine($result);
|
||||
}
|
||||
if ($result instanceof Promise || $result instanceof ReactPromise) {
|
||||
rethrow($result);
|
||||
}
|
||||
} catch (\Throwable $exception) {
|
||||
$this->error($exception);
|
||||
}
|
||||
};
|
||||
}
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function cancel(string $watcherId)
|
||||
{
|
||||
parent::cancel($watcherId);
|
||||
if (isset($this->events[$watcherId])) {
|
||||
$this->events[$watcherId]->free();
|
||||
unset($this->events[$watcherId]);
|
||||
}
|
||||
}
|
||||
public static function isSupported() : bool
|
||||
{
|
||||
return \extension_loaded("event");
|
||||
}
|
||||
/**
|
||||
* @codeCoverageIgnore
|
||||
*/
|
||||
public function __destruct()
|
||||
{
|
||||
// Unset here, otherwise $event->del() in the loop may fail with a warning, because __destruct order isn't defined.
|
||||
// Related https://github.com/amphp/amp/issues/159.
|
||||
$events = $this->events;
|
||||
$this->events = [];
|
||||
foreach ($events as $event) {
|
||||
if ($event !== null) {
|
||||
// Events may have been nulled in extension depending on destruct order.
|
||||
$event->free();
|
||||
}
|
||||
}
|
||||
// Manually free the loop handle to fully release loop resources.
|
||||
// See https://github.com/amphp/amp/issues/177.
|
||||
if ($this->handle !== null) {
|
||||
$this->handle->free();
|
||||
$this->handle = null;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function run()
|
||||
{
|
||||
$active = self::$activeSignals;
|
||||
\assert($active !== null);
|
||||
foreach ($active as $event) {
|
||||
$event->del();
|
||||
}
|
||||
self::$activeSignals =& $this->signals;
|
||||
foreach ($this->signals as $event) {
|
||||
/** @psalm-suppress TooFewArguments https://github.com/JetBrains/phpstorm-stubs/pull/763 */
|
||||
$event->add();
|
||||
}
|
||||
try {
|
||||
parent::run();
|
||||
} finally {
|
||||
foreach ($this->signals as $event) {
|
||||
$event->del();
|
||||
}
|
||||
self::$activeSignals =& $active;
|
||||
foreach ($active as $event) {
|
||||
/** @psalm-suppress TooFewArguments https://github.com/JetBrains/phpstorm-stubs/pull/763 */
|
||||
$event->add();
|
||||
}
|
||||
}
|
||||
}
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function stop()
|
||||
{
|
||||
$this->handle->stop();
|
||||
parent::stop();
|
||||
}
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function now() : int
|
||||
{
|
||||
$this->now = getCurrentTime() - $this->nowOffset;
|
||||
return $this->now;
|
||||
}
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getHandle() : \EventBase
|
||||
{
|
||||
return $this->handle;
|
||||
}
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function dispatch(bool $blocking)
|
||||
{
|
||||
$this->handle->loop($blocking ? \EventBase::LOOP_ONCE : \EventBase::LOOP_ONCE | \EventBase::LOOP_NONBLOCK);
|
||||
}
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function activate(array $watchers)
|
||||
{
|
||||
$now = $this->now();
|
||||
foreach ($watchers as $watcher) {
|
||||
if (!isset($this->events[$id = $watcher->id])) {
|
||||
switch ($watcher->type) {
|
||||
case Watcher::READABLE:
|
||||
\assert(\is_resource($watcher->value));
|
||||
$this->events[$id] = new \Event($this->handle, $watcher->value, \Event::READ | \Event::PERSIST, $this->ioCallback, $watcher);
|
||||
break;
|
||||
case Watcher::WRITABLE:
|
||||
\assert(\is_resource($watcher->value));
|
||||
$this->events[$id] = new \Event($this->handle, $watcher->value, \Event::WRITE | \Event::PERSIST, $this->ioCallback, $watcher);
|
||||
break;
|
||||
case Watcher::DELAY:
|
||||
case Watcher::REPEAT:
|
||||
\assert(\is_int($watcher->value));
|
||||
$this->events[$id] = new \Event($this->handle, -1, \Event::TIMEOUT, $this->timerCallback, $watcher);
|
||||
break;
|
||||
case Watcher::SIGNAL:
|
||||
\assert(\is_int($watcher->value));
|
||||
$this->events[$id] = new \Event($this->handle, $watcher->value, \Event::SIGNAL | \Event::PERSIST, $this->signalCallback, $watcher);
|
||||
break;
|
||||
default:
|
||||
// @codeCoverageIgnoreStart
|
||||
throw new \Error("Unknown watcher type");
|
||||
}
|
||||
}
|
||||
switch ($watcher->type) {
|
||||
case Watcher::DELAY:
|
||||
case Watcher::REPEAT:
|
||||
\assert(\is_int($watcher->value));
|
||||
$interval = \max(0, $watcher->expiration - $now);
|
||||
$this->events[$id]->add($interval > 0 ? $interval / self::MILLISEC_PER_SEC : 0);
|
||||
break;
|
||||
case Watcher::SIGNAL:
|
||||
$this->signals[$id] = $this->events[$id];
|
||||
// no break
|
||||
default:
|
||||
/** @psalm-suppress TooFewArguments https://github.com/JetBrains/phpstorm-stubs/pull/763 */
|
||||
$this->events[$id]->add();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function deactivate(Watcher $watcher)
|
||||
{
|
||||
if (isset($this->events[$id = $watcher->id])) {
|
||||
$this->events[$id]->del();
|
||||
if ($watcher->type === Watcher::SIGNAL) {
|
||||
unset($this->signals[$id]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,147 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace WP_Ultimo\Dependencies\Amp\Loop\Internal;
|
||||
|
||||
use WP_Ultimo\Dependencies\Amp\Loop\Watcher;
|
||||
/**
|
||||
* Uses a binary tree stored in an array to implement a heap.
|
||||
*/
|
||||
final class TimerQueue
|
||||
{
|
||||
/** @var Watcher[] */
|
||||
private $data = [];
|
||||
/** @var int[] */
|
||||
private $pointers = [];
|
||||
/**
|
||||
* @param int $node Rebuild the data array from the given node upward.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function heapifyUp(int $node)
|
||||
{
|
||||
$entry = $this->data[$node];
|
||||
while ($node !== 0 && $entry->expiration < $this->data[$parent = $node - 1 >> 1]->expiration) {
|
||||
$this->swap($node, $parent);
|
||||
$node = $parent;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* @param int $node Rebuild the data array from the given node downward.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function heapifyDown(int $node)
|
||||
{
|
||||
$length = \count($this->data);
|
||||
while (($child = ($node << 1) + 1) < $length) {
|
||||
if ($this->data[$child]->expiration < $this->data[$node]->expiration && ($child + 1 >= $length || $this->data[$child]->expiration < $this->data[$child + 1]->expiration)) {
|
||||
// Left child is less than parent and right child.
|
||||
$swap = $child;
|
||||
} elseif ($child + 1 < $length && $this->data[$child + 1]->expiration < $this->data[$node]->expiration) {
|
||||
// Right child is less than parent and left child.
|
||||
$swap = $child + 1;
|
||||
} else {
|
||||
// Left and right child are greater than parent.
|
||||
break;
|
||||
}
|
||||
$this->swap($node, $swap);
|
||||
$node = $swap;
|
||||
}
|
||||
}
|
||||
private function swap(int $left, int $right)
|
||||
{
|
||||
$temp = $this->data[$left];
|
||||
$this->data[$left] = $this->data[$right];
|
||||
$this->pointers[$this->data[$right]->id] = $left;
|
||||
$this->data[$right] = $temp;
|
||||
$this->pointers[$temp->id] = $right;
|
||||
}
|
||||
/**
|
||||
* Inserts the watcher into the queue. Time complexity: O(log(n)).
|
||||
*
|
||||
* @param Watcher $watcher
|
||||
*
|
||||
* @psalm-param Watcher<int> $watcher
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function insert(Watcher $watcher)
|
||||
{
|
||||
\assert($watcher->expiration !== null);
|
||||
\assert(!isset($this->pointers[$watcher->id]));
|
||||
$node = \count($this->data);
|
||||
$this->data[$node] = $watcher;
|
||||
$this->pointers[$watcher->id] = $node;
|
||||
$this->heapifyUp($node);
|
||||
}
|
||||
/**
|
||||
* Removes the given watcher from the queue. Time complexity: O(log(n)).
|
||||
*
|
||||
* @param Watcher $watcher
|
||||
*
|
||||
* @psalm-param Watcher<int> $watcher
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function remove(Watcher $watcher)
|
||||
{
|
||||
$id = $watcher->id;
|
||||
if (!isset($this->pointers[$id])) {
|
||||
return;
|
||||
}
|
||||
$this->removeAndRebuild($this->pointers[$id]);
|
||||
}
|
||||
/**
|
||||
* Deletes and returns the Watcher on top of the heap if it has expired, otherwise null is returned.
|
||||
* Time complexity: O(log(n)).
|
||||
*
|
||||
* @param int $now Current loop time.
|
||||
*
|
||||
* @return Watcher|null Expired watcher at the top of the heap or null if the watcher has not expired.
|
||||
*
|
||||
* @psalm-return Watcher<int>|null
|
||||
*/
|
||||
public function extract(int $now)
|
||||
{
|
||||
if (empty($this->data)) {
|
||||
return null;
|
||||
}
|
||||
$watcher = $this->data[0];
|
||||
if ($watcher->expiration > $now) {
|
||||
return null;
|
||||
}
|
||||
$this->removeAndRebuild(0);
|
||||
return $watcher;
|
||||
}
|
||||
/**
|
||||
* Returns the expiration time value at the top of the heap. Time complexity: O(1).
|
||||
*
|
||||
* @return int|null Expiration time of the watcher at the top of the heap or null if the heap is empty.
|
||||
*/
|
||||
public function peek()
|
||||
{
|
||||
return isset($this->data[0]) ? $this->data[0]->expiration : null;
|
||||
}
|
||||
/**
|
||||
* @param int $node Remove the given node and then rebuild the data array.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function removeAndRebuild(int $node)
|
||||
{
|
||||
$length = \count($this->data) - 1;
|
||||
$id = $this->data[$node]->id;
|
||||
$left = $this->data[$node] = $this->data[$length];
|
||||
$this->pointers[$left->id] = $node;
|
||||
unset($this->data[$length], $this->pointers[$id]);
|
||||
if ($node < $length) {
|
||||
// don't need to do anything if we removed the last element
|
||||
$parent = $node - 1 >> 1;
|
||||
if ($parent >= 0 && $this->data[$node]->expiration < $this->data[$parent]->expiration) {
|
||||
$this->heapifyUp($node);
|
||||
} else {
|
||||
$this->heapifyDown($node);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,30 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace WP_Ultimo\Dependencies\Amp\Loop;
|
||||
|
||||
/**
|
||||
* MUST be thrown if any operation (except disable() and cancel()) is attempted with an invalid watcher identifier.
|
||||
*
|
||||
* An invalid watcher identifier is any identifier that is not yet emitted by the driver or cancelled by the user.
|
||||
*/
|
||||
class InvalidWatcherError extends \Error
|
||||
{
|
||||
/** @var string */
|
||||
private $watcherId;
|
||||
/**
|
||||
* @param string $watcherId The watcher identifier.
|
||||
* @param string $message The exception message.
|
||||
*/
|
||||
public function __construct(string $watcherId, string $message)
|
||||
{
|
||||
$this->watcherId = $watcherId;
|
||||
parent::__construct($message);
|
||||
}
|
||||
/**
|
||||
* @return string The watcher identifier.
|
||||
*/
|
||||
public function getWatcherId()
|
||||
{
|
||||
return $this->watcherId;
|
||||
}
|
||||
}
|
365
dependencies/amphp/amp/lib/Loop/NativeDriver.php
vendored
365
dependencies/amphp/amp/lib/Loop/NativeDriver.php
vendored
@ -1,365 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace WP_Ultimo\Dependencies\Amp\Loop;
|
||||
|
||||
use WP_Ultimo\Dependencies\Amp\CallableMaker;
|
||||
use WP_Ultimo\Dependencies\Amp\Coroutine;
|
||||
use WP_Ultimo\Dependencies\Amp\Promise;
|
||||
use WP_Ultimo\Dependencies\React\Promise\PromiseInterface as ReactPromise;
|
||||
use function WP_Ultimo\Dependencies\Amp\Internal\getCurrentTime;
|
||||
use function WP_Ultimo\Dependencies\Amp\Promise\rethrow;
|
||||
class NativeDriver extends Driver
|
||||
{
|
||||
use CallableMaker;
|
||||
/** @var resource[] */
|
||||
private $readStreams = [];
|
||||
/** @var Watcher[][] */
|
||||
private $readWatchers = [];
|
||||
/** @var resource[] */
|
||||
private $writeStreams = [];
|
||||
/** @var Watcher[][] */
|
||||
private $writeWatchers = [];
|
||||
/** @var Internal\TimerQueue */
|
||||
private $timerQueue;
|
||||
/** @var Watcher[][] */
|
||||
private $signalWatchers = [];
|
||||
/** @var int Internal timestamp for now. */
|
||||
private $now;
|
||||
/** @var int Loop time offset */
|
||||
private $nowOffset;
|
||||
/** @var bool */
|
||||
private $signalHandling;
|
||||
/** @var callable */
|
||||
private $streamSelectErrorHandler;
|
||||
/** @var bool */
|
||||
private $streamSelectIgnoreResult = \false;
|
||||
public function __construct()
|
||||
{
|
||||
$this->timerQueue = new Internal\TimerQueue();
|
||||
$this->signalHandling = \extension_loaded("pcntl");
|
||||
$this->nowOffset = getCurrentTime();
|
||||
$this->now = \random_int(0, $this->nowOffset);
|
||||
$this->nowOffset -= $this->now;
|
||||
$this->streamSelectErrorHandler = function ($errno, $message) {
|
||||
// Casing changed in PHP 8 from 'unable' to 'Unable'
|
||||
if (\stripos($message, "stream_select(): unable to select [4]: ") === 0) {
|
||||
// EINTR
|
||||
$this->streamSelectIgnoreResult = \true;
|
||||
return;
|
||||
}
|
||||
if (\strpos($message, 'FD_SETSIZE') !== \false) {
|
||||
$message = \str_replace(["\r\n", "\n", "\r"], " ", $message);
|
||||
$pattern = '(stream_select\\(\\): You MUST recompile PHP with a larger value of FD_SETSIZE. It is set to (\\d+), but you have descriptors numbered at least as high as (\\d+)\\.)';
|
||||
if (\preg_match($pattern, $message, $match)) {
|
||||
$helpLink = 'https://amphp.org/amp/event-loop/#implementations';
|
||||
$message = 'You have reached the limits of stream_select(). It has a FD_SETSIZE of ' . $match[1] . ', but you have file descriptors numbered at least as high as ' . $match[2] . '. ' . "You can install one of the extensions listed on {$helpLink} to support a higher number of " . "concurrent file descriptors. If a large number of open file descriptors is unexpected, you " . "might be leaking file descriptors that aren't closed correctly.";
|
||||
}
|
||||
}
|
||||
throw new \Exception($message, $errno);
|
||||
};
|
||||
}
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @throws \Amp\Loop\UnsupportedFeatureException If the pcntl extension is not available.
|
||||
*/
|
||||
public function onSignal(int $signo, callable $callback, $data = null) : string
|
||||
{
|
||||
if (!$this->signalHandling) {
|
||||
throw new UnsupportedFeatureException("Signal handling requires the pcntl extension");
|
||||
}
|
||||
return parent::onSignal($signo, $callback, $data);
|
||||
}
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function now() : int
|
||||
{
|
||||
$this->now = getCurrentTime() - $this->nowOffset;
|
||||
return $this->now;
|
||||
}
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getHandle()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
/**
|
||||
* @param bool $blocking
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @throws \Throwable
|
||||
*/
|
||||
protected function dispatch(bool $blocking)
|
||||
{
|
||||
$this->selectStreams($this->readStreams, $this->writeStreams, $blocking ? $this->getTimeout() : 0);
|
||||
$now = $this->now();
|
||||
while ($watcher = $this->timerQueue->extract($now)) {
|
||||
if ($watcher->type & Watcher::REPEAT) {
|
||||
$watcher->enabled = \false;
|
||||
// Trick base class into adding to enable queue when calling enable()
|
||||
$this->enable($watcher->id);
|
||||
} else {
|
||||
$this->cancel($watcher->id);
|
||||
}
|
||||
try {
|
||||
// Execute the timer.
|
||||
$result = ($watcher->callback)($watcher->id, $watcher->data);
|
||||
if ($result === null) {
|
||||
continue;
|
||||
}
|
||||
if ($result instanceof \Generator) {
|
||||
$result = new Coroutine($result);
|
||||
}
|
||||
if ($result instanceof Promise || $result instanceof ReactPromise) {
|
||||
rethrow($result);
|
||||
}
|
||||
} catch (\Throwable $exception) {
|
||||
$this->error($exception);
|
||||
}
|
||||
}
|
||||
if ($this->signalHandling) {
|
||||
\pcntl_signal_dispatch();
|
||||
}
|
||||
}
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function activate(array $watchers)
|
||||
{
|
||||
foreach ($watchers as $watcher) {
|
||||
switch ($watcher->type) {
|
||||
case Watcher::READABLE:
|
||||
\assert(\is_resource($watcher->value));
|
||||
$streamId = (int) $watcher->value;
|
||||
$this->readWatchers[$streamId][$watcher->id] = $watcher;
|
||||
$this->readStreams[$streamId] = $watcher->value;
|
||||
break;
|
||||
case Watcher::WRITABLE:
|
||||
\assert(\is_resource($watcher->value));
|
||||
$streamId = (int) $watcher->value;
|
||||
$this->writeWatchers[$streamId][$watcher->id] = $watcher;
|
||||
$this->writeStreams[$streamId] = $watcher->value;
|
||||
break;
|
||||
case Watcher::DELAY:
|
||||
case Watcher::REPEAT:
|
||||
\assert(\is_int($watcher->value));
|
||||
$this->timerQueue->insert($watcher);
|
||||
break;
|
||||
case Watcher::SIGNAL:
|
||||
\assert(\is_int($watcher->value));
|
||||
if (!isset($this->signalWatchers[$watcher->value])) {
|
||||
if (!@\pcntl_signal($watcher->value, $this->callableFromInstanceMethod('handleSignal'))) {
|
||||
$message = "Failed to register signal handler";
|
||||
if ($error = \error_get_last()) {
|
||||
$message .= \sprintf("; Errno: %d; %s", $error["type"], $error["message"]);
|
||||
}
|
||||
throw new \Error($message);
|
||||
}
|
||||
}
|
||||
$this->signalWatchers[$watcher->value][$watcher->id] = $watcher;
|
||||
break;
|
||||
default:
|
||||
// @codeCoverageIgnoreStart
|
||||
throw new \Error("Unknown watcher type");
|
||||
}
|
||||
}
|
||||
}
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function deactivate(Watcher $watcher)
|
||||
{
|
||||
switch ($watcher->type) {
|
||||
case Watcher::READABLE:
|
||||
$streamId = (int) $watcher->value;
|
||||
unset($this->readWatchers[$streamId][$watcher->id]);
|
||||
if (empty($this->readWatchers[$streamId])) {
|
||||
unset($this->readWatchers[$streamId], $this->readStreams[$streamId]);
|
||||
}
|
||||
break;
|
||||
case Watcher::WRITABLE:
|
||||
$streamId = (int) $watcher->value;
|
||||
unset($this->writeWatchers[$streamId][$watcher->id]);
|
||||
if (empty($this->writeWatchers[$streamId])) {
|
||||
unset($this->writeWatchers[$streamId], $this->writeStreams[$streamId]);
|
||||
}
|
||||
break;
|
||||
case Watcher::DELAY:
|
||||
case Watcher::REPEAT:
|
||||
$this->timerQueue->remove($watcher);
|
||||
break;
|
||||
case Watcher::SIGNAL:
|
||||
\assert(\is_int($watcher->value));
|
||||
if (isset($this->signalWatchers[$watcher->value])) {
|
||||
unset($this->signalWatchers[$watcher->value][$watcher->id]);
|
||||
if (empty($this->signalWatchers[$watcher->value])) {
|
||||
unset($this->signalWatchers[$watcher->value]);
|
||||
@\pcntl_signal($watcher->value, \SIG_DFL);
|
||||
}
|
||||
}
|
||||
break;
|
||||
default:
|
||||
// @codeCoverageIgnoreStart
|
||||
throw new \Error("Unknown watcher type");
|
||||
}
|
||||
}
|
||||
/**
|
||||
* @param resource[] $read
|
||||
* @param resource[] $write
|
||||
* @param int $timeout
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function selectStreams(array $read, array $write, int $timeout)
|
||||
{
|
||||
$timeout /= self::MILLISEC_PER_SEC;
|
||||
if (!empty($read) || !empty($write)) {
|
||||
// Use stream_select() if there are any streams in the loop.
|
||||
if ($timeout >= 0) {
|
||||
$seconds = (int) $timeout;
|
||||
$microseconds = (int) (($timeout - $seconds) * self::MICROSEC_PER_SEC);
|
||||
} else {
|
||||
$seconds = null;
|
||||
$microseconds = null;
|
||||
}
|
||||
// Failed connection attempts are indicated via except on Windows
|
||||
// @link https://github.com/reactphp/event-loop/blob/8bd064ce23c26c4decf186c2a5a818c9a8209eb0/src/StreamSelectLoop.php#L279-L287
|
||||
// @link https://docs.microsoft.com/de-de/windows/win32/api/winsock2/nf-winsock2-select
|
||||
$except = null;
|
||||
if (\DIRECTORY_SEPARATOR === '\\') {
|
||||
$except = $write;
|
||||
}
|
||||
\set_error_handler($this->streamSelectErrorHandler);
|
||||
try {
|
||||
$result = \stream_select($read, $write, $except, $seconds, $microseconds);
|
||||
} finally {
|
||||
\restore_error_handler();
|
||||
}
|
||||
if ($this->streamSelectIgnoreResult || $result === 0) {
|
||||
$this->streamSelectIgnoreResult = \false;
|
||||
return;
|
||||
}
|
||||
if (!$result) {
|
||||
$this->error(new \Exception('Unknown error during stream_select'));
|
||||
return;
|
||||
}
|
||||
foreach ($read as $stream) {
|
||||
$streamId = (int) $stream;
|
||||
if (!isset($this->readWatchers[$streamId])) {
|
||||
continue;
|
||||
// All read watchers disabled.
|
||||
}
|
||||
foreach ($this->readWatchers[$streamId] as $watcher) {
|
||||
if (!isset($this->readWatchers[$streamId][$watcher->id])) {
|
||||
continue;
|
||||
// Watcher disabled by another IO watcher.
|
||||
}
|
||||
try {
|
||||
$result = ($watcher->callback)($watcher->id, $stream, $watcher->data);
|
||||
if ($result === null) {
|
||||
continue;
|
||||
}
|
||||
if ($result instanceof \Generator) {
|
||||
$result = new Coroutine($result);
|
||||
}
|
||||
if ($result instanceof Promise || $result instanceof ReactPromise) {
|
||||
rethrow($result);
|
||||
}
|
||||
} catch (\Throwable $exception) {
|
||||
$this->error($exception);
|
||||
}
|
||||
}
|
||||
}
|
||||
\assert(\is_array($write));
|
||||
// See https://github.com/vimeo/psalm/issues/3036
|
||||
if ($except) {
|
||||
foreach ($except as $key => $socket) {
|
||||
$write[$key] = $socket;
|
||||
}
|
||||
}
|
||||
foreach ($write as $stream) {
|
||||
$streamId = (int) $stream;
|
||||
if (!isset($this->writeWatchers[$streamId])) {
|
||||
continue;
|
||||
// All write watchers disabled.
|
||||
}
|
||||
foreach ($this->writeWatchers[$streamId] as $watcher) {
|
||||
if (!isset($this->writeWatchers[$streamId][$watcher->id])) {
|
||||
continue;
|
||||
// Watcher disabled by another IO watcher.
|
||||
}
|
||||
try {
|
||||
$result = ($watcher->callback)($watcher->id, $stream, $watcher->data);
|
||||
if ($result === null) {
|
||||
continue;
|
||||
}
|
||||
if ($result instanceof \Generator) {
|
||||
$result = new Coroutine($result);
|
||||
}
|
||||
if ($result instanceof Promise || $result instanceof ReactPromise) {
|
||||
rethrow($result);
|
||||
}
|
||||
} catch (\Throwable $exception) {
|
||||
$this->error($exception);
|
||||
}
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
if ($timeout < 0) {
|
||||
// Only signal watchers are enabled, so sleep indefinitely.
|
||||
\usleep(\PHP_INT_MAX);
|
||||
return;
|
||||
}
|
||||
if ($timeout > 0) {
|
||||
// Sleep until next timer expires.
|
||||
\usleep((int) ($timeout * self::MICROSEC_PER_SEC));
|
||||
}
|
||||
}
|
||||
/**
|
||||
* @return int Milliseconds until next timer expires or -1 if there are no pending times.
|
||||
*/
|
||||
private function getTimeout() : int
|
||||
{
|
||||
$expiration = $this->timerQueue->peek();
|
||||
if ($expiration === null) {
|
||||
return -1;
|
||||
}
|
||||
$expiration -= getCurrentTime() - $this->nowOffset;
|
||||
return $expiration > 0 ? $expiration : 0;
|
||||
}
|
||||
/**
|
||||
* @param int $signo
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function handleSignal(int $signo)
|
||||
{
|
||||
foreach ($this->signalWatchers[$signo] as $watcher) {
|
||||
if (!isset($this->signalWatchers[$signo][$watcher->id])) {
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
$result = ($watcher->callback)($watcher->id, $signo, $watcher->data);
|
||||
if ($result === null) {
|
||||
continue;
|
||||
}
|
||||
if ($result instanceof \Generator) {
|
||||
$result = new Coroutine($result);
|
||||
}
|
||||
if ($result instanceof Promise || $result instanceof ReactPromise) {
|
||||
rethrow($result);
|
||||
}
|
||||
} catch (\Throwable $exception) {
|
||||
$this->error($exception);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
197
dependencies/amphp/amp/lib/Loop/TracingDriver.php
vendored
197
dependencies/amphp/amp/lib/Loop/TracingDriver.php
vendored
@ -1,197 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace WP_Ultimo\Dependencies\Amp\Loop;
|
||||
|
||||
use function WP_Ultimo\Dependencies\Amp\Internal\formatStacktrace;
|
||||
final class TracingDriver extends Driver
|
||||
{
|
||||
/** @var Driver */
|
||||
private $driver;
|
||||
/** @var true[] */
|
||||
private $enabledWatchers = [];
|
||||
/** @var true[] */
|
||||
private $unreferencedWatchers = [];
|
||||
/** @var string[] */
|
||||
private $creationTraces = [];
|
||||
/** @var string[] */
|
||||
private $cancelTraces = [];
|
||||
public function __construct(Driver $driver)
|
||||
{
|
||||
$this->driver = $driver;
|
||||
}
|
||||
public function run()
|
||||
{
|
||||
$this->driver->run();
|
||||
}
|
||||
public function stop()
|
||||
{
|
||||
$this->driver->stop();
|
||||
}
|
||||
public function defer(callable $callback, $data = null) : string
|
||||
{
|
||||
$id = $this->driver->defer(function (...$args) use($callback) {
|
||||
$this->cancel($args[0]);
|
||||
return $callback(...$args);
|
||||
}, $data);
|
||||
$this->creationTraces[$id] = formatStacktrace(\debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS));
|
||||
$this->enabledWatchers[$id] = \true;
|
||||
return $id;
|
||||
}
|
||||
public function delay(int $delay, callable $callback, $data = null) : string
|
||||
{
|
||||
$id = $this->driver->delay($delay, function (...$args) use($callback) {
|
||||
$this->cancel($args[0]);
|
||||
return $callback(...$args);
|
||||
}, $data);
|
||||
$this->creationTraces[$id] = formatStacktrace(\debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS));
|
||||
$this->enabledWatchers[$id] = \true;
|
||||
return $id;
|
||||
}
|
||||
public function repeat(int $interval, callable $callback, $data = null) : string
|
||||
{
|
||||
$id = $this->driver->repeat($interval, $callback, $data);
|
||||
$this->creationTraces[$id] = formatStacktrace(\debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS));
|
||||
$this->enabledWatchers[$id] = \true;
|
||||
return $id;
|
||||
}
|
||||
public function onReadable($stream, callable $callback, $data = null) : string
|
||||
{
|
||||
$id = $this->driver->onReadable($stream, $callback, $data);
|
||||
$this->creationTraces[$id] = formatStacktrace(\debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS));
|
||||
$this->enabledWatchers[$id] = \true;
|
||||
return $id;
|
||||
}
|
||||
public function onWritable($stream, callable $callback, $data = null) : string
|
||||
{
|
||||
$id = $this->driver->onWritable($stream, $callback, $data);
|
||||
$this->creationTraces[$id] = formatStacktrace(\debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS));
|
||||
$this->enabledWatchers[$id] = \true;
|
||||
return $id;
|
||||
}
|
||||
public function onSignal(int $signo, callable $callback, $data = null) : string
|
||||
{
|
||||
$id = $this->driver->onSignal($signo, $callback, $data);
|
||||
$this->creationTraces[$id] = formatStacktrace(\debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS));
|
||||
$this->enabledWatchers[$id] = \true;
|
||||
return $id;
|
||||
}
|
||||
public function enable(string $watcherId)
|
||||
{
|
||||
try {
|
||||
$this->driver->enable($watcherId);
|
||||
$this->enabledWatchers[$watcherId] = \true;
|
||||
} catch (InvalidWatcherError $e) {
|
||||
throw new InvalidWatcherError($watcherId, $e->getMessage() . "\r\n\r\n" . $this->getTraces($watcherId));
|
||||
}
|
||||
}
|
||||
public function cancel(string $watcherId)
|
||||
{
|
||||
$this->driver->cancel($watcherId);
|
||||
if (!isset($this->cancelTraces[$watcherId])) {
|
||||
$this->cancelTraces[$watcherId] = formatStacktrace(\debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS));
|
||||
}
|
||||
unset($this->enabledWatchers[$watcherId], $this->unreferencedWatchers[$watcherId]);
|
||||
}
|
||||
public function disable(string $watcherId)
|
||||
{
|
||||
$this->driver->disable($watcherId);
|
||||
unset($this->enabledWatchers[$watcherId]);
|
||||
}
|
||||
public function reference(string $watcherId)
|
||||
{
|
||||
try {
|
||||
$this->driver->reference($watcherId);
|
||||
unset($this->unreferencedWatchers[$watcherId]);
|
||||
} catch (InvalidWatcherError $e) {
|
||||
throw new InvalidWatcherError($watcherId, $e->getMessage() . "\r\n\r\n" . $this->getTraces($watcherId));
|
||||
}
|
||||
}
|
||||
public function unreference(string $watcherId)
|
||||
{
|
||||
$this->driver->unreference($watcherId);
|
||||
$this->unreferencedWatchers[$watcherId] = \true;
|
||||
}
|
||||
public function setErrorHandler(callable $callback = null)
|
||||
{
|
||||
return $this->driver->setErrorHandler($callback);
|
||||
}
|
||||
/** @inheritdoc */
|
||||
public function getHandle()
|
||||
{
|
||||
$this->driver->getHandle();
|
||||
}
|
||||
public function dump() : string
|
||||
{
|
||||
$dump = "Enabled, referenced watchers keeping the loop running: ";
|
||||
foreach ($this->enabledWatchers as $watcher => $_) {
|
||||
if (isset($this->unreferencedWatchers[$watcher])) {
|
||||
continue;
|
||||
}
|
||||
$dump .= "Watcher ID: " . $watcher . "\r\n";
|
||||
$dump .= $this->getCreationTrace($watcher);
|
||||
$dump .= "\r\n\r\n";
|
||||
}
|
||||
return \rtrim($dump);
|
||||
}
|
||||
public function getInfo() : array
|
||||
{
|
||||
return $this->driver->getInfo();
|
||||
}
|
||||
public function __debugInfo()
|
||||
{
|
||||
return $this->driver->__debugInfo();
|
||||
}
|
||||
public function now() : int
|
||||
{
|
||||
return $this->driver->now();
|
||||
}
|
||||
protected function error(\Throwable $exception)
|
||||
{
|
||||
$this->driver->error($exception);
|
||||
}
|
||||
/**
|
||||
* @inheritdoc
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function activate(array $watchers)
|
||||
{
|
||||
// nothing to do in a decorator
|
||||
}
|
||||
/**
|
||||
* @inheritdoc
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function dispatch(bool $blocking)
|
||||
{
|
||||
// nothing to do in a decorator
|
||||
}
|
||||
/**
|
||||
* @inheritdoc
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function deactivate(Watcher $watcher)
|
||||
{
|
||||
// nothing to do in a decorator
|
||||
}
|
||||
private function getTraces(string $watcherId) : string
|
||||
{
|
||||
return "Creation Trace:\r\n" . $this->getCreationTrace($watcherId) . "\r\n\r\n" . "Cancellation Trace:\r\n" . $this->getCancelTrace($watcherId);
|
||||
}
|
||||
private function getCreationTrace(string $watcher) : string
|
||||
{
|
||||
if (!isset($this->creationTraces[$watcher])) {
|
||||
return 'No creation trace, yet.';
|
||||
}
|
||||
return $this->creationTraces[$watcher];
|
||||
}
|
||||
private function getCancelTrace(string $watcher) : string
|
||||
{
|
||||
if (!isset($this->cancelTraces[$watcher])) {
|
||||
return 'No cancellation trace, yet.';
|
||||
}
|
||||
return $this->cancelTraces[$watcher];
|
||||
}
|
||||
}
|
@ -1,12 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace WP_Ultimo\Dependencies\Amp\Loop;
|
||||
|
||||
/**
|
||||
* MUST be thrown if a feature is not supported by the system.
|
||||
*
|
||||
* This might happen if ext-pcntl is missing and the loop driver doesn't support another way to dispatch signals.
|
||||
*/
|
||||
class UnsupportedFeatureException extends \Exception
|
||||
{
|
||||
}
|
285
dependencies/amphp/amp/lib/Loop/UvDriver.php
vendored
285
dependencies/amphp/amp/lib/Loop/UvDriver.php
vendored
@ -1,285 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace WP_Ultimo\Dependencies\Amp\Loop;
|
||||
|
||||
use WP_Ultimo\Dependencies\Amp\Coroutine;
|
||||
use WP_Ultimo\Dependencies\Amp\Promise;
|
||||
use WP_Ultimo\Dependencies\React\Promise\PromiseInterface as ReactPromise;
|
||||
use function WP_Ultimo\Dependencies\Amp\Promise\rethrow;
|
||||
class UvDriver extends Driver
|
||||
{
|
||||
/** @var resource A uv_loop resource created with uv_loop_new() */
|
||||
private $handle;
|
||||
/** @var resource[] */
|
||||
private $events = [];
|
||||
/** @var Watcher[][] */
|
||||
private $watchers = [];
|
||||
/** @var resource[] */
|
||||
private $streams = [];
|
||||
/** @var callable */
|
||||
private $ioCallback;
|
||||
/** @var callable */
|
||||
private $timerCallback;
|
||||
/** @var callable */
|
||||
private $signalCallback;
|
||||
public function __construct()
|
||||
{
|
||||
$this->handle = \uv_loop_new();
|
||||
/**
|
||||
* @param $event
|
||||
* @param $status
|
||||
* @param $events
|
||||
* @param $resource
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
$this->ioCallback = function ($event, $status, $events, $resource) {
|
||||
$watchers = $this->watchers[(int) $event];
|
||||
switch ($status) {
|
||||
case 0:
|
||||
// OK
|
||||
break;
|
||||
default:
|
||||
// Invoke the callback on errors, as this matches behavior with other loop back-ends.
|
||||
// Re-enable watcher as libuv disables the watcher on non-zero status.
|
||||
$flags = 0;
|
||||
foreach ($watchers as $watcher) {
|
||||
$flags |= $watcher->enabled ? $watcher->type : 0;
|
||||
}
|
||||
\uv_poll_start($event, $flags, $this->ioCallback);
|
||||
break;
|
||||
}
|
||||
foreach ($watchers as $watcher) {
|
||||
// $events is OR'ed with 4 to trigger watcher if no events are indicated (0) or on UV_DISCONNECT (4).
|
||||
// http://docs.libuv.org/en/v1.x/poll.html
|
||||
if (!($watcher->enabled && ($watcher->type & $events || ($events | 4) === 4))) {
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
$result = ($watcher->callback)($watcher->id, $resource, $watcher->data);
|
||||
if ($result === null) {
|
||||
continue;
|
||||
}
|
||||
if ($result instanceof \Generator) {
|
||||
$result = new Coroutine($result);
|
||||
}
|
||||
if ($result instanceof Promise || $result instanceof ReactPromise) {
|
||||
rethrow($result);
|
||||
}
|
||||
} catch (\Throwable $exception) {
|
||||
$this->error($exception);
|
||||
}
|
||||
}
|
||||
};
|
||||
/**
|
||||
* @param $event
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
$this->timerCallback = function ($event) {
|
||||
$watcher = $this->watchers[(int) $event][0];
|
||||
if ($watcher->type & Watcher::DELAY) {
|
||||
unset($this->events[$watcher->id], $this->watchers[(int) $event]);
|
||||
// Avoid call to uv_is_active().
|
||||
$this->cancel($watcher->id);
|
||||
// Remove reference to watcher in parent.
|
||||
} elseif ($watcher->value === 0) {
|
||||
// Disable and re-enable so it's not executed repeatedly in the same tick
|
||||
// See https://github.com/amphp/amp/issues/131
|
||||
$this->disable($watcher->id);
|
||||
$this->enable($watcher->id);
|
||||
}
|
||||
try {
|
||||
$result = ($watcher->callback)($watcher->id, $watcher->data);
|
||||
if ($result === null) {
|
||||
return;
|
||||
}
|
||||
if ($result instanceof \Generator) {
|
||||
$result = new Coroutine($result);
|
||||
}
|
||||
if ($result instanceof Promise || $result instanceof ReactPromise) {
|
||||
rethrow($result);
|
||||
}
|
||||
} catch (\Throwable $exception) {
|
||||
$this->error($exception);
|
||||
}
|
||||
};
|
||||
/**
|
||||
* @param $event
|
||||
* @param $signo
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
$this->signalCallback = function ($event, $signo) {
|
||||
$watcher = $this->watchers[(int) $event][0];
|
||||
try {
|
||||
$result = ($watcher->callback)($watcher->id, $signo, $watcher->data);
|
||||
if ($result === null) {
|
||||
return;
|
||||
}
|
||||
if ($result instanceof \Generator) {
|
||||
$result = new Coroutine($result);
|
||||
}
|
||||
if ($result instanceof Promise || $result instanceof ReactPromise) {
|
||||
rethrow($result);
|
||||
}
|
||||
} catch (\Throwable $exception) {
|
||||
$this->error($exception);
|
||||
}
|
||||
};
|
||||
}
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function cancel(string $watcherId)
|
||||
{
|
||||
parent::cancel($watcherId);
|
||||
if (!isset($this->events[$watcherId])) {
|
||||
return;
|
||||
}
|
||||
$event = $this->events[$watcherId];
|
||||
$eventId = (int) $event;
|
||||
if (isset($this->watchers[$eventId][0])) {
|
||||
// All except IO watchers.
|
||||
unset($this->watchers[$eventId]);
|
||||
} elseif (isset($this->watchers[$eventId][$watcherId])) {
|
||||
$watcher = $this->watchers[$eventId][$watcherId];
|
||||
unset($this->watchers[$eventId][$watcherId]);
|
||||
if (empty($this->watchers[$eventId])) {
|
||||
unset($this->watchers[$eventId], $this->streams[(int) $watcher->value]);
|
||||
}
|
||||
}
|
||||
unset($this->events[$watcherId]);
|
||||
}
|
||||
public static function isSupported() : bool
|
||||
{
|
||||
return \extension_loaded("uv");
|
||||
}
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function now() : int
|
||||
{
|
||||
\uv_update_time($this->handle);
|
||||
/** @psalm-suppress TooManyArguments */
|
||||
return \uv_now($this->handle);
|
||||
}
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getHandle()
|
||||
{
|
||||
return $this->handle;
|
||||
}
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function dispatch(bool $blocking)
|
||||
{
|
||||
/** @psalm-suppress TooManyArguments */
|
||||
\uv_run($this->handle, $blocking ? \UV::RUN_ONCE : \UV::RUN_NOWAIT);
|
||||
}
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function activate(array $watchers)
|
||||
{
|
||||
$now = $this->now();
|
||||
foreach ($watchers as $watcher) {
|
||||
$id = $watcher->id;
|
||||
switch ($watcher->type) {
|
||||
case Watcher::READABLE:
|
||||
case Watcher::WRITABLE:
|
||||
\assert(\is_resource($watcher->value));
|
||||
$streamId = (int) $watcher->value;
|
||||
if (isset($this->streams[$streamId])) {
|
||||
$event = $this->streams[$streamId];
|
||||
} elseif (isset($this->events[$id])) {
|
||||
$event = $this->streams[$streamId] = $this->events[$id];
|
||||
} else {
|
||||
/** @psalm-suppress UndefinedFunction */
|
||||
$event = $this->streams[$streamId] = \WP_Ultimo\Dependencies\uv_poll_init_socket($this->handle, $watcher->value);
|
||||
}
|
||||
$eventId = (int) $event;
|
||||
$this->events[$id] = $event;
|
||||
$this->watchers[$eventId][$id] = $watcher;
|
||||
$flags = 0;
|
||||
foreach ($this->watchers[$eventId] as $w) {
|
||||
$flags |= $w->enabled ? $w->type : 0;
|
||||
}
|
||||
\uv_poll_start($event, $flags, $this->ioCallback);
|
||||
break;
|
||||
case Watcher::DELAY:
|
||||
case Watcher::REPEAT:
|
||||
\assert(\is_int($watcher->value));
|
||||
if (isset($this->events[$id])) {
|
||||
$event = $this->events[$id];
|
||||
} else {
|
||||
$event = $this->events[$id] = \uv_timer_init($this->handle);
|
||||
}
|
||||
$this->watchers[(int) $event] = [$watcher];
|
||||
\uv_timer_start($event, \max(0, $watcher->expiration - $now), $watcher->type & Watcher::REPEAT ? $watcher->value : 0, $this->timerCallback);
|
||||
break;
|
||||
case Watcher::SIGNAL:
|
||||
\assert(\is_int($watcher->value));
|
||||
if (isset($this->events[$id])) {
|
||||
$event = $this->events[$id];
|
||||
} else {
|
||||
/** @psalm-suppress UndefinedFunction */
|
||||
$event = $this->events[$id] = \WP_Ultimo\Dependencies\uv_signal_init($this->handle);
|
||||
}
|
||||
$this->watchers[(int) $event] = [$watcher];
|
||||
/** @psalm-suppress UndefinedFunction */
|
||||
\WP_Ultimo\Dependencies\uv_signal_start($event, $this->signalCallback, $watcher->value);
|
||||
break;
|
||||
default:
|
||||
// @codeCoverageIgnoreStart
|
||||
throw new \Error("Unknown watcher type");
|
||||
}
|
||||
}
|
||||
}
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function deactivate(Watcher $watcher)
|
||||
{
|
||||
$id = $watcher->id;
|
||||
if (!isset($this->events[$id])) {
|
||||
return;
|
||||
}
|
||||
$event = $this->events[$id];
|
||||
if (!\uv_is_active($event)) {
|
||||
return;
|
||||
}
|
||||
switch ($watcher->type) {
|
||||
case Watcher::READABLE:
|
||||
case Watcher::WRITABLE:
|
||||
$flags = 0;
|
||||
foreach ($this->watchers[(int) $event] as $w) {
|
||||
$flags |= $w->enabled ? $w->type : 0;
|
||||
}
|
||||
if ($flags) {
|
||||
\uv_poll_start($event, $flags, $this->ioCallback);
|
||||
} else {
|
||||
\uv_poll_stop($event);
|
||||
}
|
||||
break;
|
||||
case Watcher::DELAY:
|
||||
case Watcher::REPEAT:
|
||||
\uv_timer_stop($event);
|
||||
break;
|
||||
case Watcher::SIGNAL:
|
||||
\uv_signal_stop($event);
|
||||
break;
|
||||
default:
|
||||
// @codeCoverageIgnoreStart
|
||||
throw new \Error("Unknown watcher type");
|
||||
}
|
||||
}
|
||||
}
|
47
dependencies/amphp/amp/lib/Loop/Watcher.php
vendored
47
dependencies/amphp/amp/lib/Loop/Watcher.php
vendored
@ -1,47 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace WP_Ultimo\Dependencies\Amp\Loop;
|
||||
|
||||
use WP_Ultimo\Dependencies\Amp\Struct;
|
||||
/**
|
||||
* @template TValue as (int|resource|null)
|
||||
*
|
||||
* @psalm-suppress MissingConstructor
|
||||
*/
|
||||
class Watcher
|
||||
{
|
||||
use Struct;
|
||||
const IO = 0b11;
|
||||
const READABLE = 0b1;
|
||||
const WRITABLE = 0b10;
|
||||
const DEFER = 0b100;
|
||||
const TIMER = 0b11000;
|
||||
const DELAY = 0b1000;
|
||||
const REPEAT = 0b10000;
|
||||
const SIGNAL = 0b100000;
|
||||
/** @var int */
|
||||
public $type;
|
||||
/** @var bool */
|
||||
public $enabled = \true;
|
||||
/** @var bool */
|
||||
public $referenced = \true;
|
||||
/** @var string */
|
||||
public $id;
|
||||
/** @var callable */
|
||||
public $callback;
|
||||
/**
|
||||
* Data provided to the watcher callback.
|
||||
*
|
||||
* @var mixed
|
||||
*/
|
||||
public $data;
|
||||
/**
|
||||
* Watcher-dependent value storage. Stream for IO watchers, signal number for signal watchers, interval for timers.
|
||||
*
|
||||
* @var resource|int|null
|
||||
* @psalm-var TValue
|
||||
*/
|
||||
public $value;
|
||||
/** @var int|null */
|
||||
public $expiration;
|
||||
}
|
@ -1,25 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace WP_Ultimo\Dependencies\Amp;
|
||||
|
||||
class MultiReasonException extends \Exception
|
||||
{
|
||||
/** @var \Throwable[] */
|
||||
private $reasons;
|
||||
/**
|
||||
* @param \Throwable[] $reasons Array of exceptions rejecting the promise.
|
||||
* @param string|null $message
|
||||
*/
|
||||
public function __construct(array $reasons, string $message = null)
|
||||
{
|
||||
parent::__construct($message ?: "Multiple errors encountered; use " . self::class . "::getReasons() to retrieve the array of exceptions thrown");
|
||||
$this->reasons = $reasons;
|
||||
}
|
||||
/**
|
||||
* @return \Throwable[]
|
||||
*/
|
||||
public function getReasons() : array
|
||||
{
|
||||
return $this->reasons;
|
||||
}
|
||||
}
|
@ -1,50 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace WP_Ultimo\Dependencies\Amp;
|
||||
|
||||
/**
|
||||
* A NullCancellationToken can be used to avoid conditionals to check whether a token has been provided.
|
||||
*
|
||||
* Instead of writing
|
||||
*
|
||||
* ```php
|
||||
* if ($token) {
|
||||
* $token->throwIfRequested();
|
||||
* }
|
||||
* ```
|
||||
*
|
||||
* potentially multiple times, it allows writing
|
||||
*
|
||||
* ```php
|
||||
* $token = $token ?? new NullCancellationToken;
|
||||
*
|
||||
* // ...
|
||||
*
|
||||
* $token->throwIfRequested();
|
||||
* ```
|
||||
*
|
||||
* instead.
|
||||
*/
|
||||
final class NullCancellationToken implements CancellationToken
|
||||
{
|
||||
/** @inheritdoc */
|
||||
public function subscribe(callable $callback) : string
|
||||
{
|
||||
return "null-token";
|
||||
}
|
||||
/** @inheritdoc */
|
||||
public function unsubscribe(string $id)
|
||||
{
|
||||
// nothing to do
|
||||
}
|
||||
/** @inheritdoc */
|
||||
public function isRequested() : bool
|
||||
{
|
||||
return \false;
|
||||
}
|
||||
/** @inheritdoc */
|
||||
public function throwIfRequested()
|
||||
{
|
||||
// nothing to do
|
||||
}
|
||||
}
|
38
dependencies/amphp/amp/lib/Producer.php
vendored
38
dependencies/amphp/amp/lib/Producer.php
vendored
@ -1,38 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace WP_Ultimo\Dependencies\Amp;
|
||||
|
||||
/**
|
||||
* @template-covariant TValue
|
||||
* @template-implements Iterator<TValue>
|
||||
*/
|
||||
final class Producer implements Iterator
|
||||
{
|
||||
/**
|
||||
* @use Internal\Producer<TValue>
|
||||
*/
|
||||
use CallableMaker, Internal\Producer;
|
||||
/**
|
||||
* @param callable(callable(TValue):Promise):\Generator $producer
|
||||
*
|
||||
* @throws \Error Thrown if the callable does not return a Generator.
|
||||
*/
|
||||
public function __construct(callable $producer)
|
||||
{
|
||||
$result = $producer($this->callableFromInstanceMethod("emit"));
|
||||
if (!$result instanceof \Generator) {
|
||||
throw new \Error("The callable did not return a Generator");
|
||||
}
|
||||
$coroutine = new Coroutine($result);
|
||||
$coroutine->onResolve(function ($exception) {
|
||||
if ($this->complete) {
|
||||
return;
|
||||
}
|
||||
if ($exception) {
|
||||
$this->fail($exception);
|
||||
return;
|
||||
}
|
||||
$this->complete();
|
||||
});
|
||||
}
|
||||
}
|
37
dependencies/amphp/amp/lib/Promise.php
vendored
37
dependencies/amphp/amp/lib/Promise.php
vendored
@ -1,37 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace WP_Ultimo\Dependencies\Amp;
|
||||
|
||||
/**
|
||||
* Representation of the future value of an asynchronous operation.
|
||||
*
|
||||
* @template-covariant TValue
|
||||
* @psalm-yield TValue
|
||||
*/
|
||||
interface Promise
|
||||
{
|
||||
/**
|
||||
* Registers a callback to be invoked when the promise is resolved.
|
||||
*
|
||||
* If this method is called multiple times, additional handlers will be registered instead of replacing any already
|
||||
* existing handlers.
|
||||
*
|
||||
* If the promise is already resolved, the callback MUST be executed immediately.
|
||||
*
|
||||
* Exceptions MUST NOT be thrown from this method. Any exceptions thrown from invoked callbacks MUST be
|
||||
* forwarded to the event-loop error handler.
|
||||
*
|
||||
* Note: You shouldn't implement this interface yourself. Instead, provide a method that returns a promise for the
|
||||
* operation you're implementing. Objects other than pure placeholders implementing it are a very bad idea.
|
||||
*
|
||||
* @param callable $onResolved The first argument shall be `null` on success, while the second shall be `null` on
|
||||
* failure.
|
||||
*
|
||||
* @psalm-param callable(\Throwable|null, mixed): (Promise|\React\Promise\PromiseInterface|\Generator<mixed,
|
||||
* Promise|\React\Promise\PromiseInterface|array<array-key, Promise|\React\Promise\PromiseInterface>, mixed,
|
||||
* mixed>|null) | callable(\Throwable|null, mixed): void $onResolved
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function onResolve(callable $onResolved);
|
||||
}
|
68
dependencies/amphp/amp/lib/Struct.php
vendored
68
dependencies/amphp/amp/lib/Struct.php
vendored
@ -1,68 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace WP_Ultimo\Dependencies\Amp;
|
||||
|
||||
/**
|
||||
* A "safe" struct trait for public property aggregators.
|
||||
*
|
||||
* This trait is intended to make using public properties a little safer by throwing when
|
||||
* nonexistent property names are read or written.
|
||||
*/
|
||||
trait Struct
|
||||
{
|
||||
/**
|
||||
* The minimum percentage [0-100] at which to recommend a similar property
|
||||
* name when generating error messages.
|
||||
*/
|
||||
private $__propertySuggestThreshold = 70;
|
||||
/**
|
||||
* @param string $property
|
||||
*
|
||||
* @psalm-return no-return
|
||||
*/
|
||||
public function __get(string $property)
|
||||
{
|
||||
throw new \Error($this->generateStructPropertyError($property));
|
||||
}
|
||||
/**
|
||||
* @param string $property
|
||||
* @param mixed $value
|
||||
*
|
||||
* @psalm-return no-return
|
||||
*/
|
||||
public function __set(string $property, $value)
|
||||
{
|
||||
throw new \Error($this->generateStructPropertyError($property));
|
||||
}
|
||||
private function generateStructPropertyError(string $property) : string
|
||||
{
|
||||
$suggestion = $this->suggestPropertyName($property);
|
||||
$suggestStr = $suggestion == "" ? "" : " ... did you mean \"{$suggestion}?\"";
|
||||
return \sprintf(
|
||||
"%s property \"%s\" does not exist%s",
|
||||
\str_replace("\x00", "@", \get_class($this)),
|
||||
// Handle anonymous class names.
|
||||
$property,
|
||||
$suggestStr
|
||||
);
|
||||
}
|
||||
private function suggestPropertyName(string $badProperty) : string
|
||||
{
|
||||
$badProperty = \strtolower($badProperty);
|
||||
$bestMatch = "";
|
||||
$bestMatchPercentage = 0;
|
||||
/** @psalm-suppress RawObjectIteration */
|
||||
foreach ($this as $property => $value) {
|
||||
// Never suggest properties that begin with an underscore
|
||||
if ($property[0] === "_") {
|
||||
continue;
|
||||
}
|
||||
\similar_text($badProperty, \strtolower($property), $byRefPercentage);
|
||||
if ($byRefPercentage > $bestMatchPercentage) {
|
||||
$bestMatchPercentage = $byRefPercentage;
|
||||
$bestMatch = $property;
|
||||
}
|
||||
}
|
||||
return $bestMatchPercentage >= $this->__propertySuggestThreshold ? $bestMatch : "";
|
||||
}
|
||||
}
|
53
dependencies/amphp/amp/lib/Success.php
vendored
53
dependencies/amphp/amp/lib/Success.php
vendored
@ -1,53 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace WP_Ultimo\Dependencies\Amp;
|
||||
|
||||
use WP_Ultimo\Dependencies\React\Promise\PromiseInterface as ReactPromise;
|
||||
/**
|
||||
* Creates a successful promise using the given value (which can be any value except an object implementing
|
||||
* `Amp\Promise` or `React\Promise\PromiseInterface`).
|
||||
*
|
||||
* @template-covariant TValue
|
||||
* @template-implements Promise<TValue>
|
||||
*/
|
||||
final class Success implements Promise
|
||||
{
|
||||
/** @var mixed */
|
||||
private $value;
|
||||
/**
|
||||
* @param mixed $value Anything other than a Promise object.
|
||||
*
|
||||
* @psalm-param TValue $value
|
||||
*
|
||||
* @throws \Error If a promise is given as the value.
|
||||
*/
|
||||
public function __construct($value = null)
|
||||
{
|
||||
if ($value instanceof Promise || $value instanceof ReactPromise) {
|
||||
throw new \Error("Cannot use a promise as success value");
|
||||
}
|
||||
$this->value = $value;
|
||||
}
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function onResolve(callable $onResolved)
|
||||
{
|
||||
try {
|
||||
$result = $onResolved(null, $this->value);
|
||||
if ($result === null) {
|
||||
return;
|
||||
}
|
||||
if ($result instanceof \Generator) {
|
||||
$result = new Coroutine($result);
|
||||
}
|
||||
if ($result instanceof Promise || $result instanceof ReactPromise) {
|
||||
Promise\rethrow($result);
|
||||
}
|
||||
} catch (\Throwable $exception) {
|
||||
Loop::defer(static function () use($exception) {
|
||||
throw $exception;
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
@ -1,65 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace WP_Ultimo\Dependencies\Amp;
|
||||
|
||||
use function WP_Ultimo\Dependencies\Amp\Internal\formatStacktrace;
|
||||
/**
|
||||
* A TimeoutCancellationToken automatically requests cancellation after the timeout has elapsed.
|
||||
*/
|
||||
final class TimeoutCancellationToken implements CancellationToken
|
||||
{
|
||||
/** @var string */
|
||||
private $watcher;
|
||||
/** @var CancellationToken */
|
||||
private $token;
|
||||
/**
|
||||
* @param int $timeout Milliseconds until cancellation is requested.
|
||||
* @param string $message Message for TimeoutException. Default is "Operation timed out".
|
||||
*/
|
||||
public function __construct(int $timeout, string $message = "Operation timed out")
|
||||
{
|
||||
$source = new CancellationTokenSource();
|
||||
$this->token = $source->getToken();
|
||||
$trace = \debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS);
|
||||
$this->watcher = Loop::delay($timeout, static function () use($source, $message, $trace) {
|
||||
$trace = formatStacktrace($trace);
|
||||
$source->cancel(new TimeoutException("{$message}\r\nTimeoutCancellationToken was created here:\r\n{$trace}"));
|
||||
});
|
||||
Loop::unreference($this->watcher);
|
||||
}
|
||||
/**
|
||||
* Cancels the delay watcher.
|
||||
*/
|
||||
public function __destruct()
|
||||
{
|
||||
Loop::cancel($this->watcher);
|
||||
}
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function subscribe(callable $callback) : string
|
||||
{
|
||||
return $this->token->subscribe($callback);
|
||||
}
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function unsubscribe(string $id)
|
||||
{
|
||||
$this->token->unsubscribe($id);
|
||||
}
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function isRequested() : bool
|
||||
{
|
||||
return $this->token->isRequested();
|
||||
}
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function throwIfRequested()
|
||||
{
|
||||
$this->token->throwIfRequested();
|
||||
}
|
||||
}
|
19
dependencies/amphp/amp/lib/TimeoutException.php
vendored
19
dependencies/amphp/amp/lib/TimeoutException.php
vendored
@ -1,19 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace WP_Ultimo\Dependencies\Amp;
|
||||
|
||||
/**
|
||||
* Thrown if a promise doesn't resolve within a specified timeout.
|
||||
*
|
||||
* @see \Amp\Promise\timeout()
|
||||
*/
|
||||
class TimeoutException extends \Exception
|
||||
{
|
||||
/**
|
||||
* @param string $message Exception message.
|
||||
*/
|
||||
public function __construct(string $message = "Operation timed out")
|
||||
{
|
||||
parent::__construct($message);
|
||||
}
|
||||
}
|
725
dependencies/amphp/amp/lib/functions.php
vendored
725
dependencies/amphp/amp/lib/functions.php
vendored
@ -1,725 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace WP_Ultimo\Dependencies\Amp;
|
||||
|
||||
use WP_Ultimo\Dependencies\React\Promise\PromiseInterface as ReactPromise;
|
||||
/**
|
||||
* Returns a new function that wraps $callback in a promise/coroutine-aware function that automatically runs
|
||||
* Generators as coroutines. The returned function always returns a promise when invoked. Errors have to be handled
|
||||
* by the callback caller or they will go unnoticed.
|
||||
*
|
||||
* Use this function to create a coroutine-aware callable for a promise-aware callback caller.
|
||||
*
|
||||
* @template TReturn
|
||||
* @template TPromise
|
||||
* @template TGeneratorReturn
|
||||
* @template TGeneratorPromise
|
||||
*
|
||||
* @template TGenerator as TGeneratorReturn|Promise<TGeneratorPromise>
|
||||
* @template T as TReturn|Promise<TPromise>|\Generator<mixed, mixed, mixed, TGenerator>
|
||||
*
|
||||
* @formatter:off
|
||||
*
|
||||
* @param callable(...mixed): T $callback
|
||||
*
|
||||
* @return callable
|
||||
* @psalm-return (T is Promise ? (callable(mixed...): Promise<TPromise>) : (T is \Generator ? (TGenerator is Promise ? (callable(mixed...): Promise<TGeneratorPromise>) : (callable(mixed...): Promise<TGeneratorReturn>)) : (callable(mixed...): Promise<TReturn>)))
|
||||
*
|
||||
* @formatter:on
|
||||
*
|
||||
* @see asyncCoroutine()
|
||||
*
|
||||
* @psalm-suppress InvalidReturnType
|
||||
*/
|
||||
function coroutine(callable $callback) : callable
|
||||
{
|
||||
/** @psalm-suppress InvalidReturnStatement */
|
||||
return static function (...$args) use($callback) : Promise {
|
||||
return call($callback, ...$args);
|
||||
};
|
||||
}
|
||||
/**
|
||||
* Returns a new function that wraps $callback in a promise/coroutine-aware function that automatically runs
|
||||
* Generators as coroutines. The returned function always returns void when invoked. Errors are forwarded to the
|
||||
* loop's error handler using `Amp\Promise\rethrow()`.
|
||||
*
|
||||
* Use this function to create a coroutine-aware callable for a non-promise-aware callback caller.
|
||||
*
|
||||
* @param callable(...mixed): mixed $callback
|
||||
*
|
||||
* @return callable
|
||||
* @psalm-return callable(mixed...): void
|
||||
*
|
||||
* @see coroutine()
|
||||
*/
|
||||
function asyncCoroutine(callable $callback) : callable
|
||||
{
|
||||
return static function (...$args) use($callback) {
|
||||
Promise\rethrow(call($callback, ...$args));
|
||||
};
|
||||
}
|
||||
/**
|
||||
* Calls the given function, always returning a promise. If the function returns a Generator, it will be run as a
|
||||
* coroutine. If the function throws, a failed promise will be returned.
|
||||
*
|
||||
* @template TReturn
|
||||
* @template TPromise
|
||||
* @template TGeneratorReturn
|
||||
* @template TGeneratorPromise
|
||||
*
|
||||
* @template TGenerator as TGeneratorReturn|Promise<TGeneratorPromise>
|
||||
* @template T as TReturn|Promise<TPromise>|\Generator<mixed, mixed, mixed, TGenerator>
|
||||
*
|
||||
* @formatter:off
|
||||
*
|
||||
* @param callable(...mixed): T $callback
|
||||
* @param mixed ...$args Arguments to pass to the function.
|
||||
*
|
||||
* @return Promise
|
||||
* @psalm-return (T is Promise ? Promise<TPromise> : (T is \Generator ? (TGenerator is Promise ? Promise<TGeneratorPromise> : Promise<TGeneratorReturn>) : Promise<TReturn>))
|
||||
*
|
||||
* @formatter:on
|
||||
*/
|
||||
function call(callable $callback, ...$args) : Promise
|
||||
{
|
||||
try {
|
||||
$result = $callback(...$args);
|
||||
} catch (\Throwable $exception) {
|
||||
return new Failure($exception);
|
||||
}
|
||||
if ($result instanceof \Generator) {
|
||||
return new Coroutine($result);
|
||||
}
|
||||
if ($result instanceof Promise) {
|
||||
return $result;
|
||||
}
|
||||
if ($result instanceof ReactPromise) {
|
||||
return Promise\adapt($result);
|
||||
}
|
||||
return new Success($result);
|
||||
}
|
||||
/**
|
||||
* Calls the given function. If the function returns a Generator, it will be run as a coroutine. If the function
|
||||
* throws or returns a failing promise, the failure is forwarded to the loop error handler.
|
||||
*
|
||||
* @param callable(...mixed): mixed $callback
|
||||
* @param mixed ...$args Arguments to pass to the function.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
function asyncCall(callable $callback, ...$args)
|
||||
{
|
||||
Promise\rethrow(call($callback, ...$args));
|
||||
}
|
||||
/**
|
||||
* Sleeps for the specified number of milliseconds.
|
||||
*
|
||||
* @param int $milliseconds
|
||||
*
|
||||
* @return Delayed
|
||||
*/
|
||||
function delay(int $milliseconds) : Delayed
|
||||
{
|
||||
return new Delayed($milliseconds);
|
||||
}
|
||||
/**
|
||||
* Returns the current time relative to an arbitrary point in time.
|
||||
*
|
||||
* @return int Time in milliseconds.
|
||||
*/
|
||||
function getCurrentTime() : int
|
||||
{
|
||||
return Internal\getCurrentTime();
|
||||
}
|
||||
namespace WP_Ultimo\Dependencies\Amp\Promise;
|
||||
|
||||
use WP_Ultimo\Dependencies\Amp\Deferred;
|
||||
use WP_Ultimo\Dependencies\Amp\Loop;
|
||||
use WP_Ultimo\Dependencies\Amp\MultiReasonException;
|
||||
use WP_Ultimo\Dependencies\Amp\Promise;
|
||||
use WP_Ultimo\Dependencies\Amp\Success;
|
||||
use WP_Ultimo\Dependencies\Amp\TimeoutException;
|
||||
use WP_Ultimo\Dependencies\React\Promise\PromiseInterface as ReactPromise;
|
||||
use function WP_Ultimo\Dependencies\Amp\call;
|
||||
use function WP_Ultimo\Dependencies\Amp\Internal\createTypeError;
|
||||
/**
|
||||
* Registers a callback that will forward the failure reason to the event loop's error handler if the promise fails.
|
||||
*
|
||||
* Use this function if you neither return the promise nor handle a possible error yourself to prevent errors from
|
||||
* going entirely unnoticed.
|
||||
*
|
||||
* @param Promise|ReactPromise $promise Promise to register the handler on.
|
||||
*
|
||||
* @return void
|
||||
* @throws \TypeError If $promise is not an instance of \Amp\Promise or \React\Promise\PromiseInterface.
|
||||
*
|
||||
*/
|
||||
function rethrow($promise)
|
||||
{
|
||||
if (!$promise instanceof Promise) {
|
||||
if ($promise instanceof ReactPromise) {
|
||||
$promise = adapt($promise);
|
||||
} else {
|
||||
throw createTypeError([Promise::class, ReactPromise::class], $promise);
|
||||
}
|
||||
}
|
||||
$promise->onResolve(static function ($exception) {
|
||||
if ($exception) {
|
||||
throw $exception;
|
||||
}
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Runs the event loop until the promise is resolved. Should not be called within a running event loop.
|
||||
*
|
||||
* Use this function only in synchronous contexts to wait for an asynchronous operation. Use coroutines and yield to
|
||||
* await promise resolution in a fully asynchronous application instead.
|
||||
*
|
||||
* @template TPromise
|
||||
* @template T as Promise<TPromise>|ReactPromise
|
||||
*
|
||||
* @param Promise|ReactPromise $promise Promise to wait for.
|
||||
*
|
||||
* @return mixed Promise success value.
|
||||
*
|
||||
* @psalm-param T $promise
|
||||
* @psalm-return (T is Promise ? TPromise : mixed)
|
||||
*
|
||||
* @throws \TypeError If $promise is not an instance of \Amp\Promise or \React\Promise\PromiseInterface.
|
||||
* @throws \Error If the event loop stopped without the $promise being resolved.
|
||||
* @throws \Throwable Promise failure reason.
|
||||
*/
|
||||
function wait($promise)
|
||||
{
|
||||
if (!$promise instanceof Promise) {
|
||||
if ($promise instanceof ReactPromise) {
|
||||
$promise = adapt($promise);
|
||||
} else {
|
||||
throw createTypeError([Promise::class, ReactPromise::class], $promise);
|
||||
}
|
||||
}
|
||||
$resolved = \false;
|
||||
try {
|
||||
Loop::run(function () use(&$resolved, &$value, &$exception, $promise) {
|
||||
$promise->onResolve(function ($e, $v) use(&$resolved, &$value, &$exception) {
|
||||
Loop::stop();
|
||||
$resolved = \true;
|
||||
$exception = $e;
|
||||
$value = $v;
|
||||
});
|
||||
});
|
||||
} catch (\Throwable $throwable) {
|
||||
throw new \Error("Loop exceptionally stopped without resolving the promise", 0, $throwable);
|
||||
}
|
||||
if (!$resolved) {
|
||||
throw new \Error("Loop stopped without resolving the promise");
|
||||
}
|
||||
if ($exception) {
|
||||
throw $exception;
|
||||
}
|
||||
return $value;
|
||||
}
|
||||
/**
|
||||
* Creates an artificial timeout for any `Promise`.
|
||||
*
|
||||
* If the timeout expires before the promise is resolved, the returned promise fails with an instance of
|
||||
* `Amp\TimeoutException`.
|
||||
*
|
||||
* @template TReturn
|
||||
*
|
||||
* @param Promise<TReturn>|ReactPromise $promise Promise to which the timeout is applied.
|
||||
* @param int $timeout Timeout in milliseconds.
|
||||
*
|
||||
* @return Promise<TReturn>
|
||||
*
|
||||
* @throws \TypeError If $promise is not an instance of \Amp\Promise or \React\Promise\PromiseInterface.
|
||||
*/
|
||||
function timeout($promise, int $timeout) : Promise
|
||||
{
|
||||
if (!$promise instanceof Promise) {
|
||||
if ($promise instanceof ReactPromise) {
|
||||
$promise = adapt($promise);
|
||||
} else {
|
||||
throw createTypeError([Promise::class, ReactPromise::class], $promise);
|
||||
}
|
||||
}
|
||||
$deferred = new Deferred();
|
||||
$watcher = Loop::delay($timeout, static function () use(&$deferred) {
|
||||
$temp = $deferred;
|
||||
// prevent double resolve
|
||||
$deferred = null;
|
||||
$temp->fail(new TimeoutException());
|
||||
});
|
||||
Loop::unreference($watcher);
|
||||
$promise->onResolve(function () use(&$deferred, $promise, $watcher) {
|
||||
if ($deferred !== null) {
|
||||
Loop::cancel($watcher);
|
||||
$deferred->resolve($promise);
|
||||
}
|
||||
});
|
||||
return $deferred->promise();
|
||||
}
|
||||
/**
|
||||
* Creates an artificial timeout for any `Promise`.
|
||||
*
|
||||
* If the promise is resolved before the timeout expires, the result is returned
|
||||
*
|
||||
* If the timeout expires before the promise is resolved, a default value is returned
|
||||
*
|
||||
* @template TReturn
|
||||
*
|
||||
* @param Promise<TReturn>|ReactPromise $promise Promise to which the timeout is applied.
|
||||
* @param int $timeout Timeout in milliseconds.
|
||||
* @param TReturn $default
|
||||
*
|
||||
* @return Promise<TReturn>
|
||||
*
|
||||
* @throws \TypeError If $promise is not an instance of \Amp\Promise or \React\Promise\PromiseInterface.
|
||||
*/
|
||||
function timeoutWithDefault($promise, int $timeout, $default = null) : Promise
|
||||
{
|
||||
$promise = timeout($promise, $timeout);
|
||||
return call(static function () use($promise, $default) {
|
||||
try {
|
||||
return (yield $promise);
|
||||
} catch (TimeoutException $exception) {
|
||||
return $default;
|
||||
}
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Adapts any object with a done(callable $onFulfilled, callable $onRejected) or then(callable $onFulfilled,
|
||||
* callable $onRejected) method to a promise usable by components depending on placeholders implementing
|
||||
* \AsyncInterop\Promise.
|
||||
*
|
||||
* @param object $promise Object with a done() or then() method.
|
||||
*
|
||||
* @return Promise Promise resolved by the $thenable object.
|
||||
*
|
||||
* @throws \Error If the provided object does not have a then() method.
|
||||
*/
|
||||
function adapt($promise) : Promise
|
||||
{
|
||||
if (!\is_object($promise)) {
|
||||
throw new \Error("Object must be provided");
|
||||
}
|
||||
$deferred = new Deferred();
|
||||
if (\method_exists($promise, 'done')) {
|
||||
$promise->done([$deferred, 'resolve'], [$deferred, 'fail']);
|
||||
} elseif (\method_exists($promise, 'then')) {
|
||||
$promise->then([$deferred, 'resolve'], [$deferred, 'fail']);
|
||||
} else {
|
||||
throw new \Error("Object must have a 'then' or 'done' method");
|
||||
}
|
||||
return $deferred->promise();
|
||||
}
|
||||
/**
|
||||
* Returns a promise that is resolved when all promises are resolved. The returned promise will not fail.
|
||||
* Returned promise succeeds with a two-item array delineating successful and failed promise results,
|
||||
* with keys identical and corresponding to the original given array.
|
||||
*
|
||||
* This function is the same as some() with the notable exception that it will never fail even
|
||||
* if all promises in the array resolve unsuccessfully.
|
||||
*
|
||||
* @template TValue
|
||||
*
|
||||
* @param Promise<TValue>[]|ReactPromise[] $promises
|
||||
*
|
||||
* @return Promise<array{0: \Throwable[], 1: TValue[]}>
|
||||
*
|
||||
* @throws \Error If a non-Promise is in the array.
|
||||
*/
|
||||
function any(array $promises) : Promise
|
||||
{
|
||||
return some($promises, 0);
|
||||
}
|
||||
/**
|
||||
* Returns a promise that succeeds when all promises succeed, and fails if any promise fails. Returned
|
||||
* promise succeeds with an array of values used to succeed each contained promise, with keys corresponding to
|
||||
* the array of promises.
|
||||
*
|
||||
* @param Promise[]|ReactPromise[] $promises Array of only promises.
|
||||
*
|
||||
* @return Promise
|
||||
*
|
||||
* @throws \Error If a non-Promise is in the array.
|
||||
*
|
||||
* @template TValue
|
||||
*
|
||||
* @psalm-param array<array-key, Promise<TValue>|ReactPromise> $promises
|
||||
* @psalm-assert array<array-key, Promise<TValue>|ReactPromise> $promises $promises
|
||||
* @psalm-return Promise<array<array-key, TValue>>
|
||||
*/
|
||||
function all(array $promises) : Promise
|
||||
{
|
||||
if (empty($promises)) {
|
||||
return new Success([]);
|
||||
}
|
||||
$deferred = new Deferred();
|
||||
$result = $deferred->promise();
|
||||
$pending = \count($promises);
|
||||
$values = [];
|
||||
foreach ($promises as $key => $promise) {
|
||||
if ($promise instanceof ReactPromise) {
|
||||
$promise = adapt($promise);
|
||||
} elseif (!$promise instanceof Promise) {
|
||||
throw createTypeError([Promise::class, ReactPromise::class], $promise);
|
||||
}
|
||||
$values[$key] = null;
|
||||
// add entry to array to preserve order
|
||||
$promise->onResolve(function ($exception, $value) use(&$deferred, &$values, &$pending, $key) {
|
||||
if ($pending === 0) {
|
||||
return;
|
||||
}
|
||||
if ($exception) {
|
||||
$pending = 0;
|
||||
$deferred->fail($exception);
|
||||
$deferred = null;
|
||||
return;
|
||||
}
|
||||
$values[$key] = $value;
|
||||
if (0 === --$pending) {
|
||||
$deferred->resolve($values);
|
||||
}
|
||||
});
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
/**
|
||||
* Returns a promise that succeeds when the first promise succeeds, and fails only if all promises fail.
|
||||
*
|
||||
* @template TValue
|
||||
*
|
||||
* @param Promise<TValue>[]|ReactPromise[] $promises Array of only promises.
|
||||
*
|
||||
* @return Promise<TValue>
|
||||
*
|
||||
* @throws \Error If the array is empty or a non-Promise is in the array.
|
||||
*/
|
||||
function first(array $promises) : Promise
|
||||
{
|
||||
if (empty($promises)) {
|
||||
throw new \Error("No promises provided");
|
||||
}
|
||||
$deferred = new Deferred();
|
||||
$result = $deferred->promise();
|
||||
$pending = \count($promises);
|
||||
$exceptions = [];
|
||||
foreach ($promises as $key => $promise) {
|
||||
if ($promise instanceof ReactPromise) {
|
||||
$promise = adapt($promise);
|
||||
} elseif (!$promise instanceof Promise) {
|
||||
throw createTypeError([Promise::class, ReactPromise::class], $promise);
|
||||
}
|
||||
$exceptions[$key] = null;
|
||||
// add entry to array to preserve order
|
||||
$promise->onResolve(function ($error, $value) use(&$deferred, &$exceptions, &$pending, $key) {
|
||||
if ($pending === 0) {
|
||||
return;
|
||||
}
|
||||
if (!$error) {
|
||||
$pending = 0;
|
||||
$deferred->resolve($value);
|
||||
$deferred = null;
|
||||
return;
|
||||
}
|
||||
$exceptions[$key] = $error;
|
||||
if (0 === --$pending) {
|
||||
$deferred->fail(new MultiReasonException($exceptions));
|
||||
}
|
||||
});
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
/**
|
||||
* Resolves with a two-item array delineating successful and failed Promise results.
|
||||
*
|
||||
* The returned promise will only fail if the given number of required promises fail.
|
||||
*
|
||||
* @template TValue
|
||||
*
|
||||
* @param Promise<TValue>[]|ReactPromise[] $promises Array of only promises.
|
||||
* @param int $required Number of promises that must succeed for the
|
||||
* returned promise to succeed.
|
||||
*
|
||||
* @return Promise<array{0: \Throwable[], 1: TValue[]}>
|
||||
*
|
||||
* @throws \Error If a non-Promise is in the array.
|
||||
*/
|
||||
function some(array $promises, int $required = 1) : Promise
|
||||
{
|
||||
if ($required < 0) {
|
||||
throw new \Error("Number of promises required must be non-negative");
|
||||
}
|
||||
$pending = \count($promises);
|
||||
if ($required > $pending) {
|
||||
throw new \Error("Too few promises provided");
|
||||
}
|
||||
if (empty($promises)) {
|
||||
return new Success([[], []]);
|
||||
}
|
||||
$deferred = new Deferred();
|
||||
$result = $deferred->promise();
|
||||
$values = [];
|
||||
$exceptions = [];
|
||||
foreach ($promises as $key => $promise) {
|
||||
if ($promise instanceof ReactPromise) {
|
||||
$promise = adapt($promise);
|
||||
} elseif (!$promise instanceof Promise) {
|
||||
throw createTypeError([Promise::class, ReactPromise::class], $promise);
|
||||
}
|
||||
$values[$key] = $exceptions[$key] = null;
|
||||
// add entry to arrays to preserve order
|
||||
$promise->onResolve(static function ($exception, $value) use(&$values, &$exceptions, &$pending, $key, $required, $deferred) {
|
||||
if ($exception) {
|
||||
$exceptions[$key] = $exception;
|
||||
unset($values[$key]);
|
||||
} else {
|
||||
$values[$key] = $value;
|
||||
unset($exceptions[$key]);
|
||||
}
|
||||
if (0 === --$pending) {
|
||||
if (\count($values) < $required) {
|
||||
$deferred->fail(new MultiReasonException($exceptions));
|
||||
} else {
|
||||
$deferred->resolve([$exceptions, $values]);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
/**
|
||||
* Wraps a promise into another promise, altering the exception or result.
|
||||
*
|
||||
* @param Promise|ReactPromise $promise
|
||||
* @param callable $callback
|
||||
*
|
||||
* @return Promise
|
||||
*/
|
||||
function wrap($promise, callable $callback) : Promise
|
||||
{
|
||||
if ($promise instanceof ReactPromise) {
|
||||
$promise = adapt($promise);
|
||||
} elseif (!$promise instanceof Promise) {
|
||||
throw createTypeError([Promise::class, ReactPromise::class], $promise);
|
||||
}
|
||||
$deferred = new Deferred();
|
||||
$promise->onResolve(static function (\Throwable $exception = null, $result) use($deferred, $callback) {
|
||||
try {
|
||||
$result = $callback($exception, $result);
|
||||
} catch (\Throwable $exception) {
|
||||
$deferred->fail($exception);
|
||||
return;
|
||||
}
|
||||
$deferred->resolve($result);
|
||||
});
|
||||
return $deferred->promise();
|
||||
}
|
||||
namespace WP_Ultimo\Dependencies\Amp\Iterator;
|
||||
|
||||
use WP_Ultimo\Dependencies\Amp\Delayed;
|
||||
use WP_Ultimo\Dependencies\Amp\Emitter;
|
||||
use WP_Ultimo\Dependencies\Amp\Iterator;
|
||||
use WP_Ultimo\Dependencies\Amp\Producer;
|
||||
use WP_Ultimo\Dependencies\Amp\Promise;
|
||||
use function WP_Ultimo\Dependencies\Amp\call;
|
||||
use function WP_Ultimo\Dependencies\Amp\coroutine;
|
||||
use function WP_Ultimo\Dependencies\Amp\Internal\createTypeError;
|
||||
/**
|
||||
* Creates an iterator from the given iterable, emitting the each value. The iterable may contain promises. If any
|
||||
* promise fails, the iterator will fail with the same reason.
|
||||
*
|
||||
* @param array|\Traversable $iterable Elements to emit.
|
||||
* @param int $delay Delay between element emissions in milliseconds.
|
||||
*
|
||||
* @return Iterator
|
||||
*
|
||||
* @throws \TypeError If the argument is not an array or instance of \Traversable.
|
||||
*/
|
||||
function fromIterable($iterable, int $delay = 0) : Iterator
|
||||
{
|
||||
if (!$iterable instanceof \Traversable && !\is_array($iterable)) {
|
||||
throw createTypeError(["array", "Traversable"], $iterable);
|
||||
}
|
||||
if ($delay) {
|
||||
return new Producer(static function (callable $emit) use($iterable, $delay) {
|
||||
foreach ($iterable as $value) {
|
||||
(yield new Delayed($delay));
|
||||
(yield $emit($value));
|
||||
}
|
||||
});
|
||||
}
|
||||
return new Producer(static function (callable $emit) use($iterable) {
|
||||
foreach ($iterable as $value) {
|
||||
(yield $emit($value));
|
||||
}
|
||||
});
|
||||
}
|
||||
/**
|
||||
* @template TValue
|
||||
* @template TReturn
|
||||
*
|
||||
* @param Iterator<TValue> $iterator
|
||||
* @param callable (TValue $value): TReturn $onEmit
|
||||
*
|
||||
* @return Iterator<TReturn>
|
||||
*/
|
||||
function map(Iterator $iterator, callable $onEmit) : Iterator
|
||||
{
|
||||
return new Producer(static function (callable $emit) use($iterator, $onEmit) {
|
||||
while ((yield $iterator->advance())) {
|
||||
(yield $emit($onEmit($iterator->getCurrent())));
|
||||
}
|
||||
});
|
||||
}
|
||||
/**
|
||||
* @template TValue
|
||||
*
|
||||
* @param Iterator<TValue> $iterator
|
||||
* @param callable(TValue $value):bool $filter
|
||||
*
|
||||
* @return Iterator<TValue>
|
||||
*/
|
||||
function filter(Iterator $iterator, callable $filter) : Iterator
|
||||
{
|
||||
return new Producer(static function (callable $emit) use($iterator, $filter) {
|
||||
while ((yield $iterator->advance())) {
|
||||
if ($filter($iterator->getCurrent())) {
|
||||
(yield $emit($iterator->getCurrent()));
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Creates an iterator that emits values emitted from any iterator in the array of iterators.
|
||||
*
|
||||
* @param Iterator[] $iterators
|
||||
*
|
||||
* @return Iterator
|
||||
*/
|
||||
function merge(array $iterators) : Iterator
|
||||
{
|
||||
$emitter = new Emitter();
|
||||
$result = $emitter->iterate();
|
||||
$coroutine = coroutine(static function (Iterator $iterator) use(&$emitter) {
|
||||
while ((yield $iterator->advance()) && $emitter !== null) {
|
||||
(yield $emitter->emit($iterator->getCurrent()));
|
||||
}
|
||||
});
|
||||
$coroutines = [];
|
||||
foreach ($iterators as $iterator) {
|
||||
if (!$iterator instanceof Iterator) {
|
||||
throw createTypeError([Iterator::class], $iterator);
|
||||
}
|
||||
$coroutines[] = $coroutine($iterator);
|
||||
}
|
||||
Promise\all($coroutines)->onResolve(static function ($exception) use(&$emitter) {
|
||||
if ($exception) {
|
||||
$emitter->fail($exception);
|
||||
$emitter = null;
|
||||
} else {
|
||||
$emitter->complete();
|
||||
}
|
||||
});
|
||||
return $result;
|
||||
}
|
||||
/**
|
||||
* Concatenates the given iterators into a single iterator, emitting values from a single iterator at a time. The
|
||||
* prior iterator must complete before values are emitted from any subsequent iterators. Iterators are concatenated
|
||||
* in the order given (iteration order of the array).
|
||||
*
|
||||
* @param Iterator[] $iterators
|
||||
*
|
||||
* @return Iterator
|
||||
*/
|
||||
function concat(array $iterators) : Iterator
|
||||
{
|
||||
foreach ($iterators as $iterator) {
|
||||
if (!$iterator instanceof Iterator) {
|
||||
throw createTypeError([Iterator::class], $iterator);
|
||||
}
|
||||
}
|
||||
$emitter = new Emitter();
|
||||
$previous = [];
|
||||
$promise = Promise\all($previous);
|
||||
$coroutine = coroutine(static function (Iterator $iterator, callable $emit) {
|
||||
while ((yield $iterator->advance())) {
|
||||
(yield $emit($iterator->getCurrent()));
|
||||
}
|
||||
});
|
||||
foreach ($iterators as $iterator) {
|
||||
$emit = coroutine(static function ($value) use($emitter, $promise) {
|
||||
static $pending = \true, $failed = \false;
|
||||
if ($failed) {
|
||||
return;
|
||||
}
|
||||
if ($pending) {
|
||||
try {
|
||||
(yield $promise);
|
||||
$pending = \false;
|
||||
} catch (\Throwable $exception) {
|
||||
$failed = \true;
|
||||
return;
|
||||
// Prior iterator failed.
|
||||
}
|
||||
}
|
||||
(yield $emitter->emit($value));
|
||||
});
|
||||
$previous[] = $coroutine($iterator, $emit);
|
||||
$promise = Promise\all($previous);
|
||||
}
|
||||
$promise->onResolve(static function ($exception) use($emitter) {
|
||||
if ($exception) {
|
||||
$emitter->fail($exception);
|
||||
return;
|
||||
}
|
||||
$emitter->complete();
|
||||
});
|
||||
return $emitter->iterate();
|
||||
}
|
||||
/**
|
||||
* Discards all remaining items and returns the number of discarded items.
|
||||
*
|
||||
* @template TValue
|
||||
*
|
||||
* @param Iterator $iterator
|
||||
*
|
||||
* @return Promise
|
||||
*
|
||||
* @psalm-param Iterator<TValue> $iterator
|
||||
* @psalm-return Promise<int>
|
||||
*/
|
||||
function discard(Iterator $iterator) : Promise
|
||||
{
|
||||
return call(static function () use($iterator) : \Generator {
|
||||
$count = 0;
|
||||
while ((yield $iterator->advance())) {
|
||||
$count++;
|
||||
}
|
||||
return $count;
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Collects all items from an iterator into an array.
|
||||
*
|
||||
* @template TValue
|
||||
*
|
||||
* @param Iterator $iterator
|
||||
*
|
||||
* @psalm-param Iterator<TValue> $iterator
|
||||
*
|
||||
* @return Promise
|
||||
* @psalm-return Promise<array<array-key, TValue>>
|
||||
*/
|
||||
function toArray(Iterator $iterator) : Promise
|
||||
{
|
||||
return call(static function () use($iterator) {
|
||||
/** @psalm-var list $array */
|
||||
$array = [];
|
||||
while ((yield $iterator->advance())) {
|
||||
$array[] = $iterator->getCurrent();
|
||||
}
|
||||
return $array;
|
||||
});
|
||||
}
|
@ -1,51 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace WP_Ultimo\Dependencies\Amp\ByteStream\Base64;
|
||||
|
||||
use WP_Ultimo\Dependencies\Amp\ByteStream\InputStream;
|
||||
use WP_Ultimo\Dependencies\Amp\ByteStream\StreamException;
|
||||
use WP_Ultimo\Dependencies\Amp\Promise;
|
||||
use function WP_Ultimo\Dependencies\Amp\call;
|
||||
final class Base64DecodingInputStream implements InputStream
|
||||
{
|
||||
/** @var InputStream|null */
|
||||
private $source;
|
||||
/** @var string|null */
|
||||
private $buffer = '';
|
||||
public function __construct(InputStream $source)
|
||||
{
|
||||
$this->source = $source;
|
||||
}
|
||||
public function read() : Promise
|
||||
{
|
||||
return call(function () {
|
||||
if ($this->source === null) {
|
||||
throw new StreamException('Failed to read stream chunk due to invalid base64 data');
|
||||
}
|
||||
$chunk = (yield $this->source->read());
|
||||
if ($chunk === null) {
|
||||
if ($this->buffer === null) {
|
||||
return null;
|
||||
}
|
||||
$chunk = \base64_decode($this->buffer, \true);
|
||||
if ($chunk === \false) {
|
||||
$this->source = null;
|
||||
$this->buffer = null;
|
||||
throw new StreamException('Failed to read stream chunk due to invalid base64 data');
|
||||
}
|
||||
$this->buffer = null;
|
||||
return $chunk;
|
||||
}
|
||||
$this->buffer .= $chunk;
|
||||
$length = \strlen($this->buffer);
|
||||
$chunk = \base64_decode(\substr($this->buffer, 0, $length - $length % 4), \true);
|
||||
if ($chunk === \false) {
|
||||
$this->source = null;
|
||||
$this->buffer = null;
|
||||
throw new StreamException('Failed to read stream chunk due to invalid base64 data');
|
||||
}
|
||||
$this->buffer = \substr($this->buffer, $length - $length % 4);
|
||||
return $chunk;
|
||||
});
|
||||
}
|
||||
}
|
@ -1,43 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace WP_Ultimo\Dependencies\Amp\ByteStream\Base64;
|
||||
|
||||
use WP_Ultimo\Dependencies\Amp\ByteStream\OutputStream;
|
||||
use WP_Ultimo\Dependencies\Amp\ByteStream\StreamException;
|
||||
use WP_Ultimo\Dependencies\Amp\Failure;
|
||||
use WP_Ultimo\Dependencies\Amp\Promise;
|
||||
final class Base64DecodingOutputStream implements OutputStream
|
||||
{
|
||||
/** @var OutputStream */
|
||||
private $destination;
|
||||
/** @var string */
|
||||
private $buffer = '';
|
||||
/** @var int */
|
||||
private $offset = 0;
|
||||
public function __construct(OutputStream $destination)
|
||||
{
|
||||
$this->destination = $destination;
|
||||
}
|
||||
public function write(string $data) : Promise
|
||||
{
|
||||
$this->buffer .= $data;
|
||||
$length = \strlen($this->buffer);
|
||||
$chunk = \base64_decode(\substr($this->buffer, 0, $length - $length % 4), \true);
|
||||
if ($chunk === \false) {
|
||||
return new Failure(new StreamException('Invalid base64 near offset ' . $this->offset));
|
||||
}
|
||||
$this->offset += $length - $length % 4;
|
||||
$this->buffer = \substr($this->buffer, $length - $length % 4);
|
||||
return $this->destination->write($chunk);
|
||||
}
|
||||
public function end(string $finalData = "") : Promise
|
||||
{
|
||||
$this->offset += \strlen($this->buffer);
|
||||
$chunk = \base64_decode($this->buffer . $finalData, \true);
|
||||
if ($chunk === \false) {
|
||||
return new Failure(new StreamException('Invalid base64 near offset ' . $this->offset));
|
||||
}
|
||||
$this->buffer = '';
|
||||
return $this->destination->end($chunk);
|
||||
}
|
||||
}
|
@ -1,37 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace WP_Ultimo\Dependencies\Amp\ByteStream\Base64;
|
||||
|
||||
use WP_Ultimo\Dependencies\Amp\ByteStream\InputStream;
|
||||
use WP_Ultimo\Dependencies\Amp\Promise;
|
||||
use function WP_Ultimo\Dependencies\Amp\call;
|
||||
final class Base64EncodingInputStream implements InputStream
|
||||
{
|
||||
/** @var InputStream */
|
||||
private $source;
|
||||
/** @var string|null */
|
||||
private $buffer = '';
|
||||
public function __construct(InputStream $source)
|
||||
{
|
||||
$this->source = $source;
|
||||
}
|
||||
public function read() : Promise
|
||||
{
|
||||
return call(function () {
|
||||
$chunk = (yield $this->source->read());
|
||||
if ($chunk === null) {
|
||||
if ($this->buffer === null) {
|
||||
return null;
|
||||
}
|
||||
$chunk = \base64_encode($this->buffer);
|
||||
$this->buffer = null;
|
||||
return $chunk;
|
||||
}
|
||||
$this->buffer .= $chunk;
|
||||
$length = \strlen($this->buffer);
|
||||
$chunk = \base64_encode(\substr($this->buffer, 0, $length - $length % 3));
|
||||
$this->buffer = \substr($this->buffer, $length - $length % 3);
|
||||
return $chunk;
|
||||
});
|
||||
}
|
||||
}
|
@ -1,31 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace WP_Ultimo\Dependencies\Amp\ByteStream\Base64;
|
||||
|
||||
use WP_Ultimo\Dependencies\Amp\ByteStream\OutputStream;
|
||||
use WP_Ultimo\Dependencies\Amp\Promise;
|
||||
final class Base64EncodingOutputStream implements OutputStream
|
||||
{
|
||||
/** @var OutputStream */
|
||||
private $destination;
|
||||
/** @var string */
|
||||
private $buffer = '';
|
||||
public function __construct(OutputStream $destination)
|
||||
{
|
||||
$this->destination = $destination;
|
||||
}
|
||||
public function write(string $data) : Promise
|
||||
{
|
||||
$this->buffer .= $data;
|
||||
$length = \strlen($this->buffer);
|
||||
$chunk = \base64_encode(\substr($this->buffer, 0, $length - $length % 3));
|
||||
$this->buffer = \substr($this->buffer, $length - $length % 3);
|
||||
return $this->destination->write($chunk);
|
||||
}
|
||||
public function end(string $finalData = "") : Promise
|
||||
{
|
||||
$chunk = \base64_encode($this->buffer . $finalData);
|
||||
$this->buffer = '';
|
||||
return $this->destination->end($chunk);
|
||||
}
|
||||
}
|
@ -1,7 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace WP_Ultimo\Dependencies\Amp\ByteStream;
|
||||
|
||||
final class ClosedException extends StreamException
|
||||
{
|
||||
}
|
@ -1,34 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace WP_Ultimo\Dependencies\Amp\ByteStream;
|
||||
|
||||
use WP_Ultimo\Dependencies\Amp\Promise;
|
||||
use WP_Ultimo\Dependencies\Amp\Success;
|
||||
/**
|
||||
* Input stream with a single already known data chunk.
|
||||
*/
|
||||
final class InMemoryStream implements InputStream
|
||||
{
|
||||
private $contents;
|
||||
/**
|
||||
* @param string|null $contents Data chunk or `null` for no data chunk.
|
||||
*/
|
||||
public function __construct(string $contents = null)
|
||||
{
|
||||
$this->contents = $contents;
|
||||
}
|
||||
/**
|
||||
* Reads data from the stream.
|
||||
*
|
||||
* @return Promise<string|null> Resolves with the full contents or `null` if the stream has closed / already been consumed.
|
||||
*/
|
||||
public function read() : Promise
|
||||
{
|
||||
if ($this->contents === null) {
|
||||
return new Success();
|
||||
}
|
||||
$promise = new Success($this->contents);
|
||||
$this->contents = null;
|
||||
return $promise;
|
||||
}
|
||||
}
|
@ -1,37 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace WP_Ultimo\Dependencies\Amp\ByteStream;
|
||||
|
||||
use WP_Ultimo\Dependencies\Amp\Promise;
|
||||
/**
|
||||
* An `InputStream` allows reading byte streams in chunks.
|
||||
*
|
||||
* **Example**
|
||||
*
|
||||
* ```php
|
||||
* function readAll(InputStream $in): Promise {
|
||||
* return Amp\call(function () use ($in) {
|
||||
* $buffer = "";
|
||||
*
|
||||
* while (($chunk = yield $in->read()) !== null) {
|
||||
* $buffer .= $chunk;
|
||||
* }
|
||||
*
|
||||
* return $buffer;
|
||||
* });
|
||||
* }
|
||||
* ```
|
||||
*/
|
||||
interface InputStream
|
||||
{
|
||||
/**
|
||||
* Reads data from the stream.
|
||||
*
|
||||
* @return Promise Resolves with a string when new data is available or `null` if the stream has closed.
|
||||
*
|
||||
* @psalm-return Promise<string|null>
|
||||
*
|
||||
* @throws PendingReadError Thrown if another read operation is still pending.
|
||||
*/
|
||||
public function read() : Promise;
|
||||
}
|
@ -1,44 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace WP_Ultimo\Dependencies\Amp\ByteStream;
|
||||
|
||||
use WP_Ultimo\Dependencies\Amp\Promise;
|
||||
use WP_Ultimo\Dependencies\Amp\Success;
|
||||
use function WP_Ultimo\Dependencies\Amp\call;
|
||||
final class InputStreamChain implements InputStream
|
||||
{
|
||||
/** @var InputStream[] */
|
||||
private $streams;
|
||||
/** @var bool */
|
||||
private $reading = \false;
|
||||
public function __construct(InputStream ...$streams)
|
||||
{
|
||||
$this->streams = $streams;
|
||||
}
|
||||
/** @inheritDoc */
|
||||
public function read() : Promise
|
||||
{
|
||||
if ($this->reading) {
|
||||
throw new PendingReadError();
|
||||
}
|
||||
if (!$this->streams) {
|
||||
return new Success(null);
|
||||
}
|
||||
return call(function () {
|
||||
$this->reading = \true;
|
||||
try {
|
||||
while ($this->streams) {
|
||||
$chunk = (yield $this->streams[0]->read());
|
||||
if ($chunk === null) {
|
||||
\array_shift($this->streams);
|
||||
continue;
|
||||
}
|
||||
return $chunk;
|
||||
}
|
||||
return null;
|
||||
} finally {
|
||||
$this->reading = \false;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
@ -1,55 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace WP_Ultimo\Dependencies\Amp\ByteStream;
|
||||
|
||||
use WP_Ultimo\Dependencies\Amp\Deferred;
|
||||
use WP_Ultimo\Dependencies\Amp\Failure;
|
||||
use WP_Ultimo\Dependencies\Amp\Iterator;
|
||||
use WP_Ultimo\Dependencies\Amp\Promise;
|
||||
final class IteratorStream implements InputStream
|
||||
{
|
||||
/** @var Iterator<string> */
|
||||
private $iterator;
|
||||
/** @var \Throwable|null */
|
||||
private $exception;
|
||||
/** @var bool */
|
||||
private $pending = \false;
|
||||
/**
|
||||
* @psam-param Iterator<string> $iterator
|
||||
*/
|
||||
public function __construct(Iterator $iterator)
|
||||
{
|
||||
$this->iterator = $iterator;
|
||||
}
|
||||
/** @inheritdoc */
|
||||
public function read() : Promise
|
||||
{
|
||||
if ($this->exception) {
|
||||
return new Failure($this->exception);
|
||||
}
|
||||
if ($this->pending) {
|
||||
throw new PendingReadError();
|
||||
}
|
||||
$this->pending = \true;
|
||||
/** @var Deferred<string|null> $deferred */
|
||||
$deferred = new Deferred();
|
||||
$this->iterator->advance()->onResolve(function ($error, $hasNextElement) use($deferred) {
|
||||
$this->pending = \false;
|
||||
if ($error) {
|
||||
$this->exception = $error;
|
||||
$deferred->fail($error);
|
||||
} elseif ($hasNextElement) {
|
||||
$chunk = $this->iterator->getCurrent();
|
||||
if (!\is_string($chunk)) {
|
||||
$this->exception = new StreamException(\sprintf("Unexpected iterator value of type '%s', expected string", \is_object($chunk) ? \get_class($chunk) : \gettype($chunk)));
|
||||
$deferred->fail($this->exception);
|
||||
return;
|
||||
}
|
||||
$deferred->resolve($chunk);
|
||||
} else {
|
||||
$deferred->resolve();
|
||||
}
|
||||
});
|
||||
return $deferred->promise();
|
||||
}
|
||||
}
|
@ -1,59 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace WP_Ultimo\Dependencies\Amp\ByteStream;
|
||||
|
||||
use WP_Ultimo\Dependencies\Amp\Promise;
|
||||
use function WP_Ultimo\Dependencies\Amp\call;
|
||||
final class LineReader
|
||||
{
|
||||
/** @var string */
|
||||
private $delimiter;
|
||||
/** @var bool */
|
||||
private $lineMode;
|
||||
/** @var string */
|
||||
private $buffer = "";
|
||||
/** @var InputStream */
|
||||
private $source;
|
||||
public function __construct(InputStream $inputStream, string $delimiter = null)
|
||||
{
|
||||
$this->source = $inputStream;
|
||||
$this->delimiter = $delimiter === null ? "\n" : $delimiter;
|
||||
$this->lineMode = $delimiter === null;
|
||||
}
|
||||
/**
|
||||
* @return Promise<string|null>
|
||||
*/
|
||||
public function readLine() : Promise
|
||||
{
|
||||
return call(function () {
|
||||
if (\false !== \strpos($this->buffer, $this->delimiter)) {
|
||||
list($line, $this->buffer) = \explode($this->delimiter, $this->buffer, 2);
|
||||
return $this->lineMode ? \rtrim($line, "\r") : $line;
|
||||
}
|
||||
while (null !== ($chunk = (yield $this->source->read()))) {
|
||||
$this->buffer .= $chunk;
|
||||
if (\false !== \strpos($this->buffer, $this->delimiter)) {
|
||||
list($line, $this->buffer) = \explode($this->delimiter, $this->buffer, 2);
|
||||
return $this->lineMode ? \rtrim($line, "\r") : $line;
|
||||
}
|
||||
}
|
||||
if ($this->buffer === "") {
|
||||
return null;
|
||||
}
|
||||
$line = $this->buffer;
|
||||
$this->buffer = "";
|
||||
return $this->lineMode ? \rtrim($line, "\r") : $line;
|
||||
});
|
||||
}
|
||||
public function getBuffer() : string
|
||||
{
|
||||
return $this->buffer;
|
||||
}
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function clearBuffer()
|
||||
{
|
||||
$this->buffer = "";
|
||||
}
|
||||
}
|
151
dependencies/amphp/byte-stream/lib/Message.php
vendored
151
dependencies/amphp/byte-stream/lib/Message.php
vendored
@ -1,151 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace WP_Ultimo\Dependencies\Amp\ByteStream;
|
||||
|
||||
use WP_Ultimo\Dependencies\Amp\Coroutine;
|
||||
use WP_Ultimo\Dependencies\Amp\Deferred;
|
||||
use WP_Ultimo\Dependencies\Amp\Failure;
|
||||
use WP_Ultimo\Dependencies\Amp\Promise;
|
||||
use WP_Ultimo\Dependencies\Amp\Success;
|
||||
/**
|
||||
* Creates a buffered message from an InputStream. The message can be consumed in chunks using the read() API or it may
|
||||
* be buffered and accessed in its entirety by waiting for the promise to resolve.
|
||||
*
|
||||
* Other implementations may extend this class to add custom properties such as a `isBinary()` flag for WebSocket
|
||||
* messages.
|
||||
*
|
||||
* Buffering Example:
|
||||
*
|
||||
* $stream = new Message($inputStream);
|
||||
* $content = yield $stream;
|
||||
*
|
||||
* Streaming Example:
|
||||
*
|
||||
* $stream = new Message($inputStream);
|
||||
*
|
||||
* while (($chunk = yield $stream->read()) !== null) {
|
||||
* // Immediately use $chunk, reducing memory consumption since the entire message is never buffered.
|
||||
* }
|
||||
*
|
||||
* @deprecated Use Amp\ByteStream\Payload instead.
|
||||
*/
|
||||
class Message implements InputStream, Promise
|
||||
{
|
||||
/** @var InputStream */
|
||||
private $source;
|
||||
/** @var string */
|
||||
private $buffer = "";
|
||||
/** @var Deferred|null */
|
||||
private $pendingRead;
|
||||
/** @var Coroutine|null */
|
||||
private $coroutine;
|
||||
/** @var bool True if onResolve() has been called. */
|
||||
private $buffering = \false;
|
||||
/** @var Deferred|null */
|
||||
private $backpressure;
|
||||
/** @var bool True if the iterator has completed. */
|
||||
private $complete = \false;
|
||||
/** @var \Throwable|null Used to fail future reads on failure. */
|
||||
private $error;
|
||||
/**
|
||||
* @param InputStream $source An iterator that only emits strings.
|
||||
*/
|
||||
public function __construct(InputStream $source)
|
||||
{
|
||||
$this->source = $source;
|
||||
}
|
||||
private function consume() : \Generator
|
||||
{
|
||||
while (($chunk = (yield $this->source->read())) !== null) {
|
||||
$buffer = $this->buffer .= $chunk;
|
||||
if ($buffer === "") {
|
||||
continue;
|
||||
// Do not succeed reads with empty string.
|
||||
} elseif ($this->pendingRead) {
|
||||
$deferred = $this->pendingRead;
|
||||
$this->pendingRead = null;
|
||||
$this->buffer = "";
|
||||
$deferred->resolve($buffer);
|
||||
$buffer = "";
|
||||
// Destroy last emitted chunk to free memory.
|
||||
} elseif (!$this->buffering) {
|
||||
$buffer = "";
|
||||
// Destroy last emitted chunk to free memory.
|
||||
$this->backpressure = new Deferred();
|
||||
(yield $this->backpressure->promise());
|
||||
}
|
||||
}
|
||||
$this->complete = \true;
|
||||
if ($this->pendingRead) {
|
||||
$deferred = $this->pendingRead;
|
||||
$this->pendingRead = null;
|
||||
$deferred->resolve($this->buffer !== "" ? $this->buffer : null);
|
||||
$this->buffer = "";
|
||||
}
|
||||
return $this->buffer;
|
||||
}
|
||||
/** @inheritdoc */
|
||||
public final function read() : Promise
|
||||
{
|
||||
if ($this->pendingRead) {
|
||||
throw new PendingReadError();
|
||||
}
|
||||
if ($this->coroutine === null) {
|
||||
$this->coroutine = new Coroutine($this->consume());
|
||||
$this->coroutine->onResolve(function ($error) {
|
||||
if ($error) {
|
||||
$this->error = $error;
|
||||
}
|
||||
if ($this->pendingRead) {
|
||||
$deferred = $this->pendingRead;
|
||||
$this->pendingRead = null;
|
||||
$deferred->fail($error);
|
||||
}
|
||||
});
|
||||
}
|
||||
if ($this->error) {
|
||||
return new Failure($this->error);
|
||||
}
|
||||
if ($this->buffer !== "") {
|
||||
$buffer = $this->buffer;
|
||||
$this->buffer = "";
|
||||
if ($this->backpressure) {
|
||||
$backpressure = $this->backpressure;
|
||||
$this->backpressure = null;
|
||||
$backpressure->resolve();
|
||||
}
|
||||
return new Success($buffer);
|
||||
}
|
||||
if ($this->complete) {
|
||||
return new Success();
|
||||
}
|
||||
$this->pendingRead = new Deferred();
|
||||
return $this->pendingRead->promise();
|
||||
}
|
||||
/** @inheritdoc */
|
||||
public final function onResolve(callable $onResolved)
|
||||
{
|
||||
$this->buffering = \true;
|
||||
if ($this->coroutine === null) {
|
||||
$this->coroutine = new Coroutine($this->consume());
|
||||
}
|
||||
if ($this->backpressure) {
|
||||
$backpressure = $this->backpressure;
|
||||
$this->backpressure = null;
|
||||
$backpressure->resolve();
|
||||
}
|
||||
$this->coroutine->onResolve($onResolved);
|
||||
}
|
||||
/**
|
||||
* Exposes the source input stream.
|
||||
*
|
||||
* This might be required to resolve a promise with an InputStream, because promises in Amp can't be resolved with
|
||||
* other promises.
|
||||
*
|
||||
* @return InputStream
|
||||
*/
|
||||
public final function getInputStream() : InputStream
|
||||
{
|
||||
return $this->source;
|
||||
}
|
||||
}
|
@ -1,43 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace WP_Ultimo\Dependencies\Amp\ByteStream;
|
||||
|
||||
use WP_Ultimo\Dependencies\Amp\Deferred;
|
||||
use WP_Ultimo\Dependencies\Amp\Promise;
|
||||
use WP_Ultimo\Dependencies\Amp\Success;
|
||||
class OutputBuffer implements OutputStream, Promise
|
||||
{
|
||||
/** @var Deferred */
|
||||
private $deferred;
|
||||
/** @var string */
|
||||
private $contents = '';
|
||||
/** @var bool */
|
||||
private $closed = \false;
|
||||
public function __construct()
|
||||
{
|
||||
$this->deferred = new Deferred();
|
||||
}
|
||||
public function write(string $data) : Promise
|
||||
{
|
||||
if ($this->closed) {
|
||||
throw new ClosedException("The stream has already been closed.");
|
||||
}
|
||||
$this->contents .= $data;
|
||||
return new Success(\strlen($data));
|
||||
}
|
||||
public function end(string $finalData = "") : Promise
|
||||
{
|
||||
if ($this->closed) {
|
||||
throw new ClosedException("The stream has already been closed.");
|
||||
}
|
||||
$this->contents .= $finalData;
|
||||
$this->closed = \true;
|
||||
$this->deferred->resolve($this->contents);
|
||||
$this->contents = "";
|
||||
return new Success(\strlen($finalData));
|
||||
}
|
||||
public function onResolve(callable $onResolved)
|
||||
{
|
||||
$this->deferred->promise()->onResolve($onResolved);
|
||||
}
|
||||
}
|
@ -1,35 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace WP_Ultimo\Dependencies\Amp\ByteStream;
|
||||
|
||||
use WP_Ultimo\Dependencies\Amp\Promise;
|
||||
/**
|
||||
* An `OutputStream` allows writing data in chunks. Writers can wait on the returned promises to feel the backpressure.
|
||||
*/
|
||||
interface OutputStream
|
||||
{
|
||||
/**
|
||||
* Writes data to the stream.
|
||||
*
|
||||
* @param string $data Bytes to write.
|
||||
*
|
||||
* @return Promise Succeeds once the data has been successfully written to the stream.
|
||||
*
|
||||
* @throws ClosedException If the stream has already been closed.
|
||||
* @throws StreamException If writing to the stream fails.
|
||||
*/
|
||||
public function write(string $data) : Promise;
|
||||
/**
|
||||
* Marks the stream as no longer writable. Optionally writes a final data chunk before. Note that this is not the
|
||||
* same as forcefully closing the stream. This method waits for all pending writes to complete before closing the
|
||||
* stream. Socket streams implementing this interface should only close the writable side of the stream.
|
||||
*
|
||||
* @param string $finalData Bytes to write.
|
||||
*
|
||||
* @return Promise Succeeds once the data has been successfully written to the stream.
|
||||
*
|
||||
* @throws ClosedException If the stream has already been closed.
|
||||
* @throws StreamException If writing to the stream fails.
|
||||
*/
|
||||
public function end(string $finalData = "") : Promise;
|
||||
}
|
80
dependencies/amphp/byte-stream/lib/Payload.php
vendored
80
dependencies/amphp/byte-stream/lib/Payload.php
vendored
@ -1,80 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace WP_Ultimo\Dependencies\Amp\ByteStream;
|
||||
|
||||
use WP_Ultimo\Dependencies\Amp\Coroutine;
|
||||
use WP_Ultimo\Dependencies\Amp\Promise;
|
||||
use function WP_Ultimo\Dependencies\Amp\call;
|
||||
/**
|
||||
* Creates a buffered message from an InputStream. The message can be consumed in chunks using the read() API or it may
|
||||
* be buffered and accessed in its entirety by calling buffer(). Once buffering is requested through buffer(), the
|
||||
* stream cannot be read in chunks. On destruct any remaining data is read from the InputStream given to this class.
|
||||
*/
|
||||
class Payload implements InputStream
|
||||
{
|
||||
/** @var InputStream */
|
||||
private $stream;
|
||||
/** @var \Amp\Promise|null */
|
||||
private $promise;
|
||||
/** @var \Amp\Promise|null */
|
||||
private $lastRead;
|
||||
/**
|
||||
* @param \Amp\ByteStream\InputStream $stream
|
||||
*/
|
||||
public function __construct(InputStream $stream)
|
||||
{
|
||||
$this->stream = $stream;
|
||||
}
|
||||
public function __destruct()
|
||||
{
|
||||
if (!$this->promise) {
|
||||
Promise\rethrow(new Coroutine($this->consume()));
|
||||
}
|
||||
}
|
||||
private function consume() : \Generator
|
||||
{
|
||||
try {
|
||||
if ($this->lastRead && null === (yield $this->lastRead)) {
|
||||
return;
|
||||
}
|
||||
while (null !== (yield $this->stream->read())) {
|
||||
// Discard unread bytes from message.
|
||||
}
|
||||
} catch (\Throwable $exception) {
|
||||
// If exception is thrown here the connection closed anyway.
|
||||
}
|
||||
}
|
||||
/**
|
||||
* @inheritdoc
|
||||
*
|
||||
* @throws \Error If a buffered message was requested by calling buffer().
|
||||
*/
|
||||
public final function read() : Promise
|
||||
{
|
||||
if ($this->promise) {
|
||||
throw new \Error("Cannot stream message data once a buffered message has been requested");
|
||||
}
|
||||
return $this->lastRead = $this->stream->read();
|
||||
}
|
||||
/**
|
||||
* Buffers the entire message and resolves the returned promise then.
|
||||
*
|
||||
* @return Promise<string> Resolves with the entire message contents.
|
||||
*/
|
||||
public final function buffer() : Promise
|
||||
{
|
||||
if ($this->promise) {
|
||||
return $this->promise;
|
||||
}
|
||||
return $this->promise = call(function () {
|
||||
$buffer = '';
|
||||
if ($this->lastRead && null === (yield $this->lastRead)) {
|
||||
return $buffer;
|
||||
}
|
||||
while (null !== ($chunk = (yield $this->stream->read()))) {
|
||||
$buffer .= $chunk;
|
||||
}
|
||||
return $buffer;
|
||||
});
|
||||
}
|
||||
}
|
@ -1,14 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace WP_Ultimo\Dependencies\Amp\ByteStream;
|
||||
|
||||
/**
|
||||
* Thrown in case a second read operation is attempted while another read operation is still pending.
|
||||
*/
|
||||
final class PendingReadError extends \Error
|
||||
{
|
||||
public function __construct(string $message = "The previous read operation must complete before read can be called again", int $code = 0, \Throwable $previous = null)
|
||||
{
|
||||
parent::__construct($message, $code, $previous);
|
||||
}
|
||||
}
|
@ -1,211 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace WP_Ultimo\Dependencies\Amp\ByteStream;
|
||||
|
||||
use WP_Ultimo\Dependencies\Amp\Deferred;
|
||||
use WP_Ultimo\Dependencies\Amp\Loop;
|
||||
use WP_Ultimo\Dependencies\Amp\Promise;
|
||||
use WP_Ultimo\Dependencies\Amp\Success;
|
||||
/**
|
||||
* Input stream abstraction for PHP's stream resources.
|
||||
*/
|
||||
final class ResourceInputStream implements InputStream
|
||||
{
|
||||
const DEFAULT_CHUNK_SIZE = 8192;
|
||||
/** @var resource|null */
|
||||
private $resource;
|
||||
/** @var string */
|
||||
private $watcher;
|
||||
/** @var Deferred|null */
|
||||
private $deferred;
|
||||
/** @var bool */
|
||||
private $readable = \true;
|
||||
/** @var int */
|
||||
private $chunkSize;
|
||||
/** @var bool */
|
||||
private $useSingleRead;
|
||||
/** @var callable */
|
||||
private $immediateCallable;
|
||||
/** @var string|null */
|
||||
private $immediateWatcher;
|
||||
/**
|
||||
* @param resource $stream Stream resource.
|
||||
* @param int $chunkSize Chunk size per read operation.
|
||||
*
|
||||
* @throws \Error If an invalid stream or parameter has been passed.
|
||||
*/
|
||||
public function __construct($stream, int $chunkSize = self::DEFAULT_CHUNK_SIZE)
|
||||
{
|
||||
if (!\is_resource($stream) || \get_resource_type($stream) !== 'stream') {
|
||||
throw new \Error("Expected a valid stream");
|
||||
}
|
||||
$meta = \stream_get_meta_data($stream);
|
||||
$useSingleRead = $meta["stream_type"] === "udp_socket" || $meta["stream_type"] === "STDIO";
|
||||
$this->useSingleRead = $useSingleRead;
|
||||
if (\strpos($meta["mode"], "r") === \false && \strpos($meta["mode"], "+") === \false) {
|
||||
throw new \Error("Expected a readable stream");
|
||||
}
|
||||
\stream_set_blocking($stream, \false);
|
||||
\stream_set_read_buffer($stream, 0);
|
||||
$this->resource =& $stream;
|
||||
$this->chunkSize =& $chunkSize;
|
||||
$deferred =& $this->deferred;
|
||||
$readable =& $this->readable;
|
||||
$this->watcher = Loop::onReadable($this->resource, static function ($watcher) use(&$deferred, &$readable, &$stream, &$chunkSize, $useSingleRead) {
|
||||
if ($useSingleRead) {
|
||||
$data = @\fread($stream, $chunkSize);
|
||||
} else {
|
||||
$data = @\stream_get_contents($stream, $chunkSize);
|
||||
}
|
||||
\assert($data !== \false, "Trying to read from a previously fclose()'d resource. Do NOT manually fclose() resources the loop still has a reference to.");
|
||||
// Error suppression, because pthreads does crazy things with resources,
|
||||
// which might be closed during two operations.
|
||||
// See https://github.com/amphp/byte-stream/issues/32
|
||||
if ($data === '' && @\feof($stream)) {
|
||||
$readable = \false;
|
||||
$stream = null;
|
||||
$data = null;
|
||||
// Stream closed, resolve read with null.
|
||||
Loop::cancel($watcher);
|
||||
} else {
|
||||
Loop::disable($watcher);
|
||||
}
|
||||
$temp = $deferred;
|
||||
$deferred = null;
|
||||
\assert($temp instanceof Deferred);
|
||||
$temp->resolve($data);
|
||||
});
|
||||
$this->immediateCallable = static function ($watcherId, $data) use(&$deferred) {
|
||||
$temp = $deferred;
|
||||
$deferred = null;
|
||||
\assert($temp instanceof Deferred);
|
||||
$temp->resolve($data);
|
||||
};
|
||||
Loop::disable($this->watcher);
|
||||
}
|
||||
/** @inheritdoc */
|
||||
public function read() : Promise
|
||||
{
|
||||
if ($this->deferred !== null) {
|
||||
throw new PendingReadError();
|
||||
}
|
||||
if (!$this->readable) {
|
||||
return new Success();
|
||||
// Resolve with null on closed stream.
|
||||
}
|
||||
\assert($this->resource !== null);
|
||||
// Attempt a direct read, because Windows suffers from slow I/O on STDIN otherwise.
|
||||
if ($this->useSingleRead) {
|
||||
$data = @\fread($this->resource, $this->chunkSize);
|
||||
} else {
|
||||
$data = @\stream_get_contents($this->resource, $this->chunkSize);
|
||||
}
|
||||
\assert($data !== \false, "Trying to read from a previously fclose()'d resource. Do NOT manually fclose() resources the loop still has a reference to.");
|
||||
if ($data === '') {
|
||||
// Error suppression, because pthreads does crazy things with resources,
|
||||
// which might be closed during two operations.
|
||||
// See https://github.com/amphp/byte-stream/issues/32
|
||||
if (@\feof($this->resource)) {
|
||||
$this->readable = \false;
|
||||
$this->resource = null;
|
||||
Loop::cancel($this->watcher);
|
||||
return new Success();
|
||||
// Stream closed, resolve read with null.
|
||||
}
|
||||
$this->deferred = new Deferred();
|
||||
Loop::enable($this->watcher);
|
||||
return $this->deferred->promise();
|
||||
}
|
||||
// Prevent an immediate read → write loop from blocking everything
|
||||
// See e.g. examples/benchmark-throughput.php
|
||||
$this->deferred = new Deferred();
|
||||
$this->immediateWatcher = Loop::defer($this->immediateCallable, $data);
|
||||
return $this->deferred->promise();
|
||||
}
|
||||
/**
|
||||
* Closes the stream forcefully. Multiple `close()` calls are ignored.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function close()
|
||||
{
|
||||
if (\is_resource($this->resource)) {
|
||||
// Error suppression, as resource might already be closed
|
||||
$meta = @\stream_get_meta_data($this->resource);
|
||||
if ($meta && \strpos($meta["mode"], "+") !== \false) {
|
||||
@\stream_socket_shutdown($this->resource, \STREAM_SHUT_RD);
|
||||
} else {
|
||||
/** @psalm-suppress InvalidPropertyAssignmentValue */
|
||||
@\fclose($this->resource);
|
||||
}
|
||||
}
|
||||
$this->free();
|
||||
}
|
||||
/**
|
||||
* Nulls reference to resource, marks stream unreadable, and succeeds any pending read with null.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function free()
|
||||
{
|
||||
$this->readable = \false;
|
||||
$this->resource = null;
|
||||
if ($this->deferred !== null) {
|
||||
$deferred = $this->deferred;
|
||||
$this->deferred = null;
|
||||
$deferred->resolve();
|
||||
}
|
||||
Loop::cancel($this->watcher);
|
||||
if ($this->immediateWatcher !== null) {
|
||||
Loop::cancel($this->immediateWatcher);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* @return resource|null The stream resource or null if the stream has closed.
|
||||
*/
|
||||
public function getResource()
|
||||
{
|
||||
return $this->resource;
|
||||
}
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function setChunkSize(int $chunkSize)
|
||||
{
|
||||
$this->chunkSize = $chunkSize;
|
||||
}
|
||||
/**
|
||||
* References the read watcher, so the loop keeps running in case there's an active read.
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @see Loop::reference()
|
||||
*/
|
||||
public function reference()
|
||||
{
|
||||
if (!$this->resource) {
|
||||
throw new \Error("Resource has already been freed");
|
||||
}
|
||||
Loop::reference($this->watcher);
|
||||
}
|
||||
/**
|
||||
* Unreferences the read watcher, so the loop doesn't keep running even if there are active reads.
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @see Loop::unreference()
|
||||
*/
|
||||
public function unreference()
|
||||
{
|
||||
if (!$this->resource) {
|
||||
throw new \Error("Resource has already been freed");
|
||||
}
|
||||
Loop::unreference($this->watcher);
|
||||
}
|
||||
public function __destruct()
|
||||
{
|
||||
if ($this->resource !== null) {
|
||||
$this->free();
|
||||
}
|
||||
}
|
||||
}
|
@ -1,270 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace WP_Ultimo\Dependencies\Amp\ByteStream;
|
||||
|
||||
use WP_Ultimo\Dependencies\Amp\Deferred;
|
||||
use WP_Ultimo\Dependencies\Amp\Failure;
|
||||
use WP_Ultimo\Dependencies\Amp\Loop;
|
||||
use WP_Ultimo\Dependencies\Amp\Promise;
|
||||
use WP_Ultimo\Dependencies\Amp\Success;
|
||||
/**
|
||||
* Output stream abstraction for PHP's stream resources.
|
||||
*/
|
||||
final class ResourceOutputStream implements OutputStream
|
||||
{
|
||||
const MAX_CONSECUTIVE_EMPTY_WRITES = 3;
|
||||
const LARGE_CHUNK_SIZE = 128 * 1024;
|
||||
/** @var resource|null */
|
||||
private $resource;
|
||||
/** @var string */
|
||||
private $watcher;
|
||||
/** @var \SplQueue<array> */
|
||||
private $writes;
|
||||
/** @var bool */
|
||||
private $writable = \true;
|
||||
/** @var int|null */
|
||||
private $chunkSize;
|
||||
/**
|
||||
* @param resource $stream Stream resource.
|
||||
* @param int|null $chunkSize Chunk size per `fwrite()` operation.
|
||||
*/
|
||||
public function __construct($stream, int $chunkSize = null)
|
||||
{
|
||||
if (!\is_resource($stream) || \get_resource_type($stream) !== 'stream') {
|
||||
throw new \Error("Expected a valid stream");
|
||||
}
|
||||
$meta = \stream_get_meta_data($stream);
|
||||
if (\strpos($meta["mode"], "r") !== \false && \strpos($meta["mode"], "+") === \false) {
|
||||
throw new \Error("Expected a writable stream");
|
||||
}
|
||||
\stream_set_blocking($stream, \false);
|
||||
\stream_set_write_buffer($stream, 0);
|
||||
$this->resource = $stream;
|
||||
$this->chunkSize =& $chunkSize;
|
||||
$writes = $this->writes = new \SplQueue();
|
||||
$writable =& $this->writable;
|
||||
$resource =& $this->resource;
|
||||
$this->watcher = Loop::onWritable($stream, static function ($watcher, $stream) use($writes, &$chunkSize, &$writable, &$resource) {
|
||||
static $emptyWrites = 0;
|
||||
try {
|
||||
while (!$writes->isEmpty()) {
|
||||
/** @var Deferred $deferred */
|
||||
list($data, $previous, $deferred) = $writes->shift();
|
||||
$length = \strlen($data);
|
||||
if ($length === 0) {
|
||||
$deferred->resolve(0);
|
||||
continue;
|
||||
}
|
||||
if (!\is_resource($stream) || ($metaData = @\stream_get_meta_data($stream)) && $metaData['eof']) {
|
||||
throw new ClosedException("The stream was closed by the peer");
|
||||
}
|
||||
// Error reporting suppressed since fwrite() emits E_WARNING if the pipe is broken or the buffer is full.
|
||||
// Use conditional, because PHP doesn't like getting null passed
|
||||
if ($chunkSize) {
|
||||
$written = @\fwrite($stream, $data, $chunkSize);
|
||||
} else {
|
||||
$written = @\fwrite($stream, $data);
|
||||
}
|
||||
\assert(
|
||||
$written !== \false || \PHP_VERSION_ID >= 70400,
|
||||
// PHP 7.4+ returns false on EPIPE.
|
||||
"Trying to write on a previously fclose()'d resource. Do NOT manually fclose() resources the still referenced in the loop."
|
||||
);
|
||||
// PHP 7.4.0 and 7.4.1 may return false on EAGAIN.
|
||||
if ($written === \false && \PHP_VERSION_ID >= 70402) {
|
||||
$message = "Failed to write to stream";
|
||||
if ($error = \error_get_last()) {
|
||||
$message .= \sprintf("; %s", $error["message"]);
|
||||
}
|
||||
throw new StreamException($message);
|
||||
}
|
||||
// Broken pipes between processes on macOS/FreeBSD do not detect EOF properly.
|
||||
if ($written === 0 || $written === \false) {
|
||||
if ($emptyWrites++ > self::MAX_CONSECUTIVE_EMPTY_WRITES) {
|
||||
$message = "Failed to write to stream after multiple attempts";
|
||||
if ($error = \error_get_last()) {
|
||||
$message .= \sprintf("; %s", $error["message"]);
|
||||
}
|
||||
throw new StreamException($message);
|
||||
}
|
||||
$writes->unshift([$data, $previous, $deferred]);
|
||||
return;
|
||||
}
|
||||
$emptyWrites = 0;
|
||||
if ($length > $written) {
|
||||
$data = \substr($data, $written);
|
||||
$writes->unshift([$data, $written + $previous, $deferred]);
|
||||
return;
|
||||
}
|
||||
$deferred->resolve($written + $previous);
|
||||
}
|
||||
} catch (\Throwable $exception) {
|
||||
$resource = null;
|
||||
$writable = \false;
|
||||
/** @psalm-suppress PossiblyUndefinedVariable */
|
||||
$deferred->fail($exception);
|
||||
while (!$writes->isEmpty()) {
|
||||
list(, , $deferred) = $writes->shift();
|
||||
$deferred->fail($exception);
|
||||
}
|
||||
Loop::cancel($watcher);
|
||||
} finally {
|
||||
if ($writes->isEmpty()) {
|
||||
Loop::disable($watcher);
|
||||
}
|
||||
}
|
||||
});
|
||||
Loop::disable($this->watcher);
|
||||
}
|
||||
/**
|
||||
* Writes data to the stream.
|
||||
*
|
||||
* @param string $data Bytes to write.
|
||||
*
|
||||
* @return Promise Succeeds once the data has been successfully written to the stream.
|
||||
*
|
||||
* @throws ClosedException If the stream has already been closed.
|
||||
*/
|
||||
public function write(string $data) : Promise
|
||||
{
|
||||
return $this->send($data, \false);
|
||||
}
|
||||
/**
|
||||
* Closes the stream after all pending writes have been completed. Optionally writes a final data chunk before.
|
||||
*
|
||||
* @param string $finalData Bytes to write.
|
||||
*
|
||||
* @return Promise Succeeds once the data has been successfully written to the stream.
|
||||
*
|
||||
* @throws ClosedException If the stream has already been closed.
|
||||
*/
|
||||
public function end(string $finalData = "") : Promise
|
||||
{
|
||||
return $this->send($finalData, \true);
|
||||
}
|
||||
private function send(string $data, bool $end = \false) : Promise
|
||||
{
|
||||
if (!$this->writable) {
|
||||
return new Failure(new ClosedException("The stream is not writable"));
|
||||
}
|
||||
$length = \strlen($data);
|
||||
$written = 0;
|
||||
if ($end) {
|
||||
$this->writable = \false;
|
||||
}
|
||||
if ($this->writes->isEmpty()) {
|
||||
if ($length === 0) {
|
||||
if ($end) {
|
||||
$this->close();
|
||||
}
|
||||
return new Success(0);
|
||||
}
|
||||
if (!\is_resource($this->resource) || ($metaData = @\stream_get_meta_data($this->resource)) && $metaData['eof']) {
|
||||
return new Failure(new ClosedException("The stream was closed by the peer"));
|
||||
}
|
||||
// Error reporting suppressed since fwrite() emits E_WARNING if the pipe is broken or the buffer is full.
|
||||
// Use conditional, because PHP doesn't like getting null passed.
|
||||
if ($this->chunkSize) {
|
||||
$written = @\fwrite($this->resource, $data, $this->chunkSize);
|
||||
} else {
|
||||
$written = @\fwrite($this->resource, $data);
|
||||
}
|
||||
\assert(
|
||||
$written !== \false || \PHP_VERSION_ID >= 70400,
|
||||
// PHP 7.4+ returns false on EPIPE.
|
||||
"Trying to write on a previously fclose()'d resource. Do NOT manually fclose() resources the still referenced in the loop."
|
||||
);
|
||||
// PHP 7.4.0 and 7.4.1 may return false on EAGAIN.
|
||||
if ($written === \false && \PHP_VERSION_ID >= 70402) {
|
||||
$message = "Failed to write to stream";
|
||||
if ($error = \error_get_last()) {
|
||||
$message .= \sprintf("; %s", $error["message"]);
|
||||
}
|
||||
return new Failure(new StreamException($message));
|
||||
}
|
||||
$written = (int) $written;
|
||||
// Cast potential false to 0.
|
||||
if ($length === $written) {
|
||||
if ($end) {
|
||||
$this->close();
|
||||
}
|
||||
return new Success($written);
|
||||
}
|
||||
$data = \substr($data, $written);
|
||||
}
|
||||
$deferred = new Deferred();
|
||||
if ($length - $written > self::LARGE_CHUNK_SIZE) {
|
||||
$chunks = \str_split($data, self::LARGE_CHUNK_SIZE);
|
||||
$data = \array_pop($chunks);
|
||||
foreach ($chunks as $chunk) {
|
||||
$this->writes->push([$chunk, $written, new Deferred()]);
|
||||
$written += self::LARGE_CHUNK_SIZE;
|
||||
}
|
||||
}
|
||||
$this->writes->push([$data, $written, $deferred]);
|
||||
Loop::enable($this->watcher);
|
||||
$promise = $deferred->promise();
|
||||
if ($end) {
|
||||
$promise->onResolve([$this, "close"]);
|
||||
}
|
||||
return $promise;
|
||||
}
|
||||
/**
|
||||
* Closes the stream forcefully. Multiple `close()` calls are ignored.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function close()
|
||||
{
|
||||
if (\is_resource($this->resource)) {
|
||||
// Error suppression, as resource might already be closed
|
||||
$meta = @\stream_get_meta_data($this->resource);
|
||||
if ($meta && \strpos($meta["mode"], "+") !== \false) {
|
||||
@\stream_socket_shutdown($this->resource, \STREAM_SHUT_WR);
|
||||
} else {
|
||||
/** @psalm-suppress InvalidPropertyAssignmentValue psalm reports this as closed-resource */
|
||||
@\fclose($this->resource);
|
||||
}
|
||||
}
|
||||
$this->free();
|
||||
}
|
||||
/**
|
||||
* Nulls reference to resource, marks stream unwritable, and fails any pending write.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function free()
|
||||
{
|
||||
$this->resource = null;
|
||||
$this->writable = \false;
|
||||
if (!$this->writes->isEmpty()) {
|
||||
$exception = new ClosedException("The socket was closed before writing completed");
|
||||
do {
|
||||
/** @var Deferred $deferred */
|
||||
list(, , $deferred) = $this->writes->shift();
|
||||
$deferred->fail($exception);
|
||||
} while (!$this->writes->isEmpty());
|
||||
}
|
||||
Loop::cancel($this->watcher);
|
||||
}
|
||||
/**
|
||||
* @return resource|null Stream resource or null if end() has been called or the stream closed.
|
||||
*/
|
||||
public function getResource()
|
||||
{
|
||||
return $this->resource;
|
||||
}
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function setChunkSize(int $chunkSize)
|
||||
{
|
||||
$this->chunkSize = $chunkSize;
|
||||
}
|
||||
public function __destruct()
|
||||
{
|
||||
if ($this->resource !== null) {
|
||||
$this->free();
|
||||
}
|
||||
}
|
||||
}
|
@ -1,7 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace WP_Ultimo\Dependencies\Amp\ByteStream;
|
||||
|
||||
class StreamException extends \Exception
|
||||
{
|
||||
}
|
@ -1,96 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace WP_Ultimo\Dependencies\Amp\ByteStream;
|
||||
|
||||
use WP_Ultimo\Dependencies\Amp\Promise;
|
||||
use function WP_Ultimo\Dependencies\Amp\call;
|
||||
/**
|
||||
* Allows decompression of input streams using Zlib.
|
||||
*/
|
||||
final class ZlibInputStream implements InputStream
|
||||
{
|
||||
/** @var InputStream|null */
|
||||
private $source;
|
||||
/** @var int */
|
||||
private $encoding;
|
||||
/** @var array */
|
||||
private $options;
|
||||
/** @var resource|null */
|
||||
private $resource;
|
||||
/**
|
||||
* @param InputStream $source Input stream to read compressed data from.
|
||||
* @param int $encoding Compression algorithm used, see `inflate_init()`.
|
||||
* @param array $options Algorithm options, see `inflate_init()`.
|
||||
*
|
||||
* @throws StreamException
|
||||
* @throws \Error
|
||||
*
|
||||
* @see http://php.net/manual/en/function.inflate-init.php
|
||||
*/
|
||||
public function __construct(InputStream $source, int $encoding, array $options = [])
|
||||
{
|
||||
$this->source = $source;
|
||||
$this->encoding = $encoding;
|
||||
$this->options = $options;
|
||||
$this->resource = @\inflate_init($encoding, $options);
|
||||
if ($this->resource === \false) {
|
||||
throw new StreamException("Failed initializing deflate context");
|
||||
}
|
||||
}
|
||||
/** @inheritdoc */
|
||||
public function read() : Promise
|
||||
{
|
||||
return call(function () {
|
||||
if ($this->resource === null) {
|
||||
return null;
|
||||
}
|
||||
\assert($this->source !== null);
|
||||
$data = (yield $this->source->read());
|
||||
// Needs a double guard, as stream might have been closed while reading
|
||||
/** @psalm-suppress ParadoxicalCondition */
|
||||
if ($this->resource === null) {
|
||||
return null;
|
||||
}
|
||||
if ($data === null) {
|
||||
$decompressed = @\inflate_add($this->resource, "", \ZLIB_FINISH);
|
||||
if ($decompressed === \false) {
|
||||
throw new StreamException("Failed adding data to deflate context");
|
||||
}
|
||||
$this->close();
|
||||
return $decompressed;
|
||||
}
|
||||
$decompressed = @\inflate_add($this->resource, $data, \ZLIB_SYNC_FLUSH);
|
||||
if ($decompressed === \false) {
|
||||
throw new StreamException("Failed adding data to deflate context");
|
||||
}
|
||||
return $decompressed;
|
||||
});
|
||||
}
|
||||
/**
|
||||
* @internal
|
||||
* @return void
|
||||
*/
|
||||
private function close()
|
||||
{
|
||||
$this->resource = null;
|
||||
$this->source = null;
|
||||
}
|
||||
/**
|
||||
* Gets the used compression encoding.
|
||||
*
|
||||
* @return int Encoding specified on construction time.
|
||||
*/
|
||||
public function getEncoding() : int
|
||||
{
|
||||
return $this->encoding;
|
||||
}
|
||||
/**
|
||||
* Gets the used compression options.
|
||||
*
|
||||
* @return array Options array passed on construction time.
|
||||
*/
|
||||
public function getOptions() : array
|
||||
{
|
||||
return $this->options;
|
||||
}
|
||||
}
|
@ -1,101 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace WP_Ultimo\Dependencies\Amp\ByteStream;
|
||||
|
||||
use WP_Ultimo\Dependencies\Amp\Promise;
|
||||
/**
|
||||
* Allows compression of output streams using Zlib.
|
||||
*/
|
||||
final class ZlibOutputStream implements OutputStream
|
||||
{
|
||||
/** @var OutputStream|null */
|
||||
private $destination;
|
||||
/** @var int */
|
||||
private $encoding;
|
||||
/** @var array */
|
||||
private $options;
|
||||
/** @var resource|null */
|
||||
private $resource;
|
||||
/**
|
||||
* @param OutputStream $destination Output stream to write the compressed data to.
|
||||
* @param int $encoding Compression encoding to use, see `deflate_init()`.
|
||||
* @param array $options Compression options to use, see `deflate_init()`.
|
||||
*
|
||||
* @throws StreamException If an invalid encoding or invalid options have been passed.
|
||||
*
|
||||
* @see http://php.net/manual/en/function.deflate-init.php
|
||||
*/
|
||||
public function __construct(OutputStream $destination, int $encoding, array $options = [])
|
||||
{
|
||||
$this->destination = $destination;
|
||||
$this->encoding = $encoding;
|
||||
$this->options = $options;
|
||||
$this->resource = @\deflate_init($encoding, $options);
|
||||
if ($this->resource === \false) {
|
||||
throw new StreamException("Failed initializing deflate context");
|
||||
}
|
||||
}
|
||||
/** @inheritdoc */
|
||||
public function write(string $data) : Promise
|
||||
{
|
||||
if ($this->resource === null) {
|
||||
throw new ClosedException("The stream has already been closed");
|
||||
}
|
||||
\assert($this->destination !== null);
|
||||
$compressed = \deflate_add($this->resource, $data, \ZLIB_SYNC_FLUSH);
|
||||
if ($compressed === \false) {
|
||||
throw new StreamException("Failed adding data to deflate context");
|
||||
}
|
||||
$promise = $this->destination->write($compressed);
|
||||
$promise->onResolve(function ($error) {
|
||||
if ($error) {
|
||||
$this->close();
|
||||
}
|
||||
});
|
||||
return $promise;
|
||||
}
|
||||
/** @inheritdoc */
|
||||
public function end(string $finalData = "") : Promise
|
||||
{
|
||||
if ($this->resource === null) {
|
||||
throw new ClosedException("The stream has already been closed");
|
||||
}
|
||||
\assert($this->destination !== null);
|
||||
$compressed = \deflate_add($this->resource, $finalData, \ZLIB_FINISH);
|
||||
if ($compressed === \false) {
|
||||
throw new StreamException("Failed adding data to deflate context");
|
||||
}
|
||||
$promise = $this->destination->end($compressed);
|
||||
$promise->onResolve(function () {
|
||||
$this->close();
|
||||
});
|
||||
return $promise;
|
||||
}
|
||||
/**
|
||||
* @internal
|
||||
* @return void
|
||||
*/
|
||||
private function close()
|
||||
{
|
||||
$this->resource = null;
|
||||
$this->destination = null;
|
||||
}
|
||||
/**
|
||||
* Gets the used compression encoding.
|
||||
*
|
||||
* @return int Encoding specified on construction time.
|
||||
*/
|
||||
public function getEncoding() : int
|
||||
{
|
||||
return $this->encoding;
|
||||
}
|
||||
/**
|
||||
* Gets the used compression options.
|
||||
*
|
||||
* @return array Options array passed on construction time.
|
||||
*/
|
||||
public function getOptions() : array
|
||||
{
|
||||
return $this->options;
|
||||
}
|
||||
}
|
154
dependencies/amphp/byte-stream/lib/functions.php
vendored
154
dependencies/amphp/byte-stream/lib/functions.php
vendored
@ -1,154 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace WP_Ultimo\Dependencies\Amp\ByteStream;
|
||||
|
||||
use WP_Ultimo\Dependencies\Amp\Iterator;
|
||||
use WP_Ultimo\Dependencies\Amp\Loop;
|
||||
use WP_Ultimo\Dependencies\Amp\Producer;
|
||||
use WP_Ultimo\Dependencies\Amp\Promise;
|
||||
use function WP_Ultimo\Dependencies\Amp\call;
|
||||
// @codeCoverageIgnoreStart
|
||||
if (\strlen('…') !== 3) {
|
||||
throw new \Error('The mbstring.func_overload ini setting is enabled. It must be disabled to use the stream package.');
|
||||
}
|
||||
// @codeCoverageIgnoreEnd
|
||||
if (!\defined('STDOUT')) {
|
||||
\define('STDOUT', \fopen('php://stdout', 'w'));
|
||||
}
|
||||
if (!\defined('STDERR')) {
|
||||
\define('STDERR', \fopen('php://stderr', 'w'));
|
||||
}
|
||||
/**
|
||||
* @param \Amp\ByteStream\InputStream $source
|
||||
* @param \Amp\ByteStream\OutputStream $destination
|
||||
*
|
||||
* @return \Amp\Promise
|
||||
*/
|
||||
function pipe(InputStream $source, OutputStream $destination) : Promise
|
||||
{
|
||||
return call(function () use($source, $destination) : \Generator {
|
||||
$written = 0;
|
||||
while (($chunk = (yield $source->read())) !== null) {
|
||||
$written += \strlen($chunk);
|
||||
$writePromise = $destination->write($chunk);
|
||||
$chunk = null;
|
||||
// free memory
|
||||
(yield $writePromise);
|
||||
}
|
||||
return $written;
|
||||
});
|
||||
}
|
||||
/**
|
||||
* @param \Amp\ByteStream\InputStream $source
|
||||
*
|
||||
* @return \Amp\Promise
|
||||
*/
|
||||
function buffer(InputStream $source) : Promise
|
||||
{
|
||||
return call(function () use($source) : \Generator {
|
||||
$buffer = "";
|
||||
while (($chunk = (yield $source->read())) !== null) {
|
||||
$buffer .= $chunk;
|
||||
$chunk = null;
|
||||
// free memory
|
||||
}
|
||||
return $buffer;
|
||||
});
|
||||
}
|
||||
/**
|
||||
* The php://input input buffer stream for the process associated with the currently active event loop.
|
||||
*
|
||||
* @return ResourceInputStream
|
||||
*/
|
||||
function getInputBufferStream() : ResourceInputStream
|
||||
{
|
||||
static $key = InputStream::class . '\\input';
|
||||
$stream = Loop::getState($key);
|
||||
if (!$stream) {
|
||||
$stream = new ResourceInputStream(\fopen('php://input', 'rb'));
|
||||
Loop::setState($key, $stream);
|
||||
}
|
||||
return $stream;
|
||||
}
|
||||
/**
|
||||
* The php://output output buffer stream for the process associated with the currently active event loop.
|
||||
*
|
||||
* @return ResourceOutputStream
|
||||
*/
|
||||
function getOutputBufferStream() : ResourceOutputStream
|
||||
{
|
||||
static $key = OutputStream::class . '\\output';
|
||||
$stream = Loop::getState($key);
|
||||
if (!$stream) {
|
||||
$stream = new ResourceOutputStream(\fopen('php://output', 'wb'));
|
||||
Loop::setState($key, $stream);
|
||||
}
|
||||
return $stream;
|
||||
}
|
||||
/**
|
||||
* The STDIN stream for the process associated with the currently active event loop.
|
||||
*
|
||||
* @return ResourceInputStream
|
||||
*/
|
||||
function getStdin() : ResourceInputStream
|
||||
{
|
||||
static $key = InputStream::class . '\\stdin';
|
||||
$stream = Loop::getState($key);
|
||||
if (!$stream) {
|
||||
$stream = new ResourceInputStream(\STDIN);
|
||||
Loop::setState($key, $stream);
|
||||
}
|
||||
return $stream;
|
||||
}
|
||||
/**
|
||||
* The STDOUT stream for the process associated with the currently active event loop.
|
||||
*
|
||||
* @return ResourceOutputStream
|
||||
*/
|
||||
function getStdout() : ResourceOutputStream
|
||||
{
|
||||
static $key = OutputStream::class . '\\stdout';
|
||||
$stream = Loop::getState($key);
|
||||
if (!$stream) {
|
||||
$stream = new ResourceOutputStream(\STDOUT);
|
||||
Loop::setState($key, $stream);
|
||||
}
|
||||
return $stream;
|
||||
}
|
||||
/**
|
||||
* The STDERR stream for the process associated with the currently active event loop.
|
||||
*
|
||||
* @return ResourceOutputStream
|
||||
*/
|
||||
function getStderr() : ResourceOutputStream
|
||||
{
|
||||
static $key = OutputStream::class . '\\stderr';
|
||||
$stream = Loop::getState($key);
|
||||
if (!$stream) {
|
||||
$stream = new ResourceOutputStream(\STDERR);
|
||||
Loop::setState($key, $stream);
|
||||
}
|
||||
return $stream;
|
||||
}
|
||||
function parseLineDelimitedJson(InputStream $stream, bool $assoc = \false, int $depth = 512, int $options = 0) : Iterator
|
||||
{
|
||||
return new Producer(static function (callable $emit) use($stream, $assoc, $depth, $options) {
|
||||
$reader = new LineReader($stream);
|
||||
while (null !== ($line = (yield $reader->readLine()))) {
|
||||
$line = \trim($line);
|
||||
if ($line === '') {
|
||||
continue;
|
||||
}
|
||||
/** @noinspection PhpComposerExtensionStubsInspection */
|
||||
$data = \json_decode($line, $assoc, $depth, $options);
|
||||
/** @noinspection PhpComposerExtensionStubsInspection */
|
||||
$error = \json_last_error();
|
||||
/** @noinspection PhpComposerExtensionStubsInspection */
|
||||
if ($error !== \JSON_ERROR_NONE) {
|
||||
/** @noinspection PhpComposerExtensionStubsInspection */
|
||||
throw new StreamException('Failed to parse JSON: ' . \json_last_error_msg(), $error);
|
||||
}
|
||||
(yield $emit($data));
|
||||
}
|
||||
});
|
||||
}
|
53
dependencies/amphp/byte-stream/psalm.xml
vendored
53
dependencies/amphp/byte-stream/psalm.xml
vendored
@ -1,53 +0,0 @@
|
||||
<?xml version="1.0"?>
|
||||
<psalm
|
||||
totallyTyped="false"
|
||||
errorLevel="2"
|
||||
phpVersion="7.0"
|
||||
resolveFromConfigFile="true"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns="https://getpsalm.org/schema/config"
|
||||
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
|
||||
>
|
||||
<projectFiles>
|
||||
<directory name="examples"/>
|
||||
<directory name="lib"/>
|
||||
<ignoreFiles>
|
||||
<directory name="vendor"/>
|
||||
</ignoreFiles>
|
||||
</projectFiles>
|
||||
|
||||
<issueHandlers>
|
||||
<StringIncrement>
|
||||
<errorLevel type="suppress">
|
||||
<directory name="examples"/>
|
||||
<directory name="lib"/>
|
||||
</errorLevel>
|
||||
</StringIncrement>
|
||||
|
||||
<RedundantConditionGivenDocblockType>
|
||||
<errorLevel type="suppress">
|
||||
<directory name="lib"/>
|
||||
</errorLevel>
|
||||
</RedundantConditionGivenDocblockType>
|
||||
|
||||
<DocblockTypeContradiction>
|
||||
<errorLevel type="suppress">
|
||||
<directory name="lib"/>
|
||||
</errorLevel>
|
||||
</DocblockTypeContradiction>
|
||||
|
||||
<MissingClosureParamType>
|
||||
<errorLevel type="suppress">
|
||||
<directory name="examples"/>
|
||||
<directory name="lib"/>
|
||||
</errorLevel>
|
||||
</MissingClosureParamType>
|
||||
|
||||
<MissingClosureReturnType>
|
||||
<errorLevel type="suppress">
|
||||
<directory name="examples"/>
|
||||
<directory name="lib"/>
|
||||
</errorLevel>
|
||||
</MissingClosureReturnType>
|
||||
</issueHandlers>
|
||||
</psalm>
|
99
dependencies/amphp/cache/lib/ArrayCache.php
vendored
99
dependencies/amphp/cache/lib/ArrayCache.php
vendored
@ -1,99 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace WP_Ultimo\Dependencies\Amp\Cache;
|
||||
|
||||
use WP_Ultimo\Dependencies\Amp\Loop;
|
||||
use WP_Ultimo\Dependencies\Amp\Promise;
|
||||
use WP_Ultimo\Dependencies\Amp\Struct;
|
||||
use WP_Ultimo\Dependencies\Amp\Success;
|
||||
final class ArrayCache implements Cache
|
||||
{
|
||||
/** @var object */
|
||||
private $sharedState;
|
||||
/** @var string */
|
||||
private $ttlWatcherId;
|
||||
/** @var int|null */
|
||||
private $maxSize;
|
||||
/**
|
||||
* @param int $gcInterval The frequency in milliseconds at which expired cache entries should be garbage collected.
|
||||
* @param int $maxSize The maximum size of cache array (number of elements).
|
||||
*/
|
||||
public function __construct(int $gcInterval = 5000, int $maxSize = null)
|
||||
{
|
||||
// By using a shared state object we're able to use `__destruct()` for "normal" garbage collection of both this
|
||||
// instance and the loop's watcher. Otherwise this object could only be GC'd when the TTL watcher was cancelled
|
||||
// at the loop layer.
|
||||
$this->sharedState = $sharedState = new class
|
||||
{
|
||||
use Struct;
|
||||
/** @var string[] */
|
||||
public $cache = [];
|
||||
/** @var int[] */
|
||||
public $cacheTimeouts = [];
|
||||
/** @var bool */
|
||||
public $isSortNeeded = \false;
|
||||
public function collectGarbage() : void
|
||||
{
|
||||
$now = \time();
|
||||
if ($this->isSortNeeded) {
|
||||
\asort($this->cacheTimeouts);
|
||||
$this->isSortNeeded = \false;
|
||||
}
|
||||
foreach ($this->cacheTimeouts as $key => $expiry) {
|
||||
if ($now <= $expiry) {
|
||||
break;
|
||||
}
|
||||
unset($this->cache[$key], $this->cacheTimeouts[$key]);
|
||||
}
|
||||
}
|
||||
};
|
||||
$this->ttlWatcherId = Loop::repeat($gcInterval, [$sharedState, "collectGarbage"]);
|
||||
$this->maxSize = $maxSize;
|
||||
Loop::unreference($this->ttlWatcherId);
|
||||
}
|
||||
public function __destruct()
|
||||
{
|
||||
$this->sharedState->cache = [];
|
||||
$this->sharedState->cacheTimeouts = [];
|
||||
Loop::cancel($this->ttlWatcherId);
|
||||
}
|
||||
/** @inheritdoc */
|
||||
public function get(string $key) : Promise
|
||||
{
|
||||
if (!isset($this->sharedState->cache[$key])) {
|
||||
return new Success(null);
|
||||
}
|
||||
if (isset($this->sharedState->cacheTimeouts[$key]) && \time() > $this->sharedState->cacheTimeouts[$key]) {
|
||||
unset($this->sharedState->cache[$key], $this->sharedState->cacheTimeouts[$key]);
|
||||
return new Success(null);
|
||||
}
|
||||
return new Success($this->sharedState->cache[$key]);
|
||||
}
|
||||
/** @inheritdoc */
|
||||
public function set(string $key, string $value, int $ttl = null) : Promise
|
||||
{
|
||||
if ($ttl === null) {
|
||||
unset($this->sharedState->cacheTimeouts[$key]);
|
||||
} elseif ($ttl >= 0) {
|
||||
$expiry = \time() + $ttl;
|
||||
$this->sharedState->cacheTimeouts[$key] = $expiry;
|
||||
$this->sharedState->isSortNeeded = \true;
|
||||
} else {
|
||||
throw new \Error("Invalid cache TTL ({$ttl}; integer >= 0 or null required");
|
||||
}
|
||||
unset($this->sharedState->cache[$key]);
|
||||
if (\count($this->sharedState->cache) === $this->maxSize) {
|
||||
\array_shift($this->sharedState->cache);
|
||||
}
|
||||
$this->sharedState->cache[$key] = $value;
|
||||
/** @var Promise<void> */
|
||||
return new Success();
|
||||
}
|
||||
/** @inheritdoc */
|
||||
public function delete(string $key) : Promise
|
||||
{
|
||||
$exists = isset($this->sharedState->cache[$key]);
|
||||
unset($this->sharedState->cache[$key], $this->sharedState->cacheTimeouts[$key]);
|
||||
return new Success($exists);
|
||||
}
|
||||
}
|
237
dependencies/amphp/cache/lib/AtomicCache.php
vendored
237
dependencies/amphp/cache/lib/AtomicCache.php
vendored
@ -1,237 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace WP_Ultimo\Dependencies\Amp\Cache;
|
||||
|
||||
use WP_Ultimo\Dependencies\Amp\Promise;
|
||||
use WP_Ultimo\Dependencies\Amp\Serialization\SerializationException;
|
||||
use WP_Ultimo\Dependencies\Amp\Sync\KeyedMutex;
|
||||
use WP_Ultimo\Dependencies\Amp\Sync\Lock;
|
||||
use function WP_Ultimo\Dependencies\Amp\call;
|
||||
/**
|
||||
* @template TValue
|
||||
*/
|
||||
final class AtomicCache
|
||||
{
|
||||
/** @var SerializedCache<TValue> */
|
||||
private $cache;
|
||||
/** @var KeyedMutex */
|
||||
private $mutex;
|
||||
/**
|
||||
* @param SerializedCache<TValue> $cache
|
||||
* @param KeyedMutex $mutex
|
||||
*/
|
||||
public function __construct(SerializedCache $cache, KeyedMutex $mutex)
|
||||
{
|
||||
$this->cache = $cache;
|
||||
$this->mutex = $mutex;
|
||||
}
|
||||
/**
|
||||
* Obtains the lock for the given key, then invokes the $create callback with the current cached value (which may
|
||||
* be null if the key did not exist in the cache). The value returned from the callback is stored in the cache and
|
||||
* the promise returned from this method is resolved with the value.
|
||||
*
|
||||
* @param string $key
|
||||
* @param callable(string, mixed|null): mixed $create Receives $key and $value as parameters.
|
||||
* @param int|null $ttl Timeout in seconds. The default `null` $ttl value indicates no timeout.
|
||||
*
|
||||
* @return Promise<mixed>
|
||||
*
|
||||
* @psalm-param callable(string, TValue|null):(TValue|Promise<TValue>|\Generator<mixed, mixed, mixed, TValue>)
|
||||
* $create
|
||||
* @psalm-return Promise<TValue>
|
||||
*
|
||||
* @throws CacheException If the $create callback throws an exception while generating the value.
|
||||
* @throws SerializationException If serializing the value returned from the callback fails.
|
||||
*/
|
||||
public function compute(string $key, callable $create, ?int $ttl = null) : Promise
|
||||
{
|
||||
return call(function () use($key, $create, $ttl) : \Generator {
|
||||
$lock = (yield from $this->lock($key));
|
||||
\assert($lock instanceof Lock);
|
||||
try {
|
||||
$value = (yield $this->cache->get($key));
|
||||
return yield from $this->create($create, $key, $value, $ttl);
|
||||
} finally {
|
||||
$lock->release();
|
||||
}
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Attempts to get the value for the given key. If the key is not found, the key is locked, the $create callback
|
||||
* is invoked with the key as the first parameter. The value returned from the callback is stored in the cache and
|
||||
* the promise returned from this method is resolved with the value.
|
||||
*
|
||||
* @param string $key Cache key.
|
||||
* @param callable(string): mixed $create Receives $key as parameter.
|
||||
* @param int|null $ttl Timeout in seconds. The default `null` $ttl value indicates no timeout.
|
||||
*
|
||||
* @return Promise<mixed>
|
||||
*
|
||||
* @psalm-param callable(string, TValue|null):(TValue|Promise<TValue>|\Generator<mixed, mixed, mixed, TValue>)
|
||||
* $create
|
||||
* @psalm-return Promise<TValue>
|
||||
*
|
||||
* @throws CacheException If the $create callback throws an exception while generating the value.
|
||||
* @throws SerializationException If serializing the value returned from the callback fails.
|
||||
*/
|
||||
public function computeIfAbsent(string $key, callable $create, ?int $ttl = null) : Promise
|
||||
{
|
||||
return call(function () use($key, $create, $ttl) : \Generator {
|
||||
$value = (yield $this->cache->get($key));
|
||||
if ($value !== null) {
|
||||
return $value;
|
||||
}
|
||||
$lock = (yield from $this->lock($key));
|
||||
\assert($lock instanceof Lock);
|
||||
try {
|
||||
// Attempt to get the value again, since it may have been set while obtaining the lock.
|
||||
$value = (yield $this->cache->get($key));
|
||||
if ($value !== null) {
|
||||
return $value;
|
||||
}
|
||||
return yield from $this->create($create, $key, null, $ttl);
|
||||
} finally {
|
||||
$lock->release();
|
||||
}
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Attempts to get the value for the given key. If the key exists, the key is locked, the $create callback
|
||||
* is invoked with the key as the first parameter and the current key value as the second parameter. The value
|
||||
* returned from the callback is stored in the cache and the promise returned from this method is resolved with
|
||||
* the value.
|
||||
*
|
||||
* @param string $key Cache key.
|
||||
* @param callable(string, mixed): mixed $create Receives $key and $value as parameters.
|
||||
* @param int|null $ttl Timeout in seconds. The default `null` $ttl value indicates no timeout.
|
||||
*
|
||||
* @return Promise<mixed>
|
||||
*
|
||||
* @psalm-param callable(string, TValue|null): (TValue|Promise<TValue>|\Generator<mixed, mixed, mixed, TValue>) $create
|
||||
* @psalm-return Promise<TValue>
|
||||
*
|
||||
* @throws CacheException If the $create callback throws an exception while generating the value.
|
||||
* @throws SerializationException If serializing the value returned from the callback fails.
|
||||
*/
|
||||
public function computeIfPresent(string $key, callable $create, ?int $ttl = null) : Promise
|
||||
{
|
||||
return call(function () use($key, $create, $ttl) : \Generator {
|
||||
$value = (yield $this->cache->get($key));
|
||||
if ($value === null) {
|
||||
return null;
|
||||
}
|
||||
$lock = (yield from $this->lock($key));
|
||||
\assert($lock instanceof Lock);
|
||||
try {
|
||||
// Attempt to get the value again, since it may have been set while obtaining the lock.
|
||||
$value = (yield $this->cache->get($key));
|
||||
if ($value === null) {
|
||||
return null;
|
||||
}
|
||||
return yield from $this->create($create, $key, $value, $ttl);
|
||||
} finally {
|
||||
$lock->release();
|
||||
}
|
||||
});
|
||||
}
|
||||
/**
|
||||
* The lock is obtained for the key before setting the value.
|
||||
*
|
||||
* @param string $key Cache key.
|
||||
* @param mixed $value Value to cache.
|
||||
* @param int|null $ttl Timeout in seconds. The default `null` $ttl value indicates no timeout.
|
||||
*
|
||||
* @return Promise<void> Resolves either successfully or fails with a CacheException on failure.
|
||||
*
|
||||
* @psalm-param TValue $value
|
||||
* @psalm-return Promise<void>
|
||||
*
|
||||
* @throws CacheException
|
||||
* @throws SerializationException
|
||||
*
|
||||
* @see SerializedCache::set()
|
||||
*/
|
||||
public function set(string $key, $value, ?int $ttl = null) : Promise
|
||||
{
|
||||
return call(function () use($key, $value, $ttl) : \Generator {
|
||||
$lock = (yield from $this->lock($key));
|
||||
\assert($lock instanceof Lock);
|
||||
try {
|
||||
(yield $this->cache->set($key, $value, $ttl));
|
||||
} finally {
|
||||
$lock->release();
|
||||
}
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Returns the cached value for the key or the given default value if the key does not exist.
|
||||
*
|
||||
* @template TDefault
|
||||
*
|
||||
* @param string $key Cache key.
|
||||
* @param mixed $default Default value returned if the key does not exist. Null by default.
|
||||
*
|
||||
* @return Promise<mixed|null> Resolved with null iff $default is null.
|
||||
*
|
||||
* @psalm-param TDefault $default
|
||||
* @psalm-return Promise<TValue|TDefault>
|
||||
*
|
||||
* @throws CacheException
|
||||
* @throws SerializationException
|
||||
*
|
||||
* @see SerializedCache::get()
|
||||
*/
|
||||
public function get(string $key, $default = null) : Promise
|
||||
{
|
||||
return call(function () use($key, $default) : \Generator {
|
||||
$value = (yield $this->cache->get($key));
|
||||
if ($value === null) {
|
||||
return $default;
|
||||
}
|
||||
return $value;
|
||||
});
|
||||
}
|
||||
/**
|
||||
* The lock is obtained for the key before deleting the key.
|
||||
*
|
||||
* @param string $key
|
||||
*
|
||||
* @return Promise<bool|null>
|
||||
*
|
||||
* @see SerializedCache::delete()
|
||||
*/
|
||||
public function delete(string $key) : Promise
|
||||
{
|
||||
return call(function () use($key) : \Generator {
|
||||
$lock = (yield from $this->lock($key));
|
||||
\assert($lock instanceof Lock);
|
||||
try {
|
||||
return (yield $this->cache->delete($key));
|
||||
} finally {
|
||||
$lock->release();
|
||||
}
|
||||
});
|
||||
}
|
||||
private function lock(string $key) : \Generator
|
||||
{
|
||||
try {
|
||||
return (yield $this->mutex->acquire($key));
|
||||
} catch (\Throwable $exception) {
|
||||
throw new CacheException(\sprintf('Exception thrown when obtaining the lock for key "%s"', $key), 0, $exception);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* @psalm-param TValue|null $value
|
||||
* @psalm-return \Generator<mixed, mixed, mixed, TValue>
|
||||
*/
|
||||
private function create(callable $create, string $key, $value, ?int $ttl) : \Generator
|
||||
{
|
||||
try {
|
||||
$value = (yield call($create, $key, $value));
|
||||
} catch (\Throwable $exception) {
|
||||
throw new CacheException(\sprintf('Exception thrown while creating the value for key "%s"', $key), 0, $exception);
|
||||
}
|
||||
(yield $this->cache->set($key, $value, $ttl));
|
||||
return $value;
|
||||
}
|
||||
}
|
48
dependencies/amphp/cache/lib/Cache.php
vendored
48
dependencies/amphp/cache/lib/Cache.php
vendored
@ -1,48 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace WP_Ultimo\Dependencies\Amp\Cache;
|
||||
|
||||
use WP_Ultimo\Dependencies\Amp\Promise;
|
||||
interface Cache
|
||||
{
|
||||
/**
|
||||
* Gets a value associated with the given key.
|
||||
*
|
||||
* If the specified key doesn't exist implementations MUST succeed the resulting promise with `null`.
|
||||
*
|
||||
* @param $key string Cache key.
|
||||
*
|
||||
* @return Promise<string|null> Resolves to the cached value nor `null` if it doesn't exist or fails with a
|
||||
* CacheException on failure.
|
||||
*/
|
||||
public function get(string $key) : Promise;
|
||||
/**
|
||||
* Sets a value associated with the given key. Overrides existing values (if they exist).
|
||||
*
|
||||
* The eventual resolution value of the resulting promise is unimportant. The success or failure of the promise
|
||||
* indicates the operation's success.
|
||||
*
|
||||
* @param $key string Cache key.
|
||||
* @param $value string Value to cache.
|
||||
* @param $ttl int Timeout in seconds. The default `null` $ttl value indicates no timeout. Values less than 0 MUST
|
||||
* throw an \Error.
|
||||
*
|
||||
* @return Promise<void> Resolves either successfully or fails with a CacheException on failure.
|
||||
*/
|
||||
public function set(string $key, string $value, int $ttl = null) : Promise;
|
||||
/**
|
||||
* Deletes a value associated with the given key if it exists.
|
||||
*
|
||||
* Implementations SHOULD return boolean `true` or `false` to indicate whether the specified key existed at the time
|
||||
* the delete operation was requested. If such information is not available, the implementation MUST resolve the
|
||||
* promise with `null`.
|
||||
*
|
||||
* Implementations MUST transparently succeed operations for non-existent keys.
|
||||
*
|
||||
* @param $key string Cache key.
|
||||
*
|
||||
* @return Promise<bool|null> Resolves to `true` / `false` to indicate whether the key existed or fails with a
|
||||
* CacheException on failure. May also resolve with `null` if that information is not available.
|
||||
*/
|
||||
public function delete(string $key) : Promise;
|
||||
}
|
10
dependencies/amphp/cache/lib/CacheException.php
vendored
10
dependencies/amphp/cache/lib/CacheException.php
vendored
@ -1,10 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace WP_Ultimo\Dependencies\Amp\Cache;
|
||||
|
||||
/**
|
||||
* MUST be thrown in case a cache operation fails.
|
||||
*/
|
||||
class CacheException extends \Exception
|
||||
{
|
||||
}
|
147
dependencies/amphp/cache/lib/FileCache.php
vendored
147
dependencies/amphp/cache/lib/FileCache.php
vendored
@ -1,147 +0,0 @@
|
||||
<?php
|
||||
|
||||
/** @noinspection PhpUndefinedFunctionInspection */
|
||||
namespace WP_Ultimo\Dependencies\Amp\Cache;
|
||||
|
||||
use WP_Ultimo\Dependencies\Amp\File;
|
||||
use WP_Ultimo\Dependencies\Amp\File\Driver;
|
||||
use WP_Ultimo\Dependencies\Amp\Loop;
|
||||
use WP_Ultimo\Dependencies\Amp\Promise;
|
||||
use WP_Ultimo\Dependencies\Amp\Sync\KeyedMutex;
|
||||
use WP_Ultimo\Dependencies\Amp\Sync\Lock;
|
||||
use function WP_Ultimo\Dependencies\Amp\call;
|
||||
final class FileCache implements Cache
|
||||
{
|
||||
private static function getFilename(string $key) : string
|
||||
{
|
||||
return \hash('sha256', $key) . '.cache';
|
||||
}
|
||||
/** @var string */
|
||||
private $directory;
|
||||
/** @var KeyedMutex */
|
||||
private $mutex;
|
||||
/** @var string */
|
||||
private $gcWatcher;
|
||||
/** @var bool */
|
||||
private $ampFileVersion2;
|
||||
public function __construct(string $directory, KeyedMutex $mutex)
|
||||
{
|
||||
$this->directory = $directory = \rtrim($directory, "/\\");
|
||||
$this->mutex = $mutex;
|
||||
if (!\interface_exists(Driver::class)) {
|
||||
throw new \Error(__CLASS__ . ' requires amphp/file to be installed');
|
||||
}
|
||||
$this->ampFileVersion2 = $ampFileVersion2 = \function_exists('WP_Ultimo\\Dependencies\\Amp\\File\\listFiles');
|
||||
$gcWatcher = static function () use($directory, $mutex, $ampFileVersion2) : \Generator {
|
||||
try {
|
||||
/** @psalm-suppress UndefinedFunction */
|
||||
$files = (yield $ampFileVersion2 ? File\listFiles($directory) : File\scandir($directory));
|
||||
foreach ($files as $file) {
|
||||
if (\strlen($file) !== 70 || \substr($file, -\strlen('.cache')) !== '.cache') {
|
||||
continue;
|
||||
}
|
||||
/** @var Lock $lock */
|
||||
$lock = (yield $mutex->acquire($file));
|
||||
try {
|
||||
/** @var File\File $handle */
|
||||
/** @psalm-suppress UndefinedFunction */
|
||||
$handle = (yield $ampFileVersion2 ? File\openFile($directory . '/' . $file, 'r') : File\open($directory . '/' . $file, 'r'));
|
||||
$ttl = (yield $handle->read(4));
|
||||
if ($ttl === null || \strlen($ttl) !== 4) {
|
||||
(yield $handle->close());
|
||||
continue;
|
||||
}
|
||||
$ttl = \unpack('Nttl', $ttl)['ttl'];
|
||||
if ($ttl < \time()) {
|
||||
/** @psalm-suppress UndefinedFunction */
|
||||
(yield $ampFileVersion2 ? File\deleteFile($directory . '/' . $file) : File\unlink($directory . '/' . $file));
|
||||
}
|
||||
} catch (\Throwable $e) {
|
||||
// ignore
|
||||
} finally {
|
||||
$lock->release();
|
||||
}
|
||||
}
|
||||
} catch (\Throwable $e) {
|
||||
// ignore
|
||||
}
|
||||
};
|
||||
// trigger once, so short running scripts also GC and don't grow forever
|
||||
Loop::defer($gcWatcher);
|
||||
$this->gcWatcher = Loop::repeat(300000, $gcWatcher);
|
||||
}
|
||||
public function __destruct()
|
||||
{
|
||||
if ($this->gcWatcher !== null) {
|
||||
Loop::cancel($this->gcWatcher);
|
||||
}
|
||||
}
|
||||
/** @inheritdoc */
|
||||
public function get(string $key) : Promise
|
||||
{
|
||||
return call(function () use($key) {
|
||||
$filename = $this->getFilename($key);
|
||||
/** @var Lock $lock */
|
||||
$lock = (yield $this->mutex->acquire($filename));
|
||||
try {
|
||||
/** @psalm-suppress UndefinedFunction */
|
||||
$cacheContent = (yield $this->ampFileVersion2 ? File\read($this->directory . '/' . $filename) : File\get($this->directory . '/' . $filename));
|
||||
if (\strlen($cacheContent) < 4) {
|
||||
return null;
|
||||
}
|
||||
$ttl = \unpack('Nttl', \substr($cacheContent, 0, 4))['ttl'];
|
||||
if ($ttl < \time()) {
|
||||
/** @psalm-suppress UndefinedFunction */
|
||||
(yield $this->ampFileVersion2 ? File\deleteFile($this->directory . '/' . $filename) : File\unlink($this->directory . '/' . $filename));
|
||||
return null;
|
||||
}
|
||||
$value = \substr($cacheContent, 4);
|
||||
\assert(\is_string($value));
|
||||
return $value;
|
||||
} catch (\Throwable $e) {
|
||||
return null;
|
||||
} finally {
|
||||
$lock->release();
|
||||
}
|
||||
});
|
||||
}
|
||||
/** @inheritdoc */
|
||||
public function set(string $key, string $value, int $ttl = null) : Promise
|
||||
{
|
||||
if ($ttl < 0) {
|
||||
throw new \Error("Invalid cache TTL ({$ttl}); integer >= 0 or null required");
|
||||
}
|
||||
return call(function () use($key, $value, $ttl) {
|
||||
$filename = $this->getFilename($key);
|
||||
/** @var Lock $lock */
|
||||
$lock = (yield $this->mutex->acquire($filename));
|
||||
if ($ttl === null) {
|
||||
$ttl = \PHP_INT_MAX;
|
||||
} else {
|
||||
$ttl = \time() + $ttl;
|
||||
}
|
||||
$encodedTtl = \pack('N', $ttl);
|
||||
try {
|
||||
/** @psalm-suppress UndefinedFunction */
|
||||
(yield $this->ampFileVersion2 ? File\write($this->directory . '/' . $filename, $encodedTtl . $value) : File\put($this->directory . '/' . $filename, $encodedTtl . $value));
|
||||
} finally {
|
||||
$lock->release();
|
||||
}
|
||||
});
|
||||
}
|
||||
/** @inheritdoc */
|
||||
public function delete(string $key) : Promise
|
||||
{
|
||||
return call(function () use($key) {
|
||||
$filename = $this->getFilename($key);
|
||||
/** @var Lock $lock */
|
||||
$lock = (yield $this->mutex->acquire($filename));
|
||||
try {
|
||||
/** @psalm-suppress UndefinedFunction */
|
||||
return (yield $this->ampFileVersion2 ? File\deleteFile($this->directory . '/' . $filename) : File\unlink($this->directory . '/' . $filename));
|
||||
} finally {
|
||||
$lock->release();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
28
dependencies/amphp/cache/lib/NullCache.php
vendored
28
dependencies/amphp/cache/lib/NullCache.php
vendored
@ -1,28 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace WP_Ultimo\Dependencies\Amp\Cache;
|
||||
|
||||
use WP_Ultimo\Dependencies\Amp\Promise;
|
||||
use WP_Ultimo\Dependencies\Amp\Success;
|
||||
/**
|
||||
* Cache implementation that just ignores all operations and always resolves to `null`.
|
||||
*/
|
||||
class NullCache implements Cache
|
||||
{
|
||||
/** @inheritdoc */
|
||||
public function get(string $key) : Promise
|
||||
{
|
||||
return new Success();
|
||||
}
|
||||
/** @inheritdoc */
|
||||
public function set(string $key, string $value, int $ttl = null) : Promise
|
||||
{
|
||||
/** @var Promise<void> */
|
||||
return new Success();
|
||||
}
|
||||
/** @inheritdoc */
|
||||
public function delete(string $key) : Promise
|
||||
{
|
||||
return new Success(\false);
|
||||
}
|
||||
}
|
39
dependencies/amphp/cache/lib/PrefixCache.php
vendored
39
dependencies/amphp/cache/lib/PrefixCache.php
vendored
@ -1,39 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace WP_Ultimo\Dependencies\Amp\Cache;
|
||||
|
||||
use WP_Ultimo\Dependencies\Amp\Promise;
|
||||
final class PrefixCache implements Cache
|
||||
{
|
||||
private $cache;
|
||||
private $keyPrefix;
|
||||
public function __construct(Cache $cache, string $keyPrefix)
|
||||
{
|
||||
$this->cache = $cache;
|
||||
$this->keyPrefix = $keyPrefix;
|
||||
}
|
||||
/**
|
||||
* Gets the specified key prefix.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getKeyPrefix() : string
|
||||
{
|
||||
return $this->keyPrefix;
|
||||
}
|
||||
/** @inheritdoc */
|
||||
public function get(string $key) : Promise
|
||||
{
|
||||
return $this->cache->get($this->keyPrefix . $key);
|
||||
}
|
||||
/** @inheritdoc */
|
||||
public function set(string $key, string $value, int $ttl = null) : Promise
|
||||
{
|
||||
return $this->cache->set($this->keyPrefix . $key, $value, $ttl);
|
||||
}
|
||||
/** @inheritdoc */
|
||||
public function delete(string $key) : Promise
|
||||
{
|
||||
return $this->cache->delete($this->keyPrefix . $key);
|
||||
}
|
||||
}
|
86
dependencies/amphp/cache/lib/SerializedCache.php
vendored
86
dependencies/amphp/cache/lib/SerializedCache.php
vendored
@ -1,86 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace WP_Ultimo\Dependencies\Amp\Cache;
|
||||
|
||||
use WP_Ultimo\Dependencies\Amp\Failure;
|
||||
use WP_Ultimo\Dependencies\Amp\Promise;
|
||||
use WP_Ultimo\Dependencies\Amp\Serialization\SerializationException;
|
||||
use WP_Ultimo\Dependencies\Amp\Serialization\Serializer;
|
||||
use function WP_Ultimo\Dependencies\Amp\call;
|
||||
/**
|
||||
* @template TValue
|
||||
*/
|
||||
final class SerializedCache
|
||||
{
|
||||
/** @var Cache */
|
||||
private $cache;
|
||||
/** @var Serializer */
|
||||
private $serializer;
|
||||
public function __construct(Cache $cache, Serializer $serializer)
|
||||
{
|
||||
$this->cache = $cache;
|
||||
$this->serializer = $serializer;
|
||||
}
|
||||
/**
|
||||
* Fetch a value from the cache and unserialize it.
|
||||
*
|
||||
* @param $key string Cache key.
|
||||
*
|
||||
* @return Promise<mixed|null> Resolves to the cached value or `null` if it doesn't exist. Fails with a
|
||||
* CacheException or SerializationException on failure.
|
||||
*
|
||||
* @psalm-return Promise<TValue|null>
|
||||
*
|
||||
* @see Cache::get()
|
||||
*/
|
||||
public function get(string $key) : Promise
|
||||
{
|
||||
return call(function () use($key) {
|
||||
$data = (yield $this->cache->get($key));
|
||||
if ($data === null) {
|
||||
return null;
|
||||
}
|
||||
return $this->serializer->unserialize($data);
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Serializes a value and stores its serialization to the cache.
|
||||
*
|
||||
* @param $key string Cache key.
|
||||
* @param $value mixed Value to cache.
|
||||
* @param $ttl int Timeout in seconds. The default `null` $ttl value indicates no timeout. Values less than 0 MUST
|
||||
* throw an \Error.
|
||||
*
|
||||
* @psalm-param TValue $value
|
||||
*
|
||||
* @return Promise<void> Resolves either successfully or fails with a CacheException or SerializationException.
|
||||
*
|
||||
* @see Cache::set()
|
||||
*/
|
||||
public function set(string $key, $value, int $ttl = null) : Promise
|
||||
{
|
||||
if ($value === null) {
|
||||
return new Failure(new CacheException('Cannot store NULL in serialized cache'));
|
||||
}
|
||||
try {
|
||||
$value = $this->serializer->serialize($value);
|
||||
} catch (SerializationException $exception) {
|
||||
return new Failure($exception);
|
||||
}
|
||||
return $this->cache->set($key, $value, $ttl);
|
||||
}
|
||||
/**
|
||||
* Deletes a value associated with the given key if it exists.
|
||||
*
|
||||
* @param $key string Cache key.
|
||||
*
|
||||
* @return Promise<bool|null> Resolves to `true` / `false` to indicate whether the key existed or fails with a
|
||||
* CacheException on failure. May also resolve with `null` if that information is not available.
|
||||
*
|
||||
* @see Cache::delete()
|
||||
*/
|
||||
public function delete(string $key) : Promise
|
||||
{
|
||||
return $this->cache->delete($key);
|
||||
}
|
||||
}
|
43
dependencies/amphp/cache/psalm.xml
vendored
43
dependencies/amphp/cache/psalm.xml
vendored
@ -1,43 +0,0 @@
|
||||
<?xml version="1.0"?>
|
||||
<psalm
|
||||
totallyTyped="false"
|
||||
errorLevel="2"
|
||||
phpVersion="7.1"
|
||||
resolveFromConfigFile="true"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns="https://getpsalm.org/schema/config"
|
||||
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
|
||||
>
|
||||
<projectFiles>
|
||||
<directory name="lib"/>
|
||||
<ignoreFiles>
|
||||
<directory name="vendor"/>
|
||||
</ignoreFiles>
|
||||
</projectFiles>
|
||||
|
||||
<issueHandlers>
|
||||
<StringIncrement>
|
||||
<errorLevel type="suppress">
|
||||
<directory name="lib"/>
|
||||
</errorLevel>
|
||||
</StringIncrement>
|
||||
|
||||
<RedundantConditionGivenDocblockType>
|
||||
<errorLevel type="suppress">
|
||||
<directory name="lib"/>
|
||||
</errorLevel>
|
||||
</RedundantConditionGivenDocblockType>
|
||||
|
||||
<DocblockTypeContradiction>
|
||||
<errorLevel type="suppress">
|
||||
<directory name="lib"/>
|
||||
</errorLevel>
|
||||
</DocblockTypeContradiction>
|
||||
|
||||
<MissingClosureParamType>
|
||||
<errorLevel type="suppress">
|
||||
<directory name="lib"/>
|
||||
</errorLevel>
|
||||
</MissingClosureParamType>
|
||||
</issueHandlers>
|
||||
</psalm>
|
38
dependencies/amphp/dns/appveyor.yml
vendored
38
dependencies/amphp/dns/appveyor.yml
vendored
@ -1,38 +0,0 @@
|
||||
build: false
|
||||
shallow_clone: false
|
||||
|
||||
platform:
|
||||
- x86
|
||||
- x64
|
||||
|
||||
clone_folder: c:\WP_Ultimo\Dependencies\projects\amphp
|
||||
|
||||
cache:
|
||||
- c:\WP_Ultimo\Dependencies\tools\php74 -> appveyor.yml
|
||||
|
||||
init:
|
||||
- SET PATH=C:\Program WP_Ultimo\Dependencies\Files\OpenSSL;c:\WP_Ultimo\Dependencies\tools\php74;%PATH%
|
||||
- SET COMPOSER_NO_INTERACTION=1
|
||||
- SET PHP=1
|
||||
- SET ANSICON=121x90 (121x90)
|
||||
|
||||
install:
|
||||
- IF EXIST c:\WP_Ultimo\Dependencies\tools\php74 (SET PHP=0)
|
||||
- IF %PHP%==1 sc config wuauserv start= auto
|
||||
- IF %PHP%==1 net start wuauserv
|
||||
- IF %PHP%==1 cinst -y OpenSSL.Light
|
||||
- IF %PHP%==1 cinst -y php
|
||||
- cd c:\WP_Ultimo\Dependencies\tools\php74
|
||||
- IF %PHP%==1 copy php.ini-production php.ini /Y
|
||||
- IF %PHP%==1 echo date.timezone="UTC" >> php.ini
|
||||
- IF %PHP%==1 echo extension_dir=ext >> php.ini
|
||||
- IF %PHP%==1 echo extension=php_openssl.dll >> php.ini
|
||||
- IF %PHP%==1 echo extension=php_mbstring.dll >> php.ini
|
||||
- IF %PHP%==1 echo extension=php_fileinfo.dll >> php.ini
|
||||
- cd c:\WP_Ultimo\Dependencies\projects\amphp
|
||||
- appveyor DownloadFile https://getcomposer.org/composer.phar
|
||||
- php composer.phar install --prefer-dist --no-progress
|
||||
|
||||
test_script:
|
||||
- cd c:\WP_Ultimo\Dependencies\projects\amphp
|
||||
- vendor/bin/phpunit --colors=always
|
@ -1,35 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace WP_Ultimo\Dependencies\Amp\Dns;
|
||||
|
||||
use WP_Ultimo\Dependencies\Amp\Failure;
|
||||
use WP_Ultimo\Dependencies\Amp\Promise;
|
||||
use WP_Ultimo\Dependencies\Amp\Success;
|
||||
class BlockingFallbackResolver implements Resolver
|
||||
{
|
||||
public function resolve(string $name, int $typeRestriction = null) : Promise
|
||||
{
|
||||
if (!\in_array($typeRestriction, [Record::A, null], \true)) {
|
||||
return new Failure(new DnsException("Query for '{$name}' failed, because loading the system's DNS configuration failed and querying records other than A records isn't supported in blocking fallback mode."));
|
||||
}
|
||||
return $this->query($name, Record::A);
|
||||
}
|
||||
public function query(string $name, int $type) : Promise
|
||||
{
|
||||
if ($type !== Record::A) {
|
||||
return new Failure(new DnsException("Query for '{$name}' failed, because loading the system's DNS configuration failed and querying records other than A records isn't supported in blocking fallback mode."));
|
||||
}
|
||||
$result = \gethostbynamel($name);
|
||||
if ($result === \false) {
|
||||
return new Failure(new DnsException("Query for '{$name}' failed, because loading the system's DNS configuration failed and blocking fallback via gethostbynamel() failed, too."));
|
||||
}
|
||||
if ($result === []) {
|
||||
return new Failure(new NoRecordException("No records returned for '{$name}' using blocking fallback mode."));
|
||||
}
|
||||
$records = [];
|
||||
foreach ($result as $record) {
|
||||
$records[] = new Record($record, Record::A, null);
|
||||
}
|
||||
return new Success($records);
|
||||
}
|
||||
}
|
137
dependencies/amphp/dns/lib/Config.php
vendored
137
dependencies/amphp/dns/lib/Config.php
vendored
@ -1,137 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace WP_Ultimo\Dependencies\Amp\Dns;
|
||||
|
||||
final class Config
|
||||
{
|
||||
/** @var array */
|
||||
private $nameservers;
|
||||
/** @var array */
|
||||
private $knownHosts;
|
||||
/** @var int */
|
||||
private $timeout;
|
||||
/** @var int */
|
||||
private $attempts;
|
||||
/** @var array */
|
||||
private $searchList = [];
|
||||
/** @var int */
|
||||
private $ndots = 1;
|
||||
/** @var bool */
|
||||
private $rotation = \false;
|
||||
public function __construct(array $nameservers, array $knownHosts = [], int $timeout = 3000, int $attempts = 2)
|
||||
{
|
||||
if (\count($nameservers) < 1) {
|
||||
throw new ConfigException("At least one nameserver is required for a valid config");
|
||||
}
|
||||
foreach ($nameservers as $nameserver) {
|
||||
$this->validateNameserver($nameserver);
|
||||
}
|
||||
if ($timeout < 0) {
|
||||
throw new ConfigException("Invalid timeout ({$timeout}), must be 0 or greater");
|
||||
}
|
||||
if ($attempts < 1) {
|
||||
throw new ConfigException("Invalid attempt count ({$attempts}), must be 1 or greater");
|
||||
}
|
||||
// Windows does not include localhost in its host file. Fetch it from the system instead
|
||||
if (!isset($knownHosts[Record::A]["localhost"]) && !isset($knownHosts[Record::AAAA]["localhost"])) {
|
||||
// PHP currently provides no way to **resolve** IPv6 hostnames (not even with fallback)
|
||||
$local = \gethostbyname("localhost");
|
||||
if ($local !== "localhost") {
|
||||
$knownHosts[Record::A]["localhost"] = $local;
|
||||
} else {
|
||||
$knownHosts[Record::AAAA]["localhost"] = '::1';
|
||||
}
|
||||
}
|
||||
$this->nameservers = $nameservers;
|
||||
$this->knownHosts = $knownHosts;
|
||||
$this->timeout = $timeout;
|
||||
$this->attempts = $attempts;
|
||||
}
|
||||
public function withSearchList(array $searchList) : self
|
||||
{
|
||||
$self = clone $this;
|
||||
$self->searchList = $searchList;
|
||||
return $self;
|
||||
}
|
||||
/**
|
||||
* @throws ConfigException
|
||||
*/
|
||||
public function withNdots(int $ndots) : self
|
||||
{
|
||||
if ($ndots < 0) {
|
||||
throw new ConfigException("Invalid ndots ({$ndots}), must be greater or equal to 0");
|
||||
}
|
||||
if ($ndots > 15) {
|
||||
$ndots = 15;
|
||||
}
|
||||
$self = clone $this;
|
||||
$self->ndots = $ndots;
|
||||
return $self;
|
||||
}
|
||||
public function withRotationEnabled(bool $enabled = \true) : self
|
||||
{
|
||||
$self = clone $this;
|
||||
$self->rotation = $enabled;
|
||||
return $self;
|
||||
}
|
||||
private function validateNameserver($nameserver)
|
||||
{
|
||||
if (!$nameserver || !\is_string($nameserver)) {
|
||||
throw new ConfigException("Invalid nameserver: {$nameserver}");
|
||||
}
|
||||
if ($nameserver[0] === "[") {
|
||||
// IPv6
|
||||
$addr = \strstr(\substr($nameserver, 1), "]", \true);
|
||||
$port = \substr($nameserver, \strrpos($nameserver, "]") + 1);
|
||||
if ($port !== "" && !\preg_match("(^:(\\d+)\$)", $port, $match)) {
|
||||
throw new ConfigException("Invalid nameserver: {$nameserver}");
|
||||
}
|
||||
$port = $port === "" ? 53 : \substr($port, 1);
|
||||
} else {
|
||||
// IPv4
|
||||
$arr = \explode(":", $nameserver, 2);
|
||||
if (\count($arr) === 2) {
|
||||
list($addr, $port) = $arr;
|
||||
} else {
|
||||
$addr = $arr[0];
|
||||
$port = 53;
|
||||
}
|
||||
}
|
||||
$addr = \trim($addr, "[]");
|
||||
$port = (int) $port;
|
||||
if (!($inAddr = @\inet_pton($addr))) {
|
||||
throw new ConfigException("Invalid server IP: {$addr}");
|
||||
}
|
||||
if ($port < 1 || $port > 65535) {
|
||||
throw new ConfigException("Invalid server port: {$port}");
|
||||
}
|
||||
}
|
||||
public function getNameservers() : array
|
||||
{
|
||||
return $this->nameservers;
|
||||
}
|
||||
public function getKnownHosts() : array
|
||||
{
|
||||
return $this->knownHosts;
|
||||
}
|
||||
public function getTimeout() : int
|
||||
{
|
||||
return $this->timeout;
|
||||
}
|
||||
public function getAttempts() : int
|
||||
{
|
||||
return $this->attempts;
|
||||
}
|
||||
public function getSearchList() : array
|
||||
{
|
||||
return $this->searchList;
|
||||
}
|
||||
public function getNdots() : int
|
||||
{
|
||||
return $this->ndots;
|
||||
}
|
||||
public function isRotationEnabled() : bool
|
||||
{
|
||||
return $this->rotation;
|
||||
}
|
||||
}
|
15
dependencies/amphp/dns/lib/ConfigException.php
vendored
15
dependencies/amphp/dns/lib/ConfigException.php
vendored
@ -1,15 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace WP_Ultimo\Dependencies\Amp\Dns;
|
||||
|
||||
use Throwable;
|
||||
/**
|
||||
* MUST be thrown in case the config can't be read and no fallback is available.
|
||||
*/
|
||||
class ConfigException extends DnsException
|
||||
{
|
||||
public function __construct(string $message, Throwable $previous = null)
|
||||
{
|
||||
parent::__construct($message, 0, $previous);
|
||||
}
|
||||
}
|
9
dependencies/amphp/dns/lib/ConfigLoader.php
vendored
9
dependencies/amphp/dns/lib/ConfigLoader.php
vendored
@ -1,9 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace WP_Ultimo\Dependencies\Amp\Dns;
|
||||
|
||||
use WP_Ultimo\Dependencies\Amp\Promise;
|
||||
interface ConfigLoader
|
||||
{
|
||||
public function loadConfig() : Promise;
|
||||
}
|
7
dependencies/amphp/dns/lib/DnsException.php
vendored
7
dependencies/amphp/dns/lib/DnsException.php
vendored
@ -1,7 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace WP_Ultimo\Dependencies\Amp\Dns;
|
||||
|
||||
class DnsException extends \Exception
|
||||
{
|
||||
}
|
70
dependencies/amphp/dns/lib/HostLoader.php
vendored
70
dependencies/amphp/dns/lib/HostLoader.php
vendored
@ -1,70 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace WP_Ultimo\Dependencies\Amp\Dns;
|
||||
|
||||
use WP_Ultimo\Dependencies\Amp\Failure;
|
||||
use WP_Ultimo\Dependencies\Amp\Promise;
|
||||
use WP_Ultimo\Dependencies\Amp\Success;
|
||||
use function WP_Ultimo\Dependencies\Amp\call;
|
||||
class HostLoader
|
||||
{
|
||||
private $path;
|
||||
public function __construct(string $path = null)
|
||||
{
|
||||
$this->path = $path ?? $this->getDefaultPath();
|
||||
}
|
||||
private function getDefaultPath() : string
|
||||
{
|
||||
return \stripos(\PHP_OS, "win") === 0 ? 'C:\\Windows\\system32\\drivers\\etc\\hosts' : '/etc/hosts';
|
||||
}
|
||||
protected function readFile(string $path) : Promise
|
||||
{
|
||||
\set_error_handler(function (int $errno, string $message) use($path) {
|
||||
throw new ConfigException("Could not read configuration file '{$path}' ({$errno}) {$message}");
|
||||
});
|
||||
try {
|
||||
// Blocking file access, but this file should be local and usually loaded only once.
|
||||
$fileContent = \file_get_contents($path);
|
||||
} catch (ConfigException $exception) {
|
||||
return new Failure($exception);
|
||||
} finally {
|
||||
\restore_error_handler();
|
||||
}
|
||||
return new Success($fileContent);
|
||||
}
|
||||
public function loadHosts() : Promise
|
||||
{
|
||||
return call(function () {
|
||||
try {
|
||||
$contents = (yield $this->readFile($this->path));
|
||||
} catch (ConfigException $exception) {
|
||||
return [];
|
||||
}
|
||||
$data = [];
|
||||
$lines = \array_filter(\array_map("trim", \explode("\n", $contents)));
|
||||
foreach ($lines as $line) {
|
||||
if ($line[0] === "#") {
|
||||
// Skip comments
|
||||
continue;
|
||||
}
|
||||
$parts = \preg_split('/\\s+/', $line);
|
||||
if (!($ip = @\inet_pton($parts[0]))) {
|
||||
continue;
|
||||
} elseif (isset($ip[4])) {
|
||||
$key = Record::AAAA;
|
||||
} else {
|
||||
$key = Record::A;
|
||||
}
|
||||
for ($i = 1, $l = \count($parts); $i < $l; $i++) {
|
||||
try {
|
||||
$normalizedName = normalizeName($parts[$i]);
|
||||
$data[$key][$normalizedName] = $parts[0];
|
||||
} catch (InvalidNameException $e) {
|
||||
// ignore invalid entries
|
||||
}
|
||||
}
|
||||
}
|
||||
return $data;
|
||||
});
|
||||
}
|
||||
}
|
217
dependencies/amphp/dns/lib/Internal/Socket.php
vendored
217
dependencies/amphp/dns/lib/Internal/Socket.php
vendored
@ -1,217 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace WP_Ultimo\Dependencies\Amp\Dns\Internal;
|
||||
|
||||
use WP_Ultimo\Dependencies\Amp;
|
||||
use WP_Ultimo\Dependencies\Amp\ByteStream\ResourceInputStream;
|
||||
use WP_Ultimo\Dependencies\Amp\ByteStream\ResourceOutputStream;
|
||||
use WP_Ultimo\Dependencies\Amp\ByteStream\StreamException;
|
||||
use WP_Ultimo\Dependencies\Amp\Deferred;
|
||||
use WP_Ultimo\Dependencies\Amp\Dns\DnsException;
|
||||
use WP_Ultimo\Dependencies\Amp\Dns\TimeoutException;
|
||||
use WP_Ultimo\Dependencies\Amp\Promise;
|
||||
use WP_Ultimo\Dependencies\LibDNS\Messages\Message;
|
||||
use WP_Ultimo\Dependencies\LibDNS\Messages\MessageFactory;
|
||||
use WP_Ultimo\Dependencies\LibDNS\Messages\MessageTypes;
|
||||
use WP_Ultimo\Dependencies\LibDNS\Records\Question;
|
||||
use function WP_Ultimo\Dependencies\Amp\call;
|
||||
/** @internal */
|
||||
abstract class Socket
|
||||
{
|
||||
const MAX_CONCURRENT_REQUESTS = 500;
|
||||
/** @var ResourceInputStream */
|
||||
private $input;
|
||||
/** @var ResourceOutputStream */
|
||||
private $output;
|
||||
/** @var array Contains already sent queries with no response yet. For UDP this is exactly zero or one item. */
|
||||
private $pending = [];
|
||||
/** @var MessageFactory */
|
||||
private $messageFactory;
|
||||
/** @var callable */
|
||||
private $onResolve;
|
||||
/** @var int Used for determining whether the socket can be garbage collected, because it's inactive. */
|
||||
private $lastActivity;
|
||||
/** @var bool */
|
||||
private $receiving = \false;
|
||||
/** @var array Queued requests if the number of concurrent requests is too large. */
|
||||
private $queue = [];
|
||||
/**
|
||||
* @param string $uri
|
||||
*
|
||||
* @return Promise<self>
|
||||
*/
|
||||
public static abstract function connect(string $uri) : Promise;
|
||||
/**
|
||||
* @param Message $message
|
||||
*
|
||||
* @return Promise<int>
|
||||
*/
|
||||
protected abstract function send(Message $message) : Promise;
|
||||
/**
|
||||
* @return Promise<Message>
|
||||
*/
|
||||
protected abstract function receive() : Promise;
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public abstract function isAlive() : bool;
|
||||
public function getLastActivity() : int
|
||||
{
|
||||
return $this->lastActivity;
|
||||
}
|
||||
protected function __construct($socket)
|
||||
{
|
||||
$this->input = new ResourceInputStream($socket);
|
||||
$this->output = new ResourceOutputStream($socket);
|
||||
$this->messageFactory = new MessageFactory();
|
||||
$this->lastActivity = \time();
|
||||
$this->onResolve = function (\Throwable $exception = null, Message $message = null) {
|
||||
$this->lastActivity = \time();
|
||||
$this->receiving = \false;
|
||||
if ($exception) {
|
||||
$this->error($exception);
|
||||
return;
|
||||
}
|
||||
\assert($message instanceof Message);
|
||||
$id = $message->getId();
|
||||
// Ignore duplicate and invalid responses.
|
||||
if (isset($this->pending[$id]) && $this->matchesQuestion($message, $this->pending[$id]->question)) {
|
||||
/** @var Deferred $deferred */
|
||||
$deferred = $this->pending[$id]->deferred;
|
||||
unset($this->pending[$id]);
|
||||
$deferred->resolve($message);
|
||||
}
|
||||
if (empty($this->pending)) {
|
||||
$this->input->unreference();
|
||||
} elseif (!$this->receiving) {
|
||||
$this->input->reference();
|
||||
$this->receiving = \true;
|
||||
$this->receive()->onResolve($this->onResolve);
|
||||
}
|
||||
};
|
||||
}
|
||||
/**
|
||||
* @param \LibDNS\Records\Question $question
|
||||
* @param int $timeout
|
||||
*
|
||||
* @return \Amp\Promise<\LibDNS\Messages\Message>
|
||||
*/
|
||||
public final function ask(Question $question, int $timeout) : Promise
|
||||
{
|
||||
return call(function () use($question, $timeout) {
|
||||
$this->lastActivity = \time();
|
||||
if (\count($this->pending) > self::MAX_CONCURRENT_REQUESTS) {
|
||||
$deferred = new Deferred();
|
||||
$this->queue[] = $deferred;
|
||||
(yield $deferred->promise());
|
||||
}
|
||||
do {
|
||||
$id = \random_int(0, 0xffff);
|
||||
} while (isset($this->pending[$id]));
|
||||
$deferred = new Deferred();
|
||||
$pending = new class
|
||||
{
|
||||
use Amp\Struct;
|
||||
public $deferred;
|
||||
public $question;
|
||||
};
|
||||
$pending->deferred = $deferred;
|
||||
$pending->question = $question;
|
||||
$this->pending[$id] = $pending;
|
||||
$message = $this->createMessage($question, $id);
|
||||
try {
|
||||
(yield $this->send($message));
|
||||
} catch (StreamException $exception) {
|
||||
$exception = new DnsException("Sending the request failed", 0, $exception);
|
||||
$this->error($exception);
|
||||
throw $exception;
|
||||
}
|
||||
$this->input->reference();
|
||||
if (!$this->receiving) {
|
||||
$this->receiving = \true;
|
||||
$this->receive()->onResolve($this->onResolve);
|
||||
}
|
||||
try {
|
||||
// Work around an OPCache issue that returns an empty array with "return yield ...",
|
||||
// so assign to a variable first and return after the try block.
|
||||
//
|
||||
// See https://github.com/amphp/dns/issues/58.
|
||||
// See https://bugs.php.net/bug.php?id=74840.
|
||||
$result = (yield Promise\timeout($deferred->promise(), $timeout));
|
||||
} catch (Amp\TimeoutException $exception) {
|
||||
unset($this->pending[$id]);
|
||||
if (empty($this->pending)) {
|
||||
$this->input->unreference();
|
||||
}
|
||||
throw new TimeoutException("Didn't receive a response within {$timeout} milliseconds.");
|
||||
} finally {
|
||||
if ($this->queue) {
|
||||
$deferred = \array_shift($this->queue);
|
||||
$deferred->resolve();
|
||||
}
|
||||
}
|
||||
return $result;
|
||||
});
|
||||
}
|
||||
public final function close()
|
||||
{
|
||||
$this->input->close();
|
||||
$this->output->close();
|
||||
}
|
||||
private function error(\Throwable $exception)
|
||||
{
|
||||
$this->close();
|
||||
if (empty($this->pending)) {
|
||||
return;
|
||||
}
|
||||
if (!$exception instanceof DnsException) {
|
||||
$message = "Unexpected error during resolution: " . $exception->getMessage();
|
||||
$exception = new DnsException($message, 0, $exception);
|
||||
}
|
||||
$pending = $this->pending;
|
||||
$this->pending = [];
|
||||
foreach ($pending as $pendingQuestion) {
|
||||
/** @var Deferred $deferred */
|
||||
$deferred = $pendingQuestion->deferred;
|
||||
$deferred->fail($exception);
|
||||
}
|
||||
}
|
||||
protected final function read() : Promise
|
||||
{
|
||||
return $this->input->read();
|
||||
}
|
||||
protected final function write(string $data) : Promise
|
||||
{
|
||||
return $this->output->write($data);
|
||||
}
|
||||
protected final function createMessage(Question $question, int $id) : Message
|
||||
{
|
||||
$request = $this->messageFactory->create(MessageTypes::QUERY);
|
||||
$request->getQuestionRecords()->add($question);
|
||||
$request->isRecursionDesired(\true);
|
||||
$request->setID($id);
|
||||
return $request;
|
||||
}
|
||||
private function matchesQuestion(Message $message, Question $question) : bool
|
||||
{
|
||||
if ($message->getType() !== MessageTypes::RESPONSE) {
|
||||
return \false;
|
||||
}
|
||||
$questionRecords = $message->getQuestionRecords();
|
||||
// We only ever ask one question at a time
|
||||
if (\count($questionRecords) !== 1) {
|
||||
return \false;
|
||||
}
|
||||
$questionRecord = $questionRecords->getIterator()->current();
|
||||
if ($questionRecord->getClass() !== $question->getClass()) {
|
||||
return \false;
|
||||
}
|
||||
if ($questionRecord->getType() !== $question->getType()) {
|
||||
return \false;
|
||||
}
|
||||
if ($questionRecord->getName()->getValue() !== $question->getName()->getValue()) {
|
||||
return \false;
|
||||
}
|
||||
return \true;
|
||||
}
|
||||
}
|
@ -1,97 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace WP_Ultimo\Dependencies\Amp\Dns\Internal;
|
||||
|
||||
use WP_Ultimo\Dependencies\Amp;
|
||||
use WP_Ultimo\Dependencies\Amp\Deferred;
|
||||
use WP_Ultimo\Dependencies\Amp\Dns\DnsException;
|
||||
use WP_Ultimo\Dependencies\Amp\Dns\TimeoutException;
|
||||
use WP_Ultimo\Dependencies\Amp\Loop;
|
||||
use WP_Ultimo\Dependencies\Amp\Parser\Parser;
|
||||
use WP_Ultimo\Dependencies\Amp\Promise;
|
||||
use WP_Ultimo\Dependencies\Amp\Success;
|
||||
use WP_Ultimo\Dependencies\LibDNS\Decoder\DecoderFactory;
|
||||
use WP_Ultimo\Dependencies\LibDNS\Encoder\EncoderFactory;
|
||||
use WP_Ultimo\Dependencies\LibDNS\Messages\Message;
|
||||
use function WP_Ultimo\Dependencies\Amp\call;
|
||||
/** @internal */
|
||||
final class TcpSocket extends Socket
|
||||
{
|
||||
/** @var \LibDNS\Encoder\Encoder */
|
||||
private $encoder;
|
||||
/** @var \SplQueue */
|
||||
private $queue;
|
||||
/** @var Parser */
|
||||
private $parser;
|
||||
/** @var bool */
|
||||
private $isAlive = \true;
|
||||
public static function connect(string $uri, int $timeout = 5000) : Promise
|
||||
{
|
||||
if (!($socket = @\stream_socket_client($uri, $errno, $errstr, 0, \STREAM_CLIENT_ASYNC_CONNECT))) {
|
||||
throw new DnsException(\sprintf("Connection to %s failed: [Error #%d] %s", $uri, $errno, $errstr));
|
||||
}
|
||||
\stream_set_blocking($socket, \false);
|
||||
return call(function () use($uri, $socket, $timeout) {
|
||||
$deferred = new Deferred();
|
||||
$watcher = Loop::onWritable($socket, static function () use($socket, $deferred) {
|
||||
$deferred->resolve(new self($socket));
|
||||
});
|
||||
try {
|
||||
return (yield Promise\timeout($deferred->promise(), $timeout));
|
||||
} catch (Amp\TimeoutException $e) {
|
||||
throw new TimeoutException("Name resolution timed out, could not connect to server at {$uri}");
|
||||
} finally {
|
||||
Loop::cancel($watcher);
|
||||
}
|
||||
});
|
||||
}
|
||||
public static function parser(callable $callback) : \Generator
|
||||
{
|
||||
$decoder = (new DecoderFactory())->create();
|
||||
while (\true) {
|
||||
$length = (yield 2);
|
||||
$length = \unpack("n", $length)[1];
|
||||
$rawData = (yield $length);
|
||||
$callback($decoder->decode($rawData));
|
||||
}
|
||||
}
|
||||
protected function __construct($socket)
|
||||
{
|
||||
parent::__construct($socket);
|
||||
$this->encoder = (new EncoderFactory())->create();
|
||||
$this->queue = new \SplQueue();
|
||||
$this->parser = new Parser(self::parser([$this->queue, 'push']));
|
||||
}
|
||||
protected function send(Message $message) : Promise
|
||||
{
|
||||
$data = $this->encoder->encode($message);
|
||||
$promise = $this->write(\pack("n", \strlen($data)) . $data);
|
||||
$promise->onResolve(function ($error) {
|
||||
if ($error) {
|
||||
$this->isAlive = \false;
|
||||
}
|
||||
});
|
||||
return $promise;
|
||||
}
|
||||
protected function receive() : Promise
|
||||
{
|
||||
if ($this->queue->isEmpty()) {
|
||||
return call(function () {
|
||||
do {
|
||||
$chunk = (yield $this->read());
|
||||
if ($chunk === null) {
|
||||
$this->isAlive = \false;
|
||||
throw new DnsException("Reading from the server failed");
|
||||
}
|
||||
$this->parser->push($chunk);
|
||||
} while ($this->queue->isEmpty());
|
||||
return $this->queue->shift();
|
||||
});
|
||||
}
|
||||
return new Success($this->queue->shift());
|
||||
}
|
||||
public function isAlive() : bool
|
||||
{
|
||||
return $this->isAlive;
|
||||
}
|
||||
}
|
@ -1,51 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace WP_Ultimo\Dependencies\Amp\Dns\Internal;
|
||||
|
||||
use WP_Ultimo\Dependencies\Amp\Dns\DnsException;
|
||||
use WP_Ultimo\Dependencies\Amp\Promise;
|
||||
use WP_Ultimo\Dependencies\Amp\Success;
|
||||
use WP_Ultimo\Dependencies\LibDNS\Decoder\DecoderFactory;
|
||||
use WP_Ultimo\Dependencies\LibDNS\Encoder\EncoderFactory;
|
||||
use WP_Ultimo\Dependencies\LibDNS\Messages\Message;
|
||||
use function WP_Ultimo\Dependencies\Amp\call;
|
||||
/** @internal */
|
||||
final class UdpSocket extends Socket
|
||||
{
|
||||
/** @var \LibDNS\Encoder\Encoder */
|
||||
private $encoder;
|
||||
/** @var \LibDNS\Decoder\Decoder */
|
||||
private $decoder;
|
||||
public static function connect(string $uri) : Promise
|
||||
{
|
||||
if (!($socket = @\stream_socket_client($uri, $errno, $errstr, 0, \STREAM_CLIENT_ASYNC_CONNECT))) {
|
||||
throw new DnsException(\sprintf("Connection to %s failed: [Error #%d] %s", $uri, $errno, $errstr));
|
||||
}
|
||||
return new Success(new self($socket));
|
||||
}
|
||||
protected function __construct($socket)
|
||||
{
|
||||
parent::__construct($socket);
|
||||
$this->encoder = (new EncoderFactory())->create();
|
||||
$this->decoder = (new DecoderFactory())->create();
|
||||
}
|
||||
protected function send(Message $message) : Promise
|
||||
{
|
||||
$data = $this->encoder->encode($message);
|
||||
return $this->write($data);
|
||||
}
|
||||
protected function receive() : Promise
|
||||
{
|
||||
return call(function () {
|
||||
$data = (yield $this->read());
|
||||
if ($data === null) {
|
||||
throw new DnsException("Reading from the server failed");
|
||||
}
|
||||
return $this->decoder->decode($data);
|
||||
});
|
||||
}
|
||||
public function isAlive() : bool
|
||||
{
|
||||
return \true;
|
||||
}
|
||||
}
|
@ -1,7 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace WP_Ultimo\Dependencies\Amp\Dns;
|
||||
|
||||
class InvalidNameException extends DnsException
|
||||
{
|
||||
}
|
@ -1,7 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace WP_Ultimo\Dependencies\Amp\Dns;
|
||||
|
||||
class NoRecordException extends DnsException
|
||||
{
|
||||
}
|
98
dependencies/amphp/dns/lib/Record.php
vendored
98
dependencies/amphp/dns/lib/Record.php
vendored
@ -1,98 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace WP_Ultimo\Dependencies\Amp\Dns;
|
||||
|
||||
use WP_Ultimo\Dependencies\LibDNS\Records\ResourceQTypes;
|
||||
use WP_Ultimo\Dependencies\LibDNS\Records\ResourceTypes;
|
||||
final class Record
|
||||
{
|
||||
const A = ResourceTypes::A;
|
||||
const AAAA = ResourceTypes::AAAA;
|
||||
const AFSDB = ResourceTypes::AFSDB;
|
||||
// const APL = ResourceTypes::APL;
|
||||
const CAA = ResourceTypes::CAA;
|
||||
const CERT = ResourceTypes::CERT;
|
||||
const CNAME = ResourceTypes::CNAME;
|
||||
const DHCID = ResourceTypes::DHCID;
|
||||
const DLV = ResourceTypes::DLV;
|
||||
const DNAME = ResourceTypes::DNAME;
|
||||
const DNSKEY = ResourceTypes::DNSKEY;
|
||||
const DS = ResourceTypes::DS;
|
||||
const HINFO = ResourceTypes::HINFO;
|
||||
// const HIP = ResourceTypes::HIP;
|
||||
// const IPSECKEY = ResourceTypes::IPSECKEY;
|
||||
const KEY = ResourceTypes::KEY;
|
||||
const KX = ResourceTypes::KX;
|
||||
const ISDN = ResourceTypes::ISDN;
|
||||
const LOC = ResourceTypes::LOC;
|
||||
const MB = ResourceTypes::MB;
|
||||
const MD = ResourceTypes::MD;
|
||||
const MF = ResourceTypes::MF;
|
||||
const MG = ResourceTypes::MG;
|
||||
const MINFO = ResourceTypes::MINFO;
|
||||
const MR = ResourceTypes::MR;
|
||||
const MX = ResourceTypes::MX;
|
||||
const NAPTR = ResourceTypes::NAPTR;
|
||||
const NS = ResourceTypes::NS;
|
||||
// const NSEC = ResourceTypes::NSEC;
|
||||
// const NSEC3 = ResourceTypes::NSEC3;
|
||||
// const NSEC3PARAM = ResourceTypes::NSEC3PARAM;
|
||||
const NULL = ResourceTypes::NULL;
|
||||
const PTR = ResourceTypes::PTR;
|
||||
const RP = ResourceTypes::RP;
|
||||
// const RRSIG = ResourceTypes::RRSIG;
|
||||
const RT = ResourceTypes::RT;
|
||||
const SIG = ResourceTypes::SIG;
|
||||
const SOA = ResourceTypes::SOA;
|
||||
const SPF = ResourceTypes::SPF;
|
||||
const SRV = ResourceTypes::SRV;
|
||||
const TXT = ResourceTypes::TXT;
|
||||
const WKS = ResourceTypes::WKS;
|
||||
const X25 = ResourceTypes::X25;
|
||||
const AXFR = ResourceQTypes::AXFR;
|
||||
const MAILB = ResourceQTypes::MAILB;
|
||||
const MAILA = ResourceQTypes::MAILA;
|
||||
const ALL = ResourceQTypes::ALL;
|
||||
private $value;
|
||||
private $type;
|
||||
private $ttl;
|
||||
public function __construct(string $value, int $type, int $ttl = null)
|
||||
{
|
||||
$this->value = $value;
|
||||
$this->type = $type;
|
||||
$this->ttl = $ttl;
|
||||
}
|
||||
public function getValue() : string
|
||||
{
|
||||
return $this->value;
|
||||
}
|
||||
public function getType() : int
|
||||
{
|
||||
return $this->type;
|
||||
}
|
||||
public function getTtl()
|
||||
{
|
||||
return $this->ttl;
|
||||
}
|
||||
/**
|
||||
* Converts an record type integer back into its name as defined in this class.
|
||||
*
|
||||
* Returns "unknown (<type>)" in case a name for this record is not known.
|
||||
*
|
||||
* @param int $type Record type as integer.
|
||||
*
|
||||
* @return string Name of the constant for this record in this class.
|
||||
*/
|
||||
public static function getName(int $type) : string
|
||||
{
|
||||
static $types;
|
||||
if (0 > $type || 0xffff < $type) {
|
||||
$message = \sprintf('%d does not correspond to a valid record type (must be between 0 and 65535).', $type);
|
||||
throw new \Error($message);
|
||||
}
|
||||
if ($types === null) {
|
||||
$types = \array_flip((new \ReflectionClass(self::class))->getConstants());
|
||||
}
|
||||
return $types[$type] ?? "unknown ({$type})";
|
||||
}
|
||||
}
|
32
dependencies/amphp/dns/lib/Resolver.php
vendored
32
dependencies/amphp/dns/lib/Resolver.php
vendored
@ -1,32 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace WP_Ultimo\Dependencies\Amp\Dns;
|
||||
|
||||
use WP_Ultimo\Dependencies\Amp\Promise;
|
||||
interface Resolver
|
||||
{
|
||||
/**
|
||||
* Resolves a hostname name to an IP address [hostname as defined by RFC 3986].
|
||||
*
|
||||
* Upon success the returned promise resolves to an array of Record objects.
|
||||
*
|
||||
* A null $ttl value indicates the DNS name was resolved from the cache or the local hosts file.
|
||||
*
|
||||
* @param string $name The hostname to resolve.
|
||||
* @param int $typeRestriction Optional type restriction to `Record::A` or `Record::AAAA`, otherwise `null`.
|
||||
*
|
||||
* @return Promise
|
||||
*/
|
||||
public function resolve(string $name, int $typeRestriction = null) : Promise;
|
||||
/**
|
||||
* Query specific DNS records.
|
||||
*
|
||||
* Upon success the returned promise resolves to an array of Record objects.
|
||||
*
|
||||
* @param string $name Record to question, A, AAAA and PTR queries are automatically normalized.
|
||||
* @param int $type Use constants of Amp\Dns\Record.
|
||||
*
|
||||
* @return Promise
|
||||
*/
|
||||
public function query(string $name, int $type) : Promise;
|
||||
}
|
414
dependencies/amphp/dns/lib/Rfc1035StubResolver.php
vendored
414
dependencies/amphp/dns/lib/Rfc1035StubResolver.php
vendored
@ -1,414 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace WP_Ultimo\Dependencies\Amp\Dns;
|
||||
|
||||
use WP_Ultimo\Dependencies\Amp\Cache\ArrayCache;
|
||||
use WP_Ultimo\Dependencies\Amp\Cache\Cache;
|
||||
use WP_Ultimo\Dependencies\Amp\Dns\Internal\Socket;
|
||||
use WP_Ultimo\Dependencies\Amp\Dns\Internal\TcpSocket;
|
||||
use WP_Ultimo\Dependencies\Amp\Dns\Internal\UdpSocket;
|
||||
use WP_Ultimo\Dependencies\Amp\Loop;
|
||||
use WP_Ultimo\Dependencies\Amp\MultiReasonException;
|
||||
use WP_Ultimo\Dependencies\Amp\Promise;
|
||||
use WP_Ultimo\Dependencies\Amp\Success;
|
||||
use WP_Ultimo\Dependencies\LibDNS\Messages\Message;
|
||||
use WP_Ultimo\Dependencies\LibDNS\Records\Question;
|
||||
use WP_Ultimo\Dependencies\LibDNS\Records\QuestionFactory;
|
||||
use function WP_Ultimo\Dependencies\Amp\call;
|
||||
final class Rfc1035StubResolver implements Resolver
|
||||
{
|
||||
const CACHE_PREFIX = "amphp.dns.";
|
||||
const CONFIG_NOT_LOADED = 0;
|
||||
const CONFIG_LOADED = 1;
|
||||
const CONFIG_FAILED = 2;
|
||||
/** @var ConfigLoader */
|
||||
private $configLoader;
|
||||
/** @var QuestionFactory */
|
||||
private $questionFactory;
|
||||
/** @var Config|null */
|
||||
private $config;
|
||||
/** @var int */
|
||||
private $configStatus = self::CONFIG_NOT_LOADED;
|
||||
/** @var Promise|null */
|
||||
private $pendingConfig;
|
||||
/** @var Cache */
|
||||
private $cache;
|
||||
/** @var Socket[] */
|
||||
private $sockets = [];
|
||||
/** @var Promise[] */
|
||||
private $pendingSockets = [];
|
||||
/** @var Promise[] */
|
||||
private $pendingQueries = [];
|
||||
/** @var string */
|
||||
private $gcWatcher;
|
||||
/** @var BlockingFallbackResolver */
|
||||
private $blockingFallbackResolver;
|
||||
/** @var int */
|
||||
private $nextNameserver = 0;
|
||||
public function __construct(Cache $cache = null, ConfigLoader $configLoader = null)
|
||||
{
|
||||
$this->cache = $cache ?? new ArrayCache(5000, 256);
|
||||
$this->configLoader = $configLoader ?? (\stripos(\PHP_OS, "win") === 0 ? new WindowsConfigLoader() : new UnixConfigLoader());
|
||||
$this->questionFactory = new QuestionFactory();
|
||||
$this->blockingFallbackResolver = new BlockingFallbackResolver();
|
||||
$sockets =& $this->sockets;
|
||||
$this->gcWatcher = Loop::repeat(5000, static function () use(&$sockets) {
|
||||
if (!$sockets) {
|
||||
return;
|
||||
}
|
||||
$now = \time();
|
||||
foreach ($sockets as $key => $server) {
|
||||
if ($server->getLastActivity() < $now - 60) {
|
||||
$server->close();
|
||||
unset($sockets[$key]);
|
||||
}
|
||||
}
|
||||
});
|
||||
Loop::unreference($this->gcWatcher);
|
||||
}
|
||||
public function __destruct()
|
||||
{
|
||||
Loop::cancel($this->gcWatcher);
|
||||
}
|
||||
/** @inheritdoc */
|
||||
public function resolve(string $name, int $typeRestriction = null) : Promise
|
||||
{
|
||||
if ($typeRestriction !== null && $typeRestriction !== Record::A && $typeRestriction !== Record::AAAA) {
|
||||
throw new \Error("Invalid value for parameter 2: null|Record::A|Record::AAAA expected");
|
||||
}
|
||||
return call(function () use($name, $typeRestriction) {
|
||||
if ($this->configStatus === self::CONFIG_NOT_LOADED) {
|
||||
(yield $this->reloadConfig());
|
||||
}
|
||||
if ($this->configStatus === self::CONFIG_FAILED) {
|
||||
return $this->blockingFallbackResolver->resolve($name, $typeRestriction);
|
||||
}
|
||||
switch ($typeRestriction) {
|
||||
case Record::A:
|
||||
if (\filter_var($name, \FILTER_VALIDATE_IP, \FILTER_FLAG_IPV4)) {
|
||||
return [new Record($name, Record::A, null)];
|
||||
}
|
||||
if (\filter_var($name, \FILTER_VALIDATE_IP, \FILTER_FLAG_IPV6)) {
|
||||
throw new DnsException("Got an IPv6 address, but type is restricted to IPv4");
|
||||
}
|
||||
break;
|
||||
case Record::AAAA:
|
||||
if (\filter_var($name, \FILTER_VALIDATE_IP, \FILTER_FLAG_IPV6)) {
|
||||
return [new Record($name, Record::AAAA, null)];
|
||||
}
|
||||
if (\filter_var($name, \FILTER_VALIDATE_IP, \FILTER_FLAG_IPV4)) {
|
||||
throw new DnsException("Got an IPv4 address, but type is restricted to IPv6");
|
||||
}
|
||||
break;
|
||||
default:
|
||||
if (\filter_var($name, \FILTER_VALIDATE_IP, \FILTER_FLAG_IPV4)) {
|
||||
return [new Record($name, Record::A, null)];
|
||||
}
|
||||
if (\filter_var($name, \FILTER_VALIDATE_IP, \FILTER_FLAG_IPV6)) {
|
||||
return [new Record($name, Record::AAAA, null)];
|
||||
}
|
||||
break;
|
||||
}
|
||||
$dots = \substr_count($name, ".");
|
||||
// Should be replaced with $name[-1] from 7.1
|
||||
$trailingDot = \substr($name, -1, 1) === ".";
|
||||
$name = normalizeName($name);
|
||||
if ($records = $this->queryHosts($name, $typeRestriction)) {
|
||||
return $records;
|
||||
}
|
||||
// Follow RFC 6761 and never send queries for localhost to the caching DNS server
|
||||
// Usually, these queries are already resolved via queryHosts()
|
||||
if ($name === 'localhost') {
|
||||
return $typeRestriction === Record::AAAA ? [new Record('::1', Record::AAAA, null)] : [new Record('127.0.0.1', Record::A, null)];
|
||||
}
|
||||
$searchList = [null];
|
||||
if (!$trailingDot && $dots < $this->config->getNdots()) {
|
||||
$searchList = \array_merge($this->config->getSearchList(), $searchList);
|
||||
}
|
||||
foreach ($searchList as $searchIndex => $search) {
|
||||
for ($redirects = 0; $redirects < 5; $redirects++) {
|
||||
$searchName = $name;
|
||||
if ($search !== null) {
|
||||
$searchName = $name . "." . $search;
|
||||
}
|
||||
try {
|
||||
if ($typeRestriction) {
|
||||
return (yield $this->query($searchName, $typeRestriction));
|
||||
}
|
||||
try {
|
||||
list(, $records) = (yield Promise\some([$this->query($searchName, Record::A), $this->query($searchName, Record::AAAA)]));
|
||||
return \array_merge(...$records);
|
||||
} catch (MultiReasonException $e) {
|
||||
$errors = [];
|
||||
foreach ($e->getReasons() as $reason) {
|
||||
if ($reason instanceof NoRecordException) {
|
||||
throw $reason;
|
||||
}
|
||||
if ($searchIndex < \count($searchList) - 1 && \in_array($reason->getCode(), [2, 3], \true)) {
|
||||
continue 2;
|
||||
}
|
||||
$errors[] = $reason->getMessage();
|
||||
}
|
||||
throw new DnsException("All query attempts failed for {$searchName}: " . \implode(", ", $errors), 0, $e);
|
||||
}
|
||||
} catch (NoRecordException $e) {
|
||||
try {
|
||||
/** @var Record[] $cnameRecords */
|
||||
$cnameRecords = (yield $this->query($searchName, Record::CNAME));
|
||||
$name = $cnameRecords[0]->getValue();
|
||||
continue;
|
||||
} catch (NoRecordException $e) {
|
||||
/** @var Record[] $dnameRecords */
|
||||
$dnameRecords = (yield $this->query($searchName, Record::DNAME));
|
||||
$name = $dnameRecords[0]->getValue();
|
||||
continue;
|
||||
}
|
||||
} catch (DnsException $e) {
|
||||
if ($searchIndex < \count($searchList) - 1 && \in_array($e->getCode(), [2, 3], \true)) {
|
||||
continue 2;
|
||||
}
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
}
|
||||
throw new DnsException("Giving up resolution of '{$searchName}', too many redirects");
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Reloads the configuration in the background.
|
||||
*
|
||||
* Once it's finished, the configuration will be used for new requests.
|
||||
*
|
||||
* @return Promise
|
||||
*/
|
||||
public function reloadConfig() : Promise
|
||||
{
|
||||
if ($this->pendingConfig) {
|
||||
return $this->pendingConfig;
|
||||
}
|
||||
$promise = call(function () {
|
||||
try {
|
||||
$this->config = (yield $this->configLoader->loadConfig());
|
||||
$this->configStatus = self::CONFIG_LOADED;
|
||||
} catch (ConfigException $e) {
|
||||
$this->configStatus = self::CONFIG_FAILED;
|
||||
try {
|
||||
\trigger_error("Could not load the system's DNS configuration, using synchronous, blocking fallback", \E_USER_WARNING);
|
||||
} catch (\Throwable $triggerException) {
|
||||
\set_error_handler(null);
|
||||
\trigger_error("Could not load the system's DNS configuration, using synchronous, blocking fallback", \E_USER_WARNING);
|
||||
\restore_error_handler();
|
||||
}
|
||||
}
|
||||
});
|
||||
$this->pendingConfig = $promise;
|
||||
$promise->onResolve(function () {
|
||||
$this->pendingConfig = null;
|
||||
});
|
||||
return $promise;
|
||||
}
|
||||
/** @inheritdoc */
|
||||
public function query(string $name, int $type) : Promise
|
||||
{
|
||||
$pendingQueryKey = $type . " " . $name;
|
||||
if (isset($this->pendingQueries[$pendingQueryKey])) {
|
||||
return $this->pendingQueries[$pendingQueryKey];
|
||||
}
|
||||
$promise = call(function () use($name, $type) {
|
||||
if ($this->configStatus === self::CONFIG_NOT_LOADED) {
|
||||
(yield $this->reloadConfig());
|
||||
}
|
||||
if ($this->configStatus === self::CONFIG_FAILED) {
|
||||
return $this->blockingFallbackResolver->query($name, $type);
|
||||
}
|
||||
$name = $this->normalizeName($name, $type);
|
||||
$question = $this->createQuestion($name, $type);
|
||||
if (null !== ($cachedValue = (yield $this->cache->get($this->getCacheKey($name, $type))))) {
|
||||
return $this->decodeCachedResult($name, $type, $cachedValue);
|
||||
}
|
||||
$nameservers = $this->selectNameservers();
|
||||
$nameserversCount = \count($nameservers);
|
||||
$attempts = $this->config->getAttempts();
|
||||
$protocol = "udp";
|
||||
$attempt = 0;
|
||||
/** @var Socket $socket */
|
||||
$uri = $protocol . "://" . $nameservers[0];
|
||||
$socket = (yield $this->getSocket($uri));
|
||||
$attemptDescription = [];
|
||||
while ($attempt < $attempts) {
|
||||
try {
|
||||
if (!$socket->isAlive()) {
|
||||
unset($this->sockets[$uri]);
|
||||
$socket->close();
|
||||
$uri = $protocol . "://" . $nameservers[$attempt % $nameserversCount];
|
||||
$socket = (yield $this->getSocket($uri));
|
||||
}
|
||||
$attemptDescription[] = $uri;
|
||||
/** @var Message $response */
|
||||
$response = (yield $socket->ask($question, $this->config->getTimeout()));
|
||||
$this->assertAcceptableResponse($response, $name);
|
||||
// UDP sockets are never reused, they're not in the $this->sockets map
|
||||
if ($protocol === "udp") {
|
||||
// Defer call, because it interferes with the unreference() call in Internal\Socket otherwise
|
||||
Loop::defer(static function () use($socket) {
|
||||
$socket->close();
|
||||
});
|
||||
}
|
||||
if ($response->isTruncated()) {
|
||||
if ($protocol !== "tcp") {
|
||||
// Retry with TCP, don't count attempt
|
||||
$protocol = "tcp";
|
||||
$uri = $protocol . "://" . $nameservers[$attempt % $nameserversCount];
|
||||
$socket = (yield $this->getSocket($uri));
|
||||
continue;
|
||||
}
|
||||
throw new DnsException("Server returned a truncated response for '{$name}' (" . Record::getName($type) . ")");
|
||||
}
|
||||
$answers = $response->getAnswerRecords();
|
||||
$result = [];
|
||||
$ttls = [];
|
||||
/** @var \LibDNS\Records\Resource $record */
|
||||
foreach ($answers as $record) {
|
||||
$recordType = $record->getType();
|
||||
$result[$recordType][] = (string) $record->getData();
|
||||
// Cache for max one day
|
||||
$ttls[$recordType] = \min($ttls[$recordType] ?? 86400, $record->getTTL());
|
||||
}
|
||||
foreach ($result as $recordType => $records) {
|
||||
// We don't care here whether storing in the cache fails
|
||||
$this->cache->set($this->getCacheKey($name, $recordType), \json_encode($records), $ttls[$recordType]);
|
||||
}
|
||||
if (!isset($result[$type])) {
|
||||
// "it MUST NOT cache it for longer than five (5) minutes" per RFC 2308 section 7.1
|
||||
$this->cache->set($this->getCacheKey($name, $type), \json_encode([]), 300);
|
||||
throw new NoRecordException("No records returned for '{$name}' (" . Record::getName($type) . ")");
|
||||
}
|
||||
return \array_map(static function ($data) use($type, $ttls) {
|
||||
return new Record($data, $type, $ttls[$type]);
|
||||
}, $result[$type]);
|
||||
} catch (TimeoutException $e) {
|
||||
// Defer call, because it might interfere with the unreference() call in Internal\Socket otherwise
|
||||
Loop::defer(function () use($socket, $uri) {
|
||||
unset($this->sockets[$uri]);
|
||||
$socket->close();
|
||||
});
|
||||
$uri = $protocol . "://" . $nameservers[++$attempt % $nameserversCount];
|
||||
$socket = (yield $this->getSocket($uri));
|
||||
continue;
|
||||
}
|
||||
}
|
||||
throw new TimeoutException(\sprintf("No response for '%s' (%s) from any nameserver within %d ms after %d attempts, tried %s", $name, Record::getName($type), $this->config->getTimeout(), $attempts, \implode(", ", $attemptDescription)));
|
||||
});
|
||||
$this->pendingQueries[$type . " " . $name] = $promise;
|
||||
$promise->onResolve(function () use($name, $type) {
|
||||
unset($this->pendingQueries[$type . " " . $name]);
|
||||
});
|
||||
return $promise;
|
||||
}
|
||||
private function queryHosts(string $name, int $typeRestriction = null) : array
|
||||
{
|
||||
$hosts = $this->config->getKnownHosts();
|
||||
$records = [];
|
||||
$returnIPv4 = $typeRestriction === null || $typeRestriction === Record::A;
|
||||
$returnIPv6 = $typeRestriction === null || $typeRestriction === Record::AAAA;
|
||||
if ($returnIPv4 && isset($hosts[Record::A][$name])) {
|
||||
$records[] = new Record($hosts[Record::A][$name], Record::A, null);
|
||||
}
|
||||
if ($returnIPv6 && isset($hosts[Record::AAAA][$name])) {
|
||||
$records[] = new Record($hosts[Record::AAAA][$name], Record::AAAA, null);
|
||||
}
|
||||
return $records;
|
||||
}
|
||||
private function normalizeName(string $name, int $type)
|
||||
{
|
||||
if ($type === Record::PTR) {
|
||||
if (($packedIp = @\inet_pton($name)) !== \false) {
|
||||
if (isset($packedIp[4])) {
|
||||
// IPv6
|
||||
$name = \wordwrap(\strrev(\bin2hex($packedIp)), 1, ".", \true) . ".ip6.arpa";
|
||||
} else {
|
||||
// IPv4
|
||||
$name = \inet_ntop(\strrev($packedIp)) . ".in-addr.arpa";
|
||||
}
|
||||
}
|
||||
} elseif (\in_array($type, [Record::A, Record::AAAA], \true)) {
|
||||
$name = normalizeName($name);
|
||||
}
|
||||
return $name;
|
||||
}
|
||||
/**
|
||||
* @param string $name
|
||||
* @param int $type
|
||||
*
|
||||
* @return Question
|
||||
*/
|
||||
private function createQuestion(string $name, int $type) : Question
|
||||
{
|
||||
if (0 > $type || 0xffff < $type) {
|
||||
$message = \sprintf('%d does not correspond to a valid record type (must be between 0 and 65535).', $type);
|
||||
throw new \Error($message);
|
||||
}
|
||||
$question = $this->questionFactory->create($type);
|
||||
$question->setName($name);
|
||||
return $question;
|
||||
}
|
||||
private function getCacheKey(string $name, int $type) : string
|
||||
{
|
||||
return self::CACHE_PREFIX . $name . "#" . $type;
|
||||
}
|
||||
private function decodeCachedResult(string $name, int $type, string $encoded) : array
|
||||
{
|
||||
$decoded = \json_decode($encoded, \true);
|
||||
if (!$decoded) {
|
||||
throw new NoRecordException("No records returned for {$name} (cached result)");
|
||||
}
|
||||
$result = [];
|
||||
foreach ($decoded as $data) {
|
||||
$result[] = new Record($data, $type);
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
private function getSocket($uri) : Promise
|
||||
{
|
||||
// We use a new socket for each UDP request, as that increases the entropy and mitigates response forgery.
|
||||
if (\substr($uri, 0, 3) === "udp") {
|
||||
return UdpSocket::connect($uri);
|
||||
}
|
||||
// Over TCP we might reuse sockets if the server allows to keep them open. Sequence IDs in TCP are already
|
||||
// better than a random port. Additionally, a TCP connection is more expensive.
|
||||
if (isset($this->sockets[$uri])) {
|
||||
return new Success($this->sockets[$uri]);
|
||||
}
|
||||
if (isset($this->pendingSockets[$uri])) {
|
||||
return $this->pendingSockets[$uri];
|
||||
}
|
||||
$server = TcpSocket::connect($uri);
|
||||
$server->onResolve(function ($error, $server) use($uri) {
|
||||
unset($this->pendingSockets[$uri]);
|
||||
if (!$error) {
|
||||
$this->sockets[$uri] = $server;
|
||||
}
|
||||
});
|
||||
return $server;
|
||||
}
|
||||
/**
|
||||
* @throws DnsException
|
||||
*/
|
||||
private function assertAcceptableResponse(Message $response, string $name)
|
||||
{
|
||||
if ($response->getResponseCode() !== 0) {
|
||||
// https://www.iana.org/assignments/dns-parameters/dns-parameters.xhtml
|
||||
$errors = [1 => 'FormErr', 2 => 'ServFail', 3 => 'NXDomain', 4 => 'NotImp', 5 => 'Refused', 6 => 'YXDomain', 7 => 'YXRRSet', 8 => 'NXRRSet', 9 => 'NotAuth', 10 => 'NotZone', 11 => 'DSOTYPENI', 16 => 'BADVERS', 17 => 'BADKEY', 18 => 'BADTIME', 19 => 'BADMODE', 20 => 'BADNAME', 21 => 'BADALG', 22 => 'BADTRUNC', 23 => 'BADCOOKIE'];
|
||||
throw new DnsException(\sprintf("Name resolution failed for '%s'; server returned error code: %d (%s)", $name, $response->getResponseCode(), $errors[$response->getResponseCode()] ?? 'UNKNOWN'), $response->getResponseCode());
|
||||
}
|
||||
}
|
||||
private function selectNameservers() : array
|
||||
{
|
||||
$nameservers = $this->config->getNameservers();
|
||||
if ($this->config->isRotationEnabled() && ($nameserversCount = \count($nameservers)) > 1) {
|
||||
$nameservers = \array_merge(\array_slice($nameservers, $this->nextNameserver), \array_slice($nameservers, 0, $this->nextNameserver));
|
||||
$this->nextNameserver = ++$this->nextNameserver % $nameserversCount;
|
||||
}
|
||||
return $nameservers;
|
||||
}
|
||||
}
|
@ -1,7 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace WP_Ultimo\Dependencies\Amp\Dns;
|
||||
|
||||
class TimeoutException extends DnsException
|
||||
{
|
||||
}
|
158
dependencies/amphp/dns/lib/UnixConfigLoader.php
vendored
158
dependencies/amphp/dns/lib/UnixConfigLoader.php
vendored
@ -1,158 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace WP_Ultimo\Dependencies\Amp\Dns;
|
||||
|
||||
use WP_Ultimo\Dependencies\Amp\Failure;
|
||||
use WP_Ultimo\Dependencies\Amp\Promise;
|
||||
use WP_Ultimo\Dependencies\Amp\Success;
|
||||
use function WP_Ultimo\Dependencies\Amp\call;
|
||||
class UnixConfigLoader implements ConfigLoader
|
||||
{
|
||||
const MAX_NAMESERVERS = 3;
|
||||
const MAX_DNS_SEARCH = 6;
|
||||
const MAX_TIMEOUT = 30 * 1000;
|
||||
const MAX_ATTEMPTS = 5;
|
||||
const MAX_NDOTS = 15;
|
||||
const DEFAULT_TIMEOUT = 5 * 1000;
|
||||
const DEFAULT_ATTEMPTS = 2;
|
||||
const DEFAULT_NDOTS = 1;
|
||||
const DEFAULT_OPTIONS = ["timeout" => self::DEFAULT_TIMEOUT, "attempts" => self::DEFAULT_ATTEMPTS, "ndots" => self::DEFAULT_NDOTS, "rotate" => \false];
|
||||
private $path;
|
||||
private $hostLoader;
|
||||
public function __construct(string $path = "/etc/resolv.conf", HostLoader $hostLoader = null)
|
||||
{
|
||||
$this->path = $path;
|
||||
$this->hostLoader = $hostLoader ?? new HostLoader();
|
||||
}
|
||||
protected function readFile(string $path) : Promise
|
||||
{
|
||||
\set_error_handler(function (int $errno, string $message) use($path) {
|
||||
throw new ConfigException("Could not read configuration file '{$path}' ({$errno}) {$message}");
|
||||
});
|
||||
try {
|
||||
// Blocking file access, but this file should be local and usually loaded only once.
|
||||
$fileContent = \file_get_contents($path);
|
||||
} catch (ConfigException $exception) {
|
||||
return new Failure($exception);
|
||||
} finally {
|
||||
\restore_error_handler();
|
||||
}
|
||||
return new Success($fileContent);
|
||||
}
|
||||
public final function loadConfig() : Promise
|
||||
{
|
||||
return call(function () {
|
||||
$nameservers = [];
|
||||
$searchList = [];
|
||||
$options = self::DEFAULT_OPTIONS;
|
||||
$haveLocaldomainEnv = \false;
|
||||
/* Allow user to override the local domain definition. */
|
||||
if ($localdomain = \getenv("LOCALDOMAIN")) {
|
||||
/* Set search list to be blank-separated strings from rest of
|
||||
env value. Permits users of LOCALDOMAIN to still have a
|
||||
search list, and anyone to set the one that they want to use
|
||||
as an individual (even more important now that the rfc1535
|
||||
stuff restricts searches). */
|
||||
$searchList = $this->splitOnWhitespace($localdomain);
|
||||
$haveLocaldomainEnv = \true;
|
||||
}
|
||||
$fileContent = (yield $this->readFile($this->path));
|
||||
$lines = \explode("\n", $fileContent);
|
||||
foreach ($lines as $line) {
|
||||
$line = \preg_split('#\\s+#', $line, 2);
|
||||
if (\count($line) !== 2) {
|
||||
continue;
|
||||
}
|
||||
list($type, $value) = $line;
|
||||
if ($type === "nameserver") {
|
||||
if (\count($nameservers) === self::MAX_NAMESERVERS) {
|
||||
continue;
|
||||
}
|
||||
$value = \trim($value);
|
||||
$ip = @\inet_pton($value);
|
||||
if ($ip === \false) {
|
||||
continue;
|
||||
}
|
||||
if (isset($ip[15])) {
|
||||
// IPv6
|
||||
$nameservers[] = "[" . $value . "]:53";
|
||||
} else {
|
||||
// IPv4
|
||||
$nameservers[] = $value . ":53";
|
||||
}
|
||||
} elseif ($type === "domain" && !$haveLocaldomainEnv) {
|
||||
// LOCALDOMAIN env overrides config
|
||||
$searchList = $this->splitOnWhitespace($value);
|
||||
} elseif ($type === "search" && !$haveLocaldomainEnv) {
|
||||
// LOCALDOMAIN env overrides config
|
||||
$searchList = $this->splitOnWhitespace($value);
|
||||
} elseif ($type === "options") {
|
||||
$option = $this->parseOption($value);
|
||||
if (\count($option) === 2) {
|
||||
$options[$option[0]] = $option[1];
|
||||
}
|
||||
}
|
||||
}
|
||||
$hosts = (yield $this->hostLoader->loadHosts());
|
||||
if (\count($searchList) === 0) {
|
||||
$hostname = \gethostname();
|
||||
$dot = \strpos(".", $hostname);
|
||||
if ($dot !== \false && $dot < \strlen($hostname)) {
|
||||
$searchList = [\substr($hostname, $dot)];
|
||||
}
|
||||
}
|
||||
if (\count($searchList) > self::MAX_DNS_SEARCH) {
|
||||
$searchList = \array_slice($searchList, 0, self::MAX_DNS_SEARCH);
|
||||
}
|
||||
$resOptions = \getenv("RES_OPTIONS");
|
||||
if ($resOptions) {
|
||||
foreach ($this->splitOnWhitespace($resOptions) as $option) {
|
||||
$option = $this->parseOption($option);
|
||||
if (\count($option) === 2) {
|
||||
$options[$option[0]] = $option[1];
|
||||
}
|
||||
}
|
||||
}
|
||||
$config = new Config($nameservers, $hosts, $options["timeout"], $options["attempts"]);
|
||||
return $config->withSearchList($searchList)->withNdots($options["ndots"])->withRotationEnabled($options["rotate"]);
|
||||
});
|
||||
}
|
||||
private function splitOnWhitespace(string $names) : array
|
||||
{
|
||||
return \preg_split("#\\s+#", \trim($names));
|
||||
}
|
||||
private function parseOption(string $option) : array
|
||||
{
|
||||
$optline = \explode(':', $option, 2);
|
||||
list($name, $value) = $optline + [1 => null];
|
||||
switch ($name) {
|
||||
case "timeout":
|
||||
$value = (int) $value;
|
||||
if ($value < 0) {
|
||||
return [];
|
||||
// don't overwrite option value
|
||||
}
|
||||
// The value for this option is silently capped to 30s
|
||||
return ["timeout", (int) \min($value * 1000, self::MAX_TIMEOUT)];
|
||||
case "attempts":
|
||||
$value = (int) $value;
|
||||
if ($value < 0) {
|
||||
return [];
|
||||
// don't overwrite option value
|
||||
}
|
||||
// The value for this option is silently capped to 5
|
||||
return ["attempts", (int) \min($value, self::MAX_ATTEMPTS)];
|
||||
case "ndots":
|
||||
$value = (int) $value;
|
||||
if ($value < 0) {
|
||||
return [];
|
||||
// don't overwrite option value
|
||||
}
|
||||
// The value for this option is silently capped to 15
|
||||
return ["ndots", (int) \min($value, self::MAX_NDOTS)];
|
||||
case "rotate":
|
||||
return ["rotate", \true];
|
||||
}
|
||||
return [];
|
||||
}
|
||||
}
|
@ -1,68 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace WP_Ultimo\Dependencies\Amp\Dns;
|
||||
|
||||
use WP_Ultimo\Dependencies\Amp\Promise;
|
||||
use WP_Ultimo\Dependencies\Amp\WindowsRegistry\KeyNotFoundException;
|
||||
use WP_Ultimo\Dependencies\Amp\WindowsRegistry\WindowsRegistry;
|
||||
use function WP_Ultimo\Dependencies\Amp\call;
|
||||
final class WindowsConfigLoader implements ConfigLoader
|
||||
{
|
||||
private $hostLoader;
|
||||
public function __construct(HostLoader $hostLoader = null)
|
||||
{
|
||||
$this->hostLoader = $hostLoader ?? new HostLoader();
|
||||
}
|
||||
public function loadConfig() : Promise
|
||||
{
|
||||
return call(function () {
|
||||
$keys = ["WP_Ultimo\\Dependencies\\HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\NameServer", "WP_Ultimo\\Dependencies\\HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\DhcpNameServer"];
|
||||
$reader = new WindowsRegistry();
|
||||
$nameserver = "";
|
||||
while ($nameserver === "" && ($key = \array_shift($keys))) {
|
||||
try {
|
||||
$nameserver = (yield $reader->read($key));
|
||||
} catch (KeyNotFoundException $e) {
|
||||
// retry other possible locations
|
||||
}
|
||||
}
|
||||
if ($nameserver === "") {
|
||||
$interfaces = "WP_Ultimo\\Dependencies\\HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\Interfaces";
|
||||
$subKeys = (yield $reader->listKeys($interfaces));
|
||||
foreach ($subKeys as $key) {
|
||||
foreach (["NameServer", "DhcpNameServer"] as $property) {
|
||||
try {
|
||||
$nameserver = (yield $reader->read("{$key}\\{$property}"));
|
||||
if ($nameserver !== "") {
|
||||
break 2;
|
||||
}
|
||||
} catch (KeyNotFoundException $e) {
|
||||
// retry other possible locations
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if ($nameserver === "") {
|
||||
throw new ConfigException("Could not find a nameserver in the Windows Registry");
|
||||
}
|
||||
$nameservers = [];
|
||||
// Microsoft documents space as delimiter, AppVeyor uses comma, we just accept both
|
||||
foreach (\explode(" ", \strtr($nameserver, ",", " ")) as $nameserver) {
|
||||
$nameserver = \trim($nameserver);
|
||||
$ip = @\inet_pton($nameserver);
|
||||
if ($ip === \false) {
|
||||
continue;
|
||||
}
|
||||
if (isset($ip[15])) {
|
||||
// IPv6
|
||||
$nameservers[] = "[" . $nameserver . "]:53";
|
||||
} else {
|
||||
// IPv4
|
||||
$nameservers[] = $nameserver . ":53";
|
||||
}
|
||||
}
|
||||
$hosts = (yield $this->hostLoader->loadHosts());
|
||||
return new Config($nameservers, $hosts);
|
||||
});
|
||||
}
|
||||
}
|
92
dependencies/amphp/dns/lib/functions.php
vendored
92
dependencies/amphp/dns/lib/functions.php
vendored
@ -1,92 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace WP_Ultimo\Dependencies\Amp\Dns;
|
||||
|
||||
use WP_Ultimo\Dependencies\Amp\Loop;
|
||||
use WP_Ultimo\Dependencies\Amp\Promise;
|
||||
const LOOP_STATE_IDENTIFIER = Resolver::class;
|
||||
/**
|
||||
* Retrieve the application-wide dns resolver instance.
|
||||
*
|
||||
* @param \Amp\Dns\Resolver $resolver Optionally specify a new default dns resolver instance
|
||||
*
|
||||
* @return \Amp\Dns\Resolver Returns the application-wide dns resolver instance
|
||||
*/
|
||||
function resolver(Resolver $resolver = null) : Resolver
|
||||
{
|
||||
if ($resolver === null) {
|
||||
$resolver = Loop::getState(LOOP_STATE_IDENTIFIER);
|
||||
if ($resolver) {
|
||||
return $resolver;
|
||||
}
|
||||
$resolver = createDefaultResolver();
|
||||
}
|
||||
Loop::setState(LOOP_STATE_IDENTIFIER, $resolver);
|
||||
return $resolver;
|
||||
}
|
||||
/**
|
||||
* Create a new dns resolver best-suited for the current environment.
|
||||
*
|
||||
* @return \Amp\Dns\Resolver
|
||||
*/
|
||||
function createDefaultResolver() : Resolver
|
||||
{
|
||||
return new Rfc1035StubResolver();
|
||||
}
|
||||
/**
|
||||
* @see Resolver::resolve()
|
||||
*/
|
||||
function resolve(string $name, int $typeRestriction = null) : Promise
|
||||
{
|
||||
return resolver()->resolve($name, $typeRestriction);
|
||||
}
|
||||
/**
|
||||
* @see Resolver::query()
|
||||
*/
|
||||
function query(string $name, int $type) : Promise
|
||||
{
|
||||
return resolver()->query($name, $type);
|
||||
}
|
||||
/**
|
||||
* Checks whether a string is a valid DNS name.
|
||||
*
|
||||
* @param string $name String to check.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
function isValidName(string $name)
|
||||
{
|
||||
try {
|
||||
normalizeName($name);
|
||||
return \true;
|
||||
} catch (InvalidNameException $e) {
|
||||
return \false;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Normalizes a DNS name and automatically checks it for validity.
|
||||
*
|
||||
* @param string $name DNS name.
|
||||
*
|
||||
* @return string Normalized DNS name.
|
||||
* @throws InvalidNameException If an invalid name or an IDN name without ext/intl being installed has been passed.
|
||||
*/
|
||||
function normalizeName(string $name) : string
|
||||
{
|
||||
static $pattern = '/^(?<name>[a-z0-9]([a-z0-9-_]{0,61}[a-z0-9])?)(\\.(?&name))*\\.?$/i';
|
||||
if (\function_exists('idn_to_ascii') && \defined('INTL_IDNA_VARIANT_UTS46')) {
|
||||
if (\false === ($result = \idn_to_ascii($name, 0, \INTL_IDNA_VARIANT_UTS46))) {
|
||||
throw new InvalidNameException("Name '{$name}' could not be processed for IDN.");
|
||||
}
|
||||
$name = $result;
|
||||
} elseif (\preg_match('/[\\x80-\\xff]/', $name)) {
|
||||
throw new InvalidNameException("Name '{$name}' contains non-ASCII characters and IDN support is not available. " . "Verify that ext/intl is installed for IDN support and that ICU is at least version 4.6.");
|
||||
}
|
||||
if (isset($name[253]) || !\preg_match($pattern, $name)) {
|
||||
throw new InvalidNameException("Name '{$name}' is not a valid hostname.");
|
||||
}
|
||||
if ($name[\strlen($name) - 1] === '.') {
|
||||
$name = \substr($name, 0, -1);
|
||||
}
|
||||
return $name;
|
||||
}
|
44
dependencies/amphp/hpack/src/HPack.php
vendored
44
dependencies/amphp/hpack/src/HPack.php
vendored
@ -1,44 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace WP_Ultimo\Dependencies\Amp\Http;
|
||||
|
||||
use WP_Ultimo\Dependencies\Amp\Http\Internal\HPackNative;
|
||||
use WP_Ultimo\Dependencies\Amp\Http\Internal\HPackNghttp2;
|
||||
/**
|
||||
* @psalm-type HeaderArray = list<array{string, string}>
|
||||
*/
|
||||
final class HPack
|
||||
{
|
||||
/** @var HPackNative|HPackNghttp2 */
|
||||
private $implementation;
|
||||
public function __construct(int $tableSizeLimit = 4096)
|
||||
{
|
||||
if (HPackNghttp2::isSupported()) {
|
||||
$this->implementation = new HPackNghttp2($tableSizeLimit);
|
||||
} else {
|
||||
$this->implementation = new HPackNative($tableSizeLimit);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* @param string $input Input to decode.
|
||||
* @param int $maxSize Maximum deflated size.
|
||||
*
|
||||
* @return array|null Decoded headers.
|
||||
*/
|
||||
public function decode(string $input, int $maxSize) : ?array
|
||||
{
|
||||
return $this->implementation->decode($input, $maxSize);
|
||||
}
|
||||
/**
|
||||
* @param HeaderArray $headers Headers to encode.
|
||||
*
|
||||
* @return string Encoded headers.
|
||||
*
|
||||
* @throws HPackException If encoding fails.
|
||||
*/
|
||||
public function encode(array $headers) : string
|
||||
{
|
||||
return $this->implementation->encode($headers);
|
||||
}
|
||||
}
|
@ -1,8 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace WP_Ultimo\Dependencies\Amp\Http;
|
||||
|
||||
final class HPackException extends \Exception
|
||||
{
|
||||
}
|
1082
dependencies/amphp/hpack/src/Internal/HPackNative.php
vendored
1082
dependencies/amphp/hpack/src/Internal/HPackNative.php
vendored
File diff suppressed because it is too large
Load Diff
@ -1,203 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace WP_Ultimo\Dependencies\Amp\Http\Internal;
|
||||
|
||||
use WP_Ultimo\Dependencies\Amp\Http\HPack;
|
||||
use WP_Ultimo\Dependencies\Amp\Http\HPackException;
|
||||
use FFI;
|
||||
/**
|
||||
* @internal
|
||||
* @psalm-import-type HeaderArray from HPack
|
||||
*/
|
||||
final class HPackNghttp2
|
||||
{
|
||||
private const FLAG_NO_INDEX = 0x1;
|
||||
private const FLAG_NO_COPY_NAME = 0x2;
|
||||
private const FLAG_NO_COPY_VALUE = 0x4;
|
||||
private const FLAG_NO_COPY = self::FLAG_NO_COPY_NAME | self::FLAG_NO_COPY_VALUE;
|
||||
private const FLAG_NO_COPY_SENSITIVE = self::FLAG_NO_COPY | self::FLAG_NO_INDEX;
|
||||
private const SENSITIVE_HEADERS = ['authorization' => self::FLAG_NO_COPY_SENSITIVE, 'cookie' => self::FLAG_NO_COPY_SENSITIVE, 'proxy-authorization' => self::FLAG_NO_COPY_SENSITIVE, 'set-cookie' => self::FLAG_NO_COPY_SENSITIVE];
|
||||
private static $ffi;
|
||||
private static $deflatePtrType;
|
||||
private static $inflatePtrType;
|
||||
private static $nvType;
|
||||
private static $nvSize;
|
||||
private static $charType;
|
||||
private static $uint8Type;
|
||||
private static $uint8PtrType;
|
||||
private static $decodeNv;
|
||||
private static $decodeNvPtr;
|
||||
private static $decodeFlags;
|
||||
private static $decodeFlagsPtr;
|
||||
private static $supported;
|
||||
public static function isSupported() : bool
|
||||
{
|
||||
if (isset(self::$supported)) {
|
||||
return self::$supported;
|
||||
}
|
||||
if (!\extension_loaded('ffi')) {
|
||||
return self::$supported = \false;
|
||||
}
|
||||
if (!\class_exists(FFI::class)) {
|
||||
return self::$supported = \false;
|
||||
}
|
||||
try {
|
||||
self::init();
|
||||
return self::$supported = \true;
|
||||
} catch (\Throwable $e) {
|
||||
return self::$supported = \false;
|
||||
}
|
||||
}
|
||||
private static function init() : void
|
||||
{
|
||||
if (self::$ffi !== null) {
|
||||
return;
|
||||
}
|
||||
$header = \file_get_contents(__DIR__ . '/amp-hpack.h');
|
||||
$files = ['libnghttp2.so.14', 'libnghttp2.so', 'libnghttp2.dylib', '/opt/homebrew/lib/libnghttp2.dylib'];
|
||||
$error = null;
|
||||
foreach ($files as $file) {
|
||||
try {
|
||||
self::$ffi = FFI::cdef($header, $file);
|
||||
$error = null;
|
||||
break;
|
||||
} catch (\Throwable $exception) {
|
||||
$error = $error ?? $exception;
|
||||
}
|
||||
}
|
||||
if ($error) {
|
||||
throw $error;
|
||||
}
|
||||
self::$deflatePtrType = self::$ffi->type('nghttp2_hd_deflater*');
|
||||
self::$inflatePtrType = self::$ffi->type('nghttp2_hd_inflater*');
|
||||
self::$nvType = self::$ffi->type('nghttp2_nv');
|
||||
self::$nvSize = FFI::sizeof(self::$nvType);
|
||||
self::$charType = self::$ffi->type('char');
|
||||
self::$uint8Type = self::$ffi->type('uint8_t');
|
||||
self::$uint8PtrType = self::$ffi->type('uint8_t*');
|
||||
self::$decodeNv = self::$ffi->new(self::$nvType);
|
||||
self::$decodeNvPtr = FFI::addr(self::$decodeNv);
|
||||
self::$decodeFlags = self::$ffi->new('int');
|
||||
self::$decodeFlagsPtr = FFI::addr(self::$decodeFlags);
|
||||
}
|
||||
private static function createBufferFromString(string $value) : ?FFI\CData
|
||||
{
|
||||
$length = \strlen($value);
|
||||
if (!$length) {
|
||||
return null;
|
||||
}
|
||||
$buffer = self::$ffi->new(FFI::arrayType(self::$uint8Type, [$length]));
|
||||
FFI::memcpy($buffer, $value, $length);
|
||||
return $buffer;
|
||||
}
|
||||
private $deflatePtr;
|
||||
private $inflatePtr;
|
||||
/**
|
||||
* @param int $maxSize Upper limit on table size.
|
||||
*/
|
||||
public function __construct(int $maxSize = 4096)
|
||||
{
|
||||
self::init();
|
||||
$this->deflatePtr = self::$ffi->new(self::$deflatePtrType);
|
||||
$this->inflatePtr = self::$ffi->new(self::$inflatePtrType);
|
||||
$return = self::$ffi->nghttp2_hd_deflate_new(FFI::addr($this->deflatePtr), $maxSize);
|
||||
if ($return !== 0) {
|
||||
throw new \RuntimeException('Failed to init deflate context');
|
||||
}
|
||||
$return = self::$ffi->nghttp2_hd_inflate_new(FFI::addr($this->inflatePtr));
|
||||
if ($return !== 0) {
|
||||
throw new \RuntimeException('Failed to init inflate context');
|
||||
}
|
||||
}
|
||||
/**
|
||||
* @param string $input Encoded headers.
|
||||
* @param int $maxSize Maximum length of the decoded header string.
|
||||
*
|
||||
* @return string[][]|null Returns null if decoding fails or if $maxSize is exceeded.
|
||||
*/
|
||||
public function decode(string $input, int $maxSize) : ?array
|
||||
{
|
||||
$ffi = self::$ffi;
|
||||
$pair = self::$decodeNv;
|
||||
$pairPtr = self::$decodeNvPtr;
|
||||
$flags = self::$decodeFlags;
|
||||
$flagsPtr = self::$decodeFlagsPtr;
|
||||
$inflate = $this->inflatePtr;
|
||||
$size = 0;
|
||||
$bufferLength = \strlen($input);
|
||||
$buffer = self::createBufferFromString($input);
|
||||
if ($buffer === null) {
|
||||
return [];
|
||||
}
|
||||
$offset = 0;
|
||||
$bufferPtr = $ffi->cast(self::$uint8PtrType, $buffer);
|
||||
$headers = [];
|
||||
while (\true) {
|
||||
$read = $ffi->nghttp2_hd_inflate_hd2($inflate, $pairPtr, $flagsPtr, $bufferPtr, $bufferLength - $offset, 1);
|
||||
if ($read < 0) {
|
||||
return null;
|
||||
}
|
||||
$offset += $read;
|
||||
$bufferPtr += $read;
|
||||
$cFlags = $flags->cdata;
|
||||
if ($cFlags & 0x2) {
|
||||
// NGHTTP2_HD_INFLATE_EMIT
|
||||
$nameLength = $pair->namelen;
|
||||
$valueLength = $pair->valuelen;
|
||||
$headers[] = [FFI::string($pair->name, $nameLength), FFI::string($pair->value, $valueLength)];
|
||||
$size += $nameLength + $valueLength;
|
||||
if ($size > $maxSize) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
if ($cFlags & 0x1) {
|
||||
// NGHTTP2_HD_INFLATE_FINAL
|
||||
$ffi->nghttp2_hd_inflate_end_headers($inflate);
|
||||
FFI::memset($pair, 0, self::$nvSize);
|
||||
return $headers;
|
||||
}
|
||||
if ($read === 0 || $offset > $bufferLength) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
/**
|
||||
* @param HeaderArray $headers
|
||||
*
|
||||
* @return string Encoded headers.
|
||||
*/
|
||||
public function encode(array $headers) : string
|
||||
{
|
||||
$ffi = self::$ffi;
|
||||
// To keep memory buffers
|
||||
$buffers = [];
|
||||
$headerCount = \count($headers);
|
||||
$current = 0;
|
||||
$pairs = $ffi->new(FFI::arrayType(self::$nvType, [$headerCount]));
|
||||
foreach ($headers as $index => [$name, $value]) {
|
||||
\assert($index === $current);
|
||||
$name = (string) $name;
|
||||
$value = (string) $value;
|
||||
$pair = $pairs[$current];
|
||||
$nameBuffer = self::createBufferFromString($name);
|
||||
$valueBuffer = self::createBufferFromString($value);
|
||||
$pair->name = $ffi->cast(self::$uint8PtrType, $nameBuffer);
|
||||
$pair->namelen = \strlen($name);
|
||||
$pair->value = $ffi->cast(self::$uint8PtrType, $valueBuffer);
|
||||
$pair->valuelen = \strlen($value);
|
||||
$pair->flags = self::SENSITIVE_HEADERS[$name] ?? self::FLAG_NO_COPY;
|
||||
$buffers[] = $nameBuffer;
|
||||
$buffers[] = $valueBuffer;
|
||||
$current++;
|
||||
}
|
||||
$bufferLength = $ffi->nghttp2_hd_deflate_bound($this->deflatePtr, $pairs, $headerCount);
|
||||
$buffer = $ffi->new(FFI::arrayType(self::$uint8Type, [$bufferLength]));
|
||||
$bufferLength = $ffi->nghttp2_hd_deflate_hd($this->deflatePtr, $buffer, $bufferLength, $pairs, $headerCount);
|
||||
if ($bufferLength < 0) {
|
||||
throw new HPackException('Failed to compress headers using nghttp2');
|
||||
}
|
||||
return FFI::string($buffer, $bufferLength);
|
||||
}
|
||||
}
|
@ -1,29 +0,0 @@
|
||||
|
||||
#define FFI_SCOPE "amphp-hpack-nghttp2"
|
||||
#define FFI_LIB "libnghttp2.so"
|
||||
|
||||
typedef struct nghttp2_hd_deflater nghttp2_hd_deflater;
|
||||
|
||||
typedef struct nghttp2_hd_inflater nghttp2_hd_inflater;
|
||||
|
||||
typedef struct {
|
||||
uint8_t *name;
|
||||
uint8_t *value;
|
||||
|
||||
size_t namelen;
|
||||
size_t valuelen;
|
||||
|
||||
uint8_t flags;
|
||||
} nghttp2_nv;
|
||||
|
||||
int nghttp2_hd_deflate_new(nghttp2_hd_deflater **deflater_ptr, size_t deflate_hd_table_bufsize_max);
|
||||
|
||||
ssize_t nghttp2_hd_deflate_hd(nghttp2_hd_deflater *deflater, uint8_t *buf, size_t buflen, const nghttp2_nv *nva, size_t nvlen);
|
||||
|
||||
size_t nghttp2_hd_deflate_bound(nghttp2_hd_deflater *deflater, const nghttp2_nv *nva, size_t nvlen);
|
||||
|
||||
int nghttp2_hd_inflate_new(nghttp2_hd_inflater **inflater_ptr);
|
||||
|
||||
ssize_t nghttp2_hd_inflate_hd2(nghttp2_hd_inflater *inflater, nghttp2_nv *nv_out, int *inflate_flags, const uint8_t *in, size_t inlen, int in_final);
|
||||
|
||||
int nghttp2_hd_inflate_end_headers(nghttp2_hd_inflater *inflater);
|
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
114
dependencies/amphp/hpack/tools/compress.php
vendored
114
dependencies/amphp/hpack/tools/compress.php
vendored
@ -1,114 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace WP_Ultimo\Dependencies;
|
||||
|
||||
/**
|
||||
* The MIT License (MIT).
|
||||
*
|
||||
* Copyright (c) 2017 Christian Lück
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is furnished
|
||||
* to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
// Taken from https://github.com/leproxy/leproxy/blob/e2ca7917c17ac8b853800f5b390297a2a1525cf7/compile.php
|
||||
$small = '';
|
||||
$all = \token_get_all(\file_get_contents($argv[1]));
|
||||
// search next non-whitespace/non-comment token
|
||||
$next = function ($i) use(&$all) {
|
||||
for ($i = $i + 1; !isset($all[$i]) || \is_array($all[$i]) && ($all[$i][0] === \T_COMMENT || $all[$i][0] === \T_DOC_COMMENT || $all[$i][0] === \T_WHITESPACE); ++$i) {
|
||||
}
|
||||
return $i;
|
||||
};
|
||||
// search previous non-whitespace/non-comment token
|
||||
$prev = function ($i) use(&$all) {
|
||||
for ($i = $i - 1; $i >= 0 && (!isset($all[$i]) || \is_array($all[$i]) && ($all[$i][0] === \T_COMMENT || $all[$i][0] === \T_DOC_COMMENT || $all[$i][0] === \T_WHITESPACE)); --$i) {
|
||||
}
|
||||
return $i;
|
||||
};
|
||||
$first = \true;
|
||||
foreach ($all as $i => $token) {
|
||||
if (\is_array($token) && ($token[0] === \T_COMMENT || $token[0] === \T_DOC_COMMENT)) {
|
||||
// remove all comments except first
|
||||
if ($first === \true) {
|
||||
$first = \false;
|
||||
continue;
|
||||
}
|
||||
unset($all[$i]);
|
||||
} elseif (\is_array($token) && $token[0] === \T_PUBLIC) {
|
||||
// get next non-whitespace token after `public` visibility
|
||||
$token = $all[$next($i)];
|
||||
if (\is_array($token) && $token[0] === \T_VARIABLE) {
|
||||
// use shorter variable notation `public $a` => `var $a`
|
||||
$all[$i] = [\T_VAR, 'var'];
|
||||
} else {
|
||||
// remove unneeded public identifier `public static function a()` => `static function a()`
|
||||
unset($all[$i]);
|
||||
}
|
||||
} elseif (\is_array($token) && $token[0] === \T_LNUMBER) {
|
||||
// Use shorter integer notation `0x0F` => `15` and `011` => `9`.
|
||||
// Technically, hex codes may be shorter for very large ints, but adding
|
||||
// another 2 leading chars is rarely worth it.
|
||||
// Optimizing floats is not really worth it, as they have many special
|
||||
// cases, such as e-notation and we would lose types for `0.0` => `0`.
|
||||
$all[$i][1] = (string) \intval($token[1], 0);
|
||||
} elseif (\is_array($token) && $token[0] === \T_NEW) {
|
||||
// remove unneeded parenthesis for constructors without args `new a();` => `new a;`
|
||||
// jump over next token (class name), then next must be open parenthesis, followed by closing
|
||||
$open = $next($next($i));
|
||||
$close = $next($open);
|
||||
if ($all[$open] === '(' && $all[$close] === ')') {
|
||||
unset($all[$open], $all[$close]);
|
||||
}
|
||||
} elseif (\is_array($token) && $token[0] === \T_STRING) {
|
||||
// replace certain functions with their shorter alias function name
|
||||
// http://php.net/manual/en/aliases.php
|
||||
static $replace = ['implode' => 'join', 'fwrite' => 'fputs', 'array_key_exists' => 'key_exists', 'current' => 'pos'];
|
||||
// check this has a replacement and "looks like" a function call
|
||||
// this works on a number of assumptions, such as not being aliased/namespaced
|
||||
if (isset($replace[$token[1]])) {
|
||||
$p = $all[$prev($i)];
|
||||
if ($all[$next($i)] === '(' && (!\is_array($p) || !\in_array($p[0], [\T_FUNCTION, \T_OBJECT_OPERATOR, \T_DOUBLE_COLON, \T_NEW]))) {
|
||||
$all[$i][1] = $replace[$all[$i][1]];
|
||||
}
|
||||
}
|
||||
} elseif (\is_array($token) && $token[0] === \T_EXIT) {
|
||||
// replace `exit` with shorter alias `die`
|
||||
// it's a language construct, not a function (see above)
|
||||
$all[$i][1] = 'die';
|
||||
} elseif (\is_array($token) && $token[0] === \T_RETURN) {
|
||||
// replace `return null;` with `return;`
|
||||
$t = $next($i);
|
||||
if (\is_array($all[$t]) && $all[$t][0] === \T_STRING && $all[$t][1] === 'null' && $all[$next($t)] === ';') {
|
||||
unset($all[$t]);
|
||||
}
|
||||
}
|
||||
}
|
||||
$all = \array_values($all);
|
||||
foreach ($all as $i => $token) {
|
||||
if (\is_array($token) && $token[0] === \T_WHITESPACE) {
|
||||
if (\strpos($token[1], "\n") !== \false) {
|
||||
$token = \strpos("()[]<>=+-*/%|,.:?!'\"\n", \substr($small, -1)) === \false ? "\n" : '';
|
||||
} else {
|
||||
$last = \substr($small, -1);
|
||||
$next = isset($all[$i + 1]) ? \substr(\is_array($all[$i + 1]) ? $all[$i + 1][1] : $all[$i + 1], 0, 1) : ' ';
|
||||
$token = \strpos('()[]{}<>;=+-*/%&|,.:?!@\'"' . "\r\n", $last) !== \false || \strpos('()[]{}<>;=+-*/%&|,.:?!@\'"' . '\\$', $next) !== \false ? '' : ' ';
|
||||
}
|
||||
}
|
||||
$small .= isset($token[1]) ? $token[1] : $token;
|
||||
}
|
||||
\file_put_contents($argv[1], $small);
|
@ -1,7 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace WP_Ultimo\Dependencies;
|
||||
|
||||
require __DIR__ . '/../../vendor/autoload.php';
|
||||
use WP_Ultimo\Dependencies\Amp\Http\HPack;
|
||||
(new HPack())->decode(\file_get_contents($argv[1]), 8192);
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user