Debug.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. * If the Symfony ClassLoader component is available, a special
  25. * class loader is also registered.
  26. *
  27. * @param int $errorReportingLevel The level of error reporting you want
  28. * @param bool $displayErrors Whether to display errors (for development) or just log them (for production)
  29. */
  30. public static function enable($errorReportingLevel = E_ALL, $displayErrors = true)
  31. {
  32. if (static::$enabled) {
  33. return;
  34. }
  35. static::$enabled = true;
  36. if (null !== $errorReportingLevel) {
  37. error_reporting($errorReportingLevel);
  38. } else {
  39. error_reporting(E_ALL);
  40. }
  41. if ('cli' !== PHP_SAPI) {
  42. ini_set('display_errors', 0);
  43. ExceptionHandler::register();
  44. } elseif ($displayErrors && (!ini_get('log_errors') || ini_get('error_log'))) {
  45. // CLI - display errors only if they're not already logged to STDERR
  46. ini_set('display_errors', 1);
  47. }
  48. if ($displayErrors) {
  49. ErrorHandler::register(new ErrorHandler(new BufferingLogger()));
  50. } else {
  51. ErrorHandler::register()->throwAt(0, true);
  52. }
  53. DebugClassLoader::enable();
  54. }
  55. }