Debug.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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 = null, $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(-1);
  40. }
  41. if ('cli' !== php_sapi_name()) {
  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. $handler = ErrorHandler::register();
  49. if (!$displayErrors) {
  50. $handler->throwAt(0, true);
  51. }
  52. DebugClassLoader::enable();
  53. }
  54. }