1
0
mirror of https://github.com/danog/amp.git synced 2025-01-22 21:31:18 +01:00
amp/lib/InvalidYieldError.php

38 lines
1.1 KiB
PHP
Raw Normal View History

<?php
2016-08-15 23:46:26 -05:00
2016-05-24 10:39:19 -05:00
namespace Amp;
2016-05-22 00:11:03 -05:00
2016-08-11 14:35:58 -05:00
class InvalidYieldError extends \Error {
2016-05-22 09:25:40 -05:00
/**
* @param \Generator $generator
* @param string $prefix
* @param \Throwable|null $previous
2016-05-22 09:25:40 -05:00
*/
public function __construct(\Generator $generator, string $prefix, \Throwable $previous = null) {
$yielded = $generator->current();
$prefix .= \sprintf(
"; %s yielded at key %s",
2016-05-22 00:11:03 -05:00
\is_object($yielded) ? \get_class($yielded) : \gettype($yielded),
\var_export($generator->key(), true)
2016-05-22 00:11:03 -05:00
);
2016-08-11 14:35:58 -05:00
if (!$generator->valid()) {
parent::__construct($prefix, 0, $previous);
2016-05-22 00:11:03 -05:00
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()
), 0, $previous);
2016-05-22 00:11:03 -05:00
}
}