Base for a static organization website

ConsoleErrorHandler.php 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. /**
  3. * ErrorHandler for Console Shells
  4. *
  5. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  6. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  7. *
  8. * Licensed under The MIT License
  9. * For full copyright and license information, please see the LICENSE.txt
  10. * Redistributions of files must retain the above copyright notice.
  11. *
  12. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  13. * @link http://cakephp.org CakePHP(tm) Project
  14. * @since CakePHP(tm) v 2.0
  15. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  16. */
  17. App::uses('ErrorHandler', 'Error');
  18. App::uses('ConsoleOutput', 'Console');
  19. App::uses('CakeLog', 'Log');
  20. /**
  21. * Error Handler for Cake console. Does simple printing of the
  22. * exception that occurred and the stack trace of the error.
  23. *
  24. * @package Cake.Console
  25. */
  26. class ConsoleErrorHandler {
  27. /**
  28. * Standard error stream.
  29. *
  30. * @var ConsoleOutput
  31. */
  32. public static $stderr;
  33. /**
  34. * Get the stderr object for the console error handling.
  35. *
  36. * @return ConsoleOutput
  37. */
  38. public static function getStderr() {
  39. if (empty(static::$stderr)) {
  40. static::$stderr = new ConsoleOutput('php://stderr');
  41. }
  42. return static::$stderr;
  43. }
  44. /**
  45. * Handle an exception in the console environment. Prints a message to stderr.
  46. *
  47. * @param Exception $exception The exception to handle
  48. * @return void
  49. */
  50. public function handleException(Exception $exception) {
  51. $stderr = static::getStderr();
  52. $stderr->write(__d('cake_console', "<error>Error:</error> %s\n%s",
  53. $exception->getMessage(),
  54. $exception->getTraceAsString()
  55. ));
  56. $code = $exception->getCode();
  57. $code = ($code && is_int($code)) ? $code : 1;
  58. return $this->_stop($code);
  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 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 function handleError($code, $description, $file = null, $line = null, $context = null) {
  72. if (error_reporting() === 0) {
  73. return;
  74. }
  75. $stderr = static::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 int $code The exit code.
  90. * @return void
  91. */
  92. protected function _stop($code = 0) {
  93. exit($code);
  94. }
  95. }