统一生成器接口

由于内部隐式rewind,需要先调用Generator::current获取当前value,而直接调用Generator::send会跳到第二次yield。

  1. send方法参考
  2. 生成器参考
  1. <?php
  2. class Gen
  3. {
  4. public $isfirst = true;
  5. public $generator;
  6. public function __construct(\Generator $generator)
  7. {
  8. $this->generator = $generator;
  9. }
  10. public function valid()
  11. {
  12. return $this->generator->valid();
  13. }
  14. public function send($value = null)
  15. {
  16. if ($this->isfirst) {
  17. $this->isfirst = false;
  18. return $this->generator->current();
  19. } else {
  20. return $this->generator->send($value);
  21. }
  22. }
  23. }