1 | <?php |
||
2 | /** |
||
3 | * @link http://www.yiiframework.com/ |
||
4 | * @copyright Copyright (c) 2008 Yii Software LLC |
||
5 | * @license http://www.yiiframework.com/license/ |
||
6 | */ |
||
7 | |||
8 | namespace yii\console; |
||
9 | |||
10 | use Yii; |
||
11 | use yii\base\ErrorException; |
||
12 | use yii\helpers\Console; |
||
13 | |||
14 | /** |
||
15 | * ErrorHandler handles uncaught PHP errors and exceptions. |
||
16 | * |
||
17 | * ErrorHandler is configured as an application component in [[\yii\base\Application]] by default. |
||
18 | * You can access that instance via `Yii::$app->errorHandler`. |
||
19 | * |
||
20 | * @author Carsten Brandt <[email protected]> |
||
21 | * @since 2.0 |
||
22 | */ |
||
23 | class ErrorHandler extends \yii\base\ErrorHandler |
||
24 | { |
||
25 | /** |
||
26 | * Renders an exception using ansi format for console output. |
||
27 | * @param \Exception $exception the exception to be rendered. |
||
28 | */ |
||
29 | protected function renderException($exception) |
||
30 | { |
||
31 | $previous = $exception->getPrevious(); |
||
32 | if ($exception instanceof UnknownCommandException) { |
||
33 | // display message and suggest alternatives in case of unknown command |
||
34 | $message = $this->formatMessage($exception->getName() . ': ') . $exception->command; |
||
35 | $alternatives = $exception->getSuggestedAlternatives(); |
||
36 | if (count($alternatives) === 1) { |
||
37 | $message .= "\n\nDid you mean \"" . reset($alternatives) . '"?'; |
||
38 | } elseif (count($alternatives) > 1) { |
||
39 | $message .= "\n\nDid you mean one of these?\n - " . implode("\n - ", $alternatives); |
||
40 | } |
||
41 | } elseif ($exception instanceof Exception || !YII_DEBUG) { |
||
42 | $message = $this->formatMessage($exception->getName() . ': ') . $exception->getMessage(); |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
43 | } elseif (YII_DEBUG) { |
||
44 | if ($exception instanceof Exception) { |
||
45 | $message = $this->formatMessage("Exception ({$exception->getName()})"); |
||
46 | } elseif ($exception instanceof ErrorException) { |
||
47 | $message = $this->formatMessage($exception->getName()); |
||
48 | } else { |
||
49 | $message = $this->formatMessage('Exception'); |
||
50 | } |
||
51 | $message .= $this->formatMessage(" '" . get_class($exception) . "'", [Console::BOLD, Console::FG_BLUE]) |
||
52 | . ' with message ' . $this->formatMessage("'{$exception->getMessage()}'", [Console::BOLD]) //. "\n" |
||
53 | . "\n\nin " . dirname($exception->getFile()) . DIRECTORY_SEPARATOR . $this->formatMessage(basename($exception->getFile()), [Console::BOLD]) |
||
54 | . ':' . $this->formatMessage($exception->getLine(), [Console::BOLD, Console::FG_YELLOW]) . "\n"; |
||
55 | if ($exception instanceof \yii\db\Exception && !empty($exception->errorInfo)) { |
||
56 | $message .= "\n" . $this->formatMessage("Error Info:\n", [Console::BOLD]) . print_r($exception->errorInfo, true); |
||
57 | } |
||
58 | if ($previous === null) { |
||
59 | $message .= "\n" . $this->formatMessage("Stack trace:\n", [Console::BOLD]) . $exception->getTraceAsString(); |
||
60 | } |
||
61 | } else { |
||
62 | $message = $this->formatMessage('Error: ') . $exception->getMessage(); |
||
63 | } |
||
64 | |||
65 | if (PHP_SAPI === 'cli') { |
||
66 | Console::stderr($message . "\n"); |
||
67 | } else { |
||
68 | echo $message . "\n"; |
||
69 | } |
||
70 | if (YII_DEBUG && $previous !== null) { |
||
71 | $causedBy = $this->formatMessage('Caused by: ', [Console::BOLD]); |
||
72 | if (PHP_SAPI === 'cli') { |
||
73 | Console::stderr($causedBy); |
||
74 | } else { |
||
75 | echo $causedBy; |
||
76 | } |
||
77 | $this->renderException($previous); |
||
78 | } |
||
79 | } |
||
80 | |||
81 | /** |
||
82 | * Colorizes a message for console output. |
||
83 | * @param string $message the message to colorize. |
||
84 | * @param array $format the message format. |
||
85 | * @return string the colorized message. |
||
86 | * @see Console::ansiFormat() for details on how to specify the message format. |
||
87 | */ |
||
88 | protected function formatMessage($message, $format = [Console::FG_RED, Console::BOLD]) |
||
89 | { |
||
90 | $stream = (PHP_SAPI === 'cli') ? \STDERR : \STDOUT; |
||
91 | // try controller first to allow check for --color switch |
||
92 | if (Yii::$app->controller instanceof \yii\console\Controller && Yii::$app->controller->isColorEnabled($stream) |
||
93 | || Yii::$app instanceof \yii\console\Application && Console::streamSupportsAnsiColors($stream)) { |
||
94 | $message = Console::ansiFormat($message, $format); |
||
95 | } |
||
96 | |||
97 | return $message; |
||
98 | } |
||
99 | } |
||
100 |