ExceptionManager.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. namespace Symfony\Bundle\FrameworkBundle\Debug;
  3. use Symfony\Components\DependencyInjection\ContainerInterface;
  4. use Symfony\Components\HttpKernel\Exception\HttpException;
  5. use Symfony\Components\HttpFoundation\Request;
  6. use Symfony\Components\HttpFoundation\Response;
  7. use Symfony\Components\HttpKernel\Log\DebugLoggerInterface;
  8. /*
  9. * This file is part of the Symfony framework.
  10. *
  11. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  12. *
  13. * This source file is subject to the MIT license that is bundled
  14. * with this source code in the file LICENSE.
  15. */
  16. /**
  17. * ExceptionManager.
  18. *
  19. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  20. */
  21. class ExceptionManager
  22. {
  23. protected $exception;
  24. protected $request;
  25. protected $logger;
  26. protected $currentContent;
  27. public function __construct(\Exception $exception, Request $request, DebugLoggerInterface $logger = null)
  28. {
  29. $this->exception = $exception;
  30. $this->request = $request;
  31. $this->logger = $logger;
  32. $this->currentContent = '';
  33. while (false !== $content = ob_get_clean()) {
  34. $this->currentContent .= $content;
  35. }
  36. }
  37. public function getException()
  38. {
  39. return $this->exception;
  40. }
  41. public function getCurrentContent()
  42. {
  43. return $this->currentContent;
  44. }
  45. public function getLogger()
  46. {
  47. return $this->logger;
  48. }
  49. public function getLogs()
  50. {
  51. return null === $this->logger ? array() : $this->logger->getLogs();
  52. }
  53. public function countErrors()
  54. {
  55. if (null === $this->logger) {
  56. return 0;
  57. }
  58. $errors = 0;
  59. foreach ($this->logger->getLogs() as $log) {
  60. if ('ERR' === $log['priorityName']) {
  61. ++$errors;
  62. }
  63. }
  64. return $errors;
  65. }
  66. public function getFormat()
  67. {
  68. $format = $this->request->getRequestFormat();
  69. // when using CLI, we force the format to be TXT
  70. if (0 === strncasecmp(PHP_SAPI, 'cli', 3)) {
  71. $format = 'txt';
  72. }
  73. return $format;
  74. }
  75. public function getStatusCode()
  76. {
  77. return $this->exception instanceof HttpException ? $this->exception->getCode() : 500;
  78. }
  79. public function getStatusText()
  80. {
  81. return Response::$statusTexts[$this->getStatusCode()];
  82. }
  83. public function getMessage()
  84. {
  85. return null === $this->exception->getMessage() ? 'n/a' : $this->exception->getMessage();
  86. }
  87. public function getName()
  88. {
  89. return get_class($this->exception);
  90. }
  91. /**
  92. * Returns an array of exception traces.
  93. *
  94. * @return array An array of traces
  95. */
  96. public function getTraces()
  97. {
  98. $traces = array();
  99. $traces[] = array(
  100. 'class' => '',
  101. 'type' => '',
  102. 'function' => '',
  103. 'file' => $this->exception->getFile(),
  104. 'line' => $this->exception->getLine(),
  105. 'args' => array(),
  106. );
  107. foreach ($this->exception->getTrace() as $entry) {
  108. $traces[] = array(
  109. 'class' => isset($entry['class']) ? $entry['class'] : '',
  110. 'type' => isset($entry['type']) ? $entry['type'] : '',
  111. 'function' => $entry['function'],
  112. 'file' => isset($entry['file']) ? $entry['file'] : null,
  113. 'line' => isset($entry['line']) ? $entry['line'] : null,
  114. 'args' => isset($entry['args']) ? $entry['args'] : array(),
  115. );
  116. }
  117. return $traces;
  118. }
  119. }