PageRenderTime 28ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Cake/Console/ConsoleErrorHandler.php

https://bitbucket.org/udeshika/fake_twitter
PHP | 98 lines | 36 code | 8 blank | 54 comment | 4 complexity | 95b8e8d45bd1acb1f33ffdc729216224 MD5 | raw file
  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 {
  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 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. $this->_stop($exception->getCode() ? $exception->getCode() : 1);
  58. }
  59. /**
  60. * Handle errors in the console environment. Writes errors to stderr,
  61. * and logs messages if Configure::read('debug') is 0.
  62. *
  63. * @param integer $code Error code
  64. * @param string $description Description of the error.
  65. * @param string $file The file the error occurred in.
  66. * @param integer $line The line the error occurred on.
  67. * @param array $context The backtrace of the error.
  68. * @return void
  69. */
  70. public function handleError($code, $description, $file = null, $line = null, $context = null) {
  71. if (error_reporting() === 0) {
  72. return;
  73. }
  74. $stderr = self::getStderr();
  75. list($name, $log) = ErrorHandler::mapErrorCode($code);
  76. $message = __d('cake_console', '%s in [%s, line %s]', $description, $file, $line);
  77. $stderr->write(__d('cake_console', "<error>%s Error:</error> %s\n", $name, $message));
  78. if (Configure::read('debug') == 0) {
  79. CakeLog::write($log, $message);
  80. }
  81. }
  82. /**
  83. * Wrapper for exit(), used for testing.
  84. *
  85. * @param $code int The exit code.
  86. */
  87. protected function _stop($code = 0) {
  88. exit($code);
  89. }
  90. }