1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Console controller |
4
|
|
|
* |
5
|
|
|
* @link https://github.com/inblank/yii2-activeuser |
6
|
|
|
* @copyright Copyright (c) 2016 Pavel Aleksandrov <[email protected]> |
7
|
|
|
* @license http://opensource.org/licenses/MIT |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace inblank\activeuser\components; |
11
|
|
|
|
12
|
|
|
use yii; |
13
|
|
|
use yii\helpers\Console; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class ConsoleController |
17
|
|
|
* |
18
|
|
|
* @package inblank\activeuser\traits |
19
|
|
|
*/ |
20
|
|
|
class ConsoleController extends yii\console\Controller |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Confirm action |
24
|
|
|
* @param string $message message for confirm |
25
|
|
|
* @param bool $default default selection |
26
|
|
|
* @throws yii\base\ExitException |
27
|
|
|
*/ |
28
|
|
|
public function confirmAction($message, $default = false) |
29
|
|
|
{ |
30
|
|
|
if (!Console::confirm($message, $default)) { |
31
|
|
|
Console::error(Yii::t('activeuser_backend', 'Action canceled')); |
32
|
|
|
Yii::$app->end(); |
33
|
|
|
} |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Show action usage |
38
|
|
|
* @param string $required required params separated by comma |
39
|
|
|
* @param string $optional optional params separated by comma |
40
|
|
|
* @param array $errors errors to output |
41
|
|
|
* @throws yii\base\ExitException |
42
|
|
|
*/ |
43
|
|
|
protected function showUsage($required = '', $optional = '', $errors = []) |
44
|
|
|
{ |
45
|
|
|
$description = Yii::t('activeuser_backend', $this->getActionHelp($this->action)); |
46
|
|
|
Console::output($description); |
47
|
|
|
Console::output(str_pad('', mb_strlen($description), '-')); |
48
|
|
|
$paramsString = []; |
49
|
|
View Code Duplication |
if (!empty($required)) { |
|
|
|
|
50
|
|
|
foreach (explode(',', $required) as $param) { |
51
|
|
|
$paramsString[] = '<' . trim($param) . '>'; |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
View Code Duplication |
if (!empty($optional)) { |
|
|
|
|
55
|
|
|
foreach (explode(',', $optional) as $param) { |
56
|
|
|
$paramsString[] = '[' . trim($param) . ']'; |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
Console::output( |
60
|
|
|
Yii::t('activeuser_backend', 'Usage') . ': ' . |
61
|
|
|
Console::ansiFormat( |
62
|
|
|
'yii ' . $this->id . "/" . $this->action->id . ' ' . |
63
|
|
|
implode(' ', $paramsString), |
64
|
|
|
[Console::BOLD] |
65
|
|
|
) |
66
|
|
|
); |
67
|
|
|
Console::output(); |
68
|
|
|
if (!empty($errors)) { |
69
|
|
|
$this->showErrors($errors); |
70
|
|
|
} |
71
|
|
|
yii::$app->end(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Show errors |
76
|
|
|
* @param array $errors array of errors string |
77
|
|
|
* @throws yii\base\ExitException |
78
|
|
|
*/ |
79
|
|
|
protected function showErrors($errors) |
80
|
|
|
{ |
81
|
|
|
foreach ((array)$errors as $err) { |
82
|
|
|
Console::error(Console::ansiFormat(Yii::t('activeuser_backend', "Error") . ": ", [Console::FG_RED]) . $err[0]); |
83
|
|
|
} |
84
|
|
|
yii::$app->end(); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
} |
88
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.