Edit File by line
/home/barbar84/www/wp-conte.../plugins/updraftp.../vendor/guzzleht.../promises/src
File: Promise.php
<?php
[0] Fix | Delete
[1] Fix | Delete
namespace GuzzleHttp\Promise;
[2] Fix | Delete
[3] Fix | Delete
/**
[4] Fix | Delete
* Promises/A+ implementation that avoids recursion when possible.
[5] Fix | Delete
*
[6] Fix | Delete
* @link https://promisesaplus.com/
[7] Fix | Delete
*/
[8] Fix | Delete
class Promise implements PromiseInterface
[9] Fix | Delete
{
[10] Fix | Delete
private $state = self::PENDING;
[11] Fix | Delete
private $result;
[12] Fix | Delete
private $cancelFn;
[13] Fix | Delete
private $waitFn;
[14] Fix | Delete
private $waitList;
[15] Fix | Delete
private $handlers = [];
[16] Fix | Delete
[17] Fix | Delete
/**
[18] Fix | Delete
* @param callable $waitFn Fn that when invoked resolves the promise.
[19] Fix | Delete
* @param callable $cancelFn Fn that when invoked cancels the promise.
[20] Fix | Delete
*/
[21] Fix | Delete
public function __construct(
[22] Fix | Delete
callable $waitFn = null,
[23] Fix | Delete
callable $cancelFn = null
[24] Fix | Delete
) {
[25] Fix | Delete
$this->waitFn = $waitFn;
[26] Fix | Delete
$this->cancelFn = $cancelFn;
[27] Fix | Delete
}
[28] Fix | Delete
[29] Fix | Delete
public function then(
[30] Fix | Delete
callable $onFulfilled = null,
[31] Fix | Delete
callable $onRejected = null
[32] Fix | Delete
) {
[33] Fix | Delete
if ($this->state === self::PENDING) {
[34] Fix | Delete
$p = new Promise(null, [$this, 'cancel']);
[35] Fix | Delete
$this->handlers[] = [$p, $onFulfilled, $onRejected];
[36] Fix | Delete
$p->waitList = $this->waitList;
[37] Fix | Delete
$p->waitList[] = $this;
[38] Fix | Delete
return $p;
[39] Fix | Delete
}
[40] Fix | Delete
[41] Fix | Delete
// Return a fulfilled promise and immediately invoke any callbacks.
[42] Fix | Delete
if ($this->state === self::FULFILLED) {
[43] Fix | Delete
$promise = Create::promiseFor($this->result);
[44] Fix | Delete
return $onFulfilled ? $promise->then($onFulfilled) : $promise;
[45] Fix | Delete
}
[46] Fix | Delete
[47] Fix | Delete
// It's either cancelled or rejected, so return a rejected promise
[48] Fix | Delete
// and immediately invoke any callbacks.
[49] Fix | Delete
$rejection = Create::rejectionFor($this->result);
[50] Fix | Delete
return $onRejected ? $rejection->then(null, $onRejected) : $rejection;
[51] Fix | Delete
}
[52] Fix | Delete
[53] Fix | Delete
public function otherwise(callable $onRejected)
[54] Fix | Delete
{
[55] Fix | Delete
return $this->then(null, $onRejected);
[56] Fix | Delete
}
[57] Fix | Delete
[58] Fix | Delete
public function wait($unwrap = true)
[59] Fix | Delete
{
[60] Fix | Delete
$this->waitIfPending();
[61] Fix | Delete
[62] Fix | Delete
if ($this->result instanceof PromiseInterface) {
[63] Fix | Delete
return $this->result->wait($unwrap);
[64] Fix | Delete
}
[65] Fix | Delete
if ($unwrap) {
[66] Fix | Delete
if ($this->state === self::FULFILLED) {
[67] Fix | Delete
return $this->result;
[68] Fix | Delete
}
[69] Fix | Delete
// It's rejected so "unwrap" and throw an exception.
[70] Fix | Delete
throw Create::exceptionFor($this->result);
[71] Fix | Delete
}
[72] Fix | Delete
}
[73] Fix | Delete
[74] Fix | Delete
public function getState()
[75] Fix | Delete
{
[76] Fix | Delete
return $this->state;
[77] Fix | Delete
}
[78] Fix | Delete
[79] Fix | Delete
public function cancel()
[80] Fix | Delete
{
[81] Fix | Delete
if ($this->state !== self::PENDING) {
[82] Fix | Delete
return;
[83] Fix | Delete
}
[84] Fix | Delete
[85] Fix | Delete
$this->waitFn = $this->waitList = null;
[86] Fix | Delete
[87] Fix | Delete
if ($this->cancelFn) {
[88] Fix | Delete
$fn = $this->cancelFn;
[89] Fix | Delete
$this->cancelFn = null;
[90] Fix | Delete
try {
[91] Fix | Delete
$fn();
[92] Fix | Delete
} catch (\Throwable $e) {
[93] Fix | Delete
$this->reject($e);
[94] Fix | Delete
} catch (\Exception $e) {
[95] Fix | Delete
$this->reject($e);
[96] Fix | Delete
}
[97] Fix | Delete
}
[98] Fix | Delete
[99] Fix | Delete
// Reject the promise only if it wasn't rejected in a then callback.
[100] Fix | Delete
/** @psalm-suppress RedundantCondition */
[101] Fix | Delete
if ($this->state === self::PENDING) {
[102] Fix | Delete
$this->reject(new CancellationException('Promise has been cancelled'));
[103] Fix | Delete
}
[104] Fix | Delete
}
[105] Fix | Delete
[106] Fix | Delete
public function resolve($value)
[107] Fix | Delete
{
[108] Fix | Delete
$this->settle(self::FULFILLED, $value);
[109] Fix | Delete
}
[110] Fix | Delete
[111] Fix | Delete
public function reject($reason)
[112] Fix | Delete
{
[113] Fix | Delete
$this->settle(self::REJECTED, $reason);
[114] Fix | Delete
}
[115] Fix | Delete
[116] Fix | Delete
private function settle($state, $value)
[117] Fix | Delete
{
[118] Fix | Delete
if ($this->state !== self::PENDING) {
[119] Fix | Delete
// Ignore calls with the same resolution.
[120] Fix | Delete
if ($state === $this->state && $value === $this->result) {
[121] Fix | Delete
return;
[122] Fix | Delete
}
[123] Fix | Delete
throw $this->state === $state
[124] Fix | Delete
? new \LogicException("The promise is already {$state}.")
[125] Fix | Delete
: new \LogicException("Cannot change a {$this->state} promise to {$state}");
[126] Fix | Delete
}
[127] Fix | Delete
[128] Fix | Delete
if ($value === $this) {
[129] Fix | Delete
throw new \LogicException('Cannot fulfill or reject a promise with itself');
[130] Fix | Delete
}
[131] Fix | Delete
[132] Fix | Delete
// Clear out the state of the promise but stash the handlers.
[133] Fix | Delete
$this->state = $state;
[134] Fix | Delete
$this->result = $value;
[135] Fix | Delete
$handlers = $this->handlers;
[136] Fix | Delete
$this->handlers = null;
[137] Fix | Delete
$this->waitList = $this->waitFn = null;
[138] Fix | Delete
$this->cancelFn = null;
[139] Fix | Delete
[140] Fix | Delete
if (!$handlers) {
[141] Fix | Delete
return;
[142] Fix | Delete
}
[143] Fix | Delete
[144] Fix | Delete
// If the value was not a settled promise or a thenable, then resolve
[145] Fix | Delete
// it in the task queue using the correct ID.
[146] Fix | Delete
if (!is_object($value) || !method_exists($value, 'then')) {
[147] Fix | Delete
$id = $state === self::FULFILLED ? 1 : 2;
[148] Fix | Delete
// It's a success, so resolve the handlers in the queue.
[149] Fix | Delete
Utils::queue()->add(static function () use ($id, $value, $handlers) {
[150] Fix | Delete
foreach ($handlers as $handler) {
[151] Fix | Delete
self::callHandler($id, $value, $handler);
[152] Fix | Delete
}
[153] Fix | Delete
});
[154] Fix | Delete
} elseif ($value instanceof Promise && Is::pending($value)) {
[155] Fix | Delete
// We can just merge our handlers onto the next promise.
[156] Fix | Delete
$value->handlers = array_merge($value->handlers, $handlers);
[157] Fix | Delete
} else {
[158] Fix | Delete
// Resolve the handlers when the forwarded promise is resolved.
[159] Fix | Delete
$value->then(
[160] Fix | Delete
static function ($value) use ($handlers) {
[161] Fix | Delete
foreach ($handlers as $handler) {
[162] Fix | Delete
self::callHandler(1, $value, $handler);
[163] Fix | Delete
}
[164] Fix | Delete
},
[165] Fix | Delete
static function ($reason) use ($handlers) {
[166] Fix | Delete
foreach ($handlers as $handler) {
[167] Fix | Delete
self::callHandler(2, $reason, $handler);
[168] Fix | Delete
}
[169] Fix | Delete
}
[170] Fix | Delete
);
[171] Fix | Delete
}
[172] Fix | Delete
}
[173] Fix | Delete
[174] Fix | Delete
/**
[175] Fix | Delete
* Call a stack of handlers using a specific callback index and value.
[176] Fix | Delete
*
[177] Fix | Delete
* @param int $index 1 (resolve) or 2 (reject).
[178] Fix | Delete
* @param mixed $value Value to pass to the callback.
[179] Fix | Delete
* @param array $handler Array of handler data (promise and callbacks).
[180] Fix | Delete
*/
[181] Fix | Delete
private static function callHandler($index, $value, array $handler)
[182] Fix | Delete
{
[183] Fix | Delete
/** @var PromiseInterface $promise */
[184] Fix | Delete
$promise = $handler[0];
[185] Fix | Delete
[186] Fix | Delete
// The promise may have been cancelled or resolved before placing
[187] Fix | Delete
// this thunk in the queue.
[188] Fix | Delete
if (Is::settled($promise)) {
[189] Fix | Delete
return;
[190] Fix | Delete
}
[191] Fix | Delete
[192] Fix | Delete
try {
[193] Fix | Delete
if (isset($handler[$index])) {
[194] Fix | Delete
/*
[195] Fix | Delete
* If $f throws an exception, then $handler will be in the exception
[196] Fix | Delete
* stack trace. Since $handler contains a reference to the callable
[197] Fix | Delete
* itself we get a circular reference. We clear the $handler
[198] Fix | Delete
* here to avoid that memory leak.
[199] Fix | Delete
*/
[200] Fix | Delete
$f = $handler[$index];
[201] Fix | Delete
unset($handler);
[202] Fix | Delete
$promise->resolve($f($value));
[203] Fix | Delete
} elseif ($index === 1) {
[204] Fix | Delete
// Forward resolution values as-is.
[205] Fix | Delete
$promise->resolve($value);
[206] Fix | Delete
} else {
[207] Fix | Delete
// Forward rejections down the chain.
[208] Fix | Delete
$promise->reject($value);
[209] Fix | Delete
}
[210] Fix | Delete
} catch (\Throwable $reason) {
[211] Fix | Delete
$promise->reject($reason);
[212] Fix | Delete
} catch (\Exception $reason) {
[213] Fix | Delete
$promise->reject($reason);
[214] Fix | Delete
}
[215] Fix | Delete
}
[216] Fix | Delete
[217] Fix | Delete
private function waitIfPending()
[218] Fix | Delete
{
[219] Fix | Delete
if ($this->state !== self::PENDING) {
[220] Fix | Delete
return;
[221] Fix | Delete
} elseif ($this->waitFn) {
[222] Fix | Delete
$this->invokeWaitFn();
[223] Fix | Delete
} elseif ($this->waitList) {
[224] Fix | Delete
$this->invokeWaitList();
[225] Fix | Delete
} else {
[226] Fix | Delete
// If there's no wait function, then reject the promise.
[227] Fix | Delete
$this->reject('Cannot wait on a promise that has '
[228] Fix | Delete
. 'no internal wait function. You must provide a wait '
[229] Fix | Delete
. 'function when constructing the promise to be able to '
[230] Fix | Delete
. 'wait on a promise.');
[231] Fix | Delete
}
[232] Fix | Delete
[233] Fix | Delete
Utils::queue()->run();
[234] Fix | Delete
[235] Fix | Delete
/** @psalm-suppress RedundantCondition */
[236] Fix | Delete
if ($this->state === self::PENDING) {
[237] Fix | Delete
$this->reject('Invoking the wait callback did not resolve the promise');
[238] Fix | Delete
}
[239] Fix | Delete
}
[240] Fix | Delete
[241] Fix | Delete
private function invokeWaitFn()
[242] Fix | Delete
{
[243] Fix | Delete
try {
[244] Fix | Delete
$wfn = $this->waitFn;
[245] Fix | Delete
$this->waitFn = null;
[246] Fix | Delete
$wfn(true);
[247] Fix | Delete
} catch (\Exception $reason) {
[248] Fix | Delete
if ($this->state === self::PENDING) {
[249] Fix | Delete
// The promise has not been resolved yet, so reject the promise
[250] Fix | Delete
// with the exception.
[251] Fix | Delete
$this->reject($reason);
[252] Fix | Delete
} else {
[253] Fix | Delete
// The promise was already resolved, so there's a problem in
[254] Fix | Delete
// the application.
[255] Fix | Delete
throw $reason;
[256] Fix | Delete
}
[257] Fix | Delete
}
[258] Fix | Delete
}
[259] Fix | Delete
[260] Fix | Delete
private function invokeWaitList()
[261] Fix | Delete
{
[262] Fix | Delete
$waitList = $this->waitList;
[263] Fix | Delete
$this->waitList = null;
[264] Fix | Delete
[265] Fix | Delete
foreach ($waitList as $result) {
[266] Fix | Delete
do {
[267] Fix | Delete
$result->waitIfPending();
[268] Fix | Delete
$result = $result->result;
[269] Fix | Delete
} while ($result instanceof Promise);
[270] Fix | Delete
[271] Fix | Delete
if ($result instanceof PromiseInterface) {
[272] Fix | Delete
$result->wait(false);
[273] Fix | Delete
}
[274] Fix | Delete
}
[275] Fix | Delete
}
[276] Fix | Delete
}
[277] Fix | Delete
[278] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function