Edit File by line
/home/barbar84/www/wp-conte.../plugins/updraftp.../vendor/guzzleht.../promises/src
File: Utils.php
<?php
[0] Fix | Delete
[1] Fix | Delete
namespace GuzzleHttp\Promise;
[2] Fix | Delete
[3] Fix | Delete
final class Utils
[4] Fix | Delete
{
[5] Fix | Delete
/**
[6] Fix | Delete
* Get the global task queue used for promise resolution.
[7] Fix | Delete
*
[8] Fix | Delete
* This task queue MUST be run in an event loop in order for promises to be
[9] Fix | Delete
* settled asynchronously. It will be automatically run when synchronously
[10] Fix | Delete
* waiting on a promise.
[11] Fix | Delete
*
[12] Fix | Delete
* <code>
[13] Fix | Delete
* while ($eventLoop->isRunning()) {
[14] Fix | Delete
* GuzzleHttp\Promise\Utils::queue()->run();
[15] Fix | Delete
* }
[16] Fix | Delete
* </code>
[17] Fix | Delete
*
[18] Fix | Delete
* @param TaskQueueInterface $assign Optionally specify a new queue instance.
[19] Fix | Delete
*
[20] Fix | Delete
* @return TaskQueueInterface
[21] Fix | Delete
*/
[22] Fix | Delete
public static function queue(TaskQueueInterface $assign = null)
[23] Fix | Delete
{
[24] Fix | Delete
static $queue;
[25] Fix | Delete
[26] Fix | Delete
if ($assign) {
[27] Fix | Delete
$queue = $assign;
[28] Fix | Delete
} elseif (!$queue) {
[29] Fix | Delete
$queue = new TaskQueue();
[30] Fix | Delete
}
[31] Fix | Delete
[32] Fix | Delete
return $queue;
[33] Fix | Delete
}
[34] Fix | Delete
[35] Fix | Delete
/**
[36] Fix | Delete
* Adds a function to run in the task queue when it is next `run()` and
[37] Fix | Delete
* returns a promise that is fulfilled or rejected with the result.
[38] Fix | Delete
*
[39] Fix | Delete
* @param callable $task Task function to run.
[40] Fix | Delete
*
[41] Fix | Delete
* @return PromiseInterface
[42] Fix | Delete
*/
[43] Fix | Delete
public static function task(callable $task)
[44] Fix | Delete
{
[45] Fix | Delete
$queue = self::queue();
[46] Fix | Delete
$promise = new Promise([$queue, 'run']);
[47] Fix | Delete
$queue->add(function () use ($task, $promise) {
[48] Fix | Delete
try {
[49] Fix | Delete
if (Is::pending($promise)) {
[50] Fix | Delete
$promise->resolve($task());
[51] Fix | Delete
}
[52] Fix | Delete
} catch (\Throwable $e) {
[53] Fix | Delete
$promise->reject($e);
[54] Fix | Delete
} catch (\Exception $e) {
[55] Fix | Delete
$promise->reject($e);
[56] Fix | Delete
}
[57] Fix | Delete
});
[58] Fix | Delete
[59] Fix | Delete
return $promise;
[60] Fix | Delete
}
[61] Fix | Delete
[62] Fix | Delete
/**
[63] Fix | Delete
* Synchronously waits on a promise to resolve and returns an inspection
[64] Fix | Delete
* state array.
[65] Fix | Delete
*
[66] Fix | Delete
* Returns a state associative array containing a "state" key mapping to a
[67] Fix | Delete
* valid promise state. If the state of the promise is "fulfilled", the
[68] Fix | Delete
* array will contain a "value" key mapping to the fulfilled value of the
[69] Fix | Delete
* promise. If the promise is rejected, the array will contain a "reason"
[70] Fix | Delete
* key mapping to the rejection reason of the promise.
[71] Fix | Delete
*
[72] Fix | Delete
* @param PromiseInterface $promise Promise or value.
[73] Fix | Delete
*
[74] Fix | Delete
* @return array
[75] Fix | Delete
*/
[76] Fix | Delete
public static function inspect(PromiseInterface $promise)
[77] Fix | Delete
{
[78] Fix | Delete
try {
[79] Fix | Delete
return [
[80] Fix | Delete
'state' => PromiseInterface::FULFILLED,
[81] Fix | Delete
'value' => $promise->wait()
[82] Fix | Delete
];
[83] Fix | Delete
} catch (RejectionException $e) {
[84] Fix | Delete
return ['state' => PromiseInterface::REJECTED, 'reason' => $e->getReason()];
[85] Fix | Delete
} catch (\Throwable $e) {
[86] Fix | Delete
return ['state' => PromiseInterface::REJECTED, 'reason' => $e];
[87] Fix | Delete
} catch (\Exception $e) {
[88] Fix | Delete
return ['state' => PromiseInterface::REJECTED, 'reason' => $e];
[89] Fix | Delete
}
[90] Fix | Delete
}
[91] Fix | Delete
[92] Fix | Delete
/**
[93] Fix | Delete
* Waits on all of the provided promises, but does not unwrap rejected
[94] Fix | Delete
* promises as thrown exception.
[95] Fix | Delete
*
[96] Fix | Delete
* Returns an array of inspection state arrays.
[97] Fix | Delete
*
[98] Fix | Delete
* @see inspect for the inspection state array format.
[99] Fix | Delete
*
[100] Fix | Delete
* @param PromiseInterface[] $promises Traversable of promises to wait upon.
[101] Fix | Delete
*
[102] Fix | Delete
* @return array
[103] Fix | Delete
*/
[104] Fix | Delete
public static function inspectAll($promises)
[105] Fix | Delete
{
[106] Fix | Delete
$results = [];
[107] Fix | Delete
foreach ($promises as $key => $promise) {
[108] Fix | Delete
$results[$key] = inspect($promise);
[109] Fix | Delete
}
[110] Fix | Delete
[111] Fix | Delete
return $results;
[112] Fix | Delete
}
[113] Fix | Delete
[114] Fix | Delete
/**
[115] Fix | Delete
* Waits on all of the provided promises and returns the fulfilled values.
[116] Fix | Delete
*
[117] Fix | Delete
* Returns an array that contains the value of each promise (in the same
[118] Fix | Delete
* order the promises were provided). An exception is thrown if any of the
[119] Fix | Delete
* promises are rejected.
[120] Fix | Delete
*
[121] Fix | Delete
* @param iterable<PromiseInterface> $promises Iterable of PromiseInterface objects to wait on.
[122] Fix | Delete
*
[123] Fix | Delete
* @return array
[124] Fix | Delete
*
[125] Fix | Delete
* @throws \Exception on error
[126] Fix | Delete
* @throws \Throwable on error in PHP >=7
[127] Fix | Delete
*/
[128] Fix | Delete
public static function unwrap($promises)
[129] Fix | Delete
{
[130] Fix | Delete
$results = [];
[131] Fix | Delete
foreach ($promises as $key => $promise) {
[132] Fix | Delete
$results[$key] = $promise->wait();
[133] Fix | Delete
}
[134] Fix | Delete
[135] Fix | Delete
return $results;
[136] Fix | Delete
}
[137] Fix | Delete
[138] Fix | Delete
/**
[139] Fix | Delete
* Given an array of promises, return a promise that is fulfilled when all
[140] Fix | Delete
* the items in the array are fulfilled.
[141] Fix | Delete
*
[142] Fix | Delete
* The promise's fulfillment value is an array with fulfillment values at
[143] Fix | Delete
* respective positions to the original array. If any promise in the array
[144] Fix | Delete
* rejects, the returned promise is rejected with the rejection reason.
[145] Fix | Delete
*
[146] Fix | Delete
* @param mixed $promises Promises or values.
[147] Fix | Delete
* @param bool $recursive If true, resolves new promises that might have been added to the stack during its own resolution.
[148] Fix | Delete
*
[149] Fix | Delete
* @return PromiseInterface
[150] Fix | Delete
*/
[151] Fix | Delete
public static function all($promises, $recursive = false)
[152] Fix | Delete
{
[153] Fix | Delete
$results = [];
[154] Fix | Delete
$promise = Each::of(
[155] Fix | Delete
$promises,
[156] Fix | Delete
function ($value, $idx) use (&$results) {
[157] Fix | Delete
$results[$idx] = $value;
[158] Fix | Delete
},
[159] Fix | Delete
function ($reason, $idx, Promise $aggregate) {
[160] Fix | Delete
$aggregate->reject($reason);
[161] Fix | Delete
}
[162] Fix | Delete
)->then(function () use (&$results) {
[163] Fix | Delete
ksort($results);
[164] Fix | Delete
return $results;
[165] Fix | Delete
});
[166] Fix | Delete
[167] Fix | Delete
if (true === $recursive) {
[168] Fix | Delete
$promise = $promise->then(function ($results) use ($recursive, &$promises) {
[169] Fix | Delete
foreach ($promises as $promise) {
[170] Fix | Delete
if (Is::pending($promise)) {
[171] Fix | Delete
return self::all($promises, $recursive);
[172] Fix | Delete
}
[173] Fix | Delete
}
[174] Fix | Delete
return $results;
[175] Fix | Delete
});
[176] Fix | Delete
}
[177] Fix | Delete
[178] Fix | Delete
return $promise;
[179] Fix | Delete
}
[180] Fix | Delete
[181] Fix | Delete
/**
[182] Fix | Delete
* Initiate a competitive race between multiple promises or values (values
[183] Fix | Delete
* will become immediately fulfilled promises).
[184] Fix | Delete
*
[185] Fix | Delete
* When count amount of promises have been fulfilled, the returned promise
[186] Fix | Delete
* is fulfilled with an array that contains the fulfillment values of the
[187] Fix | Delete
* winners in order of resolution.
[188] Fix | Delete
*
[189] Fix | Delete
* This promise is rejected with a {@see AggregateException} if the number
[190] Fix | Delete
* of fulfilled promises is less than the desired $count.
[191] Fix | Delete
*
[192] Fix | Delete
* @param int $count Total number of promises.
[193] Fix | Delete
* @param mixed $promises Promises or values.
[194] Fix | Delete
*
[195] Fix | Delete
* @return PromiseInterface
[196] Fix | Delete
*/
[197] Fix | Delete
public static function some($count, $promises)
[198] Fix | Delete
{
[199] Fix | Delete
$results = [];
[200] Fix | Delete
$rejections = [];
[201] Fix | Delete
[202] Fix | Delete
return Each::of(
[203] Fix | Delete
$promises,
[204] Fix | Delete
function ($value, $idx, PromiseInterface $p) use (&$results, $count) {
[205] Fix | Delete
if (Is::settled($p)) {
[206] Fix | Delete
return;
[207] Fix | Delete
}
[208] Fix | Delete
$results[$idx] = $value;
[209] Fix | Delete
if (count($results) >= $count) {
[210] Fix | Delete
$p->resolve(null);
[211] Fix | Delete
}
[212] Fix | Delete
},
[213] Fix | Delete
function ($reason) use (&$rejections) {
[214] Fix | Delete
$rejections[] = $reason;
[215] Fix | Delete
}
[216] Fix | Delete
)->then(
[217] Fix | Delete
function () use (&$results, &$rejections, $count) {
[218] Fix | Delete
if (count($results) !== $count) {
[219] Fix | Delete
throw new AggregateException(
[220] Fix | Delete
'Not enough promises to fulfill count',
[221] Fix | Delete
$rejections
[222] Fix | Delete
);
[223] Fix | Delete
}
[224] Fix | Delete
ksort($results);
[225] Fix | Delete
return array_values($results);
[226] Fix | Delete
}
[227] Fix | Delete
);
[228] Fix | Delete
}
[229] Fix | Delete
[230] Fix | Delete
/**
[231] Fix | Delete
* Like some(), with 1 as count. However, if the promise fulfills, the
[232] Fix | Delete
* fulfillment value is not an array of 1 but the value directly.
[233] Fix | Delete
*
[234] Fix | Delete
* @param mixed $promises Promises or values.
[235] Fix | Delete
*
[236] Fix | Delete
* @return PromiseInterface
[237] Fix | Delete
*/
[238] Fix | Delete
public static function any($promises)
[239] Fix | Delete
{
[240] Fix | Delete
return self::some(1, $promises)->then(function ($values) {
[241] Fix | Delete
return $values[0];
[242] Fix | Delete
});
[243] Fix | Delete
}
[244] Fix | Delete
[245] Fix | Delete
/**
[246] Fix | Delete
* Returns a promise that is fulfilled when all of the provided promises have
[247] Fix | Delete
* been fulfilled or rejected.
[248] Fix | Delete
*
[249] Fix | Delete
* The returned promise is fulfilled with an array of inspection state arrays.
[250] Fix | Delete
*
[251] Fix | Delete
* @see inspect for the inspection state array format.
[252] Fix | Delete
*
[253] Fix | Delete
* @param mixed $promises Promises or values.
[254] Fix | Delete
*
[255] Fix | Delete
* @return PromiseInterface
[256] Fix | Delete
*/
[257] Fix | Delete
public static function settle($promises)
[258] Fix | Delete
{
[259] Fix | Delete
$results = [];
[260] Fix | Delete
[261] Fix | Delete
return Each::of(
[262] Fix | Delete
$promises,
[263] Fix | Delete
function ($value, $idx) use (&$results) {
[264] Fix | Delete
$results[$idx] = ['state' => PromiseInterface::FULFILLED, 'value' => $value];
[265] Fix | Delete
},
[266] Fix | Delete
function ($reason, $idx) use (&$results) {
[267] Fix | Delete
$results[$idx] = ['state' => PromiseInterface::REJECTED, 'reason' => $reason];
[268] Fix | Delete
}
[269] Fix | Delete
)->then(function () use (&$results) {
[270] Fix | Delete
ksort($results);
[271] Fix | Delete
return $results;
[272] Fix | Delete
});
[273] Fix | Delete
}
[274] Fix | Delete
}
[275] Fix | Delete
[276] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function