Completed
Push — fix-numbervalidator-comma-deci... ( 08054b...a7f0a3 )
by Alexander
40:41 queued 37:41
created

ErrorHandler::formatMessage()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 8.8571
c 0
b 0
f 0
ccs 0
cts 7
cp 0
cc 6
eloc 6
nc 4
nop 2
crap 42
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\base\UserException;
13
use yii\helpers\Console;
14
15
/**
16
 * ErrorHandler handles uncaught PHP errors and exceptions.
17
 *
18
 * ErrorHandler is configured as an application component in [[\yii\base\Application]] by default.
19
 * You can access that instance via `Yii::$app->errorHandler`.
20
 *
21
 * @author Carsten Brandt <[email protected]>
22
 * @since 2.0
23
 */
24
class ErrorHandler extends \yii\base\ErrorHandler
25
{
26
    /**
27
     * Renders an exception using ansi format for console output.
28
     * @param \Exception $exception the exception to be rendered.
29
     */
30
    protected function renderException($exception)
31
    {
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 && ($exception instanceof UserException || !YII_DEBUG)) {
42
            $message = $this->formatMessage($exception->getName() . ': ') . $exception->getMessage();
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
            $message .= "\n" . $this->formatMessage("Stack trace:\n", [Console::BOLD]) . $exception->getTraceAsString();
59
        } else {
60
            $message = $this->formatMessage('Error: ') . $exception->getMessage();
61
        }
62
63
        if (PHP_SAPI === 'cli') {
64
            Console::stderr($message . "\n");
65
        } else {
66
            echo $message . "\n";
67
        }
68
    }
69
70
    /**
71
     * Colorizes a message for console output.
72
     * @param string $message the message to colorize.
73
     * @param array $format the message format.
74
     * @return string the colorized message.
75
     * @see Console::ansiFormat() for details on how to specify the message format.
76
     */
77
    protected function formatMessage($message, $format = [Console::FG_RED, Console::BOLD])
78
    {
79
        $stream = (PHP_SAPI === 'cli') ? \STDERR : \STDOUT;
80
        // try controller first to allow check for --color switch
81
        if (Yii::$app->controller instanceof \yii\console\Controller && Yii::$app->controller->isColorEnabled($stream)
82
            || Yii::$app instanceof \yii\console\Application && Console::streamSupportsAnsiColors($stream)) {
83
            $message = Console::ansiFormat($message, $format);
84
        }
85
        return $message;
86
    }
87
}
88