ConsoleExceptionRenderer.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * For full copyright and license information, please see the LICENSE.txt
  9. * Redistributions of files must retain the above copyright notice.
  10. *
  11. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  12. * @link https://cakephp.org CakePHP(tm) Project
  13. * @since 4.4.0
  14. * @license https://opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\Error\Renderer;
  17. use Cake\Console\ConsoleOutput;
  18. use Cake\Core\Configure;
  19. use Cake\Core\Exception\CakeException;
  20. use Psr\Http\Message\ServerRequestInterface;
  21. use Throwable;
  22. /**
  23. * Plain text exception rendering with a stack trace.
  24. *
  25. * Useful in CI or plain text environments.
  26. *
  27. * @todo 5.0 Implement \Cake\Error\ExceptionRendererInterface. This implementation can't implement
  28. * the concrete interface because the return types are not compatible.
  29. */
  30. class ConsoleExceptionRenderer
  31. {
  32. /**
  33. * @var \Throwable
  34. */
  35. private $error;
  36. /**
  37. * @var \Cake\Console\ConsoleOutput
  38. */
  39. private $output;
  40. /**
  41. * @var bool
  42. */
  43. private $trace;
  44. /**
  45. * Constructor.
  46. *
  47. * @param \Throwable $error The error to render.
  48. * @param \Psr\Http\Message\ServerRequestInterface|null $request Not used.
  49. * @param array $config Error handling configuration.
  50. */
  51. public function __construct(Throwable $error, ?ServerRequestInterface $request, array $config)
  52. {
  53. $this->error = $error;
  54. $this->output = $config['stderr'] ?? new ConsoleOutput('php://stderr');
  55. $this->trace = $config['trace'] ?? true;
  56. }
  57. /**
  58. * Render an exception into a plain text message.
  59. *
  60. * @return \Psr\Http\Message\ResponseInterface|string
  61. */
  62. public function render()
  63. {
  64. $out = [];
  65. $out[] = sprintf(
  66. '<error>[%s] %s</error> in %s on line %s',
  67. get_class($this->error),
  68. $this->error->getMessage(),
  69. $this->error->getFile(),
  70. $this->error->getLine()
  71. );
  72. $debug = Configure::read('debug');
  73. if ($debug && $this->error instanceof CakeException) {
  74. $attributes = $this->error->getAttributes();
  75. if ($attributes) {
  76. $out[] = '';
  77. $out[] = '<info>Exception Attributes</info>';
  78. $out[] = '';
  79. $out[] = var_export($this->error->getAttributes(), true);
  80. }
  81. }
  82. if ($this->trace) {
  83. $out[] = '';
  84. $out[] = '<info>Stack Trace:</info>';
  85. $out[] = '';
  86. $out[] = $this->error->getTraceAsString();
  87. $out[] = '';
  88. }
  89. return join("\n", $out);
  90. }
  91. /**
  92. * Write output to the output stream
  93. *
  94. * @param string $output The output to print.
  95. * @return void
  96. */
  97. public function write($output): void
  98. {
  99. $this->output->write($output);
  100. }
  101. }