ConsoleErrorHandler.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. /**
  3. * ErrorHandler for Console Shells
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  8. * Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice.
  12. *
  13. * @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://cakephp.org CakePHP(tm) Project
  15. * @since CakePHP(tm) v 2.0
  16. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  17. */
  18. App::uses('ErrorHandler', 'Error');
  19. App::uses('ConsoleOutput', 'Console');
  20. App::uses('CakeLog', 'Log');
  21. /**
  22. * Error Handler for Cake console. Does simple printing of the
  23. * exception that occurred and the stack trace of the error.
  24. *
  25. * @package Cake.Console
  26. */
  27. class ConsoleErrorHandler extends ErrorHandler {
  28. /**
  29. * Standard error stream.
  30. *
  31. * @var ConsoleOutput
  32. */
  33. public static $stderr;
  34. /**
  35. * Get the stderr object for the console error handling.
  36. *
  37. * @return ConsoleOutput
  38. */
  39. public static function getStderr() {
  40. if (empty(self::$stderr)) {
  41. self::$stderr = new ConsoleOutput('php://stderr');
  42. }
  43. return self::$stderr;
  44. }
  45. /**
  46. * Handle a exception in the console environment. Prints a message to stderr.
  47. *
  48. * @param Exception $exception The exception to handle
  49. * @return void
  50. */
  51. public static function handleException(Exception $exception) {
  52. $stderr = self::getStderr();
  53. $stderr->write(__d('cake_console', "<error>Error:</error> %s\n%s",
  54. $exception->getMessage(),
  55. $exception->getTraceAsString()
  56. ));
  57. }
  58. /**
  59. * Handle errors in the console environment. Writes errors to stderr,
  60. * and logs messages if Configure::read('debug') is 0.
  61. *
  62. * @param integer $code Error code
  63. * @param string $description Description of the error.
  64. * @param string $file The file the error occurred in.
  65. * @param integer $line The line the error occurred on.
  66. * @param array $context The backtrace of the error.
  67. * @return void
  68. */
  69. public static function handleError($code, $description, $file = null, $line = null, $context = null) {
  70. if (error_reporting() === 0) {
  71. return;
  72. }
  73. $stderr = self::getStderr();
  74. list($name, $log) = self::_mapErrorCode($code);
  75. $message = __d('cake_console', '%s in [%s, line %s]', $description, $file, $line);
  76. $stderr->write(__d('cake_console', "<error>%s Error:</error> %s\n", $name, $message));
  77. if (Configure::read('debug') == 0) {
  78. CakeLog::write($log, $message);
  79. }
  80. }
  81. }