ConsoleErrorHandler.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. /**
  3. * ErrorHandler for Console Shells
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  8. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * For full copyright and license information, please see the LICENSE.txt
  12. * Redistributions of files must retain the above copyright notice.
  13. *
  14. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  15. * @link http://cakephp.org CakePHP(tm) Project
  16. * @since CakePHP(tm) v 2.0
  17. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  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
  27. */
  28. class ConsoleErrorHandler {
  29. /**
  30. * Standard error stream.
  31. *
  32. * @var ConsoleOutput
  33. */
  34. public static $stderr;
  35. /**
  36. * Get the stderr object for the console error handling.
  37. *
  38. * @return ConsoleOutput
  39. */
  40. public static function getStderr() {
  41. if (empty(self::$stderr)) {
  42. self::$stderr = new ConsoleOutput('php://stderr');
  43. }
  44. return self::$stderr;
  45. }
  46. /**
  47. * Handle a exception in the console environment. Prints a message to stderr.
  48. *
  49. * @param Exception $exception The exception to handle
  50. * @return void
  51. */
  52. public function handleException(Exception $exception) {
  53. $stderr = self::getStderr();
  54. $stderr->write(__d('cake_console', "<error>Error:</error> %s\n%s",
  55. $exception->getMessage(),
  56. $exception->getTraceAsString()
  57. ));
  58. return $this->_stop($exception->getCode() ? $exception->getCode() : 1);
  59. }
  60. /**
  61. * Handle errors in the console environment. Writes errors to stderr,
  62. * and logs messages if Configure::read('debug') is 0.
  63. *
  64. * @param integer $code Error code
  65. * @param string $description Description of the error.
  66. * @param string $file The file the error occurred in.
  67. * @param integer $line The line the error occurred on.
  68. * @param array $context The backtrace of the error.
  69. * @return void
  70. */
  71. public 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) = ErrorHandler::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')) {
  80. CakeLog::write($log, $message);
  81. }
  82. if ($log === LOG_ERR) {
  83. return $this->_stop(1);
  84. }
  85. }
  86. /**
  87. * Wrapper for exit(), used for testing.
  88. *
  89. * @param integer $code The exit code.
  90. * @return void
  91. */
  92. protected function _stop($code = 0) {
  93. exit($code);
  94. }
  95. }