本文整理汇总了PHP中Generator::send方法的典型用法代码示例。如果您正苦于以下问题:PHP Generator::send方法的具体用法?PHP Generator::send怎么用?PHP Generator::send使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Generator
的用法示例。
在下文中一共展示了Generator::send方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: resume
public function resume($params = null)
{
$this->generator->send($params);
if (!$this->generator->valid()) {
$this->closed = true;
}
return $this->generator->current();
}
示例2: __construct
/**
* @param \Generator $generator
*/
public function __construct(\Generator $generator)
{
$this->generator = $generator;
/**
* @param \Throwable|null $exception Exception to be thrown into the generator.
* @param mixed $value Value to be sent into the generator.
*/
$this->when = function ($exception, $value) {
if ($this->depth > self::MAX_CONTINUATION_DEPTH) {
// Defer continuation to avoid blowing up call stack.
Loop::defer(function () use($exception, $value) {
($this->when)($exception, $value);
});
return;
}
try {
if ($exception) {
// Throw exception at current execution point.
$yielded = $this->generator->throw($exception);
} else {
// Send the new value and execute to next yield statement.
$yielded = $this->generator->send($value);
}
if ($yielded instanceof Promise) {
++$this->depth;
$yielded->when($this->when);
--$this->depth;
return;
}
if ($this->generator->valid()) {
$got = \is_object($yielded) ? \get_class($yielded) : \gettype($yielded);
throw new InvalidYieldError($this->generator, \sprintf("Unexpected yield (%s expected, got %s)", Promise::class, $got));
}
$this->resolve($this->generator->getReturn());
} catch (\Throwable $exception) {
$this->dispose($exception);
}
};
try {
$yielded = $this->generator->current();
if ($yielded instanceof Promise) {
++$this->depth;
$yielded->when($this->when);
--$this->depth;
return;
}
if ($this->generator->valid()) {
$got = \is_object($yielded) ? \get_class($yielded) : \gettype($yielded);
throw new InvalidYieldError($this->generator, \sprintf("Unexpected yield (%s expected, got %s)", Promise::class, $got));
}
$this->resolve($this->generator->getReturn());
} catch (\Throwable $exception) {
$this->dispose($exception);
}
}
示例3: tick
/**
* {@inheritdoc}
*/
public function tick()
{
if ($this->firstTick) {
$this->firstTick = false;
$this->deferred->notify(new MessageEvent($this, $this->generator->current()));
} else {
$this->deferred->notify(new MessageEvent($this, $this->generator->send(null)));
}
if (!$this->generator->valid()) {
$this->deferred->resolve(new Event($this));
}
}
示例4: call
/**
* executes the current middleware
*
* @param ServerRequestInterface $request
* @param ResponseInterface $response
* @return ResponseInterface
*/
private function call(ServerRequestInterface $request, ResponseInterface $response)
{
if ($this->atStart) {
$middleware = $this->generator->current();
$this->atStart = false;
} else {
$middleware = $this->generator->send(null);
}
if (!$middleware) {
return $response;
}
$callable = $this->app->getMiddlewareCallable($middleware);
return call_user_func($callable, $request, $response, $this->next);
}
示例5: __construct
/**
* @param \Generator $generator
*/
public function __construct(Generator $generator)
{
parent::__construct();
$this->generator = $generator;
/**
* @param mixed $value The value to send to the generator.
*/
$this->send = function ($value = null) {
if ($this->paused) {
// If paused, save callable and value for resuming.
$this->next = [$this->send, $value];
return;
}
try {
// Send the new value and execute to next yield statement.
$this->next($this->generator->send($value));
} catch (Throwable $exception) {
$this->reject($exception);
$this->close();
}
};
/**
* @param \Exception $exception Exception to be thrown into the generator.
*/
$this->capture = function (Throwable $exception) {
if ($this->paused) {
// If paused, save callable and exception for resuming.
$this->next = [$this->capture, $exception];
return;
}
try {
// Throw exception at current execution point.
$this->next($this->generator->throw($exception));
} catch (Throwable $exception) {
$this->reject($exception);
$this->close();
}
};
try {
$this->next($this->generator->current());
} catch (Throwable $exception) {
$this->reject($exception);
$this->close();
}
}
示例6: __construct
/**
* @param \Generator $generator
*/
public function __construct(Generator $generator)
{
parent::__construct();
$this->generator = $generator;
/**
* @param mixed $value The value to send to the generator.
*/
$this->send = function ($value = null) {
if ($this->busy) {
Loop\queue($this->send, $value);
// Queue continuation to avoid blowing up call stack.
return;
}
try {
// Send the new value and execute to next yield statement.
$this->next($this->generator->send($value));
} catch (Throwable $exception) {
$this->reject($exception);
}
};
/**
* @param \Throwable $exception Exception to be thrown into the generator.
*/
$this->capture = function (Throwable $exception) {
if ($this->busy) {
Loop\queue($this->capture, $exception);
// Queue continuation to avoid blowing up call stack.
return;
}
try {
// Throw exception at current execution point.
$this->next($this->generator->throw($exception));
} catch (Throwable $exception) {
$this->reject($exception);
}
};
try {
$this->next($this->generator->current());
} catch (Throwable $exception) {
$this->reject($exception);
}
}
示例7: send
/**
* Send value into generator.
* @param mixed $value
* @NOTE: This method returns nothing,
* while original generator returns something.
*/
public function send($value)
{
$this->validateValidity();
try {
$this->g->send($value);
return;
} catch (\Throwable $e) {
} catch (\Exception $e) {
}
$this->e = $e;
}
示例8: __construct
/**
* @param \Generator $generator
*/
public function __construct(Generator $generator)
{
parent::__construct();
$this->generator = $generator;
/**
* @param mixed $value The value to send to the generator.
*/
$this->send = function ($value = null) {
if (null === $this->generator) {
// Coroutine may have been cancelled.
return;
}
try {
// Send the new value and execute to next yield statement.
$this->next($this->generator->send($value));
} catch (Throwable $exception) {
$this->reject($exception);
}
};
/**
* @param \Throwable $exception Exception to be thrown into the generator.
*/
$this->capture = function (Throwable $exception) {
if (null === $this->generator) {
// Coroutine may have been cancelled.
return;
}
try {
// Throw exception at current execution point.
$this->next($this->generator->throw($exception));
} catch (Throwable $exception) {
$this->reject($exception);
}
};
try {
$this->next($this->generator->current());
} catch (Throwable $exception) {
$this->reject($exception);
}
}
示例9: reduce
/**
* When you need to return Result from your function, and it also depends on another
* functions returning Results, you can make it a generator function and yield
* values from dependant functions, this pattern makes code less bloated with
* statements like this:
* $res = something();
* if ($res instanceof Ok) {
* $something = $res->unwrap();
* } else {
* return $res;
* }
*
* Instead you can write:
* $something = (yield something());
*
* @see /example.php
*
* @param \Generator $resultsGenerator Generator that produces Result instances
* @return Result
*/
public static function reduce(\Generator $resultsGenerator)
{
/** @var Result $result */
$result = $resultsGenerator->current();
while ($resultsGenerator->valid()) {
if ($result instanceof Err) {
return $result;
}
$tmpResult = $resultsGenerator->send($result->unwrap());
if ($resultsGenerator->valid()) {
$result = $tmpResult;
}
}
return $result;
}
示例10: run
/**
* run coroutine
* @return mixed
*/
public function run()
{
if ($this->first_run) {
$this->first_run = false;
return $this->coroutine->current();
}
if ($this->exception instanceof \Exception) {
$retval = $this->coroutine->throw($this->exception);
$this->exception = null;
return $retval;
}
$retval = $this->coroutine->send($this->send_value);
$this->send_value = null;
return $retval;
}
示例11: resume
/**
* Resumes the execution of the generator.
*
* @param \Generator $generator
*/
private function resume(\Generator $generator)
{
if (!$generator->current() instanceof \Generator) {
return $generator->send($generator->current());
}
if ($generator->current()->valid()) {
$this->resume($generator->current());
return;
}
$generator->send($generator->current()->getReturn());
}
示例12: __next_coroutine
/** @internal */
function __next_coroutine($yielded, \Generator $generator)
{
return promise_for($yielded)->then(function ($value) use($generator) {
$nextYield = $generator->send($value);
return $generator->valid() ? __next_coroutine($nextYield, $generator) : $value;
}, function ($reason) use($generator) {
$nextYield = $generator->throw(exception_for($reason));
// The throw was caught, so keep iterating on the coroutine
return __next_coroutine($nextYield, $generator);
});
}
示例13: __construct
/**
* @param \Generator $generator
*/
public function __construct(Generator $generator)
{
$this->generator = $generator;
parent::__construct(function (callable $resolve, callable $reject) {
/**
* @param mixed $value The value to send to the generator.
* @param \Throwable|null $exception Exception object to be thrown into the generator if not null.
*/
$this->worker = function ($value = null, Throwable $exception = null) use($resolve, $reject) {
if ($this->paused) {
// If paused, mark coroutine as ready to resume.
$this->ready = true;
return;
}
try {
if ($this->initial) {
// Get result of first yield statement.
$this->initial = false;
$this->current = $this->generator->current();
} elseif (null !== $exception) {
// Throw exception at current execution point.
$this->current = $this->generator->throw($exception);
} else {
// Send the new value and execute to next yield statement.
$this->current = $this->generator->send($value);
}
if (!$this->generator->valid()) {
$resolve($this->generator->getReturn());
$this->close();
return;
}
if ($this->current instanceof Generator) {
$this->current = new self($this->current);
}
if ($this->current instanceof PromiseInterface) {
$this->current->done($this->worker, $this->capture);
} else {
Loop\queue($this->worker, $this->current);
}
} catch (Throwable $exception) {
$reject($exception);
$this->close();
}
};
/**
* @param \Throwable $exception Exception to be thrown into the generator.
*/
$this->capture = function (Throwable $exception) {
if (null !== $this->worker) {
// Coroutine may have been closed.
($this->worker)(null, $exception);
}
};
Loop\queue($this->worker);
return function (Throwable $exception) {
try {
$current = $this->generator->current();
// Get last yielded value.
while ($this->generator->valid()) {
if ($current instanceof PromiseInterface) {
$current->cancel($exception);
}
$current = $this->generator->throw($exception);
}
} finally {
$this->close();
}
};
});
}
示例14: responseCodec
private function responseCodec(\Generator $filter, InternalRequest $ireq) : \Generator
{
while ($filter->valid()) {
$cur = $filter->send(yield);
if ($cur !== null) {
$ireq->responseWriter->send($cur);
}
}
$cur = $filter->getReturn();
if ($cur !== null) {
$ireq->responseWriter->send($cur);
}
$ireq->responseWriter->send(null);
}
示例15: send
public function send($value = null)
{
if (!$this->generator) {
$this->rewind();
}
return $this->generator->send($value);
}