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

37 lines
1009 B
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
2016-08-11 14:35:58 -05:00
* @param string $prefix
2016-05-22 09:25:40 -05:00
*/
2016-08-11 14:35:58 -05:00
public function __construct(\Generator $generator, string $prefix) {
$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()) {
2016-05-22 00:11:03 -05:00
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()
));
}
}