本文整理汇总了PHP中Generator::current方法的典型用法代码示例。如果您正苦于以下问题:PHP Generator::current方法的具体用法?PHP Generator::current怎么用?PHP Generator::current使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Generator
的用法示例。
在下文中一共展示了Generator::current方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: __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);
}
}
示例2: run
public function run()
{
if (!$this->generator) {
throw new Exception("");
} elseif (!$this->closed) {
throw new Exception("");
}
$this->closed = false;
$this->generator->rewind();
return $this->generator->current();
}
示例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: 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;
}
示例6: current
/**
* Get current element
*
* Takes into account if the callback returns a Generator
*
* @return mixed
*/
public function current()
{
if ($this->generator) {
return $this->generator->current();
}
$callable = $this->callable;
$result = $callable();
if (!$this->generator && $this->generator !== false) {
$this->generator = $result instanceof \Generator ? $result : false;
if ($this->generator) {
return $this->generator->current();
}
}
return $result;
}
示例7: current
public function current()
{
if ($this->_currentGen) {
return $this->_currentGen->current();
}
return current($this->_records);
}
示例8: execute
private function execute(\Generator $generator)
{
$stack = [];
//This is basically a simple iterative in-order tree traversal algorithm
$yielded = $generator->current();
//This is a depth-first traversal
while ($yielded instanceof \Generator) {
//... push it to the stack
$stack[] = $generator;
$generator = $yielded;
$yielded = $generator->current();
}
while (!empty($stack)) {
//We've reached the end of the branch, let's step back on the stack
$generator = array_pop($stack);
//step the generator
$yielded = $generator->send($yielded);
//Depth-first traversal
while ($yielded instanceof \Generator) {
//... push it to the stack
$stack[] = $generator;
$generator = $yielded;
$yielded = $generator->current();
}
}
return $yielded;
}
示例9: stackedCoroutine
/**
* Resolves yield calls tree
* and gives a return value if outcome of yield is CoroutineReturnValue instance
*
* @param \Generator $coroutine nested coroutine tree
* @return \Generator
*/
private static function stackedCoroutine(\Generator $coroutine)
{
$stack = new \SplStack();
while (true) {
$value = $coroutine->current();
// nested generator/coroutine
if ($value instanceof \Generator) {
$stack->push($coroutine);
$coroutine = $value;
continue;
}
// coroutine end or value is a value object instance
if (!$coroutine->valid() || $value instanceof CoroutineReturnValue) {
// if till this point, there are no coroutines in a stack thatn stop here
if ($stack->isEmpty()) {
return;
}
$coroutine = $stack->pop();
$value = $value instanceof CoroutineReturnValue ? $value->getValue() : null;
$coroutine->send($value);
continue;
}
$coroutine->send((yield $coroutine->key() => $value));
}
}
示例10: initializeBeforeRead
/**
*
*/
private function initializeBeforeRead()
{
if ($this->generator === null) {
$this->generator = $this->delegatedStream->read($this->blockSize);
$this->resource = $this->generator->current();
$this->resource->rewind();
}
}
示例11: run
/**
* [run 协程调度]
* @param \Generator $gen
* @throws \Exception
*/
public function run(\Generator $gen)
{
while (true) {
try {
/* 异常处理 */
if ($this->exception) {
$gen->throw($this->exception);
$this->exception = null;
continue;
}
$value = $gen->current();
// Logger::write(__METHOD__ . " value === " . var_export($value, true), Logger::INFO);
/* 中断内嵌 继续入栈 */
if ($value instanceof \Generator) {
$this->corStack->push($gen);
// Logger::write(__METHOD__ . " corStack push ", Logger::INFO);
$gen = $value;
continue;
}
/* if value is null and stack is not empty pop and send continue */
if (is_null($value) && !$this->corStack->isEmpty()) {
// Logger::write(__METHOD__ . " values is null stack pop and send ", Logger::INFO);
$gen = $this->corStack->pop();
$gen->send($this->callbackData);
continue;
}
if ($value instanceof ReturnValue) {
$gen = $this->corStack->pop();
$gen->send($value->getValue());
// end yeild
// Logger::write(__METHOD__ . " yield end words == " . var_export($value, true), Logger::INFO);
continue;
}
/* 中断内容为异步IO 发包 返回 */
if ($value instanceof \Swoole\Client\IBase) {
//async send push gen to stack
$this->corStack->push($gen);
$value->send(array($this, 'callback'));
return;
}
/* 出栈,回射数据 */
if ($this->corStack->isEmpty()) {
return;
}
// Logger::write(__METHOD__ . " corStack pop ", Logger::INFO);
$gen = $this->corStack->pop();
$gen->send($value);
} catch (EasyWorkException $e) {
if ($this->corStack->isEmpty()) {
throw $e;
}
$gen = $this->corStack->pop();
$this->exception = $e;
}
}
}
示例12: __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);
}
}
示例13: 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;
}
示例14: __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);
}
}
示例15: 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;
}