cakephp2-php8/lib/Cake/Console/ConsoleErrorHandler.php

106 lines
2.8 KiB
PHP
Raw Normal View History

<?php
/**
* ErrorHandler for Console Shells
*
2017-06-10 21:33:55 +00:00
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
2017-06-10 21:33:55 +00:00
* @link https://cakephp.org CakePHP(tm) Project
* @since CakePHP(tm) v 2.0
* @license https://opensource.org/licenses/mit-license.php MIT License
*/
2010-12-07 05:56:10 +00:00
App::uses('ErrorHandler', 'Error');
App::uses('ConsoleOutput', 'Console');
App::uses('CakeLog', 'Log');
/**
2011-08-16 03:55:08 +00:00
* Error Handler for Cake console. Does simple printing of the
* exception that occurred and the stack trace of the error.
*
* @package Cake.Console
*/
class ConsoleErrorHandler {
/**
* Standard error stream.
*
2011-07-29 02:03:44 +00:00
* @var ConsoleOutput
*/
public static $stderr;
/**
* Get the stderr object for the console error handling.
*
2011-07-29 02:03:44 +00:00
* @return ConsoleOutput
*/
public static function getStderr() {
if (empty(static::$stderr)) {
static::$stderr = new ConsoleOutput('php://stderr');
}
return static::$stderr;
}
/**
2014-07-30 20:11:03 +00:00
* Handle an exception in the console environment. Prints a message to stderr.
*
2015-11-09 23:32:48 +00:00
* @param Exception|ParserError $exception The exception to handle
* @return void
*/
2015-11-09 23:32:48 +00:00
public function handleException($exception) {
$stderr = static::getStderr();
$stderr->write(__d('cake_console', "<error>Error:</error> %s\n%s",
2011-08-16 03:55:08 +00:00
$exception->getMessage(),
$exception->getTraceAsString()
));
$code = $exception->getCode();
2014-06-26 12:22:34 +00:00
$code = ($code && is_int($code)) ? $code : 1;
2017-10-04 11:40:57 +00:00
return $this->_stop($code);
}
/**
* Handle errors in the console environment. Writes errors to stderr,
* and logs messages if Configure::read('debug') is 0.
*
* @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 occurred on.
* @param array $context The backtrace of the error.
* @return void
*/
public function handleError($code, $description, $file = null, $line = null, $context = null) {
if (error_reporting() === 0) {
return;
}
$stderr = static::getStderr();
list($name, $log) = ErrorHandler::mapErrorCode($code);
$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));
if (!Configure::read('debug')) {
CakeLog::write($log, $message);
}
if ($log === LOG_ERR) {
$this->_stop(1);
}
}
/**
* Wrapper for exit(), used for testing.
*
* @param int $code The exit code.
2013-07-05 15:19:22 +00:00
* @return void
*/
protected function _stop($code = 0) {
exit($code);
}
}