2016-05-21 16:44:52 +02:00
|
|
|
<?php
|
|
|
|
|
2016-05-24 05:48:28 +02:00
|
|
|
namespace Amp;
|
2016-05-21 16:44:52 +02:00
|
|
|
|
|
|
|
use Interop\Async\Awaitable;
|
|
|
|
use Interop\Async\Loop;
|
|
|
|
|
2016-06-01 19:18:11 +02:00
|
|
|
/**
|
|
|
|
* Creates an awaitable from a generator function yielding awaitables. When an awaitable is yielded, execution of the
|
|
|
|
* generator is interrupted until the awaitable is resolved. The success value is sent to the generator, while the
|
|
|
|
* failure reason is thrown into the generator. Using a coroutine, asynchronous code can be written without callbacks
|
|
|
|
* and be structured like synchronous code.
|
|
|
|
*/
|
2016-05-21 16:44:52 +02:00
|
|
|
final class Coroutine implements Awaitable {
|
|
|
|
use Internal\Placeholder;
|
|
|
|
|
|
|
|
// Maximum number of immediate coroutine continuations before deferring next continuation to the loop.
|
2016-06-01 19:06:43 +02:00
|
|
|
const MAX_CONTINUATION_DEPTH = 3;
|
2016-05-21 16:44:52 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Generator
|
|
|
|
*/
|
|
|
|
private $generator;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var callable(\Throwable|\Exception|null $exception, mixed $value): void
|
|
|
|
*/
|
|
|
|
private $when;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
private $depth = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param \Generator $generator
|
|
|
|
*/
|
2016-05-21 19:07:08 +02:00
|
|
|
public function __construct(\Generator $generator) {
|
2016-05-21 16:44:52 +02:00
|
|
|
$this->generator = $generator;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param \Throwable|\Exception|null $exception Exception to be thrown into the generator.
|
|
|
|
* @param mixed $value The value to send to the generator.
|
|
|
|
*/
|
2016-05-24 17:39:19 +02:00
|
|
|
$this->when = function ($exception, $value) {
|
2016-06-01 19:06:43 +02:00
|
|
|
if (self::MAX_CONTINUATION_DEPTH < $this->depth) { // Defer continuation to avoid blowing up call stack.
|
2016-05-21 16:44:52 +02:00
|
|
|
Loop::defer(function () use ($exception, $value) {
|
|
|
|
$when = $this->when;
|
|
|
|
$when($exception, $value);
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
if ($exception) {
|
|
|
|
// Throw exception at current execution point.
|
2016-06-15 05:35:28 +02:00
|
|
|
$yielded = $this->generator->throw($exception);
|
|
|
|
} else {
|
|
|
|
// Send the new value and execute to next yield statement.
|
|
|
|
$yielded = $this->generator->send($value);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($yielded instanceof Awaitable) {
|
|
|
|
++$this->depth;
|
|
|
|
$yielded->when($this->when);
|
|
|
|
--$this->depth;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// @todo Necessary for returning values in PHP 5.x. Remove once PHP 7 is required.
|
|
|
|
if ($yielded instanceof Internal\CoroutineResult) {
|
|
|
|
$this->settle($yielded->getValue());
|
2016-05-21 16:44:52 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-06-15 05:35:28 +02:00
|
|
|
if ($this->generator->valid()) {
|
2016-06-17 05:44:32 +02:00
|
|
|
throw new InvalidYieldException($this->generator);
|
2016-06-15 05:35:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->resolve(PHP_MAJOR_VERSION >= 7 ? $this->generator->getReturn() : null);
|
2016-05-21 16:44:52 +02:00
|
|
|
} catch (\Throwable $exception) {
|
2016-06-17 05:44:32 +02:00
|
|
|
$this->dispose($exception);
|
2016-05-21 16:44:52 +02:00
|
|
|
} catch (\Exception $exception) {
|
2016-06-17 05:44:32 +02:00
|
|
|
$this->dispose($exception);
|
2016-05-21 16:44:52 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
try {
|
2016-06-15 05:35:28 +02:00
|
|
|
$yielded = $this->generator->current();
|
2016-05-21 16:44:52 +02:00
|
|
|
|
2016-06-15 05:35:28 +02:00
|
|
|
if ($yielded instanceof Awaitable) {
|
|
|
|
++$this->depth;
|
|
|
|
$yielded->when($this->when);
|
|
|
|
--$this->depth;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// @todo Necessary for returning values in PHP 5.x. Remove once PHP 7 is required.
|
|
|
|
if ($yielded instanceof Internal\CoroutineResult) {
|
|
|
|
$this->settle($yielded->getValue());
|
|
|
|
return;
|
|
|
|
}
|
2016-05-21 16:44:52 +02:00
|
|
|
|
2016-06-15 05:35:28 +02:00
|
|
|
if ($this->generator->valid()) {
|
2016-06-17 05:44:32 +02:00
|
|
|
throw new InvalidYieldException($this->generator);
|
2016-06-02 17:46:22 +02:00
|
|
|
}
|
2016-05-21 16:44:52 +02:00
|
|
|
|
2016-06-15 05:35:28 +02:00
|
|
|
$this->resolve(PHP_MAJOR_VERSION >= 7 ? $this->generator->getReturn() : null);
|
|
|
|
} catch (\Throwable $exception) {
|
2016-06-17 05:44:32 +02:00
|
|
|
$this->dispose($exception);
|
2016-06-15 05:35:28 +02:00
|
|
|
} catch (\Exception $exception) {
|
2016-06-17 05:44:32 +02:00
|
|
|
$this->dispose($exception);
|
2016-06-15 05:35:28 +02:00
|
|
|
}
|
|
|
|
}
|
2016-06-02 17:46:22 +02:00
|
|
|
|
2016-06-17 05:44:32 +02:00
|
|
|
/**
|
|
|
|
* Runs the generator to completion then fails the coroutine with the given exception.
|
|
|
|
*
|
|
|
|
* @param \Throwable|\Exception $exception
|
|
|
|
*
|
|
|
|
* @throws \Throwable|\Exception
|
|
|
|
*/
|
|
|
|
private function dispose($exception) {
|
|
|
|
if ($this->generator->valid()) {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
// Ensure generator has run to completion to avoid throws from finally blocks on destruction.
|
|
|
|
do {
|
|
|
|
$this->generator->throw($exception);
|
|
|
|
} while ($this->generator->valid());
|
|
|
|
} finally {
|
|
|
|
// Throw from finally to attach any exception thrown from generator as previous exception.
|
|
|
|
throw $exception;
|
|
|
|
}
|
|
|
|
} catch (\Throwable $exception) {
|
|
|
|
// $exception will be used to fail the coroutine.
|
|
|
|
} catch (\Exception $exception) {
|
|
|
|
// $exception will be used to fail the coroutine.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->fail($exception);
|
|
|
|
}
|
|
|
|
|
2016-06-15 05:35:28 +02:00
|
|
|
/**
|
|
|
|
* Attempts to resolves the coroutine with the given value, failing if the generator is still valid after sending
|
|
|
|
* the null back to the generator.
|
2016-06-17 05:44:32 +02:00
|
|
|
* @todo Remove once PHP 7 is required.
|
2016-06-15 05:35:28 +02:00
|
|
|
*
|
|
|
|
* @param mixed $result Coroutine return value.
|
|
|
|
*/
|
|
|
|
private function settle($result) {
|
2016-06-17 05:44:32 +02:00
|
|
|
$this->generator->send(null);
|
2016-06-02 17:46:22 +02:00
|
|
|
|
2016-06-15 05:35:28 +02:00
|
|
|
if ($this->generator->valid()) {
|
2016-06-17 05:44:32 +02:00
|
|
|
throw new InvalidYieldException(
|
2016-05-22 16:25:40 +02:00
|
|
|
$this->generator,
|
2016-06-17 05:44:32 +02:00
|
|
|
\sprintf("Unexpected yield after %s::result() (use 'return;' to halt generator)", self::class)
|
2016-05-22 16:25:40 +02:00
|
|
|
);
|
2016-05-21 16:44:52 +02:00
|
|
|
}
|
|
|
|
|
2016-06-15 05:35:28 +02:00
|
|
|
$this->resolve($result);
|
2016-05-21 16:44:52 +02:00
|
|
|
}
|
2016-06-17 05:44:32 +02:00
|
|
|
|
2016-05-22 07:11:03 +02:00
|
|
|
/**
|
|
|
|
* Return a value from a coroutine. Required for PHP 5.x only. Use the return keyword in PHP 7.
|
2016-06-17 05:44:32 +02:00
|
|
|
* @todo Remove once PHP 7 is required.
|
2016-05-22 07:11:03 +02:00
|
|
|
*
|
|
|
|
* @param mixed $value
|
|
|
|
*
|
2016-05-24 05:48:28 +02:00
|
|
|
* @return \Amp\Internal\CoroutineResult
|
2016-05-22 07:11:03 +02:00
|
|
|
*/
|
|
|
|
public static function result($value) {
|
|
|
|
return new Internal\CoroutineResult($value);
|
|
|
|
}
|
2016-06-01 19:06:43 +02:00
|
|
|
}
|