Exemplo n.º 1
0
 /**
  * @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()));
 }
Exemplo n.º 2
0
 public static function castReflectionGenerator(\ReflectionGenerator $c, array $a, Stub $stub, $isNested)
 {
     $prefix = Caster::PREFIX_VIRTUAL;
     if ($c->getThis()) {
         $a[$prefix . 'this'] = new CutStub($c->getThis());
     }
     $x = $c->getFunction();
     $frame = array('class' => isset($x->class) ? $x->class : null, 'type' => isset($x->class) ? $x->isStatic() ? '::' : '->' : null, 'function' => $x->name, 'file' => $c->getExecutingFile(), 'line' => $c->getExecutingLine());
     if ($trace = $c->getTrace(DEBUG_BACKTRACE_IGNORE_ARGS)) {
         $x = new \ReflectionGenerator($c->getExecutingGenerator());
         array_unshift($trace, array('function' => 'yield', 'file' => $x->getExecutingFile(), 'line' => $x->getExecutingLine() - 1));
         $trace[] = $frame;
         $a[$prefix . 'trace'] = new TraceStub($trace, false, 0, -1, -1);
     } else {
         $x = new FrameStub($frame, false, true);
         $x = ExceptionCaster::castFrameStub($x, array(), $x, true);
         $a[$prefix . 'executing'] = new EnumStub(array($frame['class'] . $frame['type'] . $frame['function'] . '()' => $x[$prefix . 'src']));
     }
     return $a;
 }
Exemplo n.º 3
0
/**
 * A general purpose function for creating error messages from generator yields
 *
 * @param \Generator $generator
 * @param string $prefix
 * @return string
 */
function makeGeneratorError(\Generator $generator, $prefix = "Generator error")
{
    if (PHP_MAJOR_VERSION < 7 || !$generator->valid()) {
        return $prefix;
    }
    $reflGen = new \ReflectionGenerator($generator);
    $exeGen = $reflGen->getExecutingGenerator();
    if ($isSubgenerator = $exeGen !== $generator) {
        $reflGen = new \ReflectionGenerator($exeGen);
    }
    return sprintf("{$prefix} on line %s in %s", $reflGen->getExecutingLine(), $reflGen->getExecutingFile());
}