|
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; |
|
|
|
|
|
|
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
|
|
|
|
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare 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.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/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: