ConsoleErrorHandler.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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
  16. * @subpackage cake.cake.console
  17. * @since CakePHP(tm) v 2.0
  18. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  19. */
  20. App::uses('ErrorHandler', 'Error');
  21. App::uses('ConsoleOutput', 'Console');
  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
  27. * @subpackage cake.cake.console
  28. */
  29. class ConsoleErrorHandler extends ErrorHandler {
  30. /**
  31. * Standard error stream.
  32. *
  33. * @var filehandle
  34. * @access public
  35. */
  36. public static $stderr;
  37. /**
  38. * Get the stderr object for the console error handling.
  39. *
  40. * @param Exception $error Exception to handle.
  41. * @param array $messages Error messages
  42. */
  43. public static function getStderr() {
  44. if (empty(self::$stderr)) {
  45. self::$stderr = new ConsoleOutput('php://stderr');
  46. }
  47. return self::$stderr;
  48. }
  49. /**
  50. * Handle a exception in the console environment.
  51. *
  52. * @return void
  53. */
  54. public static function handleException(Exception $exception) {
  55. $stderr = self::getStderr();
  56. $stderr->write(sprintf(
  57. __("<error>Error:</error> %s\n%s"),
  58. $exception->getMessage(),
  59. $exception->getTraceAsString()
  60. ));
  61. }
  62. /**
  63. * Handle errors in the console environment.
  64. *
  65. * @return void
  66. */
  67. public static function handleError($code, $description, $file = null, $line = null, $context = null) {
  68. if (error_reporting() === 0) {
  69. return;
  70. }
  71. $stderr = self::getStderr();
  72. list($name, $log) = self::_mapErrorCode($code);
  73. $message = __('%s in [%s, line %s]', $description, $file, $line);
  74. $stderr->write(__("<error>%s Error:</error> %s\n", $name, $message));
  75. if (Configure::read('debug') == 0) {
  76. App::import('Core', 'CakeLog');
  77. CakeLog::write($log, $message);
  78. }
  79. }
  80. /**
  81. * undocumented function
  82. *
  83. * @return void
  84. */
  85. public function render() {
  86. $this->stderr->write(sprintf(
  87. __("<error>Error:</error> %s\n%s"),
  88. $this->error->getMessage(),
  89. $this->error->getTraceAsString()
  90. ));
  91. }
  92. }