本文整理汇总了PHP中Generator::valid方法的典型用法代码示例。如果您正苦于以下问题:PHP Generator::valid方法的具体用法?PHP Generator::valid怎么用?PHP Generator::valid使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Generator
的用法示例。
在下文中一共展示了Generator::valid方法的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: 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));
}
}
示例3: 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;
}
示例4: valid
public function valid()
{
if ($this->_currentGen) {
return $this->_currentGen->valid();
}
return !!$this->current();
}
示例5: next
/**
* Examines the value yielded from the generator and prepares the next step in interation.
*
* @param mixed $yielded
*/
private function next($yielded)
{
if (!$this->generator->valid()) {
$result = $this->generator->getReturn();
if ($result instanceof Awaitable) {
$this->reject(new AwaitableReturnedError($result));
return;
}
if ($result instanceof Generator) {
$this->reject(new GeneratorReturnedError($result));
return;
}
$this->resolve($result);
return;
}
$this->busy = true;
if ($yielded instanceof Generator) {
$yielded = new self($yielded);
}
$this->current = $yielded;
if ($yielded instanceof Awaitable) {
$yielded->done($this->send, $this->capture);
} else {
Loop\queue($this->send, $yielded);
}
$this->busy = false;
}
示例6: getReturnOrThrown
/**
* Return value that generator has returned or thrown.
* @return mixed
*/
public function getReturnOrThrown()
{
$this->validateInvalidity();
if ($this->e === null && $this->g->valid() && !$this->valid()) {
return $this->g->current();
}
if ($this->e) {
return $this->e;
}
return method_exists($this->g, 'getReturn') ? $this->g->getReturn() : null;
}
示例7: checkResult
private function checkResult(\Generator $generator, Channel $channel)
{
if (!$generator->valid()) {
foreach ($this->actions as list(, $gen, , $undo)) {
if ($gen && $gen !== $generator) {
$undo($gen->current());
}
}
return [$generator->getReturn(), $channel];
}
return false;
}
示例8: __construct
/**
* @param \Generator $generator
* @param string $prefix
*/
public function __construct(\Generator $generator, string $prefix)
{
$yielded = $generator->current();
$prefix .= \sprintf("; %s yielded at key %s", \is_object($yielded) ? \get_class($yielded) : \gettype($yielded), $generator->key());
if (!$generator->valid()) {
parent::__construct($prefix);
return;
}
$reflGen = new \ReflectionGenerator($generator);
$exeGen = $reflGen->getExecutingGenerator();
if ($isSubgenerator = $exeGen !== $generator) {
$reflGen = new \ReflectionGenerator($exeGen);
}
parent::__construct(\sprintf("%s on line %s in %s", $prefix, $reflGen->getExecutingLine(), $reflGen->getExecutingFile()));
}
示例9: next
/**
* Examines the value yielded from the generator and prepares the next step in interation.
*
* @param mixed $yielded
*/
private function next($yielded)
{
if (!$this->generator->valid()) {
$this->resolve($this->generator->getReturn());
$this->close();
return;
}
if ($yielded instanceof Generator) {
$yielded = new self($yielded);
}
if ($yielded instanceof Awaitable) {
$yielded->done($this->send, $this->capture);
} else {
Loop\queue($this->send, $yielded);
}
}
示例10: read
/**
* {@inheritdoc}
*/
public function read($length)
{
$this->initializeBeforeRead();
if (!$this->generator->valid()) {
return '';
}
if ($this->resource->eof()) {
$this->generator->next();
if (!$this->generator->valid()) {
return '';
}
$this->resource = $this->generator->current();
$this->resource->rewind();
}
return $this->resource->fread($length);
}
示例11: dispose
/**
* Runs the generator to completion then fails the coroutine with the given exception.
*
* @param \Throwable $exception
*/
private function dispose(\Throwable $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.
}
}
$this->fail($exception);
}
示例12: cancel
/**
* {@inheritdoc}
*/
public function cancel(Throwable $reason = null)
{
if (null === $reason) {
$reason = new TerminatedException();
}
if (null !== $this->generator) {
try {
do {
if ($this->current instanceof Awaitable) {
$this->current->cancel($reason);
}
$this->current = $this->generator->throw($reason);
} while ($this->generator->valid());
} catch (Throwable $exception) {
$reason = $exception;
}
}
parent::cancel($reason);
}
示例13: take
/**
*
* @param int $n
* @param \Generator $gen
*
* @return \Generator
*/
function take($n, Generator $gen)
{
while ($n-- > 0 && $gen->valid()) {
(yield $gen->current());
$gen->next();
}
}
示例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: hasTokens
/**
* Returns true, if there are still tokens left to be generated.
*
* If the lexer-generator still has tokens to generate,
* this returns true and false, if it doesn't
*
* @see \Generator->valid
*
* @return bool
*/
protected function hasTokens()
{
return $this->tokens->valid();
}