Debug.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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, an exception handler and a special class loader.
  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. }