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. // 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. public function render(\Throwable $exception): FlattenException
  22. {
  23. $cloner = new VarCloner();
  24. $dumper = new class extends CliDumper {
  25. protected function supportsColors(): bool
  26. {
  27. $outputStream = $this->outputStream;
  28. $this->outputStream = fopen('php://stdout', 'w');
  29. try {
  30. return parent::supportsColors();
  31. } finally {
  32. $this->outputStream = $outputStream;
  33. }
  34. }
  35. };
  36. return FlattenException::createFromThrowable($exception)
  37. ->setAsString($dumper->dump($cloner->cloneVar($exception), true));
  38. }
  39. }