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)
|
|
|
|
* Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
|
|
|
* @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
|
|
|
* @link http://cakephp.org CakePHP(tm) Project
|
2010-12-24 19:26:26 +00:00
|
|
|
* @package cake.console.libs
|
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
|
|
|
|
|
|
|
/**
|
|
|
|
* Error Handler for Cake console. Does simple printing of the
|
|
|
|
* exception that occurred and the stack trace of the error.
|
|
|
|
*
|
2010-12-24 19:33:41 +00:00
|
|
|
* @package cake.console.libs
|
2010-09-02 21:49:00 +00:00
|
|
|
*/
|
|
|
|
class ConsoleErrorHandler extends ErrorHandler {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Standard error stream.
|
|
|
|
*
|
|
|
|
* @var filehandle
|
|
|
|
* @access public
|
|
|
|
*/
|
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
|
|
|
*
|
|
|
|
* @param Exception $error Exception to handle.
|
|
|
|
* @param array $messages Error messages
|
|
|
|
*/
|
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
|
|
|
|
*/
|
2010-12-04 19:20:50 +00:00
|
|
|
public static 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",
|
2010-11-15 03:47:35 +00:00
|
|
|
$exception->getMessage(),
|
|
|
|
$exception->getTraceAsString()
|
|
|
|
));
|
2010-09-02 22:02:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-11-15 03:47:35 +00:00
|
|
|
* Handle errors in the console environment.
|
2010-09-02 22:02:58 +00:00
|
|
|
*
|
2010-12-22 04:11:23 +00:00
|
|
|
* @param int $code Error code
|
|
|
|
* @param string $description Description of the error.
|
|
|
|
* @param string $file The file the error occurred in.
|
2011-04-25 19:17:59 +00:00
|
|
|
* @param int $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
|
|
|
|
*/
|
2010-11-15 03:47:35 +00:00
|
|
|
public static 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();
|
|
|
|
list($name, $log) = self::_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);
|
|
|
|
}
|
2010-09-02 22:02:58 +00:00
|
|
|
}
|
2010-09-05 15:22:39 +00:00
|
|
|
|
2010-09-02 21:49:00 +00:00
|
|
|
}
|