ConsoleErrorHandler.php 2.5 KB

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