当前位置: 首页>>代码示例>>PHP>>正文


PHP Generator::send方法代码示例

本文整理汇总了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();
 }
开发者ID:panlatent,项目名称:coroutine,代码行数:8,代码来源:Coroutine.php

示例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);
     }
 }
开发者ID:amphp,项目名称:amp,代码行数:58,代码来源:Coroutine.php

示例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));
     }
 }
开发者ID:jderusse,项目名称:async,代码行数:15,代码来源:Generator.php

示例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);
 }
开发者ID:fabyscore,项目名称:fabyscore-lib,代码行数:21,代码来源:MiddlewareRunner.php

示例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();
     }
 }
开发者ID:valentinkh1,项目名称:icicle,代码行数:48,代码来源:Coroutine.php

示例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);
     }
 }
开发者ID:icicleio,项目名称:icicle,代码行数:45,代码来源:Coroutine.php

示例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;
 }
开发者ID:mpyw,项目名称:co,代码行数:17,代码来源:GeneratorContainer.php

示例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);
     }
 }
开发者ID:mrxotey,项目名称:icicle,代码行数:43,代码来源:Coroutine.php

示例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;
 }
开发者ID:nikita2206,项目名称:result,代码行数:35,代码来源:Result.php

示例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;
 }
开发者ID:zavalit,项目名称:corouser,代码行数:19,代码来源:Task.php

示例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());
 }
开发者ID:crystalplanet,项目名称:redshift,代码行数:16,代码来源:Task.php

示例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);
    });
}
开发者ID:Rayac,项目名称:search,代码行数:12,代码来源:functions.php

示例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();
             }
         };
     });
 }
开发者ID:kayandrae,项目名称:icicle,代码行数:73,代码来源:Coroutine.php

示例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);
 }
开发者ID:efueger,项目名称:aerys,代码行数:14,代码来源:Server.php

示例15: send

 public function send($value = null)
 {
     if (!$this->generator) {
         $this->rewind();
     }
     return $this->generator->send($value);
 }
开发者ID:nikic,项目名称:iter,代码行数:7,代码来源:iter.rewindable.php


注:本文中的Generator::send方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。