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 18:57:20 +00:00
|
|
|
* @package cake.console
|
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)
|
|
|
|
*/
|
|
|
|
App::import('Core', 'ErrorHandler');
|
2010-10-04 01:53:23 +00:00
|
|
|
require_once 'console_output.php';
|
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 18:57:20 +00:00
|
|
|
* @package cake.console
|
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();
|
|
|
|
$stderr->write(sprintf(
|
|
|
|
__("<error>Error:</error> %s\n%s"),
|
|
|
|
$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.
|
|
|
|
* @param int $line The line the error ocurrred on.
|
|
|
|
* @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);
|
2010-12-05 01:37:13 +00:00
|
|
|
$message = __('%s in [%s, line %s]', $description, $file, $line);
|
|
|
|
$stderr->write(__("<error>%s Error:</error> %s\n", $name, $message));
|
2010-11-26 04:16:27 +00:00
|
|
|
|
|
|
|
if (Configure::read('debug') == 0) {
|
|
|
|
App::import('Core', 'CakeLog');
|
|
|
|
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
|
|
|
}
|