2016-12-29 21:09:49 +01:00
|
|
|
<?php
|
2016-08-16 06:46:26 +02:00
|
|
|
|
2016-05-24 18:47:14 +02:00
|
|
|
namespace Amp;
|
|
|
|
|
2017-01-04 02:10:27 +01:00
|
|
|
// @codeCoverageIgnoreStart
|
|
|
|
try {
|
2017-01-04 17:24:51 +01:00
|
|
|
if (!@\assert(false)) {
|
|
|
|
development: // PHP 7 development (zend.assertions=1)
|
|
|
|
/**
|
2017-04-27 17:51:06 +02:00
|
|
|
* Deferred is a container for an iterator that can emit values using the emit() method and completed using the
|
2017-05-01 07:29:23 +02:00
|
|
|
* complete() and fail() methods of this object. The contained iterator may be accessed using the iterate()
|
2017-04-27 17:51:06 +02:00
|
|
|
* method. This object should not be part of a public API, but used internally to create and emit values to an
|
|
|
|
* iterator.
|
2017-01-04 17:24:51 +01:00
|
|
|
*/
|
2017-01-04 02:10:27 +01:00
|
|
|
final class Emitter {
|
2017-06-05 06:59:23 +02:00
|
|
|
/** @var \Amp\Iterator */
|
2017-04-27 17:51:06 +02:00
|
|
|
private $iterator;
|
2017-01-04 02:10:27 +01:00
|
|
|
|
2017-06-05 06:59:23 +02:00
|
|
|
/** @var callable */
|
2017-01-04 02:10:27 +01:00
|
|
|
private $emit;
|
2017-01-04 17:24:51 +01:00
|
|
|
|
2017-06-05 06:59:23 +02:00
|
|
|
/** @var callable */
|
2017-04-26 20:14:10 +02:00
|
|
|
private $complete;
|
2017-01-04 17:24:51 +01:00
|
|
|
|
2017-06-05 06:59:23 +02:00
|
|
|
/** @var callable */
|
2017-01-04 02:10:27 +01:00
|
|
|
private $fail;
|
|
|
|
|
|
|
|
public function __construct() {
|
2017-06-05 07:19:34 +02:00
|
|
|
$this->iterator = new class($this->emit, $this->complete, $this->fail) implements Iterator {
|
2017-06-05 06:59:23 +02:00
|
|
|
use CallableMaker, Internal\Producer;
|
|
|
|
|
2017-06-05 07:19:34 +02:00
|
|
|
public function __construct(&$emit, &$complete, &$fail) {
|
|
|
|
$emit = $this->callableFromInstanceMethod("emit");
|
|
|
|
$complete = $this->callableFromInstanceMethod("complete");
|
|
|
|
$fail = $this->callableFromInstanceMethod("fail");
|
2017-01-04 02:10:27 +01:00
|
|
|
}
|
2017-06-05 06:59:23 +02:00
|
|
|
};
|
2017-01-04 02:10:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-04-27 17:51:06 +02:00
|
|
|
* @return \Amp\Iterator
|
2017-01-04 02:10:27 +01:00
|
|
|
*/
|
2017-05-01 07:29:23 +02:00
|
|
|
public function iterate(): Iterator {
|
2017-04-27 17:51:06 +02:00
|
|
|
return $this->iterator;
|
2017-01-04 02:10:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-04-27 17:51:06 +02:00
|
|
|
* Emits a value to the iterator.
|
2017-01-04 02:10:27 +01:00
|
|
|
*
|
|
|
|
* @param mixed $value
|
|
|
|
*
|
2017-03-10 21:58:46 +01:00
|
|
|
* @return \Amp\Promise
|
2017-01-04 02:10:27 +01:00
|
|
|
*/
|
|
|
|
public function emit($value): Promise {
|
|
|
|
return ($this->emit)($value);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-04-27 17:51:06 +02:00
|
|
|
* Completes the iterator.
|
2017-01-04 02:10:27 +01:00
|
|
|
*/
|
2017-04-26 20:14:10 +02:00
|
|
|
public function complete() {
|
|
|
|
($this->complete)();
|
2017-01-04 02:10:27 +01:00
|
|
|
}
|
2016-06-01 18:19:19 +02:00
|
|
|
|
2017-01-04 02:10:27 +01:00
|
|
|
/**
|
2017-04-27 17:51:06 +02:00
|
|
|
* Fails the iterator with the given reason.
|
2017-01-04 02:10:27 +01:00
|
|
|
*
|
|
|
|
* @param \Throwable $reason
|
|
|
|
*/
|
|
|
|
public function fail(\Throwable $reason) {
|
|
|
|
($this->fail)($reason);
|
|
|
|
}
|
2016-06-01 18:19:19 +02:00
|
|
|
}
|
2017-01-04 17:24:51 +01:00
|
|
|
} else {
|
|
|
|
production: // PHP 7 production environment (zend.assertions=0)
|
|
|
|
/**
|
2017-04-27 17:51:06 +02:00
|
|
|
* An optimized version of Emitter for production environments that is itself the iterator. Eval is used to
|
2017-04-13 18:04:51 +02:00
|
|
|
* prevent IDEs and other tools from reporting multiple definitions.
|
2017-01-04 17:24:51 +01:00
|
|
|
*/
|
2017-04-16 00:16:50 +02:00
|
|
|
eval('namespace Amp;
|
2017-04-27 18:18:25 +02:00
|
|
|
final class Emitter implements Iterator {
|
2017-04-26 20:14:10 +02:00
|
|
|
use Internal\Producer { emit as public; complete as public; fail as public; }
|
2017-05-01 07:29:23 +02:00
|
|
|
public function iterate(): Iterator { return $this; }
|
2017-04-13 18:04:51 +02:00
|
|
|
}');
|
2016-05-24 18:47:14 +02:00
|
|
|
}
|
2017-01-04 02:10:27 +01:00
|
|
|
} catch (\AssertionError $exception) {
|
|
|
|
goto development; // zend.assertions=1 and assert.exception=1, use development definition.
|
|
|
|
} // @codeCoverageIgnoreEnd
|