diff options
Diffstat (limited to 'vendor/sabre/event/lib/Promise.php')
-rw-r--r-- | vendor/sabre/event/lib/Promise.php | 73 |
1 files changed, 31 insertions, 42 deletions
diff --git a/vendor/sabre/event/lib/Promise.php b/vendor/sabre/event/lib/Promise.php index 1d04bd4d4..1d4ddd74a 100644 --- a/vendor/sabre/event/lib/Promise.php +++ b/vendor/sabre/event/lib/Promise.php @@ -1,4 +1,6 @@ -<?php declare (strict_types=1); +<?php + +declare(strict_types=1); namespace Sabre\Event; @@ -21,8 +23,8 @@ use Throwable; * @author Evert Pot (http://evertpot.com/) * @license http://sabre.io/license/ Modified BSD License */ -class Promise { - +class Promise +{ /** * The asynchronous operation is pending. */ @@ -54,15 +56,14 @@ class Promise { * Each are callbacks that map to $this->fulfill and $this->reject. * Using the executor is optional. */ - function __construct(callable $executor = null) { - + public function __construct(callable $executor = null) + { if ($executor) { $executor( [$this, 'fulfill'], [$this, 'reject'] ); } - } /** @@ -84,32 +85,32 @@ class Promise { * If either of the callbacks throw an exception, the returned promise will * be rejected and the exception will be passed back. */ - function then(callable $onFulfilled = null, callable $onRejected = null) : Promise { - + public function then(callable $onFulfilled = null, callable $onRejected = null): Promise + { // This new subPromise will be returned from this function, and will // be fulfilled with the result of the onFulfilled or onRejected event // handlers. $subPromise = new self(); switch ($this->state) { - case self::PENDING : + case self::PENDING: // The operation is pending, so we keep a reference to the // event handlers so we can call them later. $this->subscribers[] = [$subPromise, $onFulfilled, $onRejected]; break; - case self::FULFILLED : + case self::FULFILLED: // The async operation is already fulfilled, so we trigger the // onFulfilled callback asap. $this->invokeCallback($subPromise, $onFulfilled); break; - case self::REJECTED : - // The async operation failed, so we call teh onRejected + case self::REJECTED: + // The async operation failed, so we call the onRejected // callback asap. $this->invokeCallback($subPromise, $onRejected); break; } - return $subPromise; + return $subPromise; } /** @@ -118,20 +119,19 @@ class Promise { * Its usage is identical to then(). However, the otherwise() function is * preferred. */ - function otherwise(callable $onRejected) : Promise { - + public function otherwise(callable $onRejected): Promise + { return $this->then(null, $onRejected); - } /** * Marks this promise as fulfilled and sets its return value. * * @param mixed $value - * @return void */ - function fulfill($value = null) { - if ($this->state !== self::PENDING) { + public function fulfill($value = null) + { + if (self::PENDING !== $this->state) { throw new PromiseAlreadyResolvedException('This promise is already resolved, and you\'re not allowed to resolve a promise more than once'); } $this->state = self::FULFILLED; @@ -143,11 +143,10 @@ class Promise { /** * Marks this promise as rejected, and set it's rejection reason. - * - * @return void */ - function reject(Throwable $reason) { - if ($this->state !== self::PENDING) { + public function reject(Throwable $reason) + { + if (self::PENDING !== $this->state) { throw new PromiseAlreadyResolvedException('This promise is already resolved, and you\'re not allowed to resolve a promise more than once'); } $this->state = self::REJECTED; @@ -155,13 +154,12 @@ class Promise { foreach ($this->subscribers as $subscriber) { $this->invokeCallback($subscriber[0], $subscriber[2]); } - } /** * Stops execution until this promise is resolved. * - * This method stops exection completely. If the promise is successful with + * This method stops execution completely. If the promise is successful with * a value, this method will return this value. If the promise was * rejected, this method will throw an exception. * @@ -171,11 +169,10 @@ class Promise { * * @return mixed */ - function wait() { - + public function wait() + { $hasEvents = true; - while ($this->state === self::PENDING) { - + while (self::PENDING === $this->state) { if (!$hasEvents) { throw new \LogicException('There were no more events in the loop. This promise will never be fulfilled.'); } @@ -183,10 +180,9 @@ class Promise { // As long as the promise is not fulfilled, we tell the event loop // to handle events, and to block. $hasEvents = Loop\tick(true); - } - if ($this->state === self::FULFILLED) { + if (self::FULFILLED === $this->state) { // If the state of this promise is fulfilled, we can return the value. return $this->value; } else { @@ -194,11 +190,8 @@ class Promise { // errored. Therefore we need to throw an exception. throw $this->value; } - - } - /** * A list of subscribers. Subscribers are the callbacks that want us to let * them know if the callback was fulfilled or rejected. @@ -224,21 +217,18 @@ class Promise { * correctly, and any chained promises are also correctly fulfilled or * rejected. * - * @param Promise $subPromise * @param callable $callBack - * @return void */ - private function invokeCallback(Promise $subPromise, callable $callBack = null) { - + private function invokeCallback(Promise $subPromise, callable $callBack = null) + { // We use 'nextTick' to ensure that the event handlers are always // triggered outside of the calling stack in which they were originally // passed to 'then'. // // This makes the order of execution more predictable. - Loop\nextTick(function() use ($callBack, $subPromise) { + Loop\nextTick(function () use ($callBack, $subPromise) { if (is_callable($callBack)) { try { - $result = $callBack($this->value); if ($result instanceof self) { // If the callback (onRejected or onFulfilled) @@ -257,7 +247,7 @@ class Promise { $subPromise->reject($e); } } else { - if ($this->state === self::FULFILLED) { + if (self::FULFILLED === $this->state) { $subPromise->fulfill($this->value); } else { $subPromise->reject($this->value); @@ -265,5 +255,4 @@ class Promise { } }); } - } |