ConsoleErrorHandler.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 2.0.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Console;
  16. use Cake\Error\BaseErrorHandler;
  17. use Cake\Error\FatalErrorException;
  18. /**
  19. * Error Handler for Cake console. Does simple printing of the
  20. * exception that occurred and the stack trace of the error.
  21. */
  22. class ConsoleErrorHandler extends BaseErrorHandler {
  23. /**
  24. * Standard error stream.
  25. *
  26. * @var ConsoleOutput
  27. */
  28. protected $_stderr;
  29. /**
  30. * Options for this instance.
  31. *
  32. * @var array
  33. */
  34. protected $_options;
  35. /**
  36. * Constructor
  37. *
  38. * @param array $options Options for the error handler.
  39. */
  40. public function __construct($options = []) {
  41. if (empty($options['stderr'])) {
  42. $options['stderr'] = new ConsoleOutput('php://stderr');
  43. }
  44. $this->_stderr = $options['stderr'];
  45. $this->_options = $options;
  46. }
  47. /**
  48. * Handle errors in the console environment. Writes errors to stderr,
  49. * and logs messages if Configure::read('debug') is false.
  50. *
  51. * @param \Exception $exception Exception instance.
  52. * @return void
  53. * @throws Exception When renderer class not found
  54. * @see http://php.net/manual/en/function.set-exception-handler.php
  55. */
  56. public function handleException(\Exception $exception) {
  57. $this->_displayException($exception);
  58. $this->_logException($exception);
  59. $code = $exception->getCode();
  60. $code = ($code && is_int($code)) ? $code : 1;
  61. $this->_stop($code);
  62. }
  63. /**
  64. * Prints an exception to stderr.
  65. *
  66. * @param \Exception $exception The exception to handle
  67. * @return void
  68. */
  69. protected function _displayException($exception) {
  70. $errorName = __d('cake_console', 'Exception:');
  71. if ($exception instanceof FatalErrorException) {
  72. $errorName = __d('cake_console', 'Fatal Error:');
  73. }
  74. $message = sprintf(
  75. "<error>%s</error> %s in [%s, line %s]",
  76. $errorName,
  77. $exception->getMessage(),
  78. $exception->getFile(),
  79. $exception->getLine()
  80. );
  81. $this->_stderr->write($message);
  82. }
  83. /**
  84. * Prints an error to stderr.
  85. *
  86. * Template method of BaseErrorHandler.
  87. *
  88. * @param array $error An array of error data.
  89. * @param bool $debug Whether or not the app is in debug mode.
  90. * @return void
  91. */
  92. protected function _displayError($error, $debug) {
  93. $message = __d('cake_console', '%s in [%s, line %s]',
  94. $error['description'],
  95. $error['file'],
  96. $error['line']
  97. );
  98. $message = __d('cake_console', "<error>%s Error:</error> %s\n",
  99. $error['error'],
  100. $message
  101. );
  102. $this->_stderr->write($message);
  103. }
  104. /**
  105. * Stop the execution and set the exit code for the process.
  106. *
  107. * @param int $code The exit code.
  108. * @return void
  109. */
  110. protected function _stop($code) {
  111. exit($code);
  112. }
  113. }