2010-09-02 21:49:00 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* ErrorHandler for Console Shells
|
|
|
|
*
|
2010-10-03 16:38:58 +00:00
|
|
|
* PHP 5
|
2010-09-02 21:49:00 +00:00
|
|
|
*
|
|
|
|
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
2012-03-13 02:46:46 +00:00
|
|
|
* Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2010-09-02 21:49:00 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2012-03-13 02:46:46 +00:00
|
|
|
* @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2010-09-02 21:49:00 +00:00
|
|
|
* @link http://cakephp.org CakePHP(tm) Project
|
2010-10-03 05:40:12 +00:00
|
|
|
* @since CakePHP(tm) v 2.0
|
2010-09-02 21:49:00 +00:00
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
|
|
|
*/
|
2010-12-07 05:56:10 +00:00
|
|
|
App::uses('ErrorHandler', 'Error');
|
|
|
|
App::uses('ConsoleOutput', 'Console');
|
2010-12-09 03:45:18 +00:00
|
|
|
App::uses('CakeLog', 'Log');
|
2010-09-02 21:49:00 +00:00
|
|
|
|
|
|
|
/**
|
2011-08-16 03:55:08 +00:00
|
|
|
* Error Handler for Cake console. Does simple printing of the
|
2010-09-02 21:49:00 +00:00
|
|
|
* exception that occurred and the stack trace of the error.
|
|
|
|
*
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Console
|
2010-09-02 21:49:00 +00:00
|
|
|
*/
|
2011-10-03 03:16:41 +00:00
|
|
|
class ConsoleErrorHandler {
|
2010-09-02 21:49:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Standard error stream.
|
|
|
|
*
|
2011-07-29 02:03:44 +00:00
|
|
|
* @var ConsoleOutput
|
2010-09-02 21:49:00 +00:00
|
|
|
*/
|
2010-11-15 03:47:35 +00:00
|
|
|
public static $stderr;
|
2010-09-02 21:49:00 +00:00
|
|
|
|
|
|
|
/**
|
2010-11-15 03:47:35 +00:00
|
|
|
* Get the stderr object for the console error handling.
|
2010-09-02 21:49:00 +00:00
|
|
|
*
|
2011-07-29 02:03:44 +00:00
|
|
|
* @return ConsoleOutput
|
2010-09-02 21:49:00 +00:00
|
|
|
*/
|
2010-11-15 03:47:35 +00:00
|
|
|
public static function getStderr() {
|
|
|
|
if (empty(self::$stderr)) {
|
|
|
|
self::$stderr = new ConsoleOutput('php://stderr');
|
|
|
|
}
|
|
|
|
return self::$stderr;
|
2010-09-02 21:49:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-12-22 04:11:23 +00:00
|
|
|
* Handle a exception in the console environment. Prints a message to stderr.
|
2010-09-02 21:49:00 +00:00
|
|
|
*
|
2010-12-22 04:11:23 +00:00
|
|
|
* @param Exception $exception The exception to handle
|
2010-09-02 21:49:00 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-10-03 03:16:41 +00:00
|
|
|
public function handleException(Exception $exception) {
|
2010-11-15 03:47:35 +00:00
|
|
|
$stderr = self::getStderr();
|
2011-04-25 19:17:59 +00:00
|
|
|
$stderr->write(__d('cake_console', "<error>Error:</error> %s\n%s",
|
2011-08-16 03:55:08 +00:00
|
|
|
$exception->getMessage(),
|
2010-11-15 03:47:35 +00:00
|
|
|
$exception->getTraceAsString()
|
|
|
|
));
|
2011-10-03 03:16:41 +00:00
|
|
|
$this->_stop($exception->getCode() ? $exception->getCode() : 1);
|
2010-09-02 22:02:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-07-26 01:46:52 +00:00
|
|
|
* Handle errors in the console environment. Writes errors to stderr,
|
|
|
|
* and logs messages if Configure::read('debug') is 0.
|
2010-09-02 22:02:58 +00:00
|
|
|
*
|
2011-08-01 02:57:17 +00:00
|
|
|
* @param integer $code Error code
|
2010-12-22 04:11:23 +00:00
|
|
|
* @param string $description Description of the error.
|
|
|
|
* @param string $file The file the error occurred in.
|
2011-08-01 02:57:17 +00:00
|
|
|
* @param integer $line The line the error occurred on.
|
2010-12-22 04:11:23 +00:00
|
|
|
* @param array $context The backtrace of the error.
|
2010-09-02 22:02:58 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-10-03 03:16:41 +00:00
|
|
|
public function handleError($code, $description, $file = null, $line = null, $context = null) {
|
2010-12-02 03:44:31 +00:00
|
|
|
if (error_reporting() === 0) {
|
|
|
|
return;
|
|
|
|
}
|
2010-11-15 03:47:35 +00:00
|
|
|
$stderr = self::getStderr();
|
2011-10-03 03:16:41 +00:00
|
|
|
list($name, $log) = ErrorHandler::mapErrorCode($code);
|
2011-03-19 17:32:35 +00:00
|
|
|
$message = __d('cake_console', '%s in [%s, line %s]', $description, $file, $line);
|
|
|
|
$stderr->write(__d('cake_console', "<error>%s Error:</error> %s\n", $name, $message));
|
2010-11-26 04:16:27 +00:00
|
|
|
|
|
|
|
if (Configure::read('debug') == 0) {
|
|
|
|
CakeLog::write($log, $message);
|
|
|
|
}
|
2012-09-14 12:17:07 +00:00
|
|
|
|
|
|
|
if ($log === LOG_ERR) {
|
|
|
|
$this->_stop(1);
|
|
|
|
}
|
2010-09-02 22:02:58 +00:00
|
|
|
}
|
2010-09-05 15:22:39 +00:00
|
|
|
|
2011-10-03 03:16:41 +00:00
|
|
|
/**
|
|
|
|
* Wrapper for exit(), used for testing.
|
|
|
|
*
|
|
|
|
* @param $code int The exit code.
|
|
|
|
*/
|
|
|
|
protected function _stop($code = 0) {
|
|
|
|
exit($code);
|
|
|
|
}
|
|
|
|
|
2010-09-02 21:49:00 +00:00
|
|
|
}
|