CliErrorRenderer.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\ErrorHandler\ErrorRenderer;
  11. use Symfony\Component\ErrorHandler\Exception\FlattenException;
  12. use Symfony\Component\VarDumper\Cloner\VarCloner;
  13. use Symfony\Component\VarDumper\Dumper\CliDumper;
  14. // Help opcache.preload discover always-needed symbols
  15. class_exists(CliDumper::class);
  16. /**
  17. * @author Nicolas Grekas <p@tchwork.com>
  18. */
  19. class CliErrorRenderer implements ErrorRendererInterface
  20. {
  21. /**
  22. * {@inheritdoc}
  23. */
  24. public function render(\Throwable $exception): FlattenException
  25. {
  26. $cloner = new VarCloner();
  27. $dumper = new class() extends CliDumper {
  28. protected function supportsColors(): bool
  29. {
  30. $outputStream = $this->outputStream;
  31. $this->outputStream = fopen('php://stdout', 'w');
  32. try {
  33. return parent::supportsColors();
  34. } finally {
  35. $this->outputStream = $outputStream;
  36. }
  37. }
  38. };
  39. return FlattenException::createFromThrowable($exception)
  40. ->setAsString($dumper->dump($cloner->cloneVar($exception), true));
  41. }
  42. }