@@ -1,6 +1,6 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -declare (strict_types=1); |
|
| 3 | +declare(strict_types=1); |
|
| 4 | 4 | namespace OCA\FullTextSearch_Elasticsearch\Vendor\GuzzleHttp\Promise; |
| 5 | 5 | |
| 6 | 6 | /** |
@@ -21,11 +21,11 @@ discard block |
||
| 21 | 21 | $this->reason = $reason; |
| 22 | 22 | $message = 'The promise was rejected'; |
| 23 | 23 | if ($description) { |
| 24 | - $message .= ' with reason: ' . $description; |
|
| 24 | + $message .= ' with reason: '.$description; |
|
| 25 | 25 | } elseif (\is_string($reason) || \is_object($reason) && \method_exists($reason, '__toString')) { |
| 26 | - $message .= ' with reason: ' . $this->reason; |
|
| 26 | + $message .= ' with reason: '.$this->reason; |
|
| 27 | 27 | } elseif ($reason instanceof \JsonSerializable) { |
| 28 | - $message .= ' with reason: ' . \json_encode($this->reason, \JSON_PRETTY_PRINT); |
|
| 28 | + $message .= ' with reason: '.\json_encode($this->reason, \JSON_PRETTY_PRINT); |
|
| 29 | 29 | } |
| 30 | 30 | parent::__construct($message); |
| 31 | 31 | } |
@@ -10,32 +10,32 @@ |
||
| 10 | 10 | */ |
| 11 | 11 | class RejectionException extends \RuntimeException |
| 12 | 12 | { |
| 13 | - /** @var mixed Rejection reason. */ |
|
| 14 | - private $reason; |
|
| 15 | - /** |
|
| 16 | - * @param mixed $reason Rejection reason. |
|
| 17 | - * @param string|null $description Optional description. |
|
| 18 | - */ |
|
| 19 | - public function __construct($reason, ?string $description = null) |
|
| 20 | - { |
|
| 21 | - $this->reason = $reason; |
|
| 22 | - $message = 'The promise was rejected'; |
|
| 23 | - if ($description) { |
|
| 24 | - $message .= ' with reason: ' . $description; |
|
| 25 | - } elseif (\is_string($reason) || \is_object($reason) && \method_exists($reason, '__toString')) { |
|
| 26 | - $message .= ' with reason: ' . $this->reason; |
|
| 27 | - } elseif ($reason instanceof \JsonSerializable) { |
|
| 28 | - $message .= ' with reason: ' . \json_encode($this->reason, \JSON_PRETTY_PRINT); |
|
| 29 | - } |
|
| 30 | - parent::__construct($message); |
|
| 31 | - } |
|
| 32 | - /** |
|
| 33 | - * Returns the rejection reason. |
|
| 34 | - * |
|
| 35 | - * @return mixed |
|
| 36 | - */ |
|
| 37 | - public function getReason() |
|
| 38 | - { |
|
| 39 | - return $this->reason; |
|
| 40 | - } |
|
| 13 | + /** @var mixed Rejection reason. */ |
|
| 14 | + private $reason; |
|
| 15 | + /** |
|
| 16 | + * @param mixed $reason Rejection reason. |
|
| 17 | + * @param string|null $description Optional description. |
|
| 18 | + */ |
|
| 19 | + public function __construct($reason, ?string $description = null) |
|
| 20 | + { |
|
| 21 | + $this->reason = $reason; |
|
| 22 | + $message = 'The promise was rejected'; |
|
| 23 | + if ($description) { |
|
| 24 | + $message .= ' with reason: ' . $description; |
|
| 25 | + } elseif (\is_string($reason) || \is_object($reason) && \method_exists($reason, '__toString')) { |
|
| 26 | + $message .= ' with reason: ' . $this->reason; |
|
| 27 | + } elseif ($reason instanceof \JsonSerializable) { |
|
| 28 | + $message .= ' with reason: ' . \json_encode($this->reason, \JSON_PRETTY_PRINT); |
|
| 29 | + } |
|
| 30 | + parent::__construct($message); |
|
| 31 | + } |
|
| 32 | + /** |
|
| 33 | + * Returns the rejection reason. |
|
| 34 | + * |
|
| 35 | + * @return mixed |
|
| 36 | + */ |
|
| 37 | + public function getReason() |
|
| 38 | + { |
|
| 39 | + return $this->reason; |
|
| 40 | + } |
|
| 41 | 41 | } |
@@ -16,50 +16,50 @@ |
||
| 16 | 16 | */ |
| 17 | 17 | class TaskQueue implements TaskQueueInterface |
| 18 | 18 | { |
| 19 | - private $enableShutdown = \true; |
|
| 20 | - private $queue = []; |
|
| 21 | - public function __construct(bool $withShutdown = \true) |
|
| 22 | - { |
|
| 23 | - if ($withShutdown) { |
|
| 24 | - \register_shutdown_function(function () : void { |
|
| 25 | - if ($this->enableShutdown) { |
|
| 26 | - // Only run the tasks if an E_ERROR didn't occur. |
|
| 27 | - $err = \error_get_last(); |
|
| 28 | - if (!$err || $err['type'] ^ \E_ERROR) { |
|
| 29 | - $this->run(); |
|
| 30 | - } |
|
| 31 | - } |
|
| 32 | - }); |
|
| 33 | - } |
|
| 34 | - } |
|
| 35 | - public function isEmpty() : bool |
|
| 36 | - { |
|
| 37 | - return !$this->queue; |
|
| 38 | - } |
|
| 39 | - public function add(callable $task) : void |
|
| 40 | - { |
|
| 41 | - $this->queue[] = $task; |
|
| 42 | - } |
|
| 43 | - public function run() : void |
|
| 44 | - { |
|
| 45 | - while ($task = \array_shift($this->queue)) { |
|
| 46 | - /** @var callable $task */ |
|
| 47 | - $task(); |
|
| 48 | - } |
|
| 49 | - } |
|
| 50 | - /** |
|
| 51 | - * The task queue will be run and exhausted by default when the process |
|
| 52 | - * exits IFF the exit is not the result of a PHP E_ERROR error. |
|
| 53 | - * |
|
| 54 | - * You can disable running the automatic shutdown of the queue by calling |
|
| 55 | - * this function. If you disable the task queue shutdown process, then you |
|
| 56 | - * MUST either run the task queue (as a result of running your event loop |
|
| 57 | - * or manually using the run() method) or wait on each outstanding promise. |
|
| 58 | - * |
|
| 59 | - * Note: This shutdown will occur before any destructors are triggered. |
|
| 60 | - */ |
|
| 61 | - public function disableShutdown() : void |
|
| 62 | - { |
|
| 63 | - $this->enableShutdown = \false; |
|
| 64 | - } |
|
| 19 | + private $enableShutdown = \true; |
|
| 20 | + private $queue = []; |
|
| 21 | + public function __construct(bool $withShutdown = \true) |
|
| 22 | + { |
|
| 23 | + if ($withShutdown) { |
|
| 24 | + \register_shutdown_function(function () : void { |
|
| 25 | + if ($this->enableShutdown) { |
|
| 26 | + // Only run the tasks if an E_ERROR didn't occur. |
|
| 27 | + $err = \error_get_last(); |
|
| 28 | + if (!$err || $err['type'] ^ \E_ERROR) { |
|
| 29 | + $this->run(); |
|
| 30 | + } |
|
| 31 | + } |
|
| 32 | + }); |
|
| 33 | + } |
|
| 34 | + } |
|
| 35 | + public function isEmpty() : bool |
|
| 36 | + { |
|
| 37 | + return !$this->queue; |
|
| 38 | + } |
|
| 39 | + public function add(callable $task) : void |
|
| 40 | + { |
|
| 41 | + $this->queue[] = $task; |
|
| 42 | + } |
|
| 43 | + public function run() : void |
|
| 44 | + { |
|
| 45 | + while ($task = \array_shift($this->queue)) { |
|
| 46 | + /** @var callable $task */ |
|
| 47 | + $task(); |
|
| 48 | + } |
|
| 49 | + } |
|
| 50 | + /** |
|
| 51 | + * The task queue will be run and exhausted by default when the process |
|
| 52 | + * exits IFF the exit is not the result of a PHP E_ERROR error. |
|
| 53 | + * |
|
| 54 | + * You can disable running the automatic shutdown of the queue by calling |
|
| 55 | + * this function. If you disable the task queue shutdown process, then you |
|
| 56 | + * MUST either run the task queue (as a result of running your event loop |
|
| 57 | + * or manually using the run() method) or wait on each outstanding promise. |
|
| 58 | + * |
|
| 59 | + * Note: This shutdown will occur before any destructors are triggered. |
|
| 60 | + */ |
|
| 61 | + public function disableShutdown() : void |
|
| 62 | + { |
|
| 63 | + $this->enableShutdown = \false; |
|
| 64 | + } |
|
| 65 | 65 | } |
@@ -1,6 +1,6 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -declare (strict_types=1); |
|
| 3 | +declare(strict_types=1); |
|
| 4 | 4 | namespace OCA\FullTextSearch_Elasticsearch\Vendor\GuzzleHttp\Promise; |
| 5 | 5 | |
| 6 | 6 | /** |
@@ -21,7 +21,7 @@ discard block |
||
| 21 | 21 | public function __construct(bool $withShutdown = \true) |
| 22 | 22 | { |
| 23 | 23 | if ($withShutdown) { |
| 24 | - \register_shutdown_function(function () : void { |
|
| 24 | + \register_shutdown_function(function() : void { |
|
| 25 | 25 | if ($this->enableShutdown) { |
| 26 | 26 | // Only run the tasks if an E_ERROR didn't occur. |
| 27 | 27 | $err = \error_get_last(); |
@@ -14,8 +14,7 @@ |
||
| 14 | 14 | * |
| 15 | 15 | * @final |
| 16 | 16 | */ |
| 17 | -class TaskQueue implements TaskQueueInterface |
|
| 18 | -{ |
|
| 17 | +class TaskQueue implements TaskQueueInterface { |
|
| 19 | 18 | private $enableShutdown = \true; |
| 20 | 19 | private $queue = []; |
| 21 | 20 | public function __construct(bool $withShutdown = \true) |
@@ -1,6 +1,6 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -declare (strict_types=1); |
|
| 3 | +declare(strict_types=1); |
|
| 4 | 4 | namespace OCA\FullTextSearch_Elasticsearch\Vendor\GuzzleHttp\Promise; |
| 5 | 5 | |
| 6 | 6 | /** |
@@ -33,7 +33,7 @@ discard block |
||
| 33 | 33 | $queue = Utils::queue(); |
| 34 | 34 | $p = new Promise([$queue, 'run']); |
| 35 | 35 | $value = $this->value; |
| 36 | - $queue->add(static function () use($p, $value, $onFulfilled) : void { |
|
| 36 | + $queue->add(static function() use($p, $value, $onFulfilled) : void { |
|
| 37 | 37 | if (Is::pending($p)) { |
| 38 | 38 | try { |
| 39 | 39 | $p->resolve($onFulfilled($value)); |
@@ -11,8 +11,7 @@ |
||
| 11 | 11 | * |
| 12 | 12 | * @final |
| 13 | 13 | */ |
| 14 | -class FulfilledPromise implements PromiseInterface |
|
| 15 | -{ |
|
| 14 | +class FulfilledPromise implements PromiseInterface { |
|
| 16 | 15 | private $value; |
| 17 | 16 | /** |
| 18 | 17 | * @param mixed $value |
@@ -13,61 +13,61 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | class FulfilledPromise implements PromiseInterface |
| 15 | 15 | { |
| 16 | - private $value; |
|
| 17 | - /** |
|
| 18 | - * @param mixed $value |
|
| 19 | - */ |
|
| 20 | - public function __construct($value) |
|
| 21 | - { |
|
| 22 | - if (\is_object($value) && \method_exists($value, 'then')) { |
|
| 23 | - throw new \InvalidArgumentException('You cannot create a FulfilledPromise with a promise.'); |
|
| 24 | - } |
|
| 25 | - $this->value = $value; |
|
| 26 | - } |
|
| 27 | - public function then(?callable $onFulfilled = null, ?callable $onRejected = null) : PromiseInterface |
|
| 28 | - { |
|
| 29 | - // Return itself if there is no onFulfilled function. |
|
| 30 | - if (!$onFulfilled) { |
|
| 31 | - return $this; |
|
| 32 | - } |
|
| 33 | - $queue = Utils::queue(); |
|
| 34 | - $p = new Promise([$queue, 'run']); |
|
| 35 | - $value = $this->value; |
|
| 36 | - $queue->add(static function () use($p, $value, $onFulfilled) : void { |
|
| 37 | - if (Is::pending($p)) { |
|
| 38 | - try { |
|
| 39 | - $p->resolve($onFulfilled($value)); |
|
| 40 | - } catch (\Throwable $e) { |
|
| 41 | - $p->reject($e); |
|
| 42 | - } |
|
| 43 | - } |
|
| 44 | - }); |
|
| 45 | - return $p; |
|
| 46 | - } |
|
| 47 | - public function otherwise(callable $onRejected) : PromiseInterface |
|
| 48 | - { |
|
| 49 | - return $this->then(null, $onRejected); |
|
| 50 | - } |
|
| 51 | - public function wait(bool $unwrap = \true) |
|
| 52 | - { |
|
| 53 | - return $unwrap ? $this->value : null; |
|
| 54 | - } |
|
| 55 | - public function getState() : string |
|
| 56 | - { |
|
| 57 | - return self::FULFILLED; |
|
| 58 | - } |
|
| 59 | - public function resolve($value) : void |
|
| 60 | - { |
|
| 61 | - if ($value !== $this->value) { |
|
| 62 | - throw new \LogicException('Cannot resolve a fulfilled promise'); |
|
| 63 | - } |
|
| 64 | - } |
|
| 65 | - public function reject($reason) : void |
|
| 66 | - { |
|
| 67 | - throw new \LogicException('Cannot reject a fulfilled promise'); |
|
| 68 | - } |
|
| 69 | - public function cancel() : void |
|
| 70 | - { |
|
| 71 | - // pass |
|
| 72 | - } |
|
| 16 | + private $value; |
|
| 17 | + /** |
|
| 18 | + * @param mixed $value |
|
| 19 | + */ |
|
| 20 | + public function __construct($value) |
|
| 21 | + { |
|
| 22 | + if (\is_object($value) && \method_exists($value, 'then')) { |
|
| 23 | + throw new \InvalidArgumentException('You cannot create a FulfilledPromise with a promise.'); |
|
| 24 | + } |
|
| 25 | + $this->value = $value; |
|
| 26 | + } |
|
| 27 | + public function then(?callable $onFulfilled = null, ?callable $onRejected = null) : PromiseInterface |
|
| 28 | + { |
|
| 29 | + // Return itself if there is no onFulfilled function. |
|
| 30 | + if (!$onFulfilled) { |
|
| 31 | + return $this; |
|
| 32 | + } |
|
| 33 | + $queue = Utils::queue(); |
|
| 34 | + $p = new Promise([$queue, 'run']); |
|
| 35 | + $value = $this->value; |
|
| 36 | + $queue->add(static function () use($p, $value, $onFulfilled) : void { |
|
| 37 | + if (Is::pending($p)) { |
|
| 38 | + try { |
|
| 39 | + $p->resolve($onFulfilled($value)); |
|
| 40 | + } catch (\Throwable $e) { |
|
| 41 | + $p->reject($e); |
|
| 42 | + } |
|
| 43 | + } |
|
| 44 | + }); |
|
| 45 | + return $p; |
|
| 46 | + } |
|
| 47 | + public function otherwise(callable $onRejected) : PromiseInterface |
|
| 48 | + { |
|
| 49 | + return $this->then(null, $onRejected); |
|
| 50 | + } |
|
| 51 | + public function wait(bool $unwrap = \true) |
|
| 52 | + { |
|
| 53 | + return $unwrap ? $this->value : null; |
|
| 54 | + } |
|
| 55 | + public function getState() : string |
|
| 56 | + { |
|
| 57 | + return self::FULFILLED; |
|
| 58 | + } |
|
| 59 | + public function resolve($value) : void |
|
| 60 | + { |
|
| 61 | + if ($value !== $this->value) { |
|
| 62 | + throw new \LogicException('Cannot resolve a fulfilled promise'); |
|
| 63 | + } |
|
| 64 | + } |
|
| 65 | + public function reject($reason) : void |
|
| 66 | + { |
|
| 67 | + throw new \LogicException('Cannot reject a fulfilled promise'); |
|
| 68 | + } |
|
| 69 | + public function cancel() : void |
|
| 70 | + { |
|
| 71 | + // pass |
|
| 72 | + } |
|
| 73 | 73 | } |
@@ -11,186 +11,186 @@ |
||
| 11 | 11 | */ |
| 12 | 12 | class EachPromise implements PromisorInterface |
| 13 | 13 | { |
| 14 | - private $pending = []; |
|
| 15 | - private $nextPendingIndex = 0; |
|
| 16 | - /** @var \Iterator|null */ |
|
| 17 | - private $iterable; |
|
| 18 | - /** @var callable|int|null */ |
|
| 19 | - private $concurrency; |
|
| 20 | - /** @var callable|null */ |
|
| 21 | - private $onFulfilled; |
|
| 22 | - /** @var callable|null */ |
|
| 23 | - private $onRejected; |
|
| 24 | - /** @var Promise|null */ |
|
| 25 | - private $aggregate; |
|
| 26 | - /** @var bool|null */ |
|
| 27 | - private $mutex; |
|
| 28 | - /** |
|
| 29 | - * Configuration hash can include the following key value pairs: |
|
| 30 | - * |
|
| 31 | - * - fulfilled: (callable) Invoked when a promise fulfills. The function |
|
| 32 | - * is invoked with three arguments: the fulfillment value, the index |
|
| 33 | - * position from the iterable list of the promise, and the aggregate |
|
| 34 | - * promise that manages all of the promises. The aggregate promise may |
|
| 35 | - * be resolved from within the callback to short-circuit the promise. |
|
| 36 | - * - rejected: (callable) Invoked when a promise is rejected. The |
|
| 37 | - * function is invoked with three arguments: the rejection reason, the |
|
| 38 | - * index position from the iterable list of the promise, and the |
|
| 39 | - * aggregate promise that manages all of the promises. The aggregate |
|
| 40 | - * promise may be resolved from within the callback to short-circuit |
|
| 41 | - * the promise. |
|
| 42 | - * - concurrency: (integer) Pass this configuration option to limit the |
|
| 43 | - * allowed number of outstanding concurrently executing promises, |
|
| 44 | - * creating a capped pool of promises. There is no limit by default. |
|
| 45 | - * |
|
| 46 | - * @param mixed $iterable Promises or values to iterate. |
|
| 47 | - * @param array $config Configuration options |
|
| 48 | - */ |
|
| 49 | - public function __construct($iterable, array $config = []) |
|
| 50 | - { |
|
| 51 | - $this->iterable = Create::iterFor($iterable); |
|
| 52 | - if (isset($config['concurrency'])) { |
|
| 53 | - $this->concurrency = $config['concurrency']; |
|
| 54 | - } |
|
| 55 | - if (isset($config['fulfilled'])) { |
|
| 56 | - $this->onFulfilled = $config['fulfilled']; |
|
| 57 | - } |
|
| 58 | - if (isset($config['rejected'])) { |
|
| 59 | - $this->onRejected = $config['rejected']; |
|
| 60 | - } |
|
| 61 | - } |
|
| 62 | - /** @psalm-suppress InvalidNullableReturnType */ |
|
| 63 | - public function promise() : PromiseInterface |
|
| 64 | - { |
|
| 65 | - if ($this->aggregate) { |
|
| 66 | - return $this->aggregate; |
|
| 67 | - } |
|
| 68 | - try { |
|
| 69 | - $this->createPromise(); |
|
| 70 | - /** @psalm-assert Promise $this->aggregate */ |
|
| 71 | - $this->iterable->rewind(); |
|
| 72 | - $this->refillPending(); |
|
| 73 | - } catch (\Throwable $e) { |
|
| 74 | - $this->aggregate->reject($e); |
|
| 75 | - } |
|
| 76 | - /** |
|
| 77 | - * @psalm-suppress NullableReturnStatement |
|
| 78 | - */ |
|
| 79 | - return $this->aggregate; |
|
| 80 | - } |
|
| 81 | - private function createPromise() : void |
|
| 82 | - { |
|
| 83 | - $this->mutex = \false; |
|
| 84 | - $this->aggregate = new Promise(function () : void { |
|
| 85 | - if ($this->checkIfFinished()) { |
|
| 86 | - return; |
|
| 87 | - } |
|
| 88 | - \reset($this->pending); |
|
| 89 | - // Consume a potentially fluctuating list of promises while |
|
| 90 | - // ensuring that indexes are maintained (precluding array_shift). |
|
| 91 | - while ($promise = \current($this->pending)) { |
|
| 92 | - \next($this->pending); |
|
| 93 | - $promise->wait(); |
|
| 94 | - if (Is::settled($this->aggregate)) { |
|
| 95 | - return; |
|
| 96 | - } |
|
| 97 | - } |
|
| 98 | - }); |
|
| 99 | - // Clear the references when the promise is resolved. |
|
| 100 | - $clearFn = function () : void { |
|
| 101 | - $this->iterable = $this->concurrency = $this->pending = null; |
|
| 102 | - $this->onFulfilled = $this->onRejected = null; |
|
| 103 | - $this->nextPendingIndex = 0; |
|
| 104 | - }; |
|
| 105 | - $this->aggregate->then($clearFn, $clearFn); |
|
| 106 | - } |
|
| 107 | - private function refillPending() : void |
|
| 108 | - { |
|
| 109 | - if (!$this->concurrency) { |
|
| 110 | - // Add all pending promises. |
|
| 111 | - while ($this->addPending() && $this->advanceIterator()) { |
|
| 112 | - } |
|
| 113 | - return; |
|
| 114 | - } |
|
| 115 | - // Add only up to N pending promises. |
|
| 116 | - $concurrency = \is_callable($this->concurrency) ? ($this->concurrency)(\count($this->pending)) : $this->concurrency; |
|
| 117 | - $concurrency = \max($concurrency - \count($this->pending), 0); |
|
| 118 | - // Concurrency may be set to 0 to disallow new promises. |
|
| 119 | - if (!$concurrency) { |
|
| 120 | - return; |
|
| 121 | - } |
|
| 122 | - // Add the first pending promise. |
|
| 123 | - $this->addPending(); |
|
| 124 | - // Note this is special handling for concurrency=1 so that we do |
|
| 125 | - // not advance the iterator after adding the first promise. This |
|
| 126 | - // helps work around issues with generators that might not have the |
|
| 127 | - // next value to yield until promise callbacks are called. |
|
| 128 | - while (--$concurrency && $this->advanceIterator() && $this->addPending()) { |
|
| 129 | - } |
|
| 130 | - } |
|
| 131 | - private function addPending() : bool |
|
| 132 | - { |
|
| 133 | - if (!$this->iterable || !$this->iterable->valid()) { |
|
| 134 | - return \false; |
|
| 135 | - } |
|
| 136 | - $promise = Create::promiseFor($this->iterable->current()); |
|
| 137 | - $key = $this->iterable->key(); |
|
| 138 | - // Iterable keys may not be unique, so we use a counter to |
|
| 139 | - // guarantee uniqueness |
|
| 140 | - $idx = $this->nextPendingIndex++; |
|
| 141 | - $this->pending[$idx] = $promise->then(function ($value) use($idx, $key) : void { |
|
| 142 | - if ($this->onFulfilled) { |
|
| 143 | - ($this->onFulfilled)($value, $key, $this->aggregate); |
|
| 144 | - } |
|
| 145 | - $this->step($idx); |
|
| 146 | - }, function ($reason) use($idx, $key) : void { |
|
| 147 | - if ($this->onRejected) { |
|
| 148 | - ($this->onRejected)($reason, $key, $this->aggregate); |
|
| 149 | - } |
|
| 150 | - $this->step($idx); |
|
| 151 | - }); |
|
| 152 | - return \true; |
|
| 153 | - } |
|
| 154 | - private function advanceIterator() : bool |
|
| 155 | - { |
|
| 156 | - // Place a lock on the iterator so that we ensure to not recurse, |
|
| 157 | - // preventing fatal generator errors. |
|
| 158 | - if ($this->mutex) { |
|
| 159 | - return \false; |
|
| 160 | - } |
|
| 161 | - $this->mutex = \true; |
|
| 162 | - try { |
|
| 163 | - $this->iterable->next(); |
|
| 164 | - $this->mutex = \false; |
|
| 165 | - return \true; |
|
| 166 | - } catch (\Throwable $e) { |
|
| 167 | - $this->aggregate->reject($e); |
|
| 168 | - $this->mutex = \false; |
|
| 169 | - return \false; |
|
| 170 | - } |
|
| 171 | - } |
|
| 172 | - private function step(int $idx) : void |
|
| 173 | - { |
|
| 174 | - // If the promise was already resolved, then ignore this step. |
|
| 175 | - if (Is::settled($this->aggregate)) { |
|
| 176 | - return; |
|
| 177 | - } |
|
| 178 | - unset($this->pending[$idx]); |
|
| 179 | - // Only refill pending promises if we are not locked, preventing the |
|
| 180 | - // EachPromise to recursively invoke the provided iterator, which |
|
| 181 | - // cause a fatal error: "Cannot resume an already running generator" |
|
| 182 | - if ($this->advanceIterator() && !$this->checkIfFinished()) { |
|
| 183 | - // Add more pending promises if possible. |
|
| 184 | - $this->refillPending(); |
|
| 185 | - } |
|
| 186 | - } |
|
| 187 | - private function checkIfFinished() : bool |
|
| 188 | - { |
|
| 189 | - if (!$this->pending && !$this->iterable->valid()) { |
|
| 190 | - // Resolve the promise if there's nothing left to do. |
|
| 191 | - $this->aggregate->resolve(null); |
|
| 192 | - return \true; |
|
| 193 | - } |
|
| 194 | - return \false; |
|
| 195 | - } |
|
| 14 | + private $pending = []; |
|
| 15 | + private $nextPendingIndex = 0; |
|
| 16 | + /** @var \Iterator|null */ |
|
| 17 | + private $iterable; |
|
| 18 | + /** @var callable|int|null */ |
|
| 19 | + private $concurrency; |
|
| 20 | + /** @var callable|null */ |
|
| 21 | + private $onFulfilled; |
|
| 22 | + /** @var callable|null */ |
|
| 23 | + private $onRejected; |
|
| 24 | + /** @var Promise|null */ |
|
| 25 | + private $aggregate; |
|
| 26 | + /** @var bool|null */ |
|
| 27 | + private $mutex; |
|
| 28 | + /** |
|
| 29 | + * Configuration hash can include the following key value pairs: |
|
| 30 | + * |
|
| 31 | + * - fulfilled: (callable) Invoked when a promise fulfills. The function |
|
| 32 | + * is invoked with three arguments: the fulfillment value, the index |
|
| 33 | + * position from the iterable list of the promise, and the aggregate |
|
| 34 | + * promise that manages all of the promises. The aggregate promise may |
|
| 35 | + * be resolved from within the callback to short-circuit the promise. |
|
| 36 | + * - rejected: (callable) Invoked when a promise is rejected. The |
|
| 37 | + * function is invoked with three arguments: the rejection reason, the |
|
| 38 | + * index position from the iterable list of the promise, and the |
|
| 39 | + * aggregate promise that manages all of the promises. The aggregate |
|
| 40 | + * promise may be resolved from within the callback to short-circuit |
|
| 41 | + * the promise. |
|
| 42 | + * - concurrency: (integer) Pass this configuration option to limit the |
|
| 43 | + * allowed number of outstanding concurrently executing promises, |
|
| 44 | + * creating a capped pool of promises. There is no limit by default. |
|
| 45 | + * |
|
| 46 | + * @param mixed $iterable Promises or values to iterate. |
|
| 47 | + * @param array $config Configuration options |
|
| 48 | + */ |
|
| 49 | + public function __construct($iterable, array $config = []) |
|
| 50 | + { |
|
| 51 | + $this->iterable = Create::iterFor($iterable); |
|
| 52 | + if (isset($config['concurrency'])) { |
|
| 53 | + $this->concurrency = $config['concurrency']; |
|
| 54 | + } |
|
| 55 | + if (isset($config['fulfilled'])) { |
|
| 56 | + $this->onFulfilled = $config['fulfilled']; |
|
| 57 | + } |
|
| 58 | + if (isset($config['rejected'])) { |
|
| 59 | + $this->onRejected = $config['rejected']; |
|
| 60 | + } |
|
| 61 | + } |
|
| 62 | + /** @psalm-suppress InvalidNullableReturnType */ |
|
| 63 | + public function promise() : PromiseInterface |
|
| 64 | + { |
|
| 65 | + if ($this->aggregate) { |
|
| 66 | + return $this->aggregate; |
|
| 67 | + } |
|
| 68 | + try { |
|
| 69 | + $this->createPromise(); |
|
| 70 | + /** @psalm-assert Promise $this->aggregate */ |
|
| 71 | + $this->iterable->rewind(); |
|
| 72 | + $this->refillPending(); |
|
| 73 | + } catch (\Throwable $e) { |
|
| 74 | + $this->aggregate->reject($e); |
|
| 75 | + } |
|
| 76 | + /** |
|
| 77 | + * @psalm-suppress NullableReturnStatement |
|
| 78 | + */ |
|
| 79 | + return $this->aggregate; |
|
| 80 | + } |
|
| 81 | + private function createPromise() : void |
|
| 82 | + { |
|
| 83 | + $this->mutex = \false; |
|
| 84 | + $this->aggregate = new Promise(function () : void { |
|
| 85 | + if ($this->checkIfFinished()) { |
|
| 86 | + return; |
|
| 87 | + } |
|
| 88 | + \reset($this->pending); |
|
| 89 | + // Consume a potentially fluctuating list of promises while |
|
| 90 | + // ensuring that indexes are maintained (precluding array_shift). |
|
| 91 | + while ($promise = \current($this->pending)) { |
|
| 92 | + \next($this->pending); |
|
| 93 | + $promise->wait(); |
|
| 94 | + if (Is::settled($this->aggregate)) { |
|
| 95 | + return; |
|
| 96 | + } |
|
| 97 | + } |
|
| 98 | + }); |
|
| 99 | + // Clear the references when the promise is resolved. |
|
| 100 | + $clearFn = function () : void { |
|
| 101 | + $this->iterable = $this->concurrency = $this->pending = null; |
|
| 102 | + $this->onFulfilled = $this->onRejected = null; |
|
| 103 | + $this->nextPendingIndex = 0; |
|
| 104 | + }; |
|
| 105 | + $this->aggregate->then($clearFn, $clearFn); |
|
| 106 | + } |
|
| 107 | + private function refillPending() : void |
|
| 108 | + { |
|
| 109 | + if (!$this->concurrency) { |
|
| 110 | + // Add all pending promises. |
|
| 111 | + while ($this->addPending() && $this->advanceIterator()) { |
|
| 112 | + } |
|
| 113 | + return; |
|
| 114 | + } |
|
| 115 | + // Add only up to N pending promises. |
|
| 116 | + $concurrency = \is_callable($this->concurrency) ? ($this->concurrency)(\count($this->pending)) : $this->concurrency; |
|
| 117 | + $concurrency = \max($concurrency - \count($this->pending), 0); |
|
| 118 | + // Concurrency may be set to 0 to disallow new promises. |
|
| 119 | + if (!$concurrency) { |
|
| 120 | + return; |
|
| 121 | + } |
|
| 122 | + // Add the first pending promise. |
|
| 123 | + $this->addPending(); |
|
| 124 | + // Note this is special handling for concurrency=1 so that we do |
|
| 125 | + // not advance the iterator after adding the first promise. This |
|
| 126 | + // helps work around issues with generators that might not have the |
|
| 127 | + // next value to yield until promise callbacks are called. |
|
| 128 | + while (--$concurrency && $this->advanceIterator() && $this->addPending()) { |
|
| 129 | + } |
|
| 130 | + } |
|
| 131 | + private function addPending() : bool |
|
| 132 | + { |
|
| 133 | + if (!$this->iterable || !$this->iterable->valid()) { |
|
| 134 | + return \false; |
|
| 135 | + } |
|
| 136 | + $promise = Create::promiseFor($this->iterable->current()); |
|
| 137 | + $key = $this->iterable->key(); |
|
| 138 | + // Iterable keys may not be unique, so we use a counter to |
|
| 139 | + // guarantee uniqueness |
|
| 140 | + $idx = $this->nextPendingIndex++; |
|
| 141 | + $this->pending[$idx] = $promise->then(function ($value) use($idx, $key) : void { |
|
| 142 | + if ($this->onFulfilled) { |
|
| 143 | + ($this->onFulfilled)($value, $key, $this->aggregate); |
|
| 144 | + } |
|
| 145 | + $this->step($idx); |
|
| 146 | + }, function ($reason) use($idx, $key) : void { |
|
| 147 | + if ($this->onRejected) { |
|
| 148 | + ($this->onRejected)($reason, $key, $this->aggregate); |
|
| 149 | + } |
|
| 150 | + $this->step($idx); |
|
| 151 | + }); |
|
| 152 | + return \true; |
|
| 153 | + } |
|
| 154 | + private function advanceIterator() : bool |
|
| 155 | + { |
|
| 156 | + // Place a lock on the iterator so that we ensure to not recurse, |
|
| 157 | + // preventing fatal generator errors. |
|
| 158 | + if ($this->mutex) { |
|
| 159 | + return \false; |
|
| 160 | + } |
|
| 161 | + $this->mutex = \true; |
|
| 162 | + try { |
|
| 163 | + $this->iterable->next(); |
|
| 164 | + $this->mutex = \false; |
|
| 165 | + return \true; |
|
| 166 | + } catch (\Throwable $e) { |
|
| 167 | + $this->aggregate->reject($e); |
|
| 168 | + $this->mutex = \false; |
|
| 169 | + return \false; |
|
| 170 | + } |
|
| 171 | + } |
|
| 172 | + private function step(int $idx) : void |
|
| 173 | + { |
|
| 174 | + // If the promise was already resolved, then ignore this step. |
|
| 175 | + if (Is::settled($this->aggregate)) { |
|
| 176 | + return; |
|
| 177 | + } |
|
| 178 | + unset($this->pending[$idx]); |
|
| 179 | + // Only refill pending promises if we are not locked, preventing the |
|
| 180 | + // EachPromise to recursively invoke the provided iterator, which |
|
| 181 | + // cause a fatal error: "Cannot resume an already running generator" |
|
| 182 | + if ($this->advanceIterator() && !$this->checkIfFinished()) { |
|
| 183 | + // Add more pending promises if possible. |
|
| 184 | + $this->refillPending(); |
|
| 185 | + } |
|
| 186 | + } |
|
| 187 | + private function checkIfFinished() : bool |
|
| 188 | + { |
|
| 189 | + if (!$this->pending && !$this->iterable->valid()) { |
|
| 190 | + // Resolve the promise if there's nothing left to do. |
|
| 191 | + $this->aggregate->resolve(null); |
|
| 192 | + return \true; |
|
| 193 | + } |
|
| 194 | + return \false; |
|
| 195 | + } |
|
| 196 | 196 | } |
@@ -1,6 +1,6 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -declare (strict_types=1); |
|
| 3 | +declare(strict_types=1); |
|
| 4 | 4 | namespace OCA\FullTextSearch_Elasticsearch\Vendor\GuzzleHttp\Promise; |
| 5 | 5 | |
| 6 | 6 | /** |
@@ -81,7 +81,7 @@ discard block |
||
| 81 | 81 | private function createPromise() : void |
| 82 | 82 | { |
| 83 | 83 | $this->mutex = \false; |
| 84 | - $this->aggregate = new Promise(function () : void { |
|
| 84 | + $this->aggregate = new Promise(function() : void { |
|
| 85 | 85 | if ($this->checkIfFinished()) { |
| 86 | 86 | return; |
| 87 | 87 | } |
@@ -97,7 +97,7 @@ discard block |
||
| 97 | 97 | } |
| 98 | 98 | }); |
| 99 | 99 | // Clear the references when the promise is resolved. |
| 100 | - $clearFn = function () : void { |
|
| 100 | + $clearFn = function() : void { |
|
| 101 | 101 | $this->iterable = $this->concurrency = $this->pending = null; |
| 102 | 102 | $this->onFulfilled = $this->onRejected = null; |
| 103 | 103 | $this->nextPendingIndex = 0; |
@@ -138,12 +138,12 @@ discard block |
||
| 138 | 138 | // Iterable keys may not be unique, so we use a counter to |
| 139 | 139 | // guarantee uniqueness |
| 140 | 140 | $idx = $this->nextPendingIndex++; |
| 141 | - $this->pending[$idx] = $promise->then(function ($value) use($idx, $key) : void { |
|
| 141 | + $this->pending[$idx] = $promise->then(function($value) use($idx, $key) : void { |
|
| 142 | 142 | if ($this->onFulfilled) { |
| 143 | 143 | ($this->onFulfilled)($value, $key, $this->aggregate); |
| 144 | 144 | } |
| 145 | 145 | $this->step($idx); |
| 146 | - }, function ($reason) use($idx, $key) : void { |
|
| 146 | + }, function($reason) use($idx, $key) : void { |
|
| 147 | 147 | if ($this->onRejected) { |
| 148 | 148 | ($this->onRejected)($reason, $key, $this->aggregate); |
| 149 | 149 | } |
@@ -9,8 +9,7 @@ |
||
| 9 | 9 | * |
| 10 | 10 | * @final |
| 11 | 11 | */ |
| 12 | -class EachPromise implements PromisorInterface |
|
| 13 | -{ |
|
| 12 | +class EachPromise implements PromisorInterface { |
|
| 14 | 13 | private $pending = []; |
| 15 | 14 | private $nextPendingIndex = 0; |
| 16 | 15 | /** @var \Iterator|null */ |
@@ -5,32 +5,32 @@ |
||
| 5 | 5 | |
| 6 | 6 | final class Is |
| 7 | 7 | { |
| 8 | - /** |
|
| 9 | - * Returns true if a promise is pending. |
|
| 10 | - */ |
|
| 11 | - public static function pending(PromiseInterface $promise) : bool |
|
| 12 | - { |
|
| 13 | - return $promise->getState() === PromiseInterface::PENDING; |
|
| 14 | - } |
|
| 15 | - /** |
|
| 16 | - * Returns true if a promise is fulfilled or rejected. |
|
| 17 | - */ |
|
| 18 | - public static function settled(PromiseInterface $promise) : bool |
|
| 19 | - { |
|
| 20 | - return $promise->getState() !== PromiseInterface::PENDING; |
|
| 21 | - } |
|
| 22 | - /** |
|
| 23 | - * Returns true if a promise is fulfilled. |
|
| 24 | - */ |
|
| 25 | - public static function fulfilled(PromiseInterface $promise) : bool |
|
| 26 | - { |
|
| 27 | - return $promise->getState() === PromiseInterface::FULFILLED; |
|
| 28 | - } |
|
| 29 | - /** |
|
| 30 | - * Returns true if a promise is rejected. |
|
| 31 | - */ |
|
| 32 | - public static function rejected(PromiseInterface $promise) : bool |
|
| 33 | - { |
|
| 34 | - return $promise->getState() === PromiseInterface::REJECTED; |
|
| 35 | - } |
|
| 8 | + /** |
|
| 9 | + * Returns true if a promise is pending. |
|
| 10 | + */ |
|
| 11 | + public static function pending(PromiseInterface $promise) : bool |
|
| 12 | + { |
|
| 13 | + return $promise->getState() === PromiseInterface::PENDING; |
|
| 14 | + } |
|
| 15 | + /** |
|
| 16 | + * Returns true if a promise is fulfilled or rejected. |
|
| 17 | + */ |
|
| 18 | + public static function settled(PromiseInterface $promise) : bool |
|
| 19 | + { |
|
| 20 | + return $promise->getState() !== PromiseInterface::PENDING; |
|
| 21 | + } |
|
| 22 | + /** |
|
| 23 | + * Returns true if a promise is fulfilled. |
|
| 24 | + */ |
|
| 25 | + public static function fulfilled(PromiseInterface $promise) : bool |
|
| 26 | + { |
|
| 27 | + return $promise->getState() === PromiseInterface::FULFILLED; |
|
| 28 | + } |
|
| 29 | + /** |
|
| 30 | + * Returns true if a promise is rejected. |
|
| 31 | + */ |
|
| 32 | + public static function rejected(PromiseInterface $promise) : bool |
|
| 33 | + { |
|
| 34 | + return $promise->getState() === PromiseInterface::REJECTED; |
|
| 35 | + } |
|
| 36 | 36 | } |
@@ -1,6 +1,6 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -declare (strict_types=1); |
|
| 3 | +declare(strict_types=1); |
|
| 4 | 4 | namespace OCA\FullTextSearch_Elasticsearch\Vendor\GuzzleHttp\Promise; |
| 5 | 5 | |
| 6 | 6 | final class Is |
@@ -3,8 +3,7 @@ |
||
| 3 | 3 | declare (strict_types=1); |
| 4 | 4 | namespace OCA\FullTextSearch_Elasticsearch\Vendor\GuzzleHttp\Promise; |
| 5 | 5 | |
| 6 | -final class Is |
|
| 7 | -{ |
|
| 6 | +final class Is { |
|
| 8 | 7 | /** |
| 9 | 8 | * Returns true if a promise is pending. |
| 10 | 9 | */ |
@@ -1,6 +1,6 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -declare (strict_types=1); |
|
| 3 | +declare(strict_types=1); |
|
| 4 | 4 | namespace OCA\FullTextSearch_Elasticsearch\Vendor\GuzzleHttp\Promise; |
| 5 | 5 | |
| 6 | 6 | /** |
@@ -33,7 +33,7 @@ discard block |
||
| 33 | 33 | $queue = Utils::queue(); |
| 34 | 34 | $reason = $this->reason; |
| 35 | 35 | $p = new Promise([$queue, 'run']); |
| 36 | - $queue->add(static function () use($p, $reason, $onRejected) : void { |
|
| 36 | + $queue->add(static function() use($p, $reason, $onRejected) : void { |
|
| 37 | 37 | if (Is::pending($p)) { |
| 38 | 38 | try { |
| 39 | 39 | // Return a resolved promise if onRejected does not throw. |
@@ -11,8 +11,7 @@ |
||
| 11 | 11 | * |
| 12 | 12 | * @final |
| 13 | 13 | */ |
| 14 | -class RejectedPromise implements PromiseInterface |
|
| 15 | -{ |
|
| 14 | +class RejectedPromise implements PromiseInterface { |
|
| 16 | 15 | private $reason; |
| 17 | 16 | /** |
| 18 | 17 | * @param mixed $reason |
@@ -13,66 +13,66 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | class RejectedPromise implements PromiseInterface |
| 15 | 15 | { |
| 16 | - private $reason; |
|
| 17 | - /** |
|
| 18 | - * @param mixed $reason |
|
| 19 | - */ |
|
| 20 | - public function __construct($reason) |
|
| 21 | - { |
|
| 22 | - if (\is_object($reason) && \method_exists($reason, 'then')) { |
|
| 23 | - throw new \InvalidArgumentException('You cannot create a RejectedPromise with a promise.'); |
|
| 24 | - } |
|
| 25 | - $this->reason = $reason; |
|
| 26 | - } |
|
| 27 | - public function then(?callable $onFulfilled = null, ?callable $onRejected = null) : PromiseInterface |
|
| 28 | - { |
|
| 29 | - // If there's no onRejected callback then just return self. |
|
| 30 | - if (!$onRejected) { |
|
| 31 | - return $this; |
|
| 32 | - } |
|
| 33 | - $queue = Utils::queue(); |
|
| 34 | - $reason = $this->reason; |
|
| 35 | - $p = new Promise([$queue, 'run']); |
|
| 36 | - $queue->add(static function () use($p, $reason, $onRejected) : void { |
|
| 37 | - if (Is::pending($p)) { |
|
| 38 | - try { |
|
| 39 | - // Return a resolved promise if onRejected does not throw. |
|
| 40 | - $p->resolve($onRejected($reason)); |
|
| 41 | - } catch (\Throwable $e) { |
|
| 42 | - // onRejected threw, so return a rejected promise. |
|
| 43 | - $p->reject($e); |
|
| 44 | - } |
|
| 45 | - } |
|
| 46 | - }); |
|
| 47 | - return $p; |
|
| 48 | - } |
|
| 49 | - public function otherwise(callable $onRejected) : PromiseInterface |
|
| 50 | - { |
|
| 51 | - return $this->then(null, $onRejected); |
|
| 52 | - } |
|
| 53 | - public function wait(bool $unwrap = \true) |
|
| 54 | - { |
|
| 55 | - if ($unwrap) { |
|
| 56 | - throw Create::exceptionFor($this->reason); |
|
| 57 | - } |
|
| 58 | - return null; |
|
| 59 | - } |
|
| 60 | - public function getState() : string |
|
| 61 | - { |
|
| 62 | - return self::REJECTED; |
|
| 63 | - } |
|
| 64 | - public function resolve($value) : void |
|
| 65 | - { |
|
| 66 | - throw new \LogicException('Cannot resolve a rejected promise'); |
|
| 67 | - } |
|
| 68 | - public function reject($reason) : void |
|
| 69 | - { |
|
| 70 | - if ($reason !== $this->reason) { |
|
| 71 | - throw new \LogicException('Cannot reject a rejected promise'); |
|
| 72 | - } |
|
| 73 | - } |
|
| 74 | - public function cancel() : void |
|
| 75 | - { |
|
| 76 | - // pass |
|
| 77 | - } |
|
| 16 | + private $reason; |
|
| 17 | + /** |
|
| 18 | + * @param mixed $reason |
|
| 19 | + */ |
|
| 20 | + public function __construct($reason) |
|
| 21 | + { |
|
| 22 | + if (\is_object($reason) && \method_exists($reason, 'then')) { |
|
| 23 | + throw new \InvalidArgumentException('You cannot create a RejectedPromise with a promise.'); |
|
| 24 | + } |
|
| 25 | + $this->reason = $reason; |
|
| 26 | + } |
|
| 27 | + public function then(?callable $onFulfilled = null, ?callable $onRejected = null) : PromiseInterface |
|
| 28 | + { |
|
| 29 | + // If there's no onRejected callback then just return self. |
|
| 30 | + if (!$onRejected) { |
|
| 31 | + return $this; |
|
| 32 | + } |
|
| 33 | + $queue = Utils::queue(); |
|
| 34 | + $reason = $this->reason; |
|
| 35 | + $p = new Promise([$queue, 'run']); |
|
| 36 | + $queue->add(static function () use($p, $reason, $onRejected) : void { |
|
| 37 | + if (Is::pending($p)) { |
|
| 38 | + try { |
|
| 39 | + // Return a resolved promise if onRejected does not throw. |
|
| 40 | + $p->resolve($onRejected($reason)); |
|
| 41 | + } catch (\Throwable $e) { |
|
| 42 | + // onRejected threw, so return a rejected promise. |
|
| 43 | + $p->reject($e); |
|
| 44 | + } |
|
| 45 | + } |
|
| 46 | + }); |
|
| 47 | + return $p; |
|
| 48 | + } |
|
| 49 | + public function otherwise(callable $onRejected) : PromiseInterface |
|
| 50 | + { |
|
| 51 | + return $this->then(null, $onRejected); |
|
| 52 | + } |
|
| 53 | + public function wait(bool $unwrap = \true) |
|
| 54 | + { |
|
| 55 | + if ($unwrap) { |
|
| 56 | + throw Create::exceptionFor($this->reason); |
|
| 57 | + } |
|
| 58 | + return null; |
|
| 59 | + } |
|
| 60 | + public function getState() : string |
|
| 61 | + { |
|
| 62 | + return self::REJECTED; |
|
| 63 | + } |
|
| 64 | + public function resolve($value) : void |
|
| 65 | + { |
|
| 66 | + throw new \LogicException('Cannot resolve a rejected promise'); |
|
| 67 | + } |
|
| 68 | + public function reject($reason) : void |
|
| 69 | + { |
|
| 70 | + if ($reason !== $this->reason) { |
|
| 71 | + throw new \LogicException('Cannot reject a rejected promise'); |
|
| 72 | + } |
|
| 73 | + } |
|
| 74 | + public function cancel() : void |
|
| 75 | + { |
|
| 76 | + // pass |
|
| 77 | + } |
|
| 78 | 78 | } |
@@ -5,64 +5,64 @@ |
||
| 5 | 5 | |
| 6 | 6 | final class Create |
| 7 | 7 | { |
| 8 | - /** |
|
| 9 | - * Creates a promise for a value if the value is not a promise. |
|
| 10 | - * |
|
| 11 | - * @param mixed $value Promise or value. |
|
| 12 | - */ |
|
| 13 | - public static function promiseFor($value) : PromiseInterface |
|
| 14 | - { |
|
| 15 | - if ($value instanceof PromiseInterface) { |
|
| 16 | - return $value; |
|
| 17 | - } |
|
| 18 | - // Return a Guzzle promise that shadows the given promise. |
|
| 19 | - if (\is_object($value) && \method_exists($value, 'then')) { |
|
| 20 | - $wfn = \method_exists($value, 'wait') ? [$value, 'wait'] : null; |
|
| 21 | - $cfn = \method_exists($value, 'cancel') ? [$value, 'cancel'] : null; |
|
| 22 | - $promise = new Promise($wfn, $cfn); |
|
| 23 | - $value->then([$promise, 'resolve'], [$promise, 'reject']); |
|
| 24 | - return $promise; |
|
| 25 | - } |
|
| 26 | - return new FulfilledPromise($value); |
|
| 27 | - } |
|
| 28 | - /** |
|
| 29 | - * Creates a rejected promise for a reason if the reason is not a promise. |
|
| 30 | - * If the provided reason is a promise, then it is returned as-is. |
|
| 31 | - * |
|
| 32 | - * @param mixed $reason Promise or reason. |
|
| 33 | - */ |
|
| 34 | - public static function rejectionFor($reason) : PromiseInterface |
|
| 35 | - { |
|
| 36 | - if ($reason instanceof PromiseInterface) { |
|
| 37 | - return $reason; |
|
| 38 | - } |
|
| 39 | - return new RejectedPromise($reason); |
|
| 40 | - } |
|
| 41 | - /** |
|
| 42 | - * Create an exception for a rejected promise value. |
|
| 43 | - * |
|
| 44 | - * @param mixed $reason |
|
| 45 | - */ |
|
| 46 | - public static function exceptionFor($reason) : \Throwable |
|
| 47 | - { |
|
| 48 | - if ($reason instanceof \Throwable) { |
|
| 49 | - return $reason; |
|
| 50 | - } |
|
| 51 | - return new RejectionException($reason); |
|
| 52 | - } |
|
| 53 | - /** |
|
| 54 | - * Returns an iterator for the given value. |
|
| 55 | - * |
|
| 56 | - * @param mixed $value |
|
| 57 | - */ |
|
| 58 | - public static function iterFor($value) : \Iterator |
|
| 59 | - { |
|
| 60 | - if ($value instanceof \Iterator) { |
|
| 61 | - return $value; |
|
| 62 | - } |
|
| 63 | - if (\is_array($value)) { |
|
| 64 | - return new \ArrayIterator($value); |
|
| 65 | - } |
|
| 66 | - return new \ArrayIterator([$value]); |
|
| 67 | - } |
|
| 8 | + /** |
|
| 9 | + * Creates a promise for a value if the value is not a promise. |
|
| 10 | + * |
|
| 11 | + * @param mixed $value Promise or value. |
|
| 12 | + */ |
|
| 13 | + public static function promiseFor($value) : PromiseInterface |
|
| 14 | + { |
|
| 15 | + if ($value instanceof PromiseInterface) { |
|
| 16 | + return $value; |
|
| 17 | + } |
|
| 18 | + // Return a Guzzle promise that shadows the given promise. |
|
| 19 | + if (\is_object($value) && \method_exists($value, 'then')) { |
|
| 20 | + $wfn = \method_exists($value, 'wait') ? [$value, 'wait'] : null; |
|
| 21 | + $cfn = \method_exists($value, 'cancel') ? [$value, 'cancel'] : null; |
|
| 22 | + $promise = new Promise($wfn, $cfn); |
|
| 23 | + $value->then([$promise, 'resolve'], [$promise, 'reject']); |
|
| 24 | + return $promise; |
|
| 25 | + } |
|
| 26 | + return new FulfilledPromise($value); |
|
| 27 | + } |
|
| 28 | + /** |
|
| 29 | + * Creates a rejected promise for a reason if the reason is not a promise. |
|
| 30 | + * If the provided reason is a promise, then it is returned as-is. |
|
| 31 | + * |
|
| 32 | + * @param mixed $reason Promise or reason. |
|
| 33 | + */ |
|
| 34 | + public static function rejectionFor($reason) : PromiseInterface |
|
| 35 | + { |
|
| 36 | + if ($reason instanceof PromiseInterface) { |
|
| 37 | + return $reason; |
|
| 38 | + } |
|
| 39 | + return new RejectedPromise($reason); |
|
| 40 | + } |
|
| 41 | + /** |
|
| 42 | + * Create an exception for a rejected promise value. |
|
| 43 | + * |
|
| 44 | + * @param mixed $reason |
|
| 45 | + */ |
|
| 46 | + public static function exceptionFor($reason) : \Throwable |
|
| 47 | + { |
|
| 48 | + if ($reason instanceof \Throwable) { |
|
| 49 | + return $reason; |
|
| 50 | + } |
|
| 51 | + return new RejectionException($reason); |
|
| 52 | + } |
|
| 53 | + /** |
|
| 54 | + * Returns an iterator for the given value. |
|
| 55 | + * |
|
| 56 | + * @param mixed $value |
|
| 57 | + */ |
|
| 58 | + public static function iterFor($value) : \Iterator |
|
| 59 | + { |
|
| 60 | + if ($value instanceof \Iterator) { |
|
| 61 | + return $value; |
|
| 62 | + } |
|
| 63 | + if (\is_array($value)) { |
|
| 64 | + return new \ArrayIterator($value); |
|
| 65 | + } |
|
| 66 | + return new \ArrayIterator([$value]); |
|
| 67 | + } |
|
| 68 | 68 | } |
@@ -1,6 +1,6 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -declare (strict_types=1); |
|
| 3 | +declare(strict_types=1); |
|
| 4 | 4 | namespace OCA\FullTextSearch_Elasticsearch\Vendor\GuzzleHttp\Promise; |
| 5 | 5 | |
| 6 | 6 | final class Create |
@@ -3,8 +3,7 @@ |
||
| 3 | 3 | declare (strict_types=1); |
| 4 | 4 | namespace OCA\FullTextSearch_Elasticsearch\Vendor\GuzzleHttp\Promise; |
| 5 | 5 | |
| 6 | -final class Create |
|
| 7 | -{ |
|
| 6 | +final class Create { |
|
| 8 | 7 | /** |
| 9 | 8 | * Creates a promise for a value if the value is not a promise. |
| 10 | 9 | * |
@@ -3,8 +3,7 @@ |
||
| 3 | 3 | declare (strict_types=1); |
| 4 | 4 | namespace OCA\FullTextSearch_Elasticsearch\Vendor\GuzzleHttp\Promise; |
| 5 | 5 | |
| 6 | -final class Each |
|
| 7 | -{ |
|
| 6 | +final class Each { |
|
| 8 | 7 | /** |
| 9 | 8 | * Given an iterator that yields promises or values, returns a promise that |
| 10 | 9 | * is fulfilled with a null value when the iterator has been consumed or |
@@ -5,52 +5,52 @@ |
||
| 5 | 5 | |
| 6 | 6 | final class Each |
| 7 | 7 | { |
| 8 | - /** |
|
| 9 | - * Given an iterator that yields promises or values, returns a promise that |
|
| 10 | - * is fulfilled with a null value when the iterator has been consumed or |
|
| 11 | - * the aggregate promise has been fulfilled or rejected. |
|
| 12 | - * |
|
| 13 | - * $onFulfilled is a function that accepts the fulfilled value, iterator |
|
| 14 | - * index, and the aggregate promise. The callback can invoke any necessary |
|
| 15 | - * side effects and choose to resolve or reject the aggregate if needed. |
|
| 16 | - * |
|
| 17 | - * $onRejected is a function that accepts the rejection reason, iterator |
|
| 18 | - * index, and the aggregate promise. The callback can invoke any necessary |
|
| 19 | - * side effects and choose to resolve or reject the aggregate if needed. |
|
| 20 | - * |
|
| 21 | - * @param mixed $iterable Iterator or array to iterate over. |
|
| 22 | - */ |
|
| 23 | - public static function of($iterable, ?callable $onFulfilled = null, ?callable $onRejected = null) : PromiseInterface |
|
| 24 | - { |
|
| 25 | - return (new EachPromise($iterable, ['fulfilled' => $onFulfilled, 'rejected' => $onRejected]))->promise(); |
|
| 26 | - } |
|
| 27 | - /** |
|
| 28 | - * Like of, but only allows a certain number of outstanding promises at any |
|
| 29 | - * given time. |
|
| 30 | - * |
|
| 31 | - * $concurrency may be an integer or a function that accepts the number of |
|
| 32 | - * pending promises and returns a numeric concurrency limit value to allow |
|
| 33 | - * for dynamic a concurrency size. |
|
| 34 | - * |
|
| 35 | - * @param mixed $iterable |
|
| 36 | - * @param int|callable $concurrency |
|
| 37 | - */ |
|
| 38 | - public static function ofLimit($iterable, $concurrency, ?callable $onFulfilled = null, ?callable $onRejected = null) : PromiseInterface |
|
| 39 | - { |
|
| 40 | - return (new EachPromise($iterable, ['fulfilled' => $onFulfilled, 'rejected' => $onRejected, 'concurrency' => $concurrency]))->promise(); |
|
| 41 | - } |
|
| 42 | - /** |
|
| 43 | - * Like limit, but ensures that no promise in the given $iterable argument |
|
| 44 | - * is rejected. If any promise is rejected, then the aggregate promise is |
|
| 45 | - * rejected with the encountered rejection. |
|
| 46 | - * |
|
| 47 | - * @param mixed $iterable |
|
| 48 | - * @param int|callable $concurrency |
|
| 49 | - */ |
|
| 50 | - public static function ofLimitAll($iterable, $concurrency, ?callable $onFulfilled = null) : PromiseInterface |
|
| 51 | - { |
|
| 52 | - return self::ofLimit($iterable, $concurrency, $onFulfilled, function ($reason, $idx, PromiseInterface $aggregate) : void { |
|
| 53 | - $aggregate->reject($reason); |
|
| 54 | - }); |
|
| 55 | - } |
|
| 8 | + /** |
|
| 9 | + * Given an iterator that yields promises or values, returns a promise that |
|
| 10 | + * is fulfilled with a null value when the iterator has been consumed or |
|
| 11 | + * the aggregate promise has been fulfilled or rejected. |
|
| 12 | + * |
|
| 13 | + * $onFulfilled is a function that accepts the fulfilled value, iterator |
|
| 14 | + * index, and the aggregate promise. The callback can invoke any necessary |
|
| 15 | + * side effects and choose to resolve or reject the aggregate if needed. |
|
| 16 | + * |
|
| 17 | + * $onRejected is a function that accepts the rejection reason, iterator |
|
| 18 | + * index, and the aggregate promise. The callback can invoke any necessary |
|
| 19 | + * side effects and choose to resolve or reject the aggregate if needed. |
|
| 20 | + * |
|
| 21 | + * @param mixed $iterable Iterator or array to iterate over. |
|
| 22 | + */ |
|
| 23 | + public static function of($iterable, ?callable $onFulfilled = null, ?callable $onRejected = null) : PromiseInterface |
|
| 24 | + { |
|
| 25 | + return (new EachPromise($iterable, ['fulfilled' => $onFulfilled, 'rejected' => $onRejected]))->promise(); |
|
| 26 | + } |
|
| 27 | + /** |
|
| 28 | + * Like of, but only allows a certain number of outstanding promises at any |
|
| 29 | + * given time. |
|
| 30 | + * |
|
| 31 | + * $concurrency may be an integer or a function that accepts the number of |
|
| 32 | + * pending promises and returns a numeric concurrency limit value to allow |
|
| 33 | + * for dynamic a concurrency size. |
|
| 34 | + * |
|
| 35 | + * @param mixed $iterable |
|
| 36 | + * @param int|callable $concurrency |
|
| 37 | + */ |
|
| 38 | + public static function ofLimit($iterable, $concurrency, ?callable $onFulfilled = null, ?callable $onRejected = null) : PromiseInterface |
|
| 39 | + { |
|
| 40 | + return (new EachPromise($iterable, ['fulfilled' => $onFulfilled, 'rejected' => $onRejected, 'concurrency' => $concurrency]))->promise(); |
|
| 41 | + } |
|
| 42 | + /** |
|
| 43 | + * Like limit, but ensures that no promise in the given $iterable argument |
|
| 44 | + * is rejected. If any promise is rejected, then the aggregate promise is |
|
| 45 | + * rejected with the encountered rejection. |
|
| 46 | + * |
|
| 47 | + * @param mixed $iterable |
|
| 48 | + * @param int|callable $concurrency |
|
| 49 | + */ |
|
| 50 | + public static function ofLimitAll($iterable, $concurrency, ?callable $onFulfilled = null) : PromiseInterface |
|
| 51 | + { |
|
| 52 | + return self::ofLimit($iterable, $concurrency, $onFulfilled, function ($reason, $idx, PromiseInterface $aggregate) : void { |
|
| 53 | + $aggregate->reject($reason); |
|
| 54 | + }); |
|
| 55 | + } |
|
| 56 | 56 | } |
@@ -1,6 +1,6 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -declare (strict_types=1); |
|
| 3 | +declare(strict_types=1); |
|
| 4 | 4 | namespace OCA\FullTextSearch_Elasticsearch\Vendor\GuzzleHttp\Promise; |
| 5 | 5 | |
| 6 | 6 | final class Each |
@@ -49,7 +49,7 @@ discard block |
||
| 49 | 49 | */ |
| 50 | 50 | public static function ofLimitAll($iterable, $concurrency, ?callable $onFulfilled = null) : PromiseInterface |
| 51 | 51 | { |
| 52 | - return self::ofLimit($iterable, $concurrency, $onFulfilled, function ($reason, $idx, PromiseInterface $aggregate) : void { |
|
| 52 | + return self::ofLimit($iterable, $concurrency, $onFulfilled, function($reason, $idx, PromiseInterface $aggregate) : void { |
|
| 53 | 53 | $aggregate->reject($reason); |
| 54 | 54 | }); |
| 55 | 55 | } |
@@ -8,8 +8,8 @@ |
||
| 8 | 8 | */ |
| 9 | 9 | class AggregateException extends RejectionException |
| 10 | 10 | { |
| 11 | - public function __construct(string $msg, array $reasons) |
|
| 12 | - { |
|
| 13 | - parent::__construct($reasons, \sprintf('%s; %d rejected promises', $msg, \count($reasons))); |
|
| 14 | - } |
|
| 11 | + public function __construct(string $msg, array $reasons) |
|
| 12 | + { |
|
| 13 | + parent::__construct($reasons, \sprintf('%s; %d rejected promises', $msg, \count($reasons))); |
|
| 14 | + } |
|
| 15 | 15 | } |
@@ -6,8 +6,7 @@ |
||
| 6 | 6 | /** |
| 7 | 7 | * Exception thrown when too many errors occur in the some() or any() methods. |
| 8 | 8 | */ |
| 9 | -class AggregateException extends RejectionException |
|
| 10 | -{ |
|
| 9 | +class AggregateException extends RejectionException { |
|
| 11 | 10 | public function __construct(string $msg, array $reasons) |
| 12 | 11 | { |
| 13 | 12 | parent::__construct($reasons, \sprintf('%s; %d rejected promises', $msg, \count($reasons))); |
@@ -1,6 +1,6 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -declare (strict_types=1); |
|
| 3 | +declare(strict_types=1); |
|
| 4 | 4 | namespace OCA\FullTextSearch_Elasticsearch\Vendor\GuzzleHttp\Promise; |
| 5 | 5 | |
| 6 | 6 | /** |