Debug.php 1.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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\Debug;
  11. /**
  12. * Registers all the debug tools.
  13. *
  14. * @author Fabien Potencier <fabien@symfony.com>
  15. */
  16. class Debug
  17. {
  18. private static $enabled = false;
  19. /**
  20. * Enables the debug tools.
  21. *
  22. * This method registers an error handler and an exception handler.
  23. *
  24. * @param int $errorReportingLevel The level of error reporting you want
  25. * @param bool $displayErrors Whether to display errors (for development) or just log them (for production)
  26. */
  27. public static function enable($errorReportingLevel = E_ALL, $displayErrors = true)
  28. {
  29. if (static::$enabled) {
  30. return;
  31. }
  32. static::$enabled = true;
  33. if (null !== $errorReportingLevel) {
  34. error_reporting($errorReportingLevel);
  35. } else {
  36. error_reporting(E_ALL);
  37. }
  38. if (!\in_array(PHP_SAPI, array('cli', 'phpdbg'), true)) {
  39. ini_set('display_errors', 0);
  40. ExceptionHandler::register();
  41. } elseif ($displayErrors && (!ini_get('log_errors') || ini_get('error_log'))) {
  42. // CLI - display errors only if they're not already logged to STDERR
  43. ini_set('display_errors', 1);
  44. }
  45. if ($displayErrors) {
  46. ErrorHandler::register(new ErrorHandler(new BufferingLogger()));
  47. } else {
  48. ErrorHandler::register()->throwAt(0, true);
  49. }
  50. DebugClassLoader::enable();
  51. }
  52. }