1
0
mirror of https://github.com/danog/amp.git synced 2024-12-12 01:19:46 +01:00
amp/lib/InvalidYieldError.php

38 lines
1.1 KiB
PHP
Raw Normal View History

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