Completed
Push — master ( ebde8b...0dc0c1 )
by Basil
03:43 queued 14s
created

Controller::init()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
namespace luya\console;
4
5
use Yii;
6
use yii\helpers\Console;
7
use yii\console\Controller as BaseController;
8
use luya\helpers\ObjectHelper;
9
use yii\base\InvalidCallException;
10
use yii\console\Application;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, luya\console\Application.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
11
12
/**
13
 * Console Controller base class.
14
 *
15
 * Extends the base controller by adding helper methods to output responses based on its
16
 * muted behavior to run unit tests without respones.
17
 *
18
 * @author Basil Suter <[email protected]>
19
 * @since 1.0.0
20
 */
21
abstract class Controller extends BaseController
22
{
23
    public function init()
24
    {
25
        parent::init();
26
27
        // Ensure the console command is running under web application object.
28
        if (!ObjectHelper::isInstanceOf(Yii::$app, Application::class, false)) {
29
            throw new InvalidCallException("The console controller can only run within a console Application context.");
30
        }
31
    }
32
33
    /**
34
     * Helper method to see if the current Application is muted or not. If the Application is muted, no output
35
     * will displayed.
36
     *
37
     * @return bool
38
     */
39
    public function isMuted()
40
    {
41
        return Yii::$app->mute;
42
    }
43
44
    /**
45
     * Helper method for writting console application output, include before and after wrappers.
46
     *
47
     * @param string $message The message which is displayed
48
     * @param string $color A color from {{\yii\helpers\Console}} color constants.
49
     */
50
    protected function output($message, $color = null)
51
    {
52
        $format = [];
53
        if (!$this->isMuted()) {
54
            if ($color !== null) {
55
                $format[] = $color;
56
            }
57
            echo Console::ansiFormat("\r".$message."\n", $format);
58
        }
59
    }
60
61
    /**
62
     * Helper method to stop the console command with an error message, outputError returns exit code 1.
63
     *
64
     * @param string $message The message which should be displayed.
65
     * @return int Exit code 1
66
     */
67
    public function outputError($message)
68
    {
69
        $this->output($message, Console::FG_RED);
70
71
        return 1;
72
    }
73
74
    /**
75
     * Helper method to stop the console command with a success message, outputSuccess returns exit code 0.
76
     *
77
     * @param string $message The message which sould be displayed
78
     * @return int Exit code 0
79
     */
80
    public function outputSuccess($message)
81
    {
82
        $this->output($message, Console::FG_GREEN);
83
84
        return 0;
85
    }
86
    
87
    /**
88
     * Helper method to stop the console command with a info message which is threated in case of returns as success
89
     * but does have a different output color (blue). outputInfo returns exit code 0.
90
     *
91
     * @param string $message The message which sould be displayed.
92
     * @return int Exit code 0
93
     */
94
    public function outputInfo($message)
95
    {
96
        $this->output($message, Console::FG_CYAN);
97
        
98
        return 0;
99
    }
100
}
101