CliErrorRenderer.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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. /**
  15. * @author Nicolas Grekas <p@tchwork.com>
  16. */
  17. class CliErrorRenderer implements ErrorRendererInterface
  18. {
  19. /**
  20. * {@inheritdoc}
  21. */
  22. public function render(\Throwable $exception): FlattenException
  23. {
  24. $cloner = new VarCloner();
  25. $dumper = new class() extends CliDumper {
  26. protected function supportsColors(): bool
  27. {
  28. $outputStream = $this->outputStream;
  29. $this->outputStream = fopen('php://stdout', 'w');
  30. try {
  31. return parent::supportsColors();
  32. } finally {
  33. $this->outputStream = $outputStream;
  34. }
  35. }
  36. };
  37. return FlattenException::createFromThrowable($exception)
  38. ->setAsString($dumper->dump($cloner->cloneVar($exception), true));
  39. }
  40. }