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