1: <?php
2: /**
3: * ErrorHandler for Console Shells
4: *
5: * PHP 5
6: *
7: * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
8: * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
9: *
10: * Licensed under The MIT License
11: * For full copyright and license information, please see the LICENSE.txt
12: * Redistributions of files must retain the above copyright notice.
13: *
14: * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
15: * @link http://cakephp.org CakePHP(tm) Project
16: * @since CakePHP(tm) v 2.0
17: * @license http://www.opensource.org/licenses/mit-license.php MIT License
18: */
19:
20: App::uses('ErrorHandler', 'Error');
21: App::uses('ConsoleOutput', 'Console');
22: App::uses('CakeLog', 'Log');
23:
24: /**
25: * Error Handler for Cake console. Does simple printing of the
26: * exception that occurred and the stack trace of the error.
27: *
28: * @package Cake.Console
29: */
30: class ConsoleErrorHandler {
31:
32: /**
33: * Standard error stream.
34: *
35: * @var ConsoleOutput
36: */
37: public static $stderr;
38:
39: /**
40: * Get the stderr object for the console error handling.
41: *
42: * @return ConsoleOutput
43: */
44: public static function getStderr() {
45: if (empty(self::$stderr)) {
46: self::$stderr = new ConsoleOutput('php://stderr');
47: }
48: return self::$stderr;
49: }
50:
51: /**
52: * Handle a exception in the console environment. Prints a message to stderr.
53: *
54: * @param Exception $exception The exception to handle
55: * @return void
56: */
57: public function handleException(Exception $exception) {
58: $stderr = self::getStderr();
59: $stderr->write(__d('cake_console', "<error>Error:</error> %s\n%s",
60: $exception->getMessage(),
61: $exception->getTraceAsString()
62: ));
63: return $this->_stop($exception->getCode() ? $exception->getCode() : 1);
64: }
65:
66: /**
67: * Handle errors in the console environment. Writes errors to stderr,
68: * and logs messages if Configure::read('debug') is 0.
69: *
70: * @param integer $code Error code
71: * @param string $description Description of the error.
72: * @param string $file The file the error occurred in.
73: * @param integer $line The line the error occurred on.
74: * @param array $context The backtrace of the error.
75: * @return void
76: */
77: public function handleError($code, $description, $file = null, $line = null, $context = null) {
78: if (error_reporting() === 0) {
79: return;
80: }
81: $stderr = self::getStderr();
82: list($name, $log) = ErrorHandler::mapErrorCode($code);
83: $message = __d('cake_console', '%s in [%s, line %s]', $description, $file, $line);
84: $stderr->write(__d('cake_console', "<error>%s Error:</error> %s\n", $name, $message));
85:
86: if (!Configure::read('debug')) {
87: CakeLog::write($log, $message);
88: }
89:
90: if ($log === LOG_ERR) {
91: return $this->_stop(1);
92: }
93: }
94:
95: /**
96: * Wrapper for exit(), used for testing.
97: *
98: * @param int $code The exit code.
99: * @return void
100: */
101: protected function _stop($code = 0) {
102: exit($code);
103: }
104:
105: }
106: