2014-09-22 22:47:48 +02:00
|
|
|
<?php
|
|
|
|
|
2014-09-23 04:38:32 +02:00
|
|
|
namespace Amp;
|
2014-09-22 22:47:48 +02:00
|
|
|
|
|
|
|
/**
|
2015-07-29 07:15:43 +02:00
|
|
|
* Retrieve the application-wide event reactor instance
|
2015-05-20 21:18:30 +02:00
|
|
|
*
|
2015-07-29 07:15:43 +02:00
|
|
|
* @param \Amp\Reactor $assign Optionally specify a new default event reactor instance
|
|
|
|
* @return \Amp\Reactor Returns the application-wide reactor instance
|
2014-09-24 05:47:20 +02:00
|
|
|
*/
|
2015-07-29 07:15:43 +02:00
|
|
|
function reactor(Reactor $assign = null) {
|
2014-09-24 05:47:20 +02:00
|
|
|
static $reactor;
|
2015-07-29 07:15:43 +02:00
|
|
|
if ($assign) {
|
|
|
|
return ($reactor = $assign);
|
2015-05-20 21:18:30 +02:00
|
|
|
} elseif ($reactor) {
|
|
|
|
return $reactor;
|
2015-07-29 07:15:43 +02:00
|
|
|
} else {
|
2015-07-31 15:30:57 +02:00
|
|
|
return ($reactor = driver());
|
2015-07-29 07:15:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-07-31 15:30:57 +02:00
|
|
|
* Create a new event reactor best-suited for the current environment
|
2015-07-29 07:15:43 +02:00
|
|
|
*
|
|
|
|
* @return \Amp\Reactor
|
|
|
|
*/
|
2015-07-31 15:30:57 +02:00
|
|
|
function driver() {
|
2015-07-29 07:15:43 +02:00
|
|
|
if (\extension_loaded("uv")) {
|
|
|
|
return new UvReactor;
|
2015-07-20 21:42:42 +02:00
|
|
|
} elseif (\extension_loaded("ev")) {
|
2015-07-29 07:15:43 +02:00
|
|
|
return new EvReactor;
|
2015-07-20 21:42:42 +02:00
|
|
|
} elseif (\extension_loaded("libevent")) {
|
2015-07-29 07:15:43 +02:00
|
|
|
return new LibeventReactor;
|
2014-12-02 01:45:57 +01:00
|
|
|
} else {
|
2015-07-29 07:15:43 +02:00
|
|
|
return new NativeReactor;
|
2014-12-02 01:45:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-07-29 07:15:43 +02:00
|
|
|
* Start the event reactor and assume program flow control
|
2014-12-02 01:45:57 +01:00
|
|
|
*
|
2015-05-20 21:18:30 +02:00
|
|
|
* @param callable $onStart An optional callback to invoke immediately when the Reactor starts
|
|
|
|
* @return void
|
2014-12-02 01:45:57 +01:00
|
|
|
*/
|
2015-05-20 21:18:30 +02:00
|
|
|
function run(callable $onStart = null) {
|
|
|
|
reactor()->run($onStart);
|
2014-12-02 01:45:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-07-29 07:15:43 +02:00
|
|
|
* Execute a single event loop iteration
|
2014-12-02 01:45:57 +01:00
|
|
|
*
|
2015-07-29 07:15:43 +02:00
|
|
|
* @param bool $noWait Should the function return immediately if no watchers are ready to trigger?
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function tick($noWait = false) {
|
|
|
|
reactor()->tick($noWait);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stop the default event reactor and return program flow control
|
2015-04-30 19:41:14 +02:00
|
|
|
*
|
2015-05-20 21:18:30 +02:00
|
|
|
* @return void
|
2014-12-02 01:45:57 +01:00
|
|
|
*/
|
2015-05-20 21:18:30 +02:00
|
|
|
function stop() {
|
|
|
|
reactor()->stop();
|
2014-12-02 01:45:57 +01:00
|
|
|
}
|
|
|
|
|
2015-07-29 07:15:43 +02:00
|
|
|
/**
|
|
|
|
* Schedule a callback for immediate invocation in the next event loop iteration
|
|
|
|
*
|
|
|
|
* @param callable $callback A callback to invoke in the next iteration of the event loop
|
|
|
|
* @param array $options Watcher options
|
|
|
|
* @return string Returns unique (to the process) string watcher ID
|
|
|
|
*/
|
|
|
|
function immediately(callable $callback, array $options = []) {
|
|
|
|
return reactor()->immediately($callback, $options);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Schedule a callback to execute once
|
|
|
|
*
|
|
|
|
* @param callable $callback A callback to invoke after the specified millisecond delay
|
|
|
|
* @param int $msDelay the number of milliseconds to wait before invoking $callback
|
|
|
|
* @param array $options Watcher options
|
|
|
|
* @return string Returns unique (to the process) string watcher ID
|
|
|
|
*/
|
|
|
|
function once(callable $callback, $msDelay, array $options = []) {
|
|
|
|
return reactor()->once($callback, $msDelay, $options);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Schedule a recurring callback to execute every $interval seconds until cancelled
|
|
|
|
*
|
|
|
|
* @param callable $callback A callback to invoke at the $msDelay interval until cancelled
|
|
|
|
* @param int $msInterval The interval at which to repeat $callback invocations
|
|
|
|
* @param array $options Watcher options
|
|
|
|
* @return string Returns unique (to the process) string watcher ID
|
|
|
|
*/
|
|
|
|
function repeat(callable $callback, $msInterval, array $options = []) {
|
|
|
|
return reactor()->repeat($callback, $msInterval, $options);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Watch a stream resource for readable data and trigger the callback when actionable
|
|
|
|
*
|
|
|
|
* @param resource $stream The stream resource to watch for readability
|
|
|
|
* @param callable $callback A callback to invoke when the stream reports as readable
|
|
|
|
* @param array $options Watcher options
|
|
|
|
* @return string Returns unique (to the process) string watcher ID
|
|
|
|
*/
|
|
|
|
function onReadable($stream, callable $callback, array $options = []) {
|
|
|
|
return reactor()->onReadable($stream, $callback, $options);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Watch a stream resource to become writable and trigger the callback when actionable
|
|
|
|
*
|
|
|
|
* @param resource $stream The stream resource to watch for writability
|
|
|
|
* @param callable $callback A callback to invoke when the stream reports as writable
|
|
|
|
* @param array $options Watcher options
|
|
|
|
* @return string Returns unique (to the process) string watcher ID
|
|
|
|
*/
|
|
|
|
function onWritable($stream, callable $callback, array $options = []) {
|
|
|
|
return reactor()->onWritable($stream, $callback, $options);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* React to process control signals
|
|
|
|
*
|
|
|
|
* @param int $signo The signal number for which to watch
|
|
|
|
* @param callable $callback A callback to invoke when the specified signal is received
|
|
|
|
* @param array $options Watcher options
|
|
|
|
* @return string Returns unique (to the process) string watcher ID
|
|
|
|
*/
|
|
|
|
function onSignal($signo, callable $callback, array $options = []) {
|
|
|
|
return reactor()->onSignal($signo, $callback, $options);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* An optional "last-chance" exception handler for errors resulting during callback invocation
|
|
|
|
*
|
|
|
|
* If an application throws inside the event loop and no onError callback is specified the
|
|
|
|
* exception bubbles up and the event loop is stopped. This is undesirable in long-running
|
|
|
|
* applications (like servers) where stopping the event loop for an application error is
|
|
|
|
* problematic. Amp applications can instead specify the onError callback to handle uncaught
|
|
|
|
* exceptions without stopping the event loop.
|
|
|
|
*
|
|
|
|
* Additionally, generator callbacks which are auto-resolved by the event reactor may fail.
|
|
|
|
* Coroutine resolution failures are treated like uncaught exceptions and stop the event reactor
|
|
|
|
* if no onError callback is specified to handle these situations.
|
|
|
|
*
|
|
|
|
* onError callback functions are passed a single parameter: the uncaught exception.
|
|
|
|
*
|
|
|
|
* @param callable $callback A callback to invoke when an exception occurs inside the event loop
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function onError(callable $callback) {
|
|
|
|
reactor()->onError($callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Cancel an existing timer/stream watcher
|
|
|
|
*
|
|
|
|
* @param string $watcherId The watcher ID to be canceled
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function cancel($watcherId) {
|
|
|
|
reactor()->cancel($watcherId);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Temporarily disable (but don't cancel) an existing timer/stream watcher
|
|
|
|
*
|
|
|
|
* @param string $watcherId The watcher ID to be disabled
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function disable($watcherId) {
|
|
|
|
reactor()->disable($watcherId);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enable a disabled timer/stream watcher
|
|
|
|
*
|
|
|
|
* @param string $watcherId The watcher ID to be enabled
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function enable($watcherId) {
|
|
|
|
reactor()->enable($watcherId);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve an associative array of information about the event reactor
|
|
|
|
*
|
|
|
|
* The returned array matches the following data describing the reactor's
|
|
|
|
* currently registered watchers:
|
|
|
|
*
|
|
|
|
* [
|
|
|
|
* "immediately" => ["enabled" => int, "disabled" => int],
|
|
|
|
* "once" => ["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],
|
2015-07-31 07:21:21 +02:00
|
|
|
* "keep_alive" => int,
|
2015-08-17 02:33:07 +02:00
|
|
|
* "state" => int,
|
2015-07-29 07:15:43 +02:00
|
|
|
* ];
|
|
|
|
*
|
|
|
|
* Reactor implementations may optionally add more information in the return array but
|
|
|
|
* at minimum the above key=>value format is always provided.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
function info() {
|
|
|
|
return reactor()->info();
|
|
|
|
}
|
|
|
|
|
2014-12-02 01:45:57 +01:00
|
|
|
/**
|
2015-05-20 21:18:30 +02:00
|
|
|
* Flatten an array of promises into a single promise
|
2014-12-02 01:45:57 +01:00
|
|
|
*
|
2015-05-20 21:18:30 +02:00
|
|
|
* Upon resolution the returned promise's $result parameter is set to an array
|
|
|
|
* whose keys match the original input array and whose values match the individual
|
|
|
|
* resolution results of its component promises.
|
2014-12-02 01:45:57 +01:00
|
|
|
*
|
2015-05-20 21:18:30 +02:00
|
|
|
* If any one of the Promises fails the resulting Promise will immediately fail.
|
2015-04-30 19:41:14 +02:00
|
|
|
*
|
2015-05-20 21:18:30 +02:00
|
|
|
* @param array An array of promises to flatten into a single promise
|
|
|
|
* @return \Amp\Promise
|
2014-09-22 22:47:48 +02:00
|
|
|
*/
|
2015-04-30 19:41:14 +02:00
|
|
|
function all(array $promises) {
|
2014-11-25 05:14:07 +01:00
|
|
|
if (empty($promises)) {
|
|
|
|
return new Success([]);
|
|
|
|
}
|
|
|
|
|
2015-05-20 21:18:30 +02:00
|
|
|
$struct = new \StdClass;
|
|
|
|
$struct->remaining = count($promises);
|
|
|
|
$struct->results = [];
|
|
|
|
$struct->promisor = new Deferred;
|
2014-11-25 05:14:07 +01:00
|
|
|
|
2015-07-29 07:15:43 +02:00
|
|
|
$onResolve = function ($error, $result, $cbData) {
|
2015-05-20 21:18:30 +02:00
|
|
|
list($struct, $key) = $cbData;
|
|
|
|
if (empty($struct->remaining)) {
|
|
|
|
// If the promisor already resolved we don't need to bother
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ($error) {
|
|
|
|
$struct->results = null;
|
|
|
|
$struct->remaining = 0;
|
|
|
|
$struct->promisor->fail($error);
|
|
|
|
return;
|
2014-11-25 05:14:07 +01:00
|
|
|
}
|
|
|
|
|
2015-05-20 21:18:30 +02:00
|
|
|
$struct->results[$key] = $result;
|
|
|
|
if (--$struct->remaining === 0) {
|
|
|
|
$struct->promisor->succeed($struct->results);
|
|
|
|
}
|
|
|
|
};
|
2014-11-25 05:14:07 +01:00
|
|
|
|
2015-05-20 21:18:30 +02:00
|
|
|
foreach ($promises as $key => $promise) {
|
|
|
|
if ($promise instanceof Promise) {
|
|
|
|
$promise->when($onResolve, [$struct, $key]);
|
|
|
|
} else {
|
|
|
|
$struct->results[$key] = $promise;
|
|
|
|
if (--$struct->remaining === 0) {
|
|
|
|
$struct->promisor->succeed($struct->results);
|
2014-11-25 05:14:07 +01:00
|
|
|
}
|
2015-05-20 21:18:30 +02:00
|
|
|
}
|
2014-11-25 05:14:07 +01:00
|
|
|
}
|
|
|
|
|
2015-05-20 21:18:30 +02:00
|
|
|
return $struct->promisor->promise();
|
2014-09-22 22:47:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resolves with a two-item array delineating successful and failed Promise results.
|
|
|
|
*
|
|
|
|
* The resulting Promise will only fail if ALL of the Promise values fail or if the
|
|
|
|
* Promise array is empty.
|
|
|
|
*
|
|
|
|
* The resulting Promise is resolved with an indexed two-item array of the following form:
|
|
|
|
*
|
|
|
|
* [$arrayOfFailures, $arrayOfSuccesses]
|
|
|
|
*
|
|
|
|
* The individual keys in the resulting arrays are preserved from the initial Promise array
|
|
|
|
* passed to the function for evaluation.
|
2015-05-20 21:18:30 +02:00
|
|
|
*
|
|
|
|
* @param array An array of promises to flatten into a single promise
|
|
|
|
* @return \Amp\Promise
|
2014-09-22 22:47:48 +02:00
|
|
|
*/
|
2015-04-30 19:41:14 +02:00
|
|
|
function some(array $promises) {
|
2014-11-25 05:14:07 +01:00
|
|
|
if (empty($promises)) {
|
|
|
|
return new Failure(new \LogicException(
|
2015-05-20 21:18:30 +02:00
|
|
|
"No promises or values provided for resolution"
|
2014-11-25 05:14:07 +01:00
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2015-05-20 21:18:30 +02:00
|
|
|
$struct = new \StdClass;
|
|
|
|
$struct->remaining = count($promises);
|
|
|
|
$struct->errors = [];
|
|
|
|
$struct->results = [];
|
|
|
|
$struct->promisor = new Deferred;
|
2014-11-25 05:14:07 +01:00
|
|
|
|
2015-07-29 07:15:43 +02:00
|
|
|
$onResolve = function ($error, $result, $cbData) {
|
2015-05-20 21:18:30 +02:00
|
|
|
list($struct, $key) = $cbData;
|
|
|
|
if ($error) {
|
|
|
|
$struct->errors[$key] = $error;
|
|
|
|
} else {
|
|
|
|
$struct->results[$key] = $result;
|
2014-11-25 05:14:07 +01:00
|
|
|
}
|
2015-05-20 21:18:30 +02:00
|
|
|
if (--$struct->remaining) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (empty($struct->results)) {
|
|
|
|
array_unshift($struct->errors, "All promises passed to Amp\some() failed");
|
|
|
|
$struct->promisor->fail(new \RuntimeException(
|
|
|
|
implode("\n\n", $struct->errors)
|
|
|
|
));
|
|
|
|
} else {
|
|
|
|
$struct->promisor->succeed([$struct->errors, $struct->results]);
|
|
|
|
}
|
|
|
|
};
|
2014-11-25 05:14:07 +01:00
|
|
|
|
2015-05-20 21:18:30 +02:00
|
|
|
foreach ($promises as $key => $promise) {
|
|
|
|
if ($promise instanceof Promise) {
|
|
|
|
$promise->when($onResolve, [$struct, $key]);
|
|
|
|
} else {
|
|
|
|
$struct->results[$key] = $promise;
|
|
|
|
if (--$struct->remaining === 0) {
|
|
|
|
$struct->promisor->succeed([$struct->errors, $struct->results]);
|
2014-11-25 05:14:07 +01:00
|
|
|
}
|
2015-05-20 21:18:30 +02:00
|
|
|
}
|
2014-11-25 05:14:07 +01:00
|
|
|
}
|
|
|
|
|
2015-05-20 21:18:30 +02:00
|
|
|
return $struct->promisor->promise();
|
2014-09-22 22:47:48 +02:00
|
|
|
}
|
|
|
|
|
2014-10-23 00:14:43 +02:00
|
|
|
/**
|
|
|
|
* Resolves with a two-item array delineating successful and failed Promise results.
|
|
|
|
*
|
|
|
|
* 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.
|
2015-05-20 21:18:30 +02:00
|
|
|
*
|
|
|
|
* @param array An array of promises to flatten into a single promise
|
|
|
|
* @return \Amp\Promise
|
2014-10-23 00:14:43 +02:00
|
|
|
*/
|
2015-04-30 19:41:14 +02:00
|
|
|
function any(array $promises) {
|
2014-11-25 05:14:07 +01:00
|
|
|
if (empty($promises)) {
|
2015-05-15 21:58:43 +02:00
|
|
|
return new Success([[], []]);
|
2014-11-25 05:14:07 +01:00
|
|
|
}
|
|
|
|
|
2015-05-20 21:18:30 +02:00
|
|
|
$struct = new \StdClass;
|
|
|
|
$struct->remaining = count($promises);
|
|
|
|
$struct->errors = [];
|
|
|
|
$struct->results = [];
|
|
|
|
$struct->promisor = new Deferred;
|
2014-11-25 05:14:07 +01:00
|
|
|
|
2015-07-29 07:15:43 +02:00
|
|
|
$onResolve = function ($error, $result, $cbData) {
|
2015-05-20 21:18:30 +02:00
|
|
|
list($struct, $key) = $cbData;
|
|
|
|
if ($error) {
|
|
|
|
$struct->errors[$key] = $error;
|
|
|
|
} else {
|
|
|
|
$struct->results[$key] = $result;
|
2014-11-25 05:14:07 +01:00
|
|
|
}
|
2015-05-20 21:18:30 +02:00
|
|
|
if (--$struct->remaining === 0) {
|
|
|
|
$struct->promisor->succeed([$struct->errors, $struct->results]);
|
|
|
|
}
|
|
|
|
};
|
2014-11-25 05:14:07 +01:00
|
|
|
|
2015-05-20 21:18:30 +02:00
|
|
|
foreach ($promises as $key => $promise) {
|
|
|
|
if ($promise instanceof Promise) {
|
|
|
|
$promise->when($onResolve, [$struct, $key]);
|
|
|
|
} else {
|
|
|
|
$struct->results[$key] = $promise;
|
|
|
|
if (--$struct->remaining === 0) {
|
|
|
|
$struct->promisor->succeed([$struct->errors, $struct->results]);
|
2014-11-25 05:14:07 +01:00
|
|
|
}
|
2015-05-20 21:18:30 +02:00
|
|
|
}
|
2014-11-25 05:14:07 +01:00
|
|
|
}
|
|
|
|
|
2015-05-20 21:18:30 +02:00
|
|
|
return $struct->promisor->promise();
|
2014-10-23 00:14:43 +02:00
|
|
|
}
|
|
|
|
|
2014-09-22 22:47:48 +02:00
|
|
|
/**
|
|
|
|
* Resolves with the first successful Promise value. The resulting Promise will only fail if all
|
|
|
|
* Promise values in the group fail or if the initial Promise array is empty.
|
2015-05-20 21:18:30 +02:00
|
|
|
*
|
|
|
|
* @param array An array of promises to flatten into a single promise
|
|
|
|
* @return \Amp\Promise
|
2014-09-22 22:47:48 +02:00
|
|
|
*/
|
2015-04-30 19:41:14 +02:00
|
|
|
function first(array $promises) {
|
2014-11-25 05:14:07 +01:00
|
|
|
if (empty($promises)) {
|
|
|
|
return new Failure(new \LogicException(
|
2015-07-21 23:25:18 +02:00
|
|
|
"No promises or values provided"
|
2014-11-25 05:14:07 +01:00
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2015-05-20 21:18:30 +02:00
|
|
|
$struct = new \StdClass;
|
|
|
|
$struct->remaining = count($promises);
|
|
|
|
$struct->promisor = new Deferred;
|
2014-11-25 05:14:07 +01:00
|
|
|
|
2015-07-29 07:15:43 +02:00
|
|
|
$onResolve = function ($error, $result, $cbData) {
|
|
|
|
$struct = $cbData;
|
2015-05-20 21:18:30 +02:00
|
|
|
if (empty($struct->remaining)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (empty($error)) {
|
|
|
|
$struct->remaining = 0;
|
|
|
|
$struct->promisor->succeed($result);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (--$struct->remaining === 0) {
|
|
|
|
$struct->promisor->fail(new \RuntimeException(
|
2015-07-21 23:25:18 +02:00
|
|
|
"All promises failed"
|
2015-05-20 21:18:30 +02:00
|
|
|
));
|
2014-11-25 05:14:07 +01:00
|
|
|
}
|
2015-05-20 21:18:30 +02:00
|
|
|
};
|
2014-11-25 05:14:07 +01:00
|
|
|
|
2015-05-20 21:18:30 +02:00
|
|
|
foreach ($promises as $key => $promise) {
|
|
|
|
if ($promise instanceof Promise) {
|
2015-07-29 07:15:43 +02:00
|
|
|
$promise->when($onResolve, $struct);
|
2015-05-20 21:18:30 +02:00
|
|
|
} else {
|
|
|
|
$struct->remaining = 0;
|
|
|
|
$struct->promisor->succeed($promise);
|
|
|
|
break;
|
|
|
|
}
|
2014-11-25 05:14:07 +01:00
|
|
|
}
|
|
|
|
|
2015-05-20 21:18:30 +02:00
|
|
|
return $struct->promisor->promise();
|
2014-09-22 22:47:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-05-19 06:21:33 +02:00
|
|
|
* Map promised deferred values using the specified functor
|
2015-05-20 21:18:30 +02:00
|
|
|
*
|
|
|
|
* @param array An array of promises whose values -- once resoved -- will be mapped by the functor
|
|
|
|
* @param callable $functor The mapping function to apply to eventual promise results
|
|
|
|
* @return \Amp\Promise
|
2014-09-22 22:47:48 +02:00
|
|
|
*/
|
2015-04-30 19:41:14 +02:00
|
|
|
function map(array $promises, callable $functor) {
|
2014-11-25 05:14:07 +01:00
|
|
|
if (empty($promises)) {
|
|
|
|
return new Success([]);
|
|
|
|
}
|
|
|
|
|
2015-05-20 21:18:30 +02:00
|
|
|
$struct = new \StdClass;
|
|
|
|
$struct->remaining = count($promises);
|
|
|
|
$struct->results = [];
|
|
|
|
$struct->promisor = new Deferred;
|
|
|
|
$struct->functor = $functor;
|
2014-11-25 05:14:07 +01:00
|
|
|
|
2015-07-29 07:15:43 +02:00
|
|
|
$onResolve = function ($error, $result, $cbData) {
|
2015-05-20 21:18:30 +02:00
|
|
|
list($struct, $key) = $cbData;
|
|
|
|
if (empty($struct->remaining)) {
|
|
|
|
// If the promisor already resolved we don't need to bother
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ($error) {
|
|
|
|
$struct->results = null;
|
|
|
|
$struct->remaining = 0;
|
|
|
|
$struct->promisor->fail($error);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$struct->remaining--;
|
|
|
|
try {
|
2015-07-21 18:25:34 +02:00
|
|
|
$struct->results[$key] = \call_user_func($struct->functor, $result);
|
2015-07-22 18:34:42 +02:00
|
|
|
if ($struct->remaining === 0) {
|
|
|
|
$struct->promisor->succeed($struct->results);
|
|
|
|
}
|
2015-07-21 23:25:18 +02:00
|
|
|
} catch (\Throwable $e) {
|
2015-07-22 18:34:42 +02:00
|
|
|
// @TODO Remove coverage ignore block once PHP5 support is no longer required
|
|
|
|
// @codeCoverageIgnoreStart
|
2015-07-21 23:25:18 +02:00
|
|
|
$struct->remaining = 0;
|
|
|
|
$struct->promisor->fail($e);
|
2015-07-22 18:34:42 +02:00
|
|
|
// @codeCoverageIgnoreEnd
|
2015-05-20 21:18:30 +02:00
|
|
|
} catch (\Exception $e) {
|
2015-07-22 18:34:42 +02:00
|
|
|
// @TODO Remove this catch block once PHP5 support is no longer required
|
2015-05-20 21:18:30 +02:00
|
|
|
$struct->remaining = 0;
|
|
|
|
$struct->promisor->fail($e);
|
|
|
|
}
|
|
|
|
};
|
2014-11-25 05:14:07 +01:00
|
|
|
|
2015-05-20 21:18:30 +02:00
|
|
|
foreach ($promises as $key => $promise) {
|
|
|
|
if ($promise instanceof Promise) {
|
|
|
|
$promise->when($onResolve, [$struct, $key]);
|
|
|
|
} else {
|
|
|
|
$struct->remaining--;
|
2014-11-25 05:14:07 +01:00
|
|
|
try {
|
2015-07-21 18:25:34 +02:00
|
|
|
$struct->results[$key] = \call_user_func($struct->functor, $promise);
|
2015-07-21 23:25:18 +02:00
|
|
|
} catch (\Throwable $e) {
|
2015-07-22 18:34:42 +02:00
|
|
|
// @TODO Remove coverage ignore block once PHP5 support is no longer required
|
|
|
|
// @codeCoverageIgnoreStart
|
2015-07-21 23:25:18 +02:00
|
|
|
$struct->remaining = 0;
|
|
|
|
$struct->promisor->fail($e);
|
2015-07-22 18:34:42 +02:00
|
|
|
// @codeCoverageIgnoreEnd
|
2015-05-20 21:18:30 +02:00
|
|
|
} catch (\Exception $e) {
|
2015-07-22 18:34:42 +02:00
|
|
|
// @TODO Remove this catch block once PHP5 support is no longer required
|
2015-05-20 21:18:30 +02:00
|
|
|
$struct->remaining = 0;
|
|
|
|
$struct->promisor->fail($e);
|
2014-11-25 05:14:07 +01:00
|
|
|
}
|
2015-05-20 21:18:30 +02:00
|
|
|
if ($struct->remaining === 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-11-25 05:14:07 +01:00
|
|
|
}
|
|
|
|
|
2015-05-20 21:18:30 +02:00
|
|
|
return $struct->promisor->promise();
|
2014-09-22 22:47:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-05-19 06:21:33 +02:00
|
|
|
* Filter deferred values using the specified functor
|
2014-09-22 22:47:48 +02:00
|
|
|
*
|
|
|
|
* If the functor returns a truthy value the resolved promise result is retained, otherwise it is
|
|
|
|
* discarded. Array keys are retained for any results not filtered out by the functor.
|
2015-05-20 21:18:30 +02:00
|
|
|
*
|
|
|
|
* @param array An array of promises whose values -- once resoved -- will be filtered by the functor
|
|
|
|
* @param callable $functor The filtering function to apply to eventual promise results
|
|
|
|
* @return \Amp\Promise
|
2014-09-22 22:47:48 +02:00
|
|
|
*/
|
2015-08-01 19:28:41 +02:00
|
|
|
function filter(array $promises, callable $functor = null) {
|
2014-11-25 05:14:07 +01:00
|
|
|
if (empty($promises)) {
|
|
|
|
return new Success([]);
|
|
|
|
}
|
|
|
|
|
2015-08-01 19:28:41 +02:00
|
|
|
if (empty($functor)) {
|
|
|
|
$functor = function ($r) {
|
|
|
|
return (bool) $r;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2015-05-20 21:18:30 +02:00
|
|
|
$struct = new \StdClass;
|
|
|
|
$struct->remaining = count($promises);
|
|
|
|
$struct->results = [];
|
|
|
|
$struct->promisor = new Deferred;
|
|
|
|
$struct->functor = $functor;
|
2014-11-25 05:14:07 +01:00
|
|
|
|
2015-07-29 07:15:43 +02:00
|
|
|
$onResolve = function ($error, $result, $cbData) {
|
2015-05-20 21:18:30 +02:00
|
|
|
list($struct, $key) = $cbData;
|
|
|
|
if (empty($struct->remaining)) {
|
|
|
|
// If the promisor already resolved we don't need to bother
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ($error) {
|
|
|
|
$struct->results = null;
|
|
|
|
$struct->remaining = 0;
|
|
|
|
$struct->promisor->fail($error);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$struct->remaining--;
|
|
|
|
try {
|
2015-07-21 18:25:34 +02:00
|
|
|
if (\call_user_func($struct->functor, $result)) {
|
2015-05-20 21:18:30 +02:00
|
|
|
$struct->results[$key] = $result;
|
2014-11-25 05:14:07 +01:00
|
|
|
}
|
2015-07-22 02:09:27 +02:00
|
|
|
if ($struct->remaining === 0) {
|
|
|
|
$struct->promisor->succeed($struct->results);
|
|
|
|
}
|
2015-07-21 23:25:18 +02:00
|
|
|
} catch (\Throwable $e) {
|
2015-07-22 18:34:42 +02:00
|
|
|
// @TODO Remove coverage ignore block once PHP5 support is no longer required
|
|
|
|
// @codeCoverageIgnoreStart
|
2015-07-21 23:25:18 +02:00
|
|
|
$struct->remaining = 0;
|
|
|
|
$struct->promisor->fail($e);
|
2015-07-22 18:34:42 +02:00
|
|
|
// @codeCoverageIgnoreEnd
|
2015-05-20 21:18:30 +02:00
|
|
|
} catch (\Exception $e) {
|
2015-07-22 18:34:42 +02:00
|
|
|
// @TODO Remove this catch block once PHP5 support is no longer required
|
2015-05-20 21:18:30 +02:00
|
|
|
$struct->remaining = 0;
|
|
|
|
$struct->promisor->fail($e);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
foreach ($promises as $key => $promise) {
|
|
|
|
if ($promise instanceof Promise) {
|
|
|
|
$promise->when($onResolve, [$struct, $key]);
|
|
|
|
} else {
|
|
|
|
$struct->remaining--;
|
2014-11-25 05:14:07 +01:00
|
|
|
try {
|
2015-07-21 18:25:34 +02:00
|
|
|
if (\call_user_func($struct->functor, $promise)) {
|
2015-05-20 21:18:30 +02:00
|
|
|
$struct->results[$key] = $promise;
|
2014-11-25 05:14:07 +01:00
|
|
|
}
|
2015-07-22 02:09:27 +02:00
|
|
|
if ($struct->remaining === 0) {
|
|
|
|
$struct->promisor->succeed($struct->results);
|
|
|
|
break;
|
|
|
|
}
|
2015-07-21 23:25:18 +02:00
|
|
|
} catch (\Throwable $e) {
|
2015-07-22 18:34:42 +02:00
|
|
|
// @TODO Remove coverage ignore block once PHP5 support is no longer required
|
|
|
|
// @codeCoverageIgnoreStart
|
2015-07-21 23:25:18 +02:00
|
|
|
$struct->remaining = 0;
|
|
|
|
$struct->promisor->fail($e);
|
2015-07-22 02:09:27 +02:00
|
|
|
break;
|
2015-07-22 18:34:42 +02:00
|
|
|
// @codeCoverageIgnoreEnd
|
2015-05-20 21:18:30 +02:00
|
|
|
} catch (\Exception $e) {
|
2015-07-22 18:34:42 +02:00
|
|
|
// @TODO Remove this catch block once PHP5 support is no longer required
|
2015-05-20 21:18:30 +02:00
|
|
|
$struct->remaining = 0;
|
|
|
|
$struct->promisor->fail($e);
|
|
|
|
break;
|
2014-11-25 05:14:07 +01:00
|
|
|
}
|
2015-05-20 21:18:30 +02:00
|
|
|
}
|
2014-11-25 05:14:07 +01:00
|
|
|
}
|
|
|
|
|
2015-05-20 21:18:30 +02:00
|
|
|
return $struct->promisor->promise();
|
2014-09-22 22:47:48 +02:00
|
|
|
}
|
|
|
|
|
2015-05-19 17:29:36 +02:00
|
|
|
/**
|
|
|
|
* Pipe the promised value through the specified functor once it resolves
|
|
|
|
*
|
2015-05-20 05:12:32 +02:00
|
|
|
* @param mixed $promise Any value is acceptable -- non-promises are normalized to promise form
|
|
|
|
* @param callable $functor The functor through which to pipe the resolved promise value
|
2015-05-19 17:29:36 +02:00
|
|
|
* @return \Amp\Promise
|
|
|
|
*/
|
|
|
|
function pipe($promise, callable $functor) {
|
2015-07-21 23:25:18 +02:00
|
|
|
if (!$promise instanceof Promise) {
|
2015-05-20 05:12:32 +02:00
|
|
|
try {
|
2015-07-21 18:25:34 +02:00
|
|
|
return new Success(\call_user_func($functor, $promise));
|
2015-07-21 23:25:18 +02:00
|
|
|
} catch (\Throwable $e) {
|
2015-07-22 18:34:42 +02:00
|
|
|
// @TODO Remove coverage ignore block once PHP5 support is no longer required
|
|
|
|
// @codeCoverageIgnoreStart
|
2015-07-21 23:25:18 +02:00
|
|
|
return new Failure($e);
|
2015-07-22 18:34:42 +02:00
|
|
|
// @codeCoverageIgnoreEnd
|
2015-05-20 05:12:32 +02:00
|
|
|
} catch (\Exception $e) {
|
2015-07-22 18:34:42 +02:00
|
|
|
// @TODO Remove this catch block once PHP5 support is no longer required
|
2015-05-20 05:12:32 +02:00
|
|
|
return new Failure($e);
|
|
|
|
}
|
2015-05-19 17:29:36 +02:00
|
|
|
}
|
2015-05-20 05:12:32 +02:00
|
|
|
|
2015-05-19 17:29:36 +02:00
|
|
|
$promisor = new Deferred;
|
2015-07-29 07:15:43 +02:00
|
|
|
$promise->when(function ($error, $result) use ($promisor, $functor) {
|
2015-05-19 17:29:36 +02:00
|
|
|
if ($error) {
|
|
|
|
$promisor->fail($error);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
try {
|
2015-07-21 18:25:34 +02:00
|
|
|
$promisor->succeed(\call_user_func($functor, $result));
|
2015-07-21 23:25:18 +02:00
|
|
|
} catch (\Throwable $error) {
|
2015-07-22 18:34:42 +02:00
|
|
|
// @TODO Remove coverage ignore block once PHP5 support is no longer required
|
|
|
|
// @codeCoverageIgnoreStart
|
2015-07-21 23:25:18 +02:00
|
|
|
$promisor->fail($error);
|
2015-07-22 18:34:42 +02:00
|
|
|
// @codeCoverageIgnoreEnd
|
2015-05-19 17:29:36 +02:00
|
|
|
} catch (\Exception $error) {
|
2015-07-22 18:34:42 +02:00
|
|
|
// @TODO Remove this catch block once PHP5 support is no longer required
|
2015-05-19 17:29:36 +02:00
|
|
|
$promisor->fail($error);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return $promisor->promise();
|
|
|
|
}
|
|
|
|
|
2015-06-09 17:49:22 +02:00
|
|
|
/**
|
|
|
|
* Normalize an array of mixed values/Promises/Promisors to array<Promise>
|
|
|
|
*
|
|
|
|
* @param array $values
|
|
|
|
* @return array Returns an array of Promise instances
|
|
|
|
*/
|
|
|
|
function promises(array $values) {
|
|
|
|
foreach ($values as $key => $value) {
|
|
|
|
if ($value instanceof Promise) {
|
|
|
|
continue;
|
|
|
|
} elseif ($value instanceof Promisor) {
|
|
|
|
$values[$key] = $value->promise();
|
|
|
|
} else {
|
|
|
|
$values[$key] = new Success($value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $values;
|
|
|
|
}
|
|
|
|
|
2015-06-01 02:13:39 +02:00
|
|
|
/**
|
|
|
|
* Create an artificial timeout for any Promise instance
|
|
|
|
*
|
|
|
|
* If the timeout expires prior to promise resolution the returned
|
|
|
|
* promise is failed.
|
|
|
|
*
|
|
|
|
* @param \Amp\Promise $promise The promise to which the timeout applies
|
|
|
|
* @param int $msTimeout The timeout in milliseconds
|
|
|
|
* @return \Amp\Promise
|
|
|
|
*/
|
2015-07-29 07:15:43 +02:00
|
|
|
function timeout(Promise $promise, $msTimeout) {
|
2015-06-01 02:13:39 +02:00
|
|
|
$resolved = false;
|
|
|
|
$promisor = new Deferred;
|
2015-07-29 07:15:43 +02:00
|
|
|
$watcherId = once(function () use ($promisor, &$resolved) {
|
2015-06-01 02:13:39 +02:00
|
|
|
$resolved = true;
|
2015-07-31 21:49:05 +02:00
|
|
|
$promisor->fail(new TimeoutException(
|
2015-06-01 02:13:39 +02:00
|
|
|
"Promise resolution timed out"
|
|
|
|
));
|
|
|
|
}, $msTimeout);
|
2015-07-29 07:15:43 +02:00
|
|
|
$promise->when(function ($error = null, $result = null) use ($promisor, $watcherId, &$resolved) {
|
2015-06-01 02:13:39 +02:00
|
|
|
if ($resolved) {
|
|
|
|
return;
|
|
|
|
}
|
2015-07-21 23:25:18 +02:00
|
|
|
$resolved = true;
|
2015-07-29 07:15:43 +02:00
|
|
|
cancel($watcherId);
|
2015-06-01 02:13:39 +02:00
|
|
|
if ($error) {
|
|
|
|
$promisor->fail($error);
|
|
|
|
} else {
|
|
|
|
$promisor->succeed($result);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return $promisor->promise();
|
|
|
|
}
|
|
|
|
|
2014-09-22 22:47:48 +02:00
|
|
|
/**
|
2014-12-02 01:45:57 +01:00
|
|
|
* Block script execution indefinitely until the specified Promise resolves
|
2014-11-25 05:14:07 +01:00
|
|
|
*
|
2014-12-02 01:45:57 +01:00
|
|
|
* In the event of promise failure this method will throw the exception responsible for the failure.
|
|
|
|
* Otherwise the promise's resolved value is returned.
|
2014-11-25 05:14:07 +01:00
|
|
|
*
|
2015-05-20 21:18:30 +02:00
|
|
|
* @param \Amp\Promise $promise The promise on which to wait
|
2015-03-19 16:14:21 +01:00
|
|
|
* @throws \Exception if the promise fails
|
2015-05-20 21:18:30 +02:00
|
|
|
* @return mixed Returns the eventual resolution result for the specified promise
|
2014-09-22 22:47:48 +02:00
|
|
|
*/
|
2015-07-29 07:15:43 +02:00
|
|
|
function wait(Promise $promise) {
|
2014-12-02 01:45:57 +01:00
|
|
|
$isWaiting = true;
|
|
|
|
$resolvedError = null;
|
|
|
|
$resolvedResult = null;
|
|
|
|
|
2015-07-29 07:15:43 +02:00
|
|
|
$promise->when(function ($error, $result) use (&$isWaiting, &$resolvedError, &$resolvedResult) {
|
2014-12-02 01:45:57 +01:00
|
|
|
$isWaiting = false;
|
|
|
|
$resolvedError = $error;
|
|
|
|
$resolvedResult = $result;
|
|
|
|
});
|
|
|
|
|
|
|
|
while ($isWaiting) {
|
2015-07-29 07:15:43 +02:00
|
|
|
tick();
|
2014-11-25 05:14:07 +01:00
|
|
|
}
|
|
|
|
|
2014-12-02 01:45:57 +01:00
|
|
|
if ($resolvedError) {
|
|
|
|
throw $resolvedError;
|
2014-11-25 05:14:07 +01:00
|
|
|
}
|
|
|
|
|
2014-12-02 01:45:57 +01:00
|
|
|
return $resolvedResult;
|
|
|
|
}
|
2014-11-25 05:14:07 +01:00
|
|
|
|
2015-07-23 07:30:53 +02:00
|
|
|
/**
|
|
|
|
* Return a new function that will be resolved as a coroutine when invoked
|
|
|
|
*
|
|
|
|
* @param callable $func The callable to be wrapped for coroutine resolution
|
|
|
|
* @return callable Returns a wrapped callable
|
|
|
|
* @TODO Use variadic function instead of func_get_args() once PHP5.5 is no longer supported
|
|
|
|
*/
|
2015-07-29 07:15:43 +02:00
|
|
|
function coroutine(callable $func) {
|
|
|
|
return function () use ($func) {
|
2015-07-23 07:30:53 +02:00
|
|
|
$out = \call_user_func_array($func, \func_get_args());
|
2015-08-07 16:41:14 +02:00
|
|
|
if ($out instanceof \Generator) {
|
|
|
|
return resolve($out);
|
|
|
|
} elseif ($out instanceof Promise) {
|
|
|
|
return $out;
|
|
|
|
} else {
|
|
|
|
return new Success($out);
|
|
|
|
}
|
2015-07-23 07:30:53 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resolve a Generator coroutine function
|
|
|
|
*
|
|
|
|
* Upon resolution the Generator return value is used to succeed the promised result. If an
|
|
|
|
* error occurs during coroutine resolution the returned promise fails.
|
|
|
|
*
|
|
|
|
* @param \Generator $generator The generator to resolve as a coroutine
|
|
|
|
*/
|
2015-07-29 07:15:43 +02:00
|
|
|
function resolve(\Generator $generator) {
|
2015-07-23 07:30:53 +02:00
|
|
|
$cs = new CoroutineState;
|
|
|
|
$cs->promisor = new Deferred;
|
|
|
|
$cs->generator = $generator;
|
|
|
|
$cs->returnValue = null;
|
|
|
|
$cs->currentPromise = null;
|
|
|
|
$cs->nestingLevel = 0;
|
2015-07-29 07:15:43 +02:00
|
|
|
$cs->reactor = reactor();
|
2015-07-23 07:30:53 +02:00
|
|
|
|
|
|
|
__coroutineAdvance($cs);
|
|
|
|
|
|
|
|
return $cs->promisor->promise();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function is used internally when resolving coroutines.
|
|
|
|
* It is not considered part of the public API and library users
|
|
|
|
* should not rely upon it in applications.
|
|
|
|
*/
|
|
|
|
function __coroutineAdvance(CoroutineState $cs) {
|
|
|
|
try {
|
|
|
|
$yielded = $cs->generator->current();
|
|
|
|
if (!isset($yielded)) {
|
|
|
|
if ($cs->generator->valid()) {
|
|
|
|
$cs->reactor->immediately("Amp\__coroutineNextTick", ["cb_data" => $cs]);
|
|
|
|
} elseif (isset($cs->returnValue)) {
|
|
|
|
$cs->promisor->succeed($cs->returnValue);
|
|
|
|
} else {
|
|
|
|
$result = (PHP_MAJOR_VERSION >= 7) ? $cs->generator->getReturn() : null;
|
|
|
|
$cs->promisor->succeed($result);
|
|
|
|
}
|
|
|
|
} elseif ($yielded instanceof Promise) {
|
|
|
|
if ($cs->nestingLevel < 3) {
|
|
|
|
$cs->nestingLevel++;
|
|
|
|
$yielded->when("Amp\__coroutineSend", $cs);
|
|
|
|
$cs->nestingLevel--;
|
|
|
|
} else {
|
|
|
|
$cs->currentPromise = $yielded;
|
|
|
|
$cs->reactor->immediately("Amp\__coroutineNextTick", ["cb_data" => $cs]);
|
|
|
|
}
|
|
|
|
} elseif ($yielded instanceof CoroutineResult) {
|
|
|
|
/**
|
|
|
|
* @TODO This block is necessary for PHP5; remove once PHP7 is required and
|
|
|
|
* we have return expressions in generators
|
|
|
|
*/
|
|
|
|
$cs->returnValue = $yielded->getReturn();
|
|
|
|
__coroutineSend(null, null, $cs);
|
|
|
|
} else {
|
|
|
|
/**
|
|
|
|
* @TODO Remove CoroutineResult from error message once PHP7 is required
|
|
|
|
*/
|
|
|
|
$error = new \DomainException(makeGeneratorError($cs->generator, \sprintf(
|
|
|
|
"Unexpected yield (Promise|CoroutineResult|null expected); %s yielded at key %s",
|
|
|
|
\is_object($yielded) ? \get_class($yielded) : \gettype($yielded),
|
|
|
|
$cs->generator->key()
|
|
|
|
)));
|
2015-07-29 07:15:43 +02:00
|
|
|
$cs->reactor->immediately(function () use ($cs, $error) {
|
2015-07-23 07:30:53 +02:00
|
|
|
$cs->promisor->fail($error);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} catch (\Throwable $uncaught) {
|
|
|
|
/**
|
|
|
|
* @codeCoverageIgnoreStart
|
|
|
|
* @TODO Remove these coverage ignore lines once PHP7 is required
|
|
|
|
*/
|
2015-07-29 07:15:43 +02:00
|
|
|
$cs->reactor->immediately(function () use ($cs, $uncaught) {
|
2015-07-23 07:30:53 +02:00
|
|
|
$cs->promisor->fail($uncaught);
|
|
|
|
});
|
|
|
|
/**
|
|
|
|
* @codeCoverageIgnoreEnd
|
|
|
|
*/
|
|
|
|
} catch (\Exception $uncaught) {
|
|
|
|
/**
|
|
|
|
* @TODO This extra catch block is necessary for PHP5; remove once PHP7 is required
|
|
|
|
*/
|
2015-07-29 07:15:43 +02:00
|
|
|
$cs->reactor->immediately(function () use ($cs, $uncaught) {
|
2015-07-23 07:30:53 +02:00
|
|
|
$cs->promisor->fail($uncaught);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function is used internally when resolving coroutines.
|
|
|
|
* It is not considered part of the public API and library users
|
|
|
|
* should not rely upon it in applications.
|
|
|
|
*/
|
2015-07-29 07:15:43 +02:00
|
|
|
function __coroutineNextTick($watcherId, CoroutineState $cs) {
|
2015-07-23 07:30:53 +02:00
|
|
|
if ($cs->currentPromise) {
|
|
|
|
$promise = $cs->currentPromise;
|
|
|
|
$cs->currentPromise = null;
|
|
|
|
$promise->when("Amp\__coroutineSend", $cs);
|
|
|
|
} else {
|
|
|
|
__coroutineSend(null, null, $cs);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function is used internally when resolving coroutines.
|
|
|
|
* It is not considered part of the public API and library users
|
|
|
|
* should not rely upon it in applications.
|
|
|
|
*/
|
|
|
|
function __coroutineSend($error, $result, CoroutineState $cs) {
|
|
|
|
try {
|
|
|
|
if ($error) {
|
|
|
|
$cs->generator->throw($error);
|
|
|
|
} else {
|
|
|
|
$cs->generator->send($result);
|
|
|
|
}
|
|
|
|
__coroutineAdvance($cs);
|
2015-08-18 08:55:44 +02:00
|
|
|
} catch (\Throwable $uncaught) {
|
|
|
|
/**
|
|
|
|
* @codeCoverageIgnoreStart
|
|
|
|
* @TODO Remove these coverage ignore lines once PHP7 is required
|
|
|
|
*/
|
|
|
|
$cs->reactor->immediately(function () use ($cs, $uncaught) {
|
|
|
|
$cs->promisor->fail($uncaught);
|
|
|
|
});
|
|
|
|
/**
|
|
|
|
* @codeCoverageIgnoreEnd
|
|
|
|
*/
|
2015-07-23 07:30:53 +02:00
|
|
|
} catch (\Exception $uncaught) {
|
2015-08-18 08:55:44 +02:00
|
|
|
/**
|
|
|
|
* @TODO This extra catch block is necessary for PHP5; remove once PHP7 is required
|
|
|
|
*/
|
2015-07-29 07:15:43 +02:00
|
|
|
$cs->reactor->immediately(function () use ($cs, $uncaught) {
|
2015-07-23 07:30:53 +02:00
|
|
|
$cs->promisor->fail($uncaught);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-16 16:59:44 +02:00
|
|
|
/**
|
|
|
|
* A general purpose function for creating error messages from generator yields
|
|
|
|
*
|
|
|
|
* @param \Generator $generator
|
|
|
|
* @param string $prefix
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function makeGeneratorError(\Generator $generator, $prefix = "Generator error") {
|
|
|
|
if (PHP_MAJOR_VERSION < 7 || !$generator->valid()) {
|
|
|
|
return $prefix;
|
2015-06-15 17:56:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$reflGen = new \ReflectionGenerator($generator);
|
|
|
|
$exeGen = $reflGen->getExecutingGenerator();
|
2015-06-16 16:59:44 +02:00
|
|
|
if ($isSubgenerator = ($exeGen !== $generator)) {
|
2015-06-15 17:56:42 +02:00
|
|
|
$reflGen = new \ReflectionGenerator($exeGen);
|
|
|
|
}
|
|
|
|
|
2015-05-21 01:51:03 +02:00
|
|
|
return sprintf(
|
2015-06-16 16:59:44 +02:00
|
|
|
"{$prefix} on line %s in %s",
|
2015-06-15 17:56:42 +02:00
|
|
|
$reflGen->getExecutingLine(),
|
|
|
|
$reflGen->getExecutingFile()
|
2015-05-21 01:51:03 +02:00
|
|
|
);
|
|
|
|
}
|