1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace app\commands; |
4
|
|
|
|
5
|
|
|
use mikehaertl\shellcommand\Command; |
6
|
|
|
use Yii; |
7
|
|
|
use yii\console\Controller; |
8
|
|
|
use yii\helpers\ArrayHelper; |
9
|
|
|
use yii\helpers\VarDumper; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @link http://www.diemeisterei.de/ |
13
|
|
|
* |
14
|
|
|
* @copyright Copyright (c) 2016 diemeisterei GmbH, Stuttgart |
15
|
|
|
* |
16
|
|
|
* For the full copyright and license information, please view the LICENSE |
17
|
|
|
* file that was distributed with this source code |
18
|
|
|
*/ |
19
|
|
|
class AppController extends Controller |
20
|
|
|
{ |
21
|
|
|
public $defaultAction = 'version'; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var int config levels to show |
25
|
|
|
*/ |
26
|
|
|
public $level = 10; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @inheritdoc |
30
|
|
|
*/ |
31
|
|
|
public function options($actionID) |
32
|
|
|
{ |
33
|
|
|
switch ($actionID) { |
34
|
|
|
case 'config': |
35
|
|
|
$additionalOptions = ['level']; |
36
|
|
|
break; |
37
|
|
|
default: |
38
|
|
|
$additionalOptions = []; |
39
|
|
|
} |
40
|
|
|
return ArrayHelper::merge( |
41
|
|
|
$additionalOptions, |
42
|
|
|
parent::options($actionID) |
43
|
|
|
); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @inheritdoc |
48
|
|
|
*/ |
49
|
|
|
public function optionAliases() |
50
|
|
|
{ |
51
|
|
|
return ArrayHelper::merge( |
52
|
|
|
[ |
53
|
|
|
'l' => 'level' |
54
|
|
|
], |
55
|
|
|
parent::optionAliases() |
56
|
|
|
); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Shows application configuration |
61
|
|
|
* Note that this action shows console configuration. |
62
|
|
|
* |
63
|
|
|
* @param null $key configuration section |
64
|
|
|
*/ |
65
|
|
|
public function actionConfig($key = null) |
66
|
|
|
{ |
67
|
|
|
// get config from global variable (TODO) |
68
|
|
|
$data = $GLOBALS['config']; |
69
|
|
|
if ($key) { |
70
|
|
|
$keys = explode('.', $key); |
71
|
|
|
if (isset($keys[0])) { |
72
|
|
|
$data = $GLOBALS['config'][$keys[0]]; |
73
|
|
|
} |
74
|
|
|
if (isset($keys[1])) { |
75
|
|
|
$data = $GLOBALS['config'][$keys[0]][$keys[1]]; |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
$this->stdout(VarDumper::dumpAsString($data, $this->level)); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Shows application environment variables. |
83
|
|
|
*/ |
84
|
|
|
public function actionEnv() |
85
|
|
|
{ |
86
|
|
|
$env = $_ENV; |
87
|
|
|
ksort($env); |
88
|
|
|
$this->stdout(VarDumper::dumpAsString($env)); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Displays application version from APP_VERSION constant. |
93
|
|
|
*/ |
94
|
|
|
public function actionVersion() |
95
|
|
|
{ |
96
|
|
|
$this->stdout('Application Version: '); |
97
|
|
|
$this->stdout(getenv('APP_NAME') . ' '); |
98
|
|
|
$this->stdout(APP_VERSION . "\n"); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Initializes application. |
103
|
|
|
*/ |
104
|
|
|
public function actionSetup() |
105
|
|
|
{ |
106
|
|
|
$this->stdout("\n"); |
107
|
|
|
$this->stdout("phd application setup\n"); |
108
|
|
|
$this->stdout("=====================\n"); |
109
|
|
|
$this->stdout("Initializing application\n"); |
110
|
|
|
|
111
|
|
|
$this->interactive = (bool)getenv('APP_INTERACTIVE'); |
112
|
|
|
|
113
|
|
|
$this->stdout("\nDatabase\n"); |
114
|
|
|
$this->stdout("--------\n"); |
115
|
|
|
$this->run('db/create', [getenv('DB_ENV_MYSQL_ROOT_USER'),getenv('DB_ENV_MYSQL_ROOT_PASSWORD')]); |
116
|
|
|
$this->run('migrate/up', ['interactive' => (bool)getenv('APP_INTERACTIVE')]); |
117
|
|
|
|
118
|
|
|
$this->stdout("\nUser\n"); |
119
|
|
|
$this->stdout("----\n"); |
120
|
|
|
$adminPassword = $this->prompt( |
121
|
|
|
'Enter admin password', |
122
|
|
|
[ |
123
|
|
|
'default' => getenv('APP_ADMIN_PASSWORD') ?: Yii::$app->security->generateRandomString(8), |
124
|
|
|
] |
125
|
|
|
); |
126
|
|
|
$this->run('user/create', [getenv('APP_ADMIN_EMAIL'), 'admin', $adminPassword]); |
127
|
|
|
|
128
|
|
|
$this->stdout('Initializing modules'); |
129
|
|
|
|
130
|
|
|
$this->stdout("\n\nDone.\n"); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Clean cache, assets and audit tables |
135
|
|
|
*/ |
136
|
|
|
public function actionCleanup() |
137
|
|
|
{ |
138
|
|
|
$this->stdout("\nCleanup\n"); |
139
|
|
|
$this->stdout("-------\n"); |
140
|
|
|
$this->run('cache/flush-all'); |
141
|
|
|
$this->run('audit/cleanup', ['age' => 30, 'interactive' => (bool)getenv('APP_INTERACTIVE')]); |
142
|
|
|
$this->run('app/clear-assets', ['interactive' => (bool)getenv('APP_INTERACTIVE')]); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Clear [application]/web/assets folder. |
147
|
|
|
*/ |
148
|
|
|
public function actionClearAssets() |
149
|
|
|
{ |
150
|
|
|
$assets = Yii::getAlias('@web/assets'); |
151
|
|
|
|
152
|
|
|
// Matches from 7-8 char folder names, the 8. char is optional |
153
|
|
|
$matchRegex = '"^[a-z0-9][a-z0-9][a-z0-9][a-z0-9][a-z0-9][a-z0-9][a-z0-9]\?[a-z0-9]$"'; |
154
|
|
|
|
155
|
|
|
// create $cmd command |
156
|
|
|
$cmd = 'cd "' . $assets . '" && ls | grep -e ' . $matchRegex . ' | xargs rm -rf '; |
157
|
|
|
|
158
|
|
|
// Set command |
159
|
|
|
$command = new Command($cmd); |
160
|
|
|
|
161
|
|
|
// Prompt user |
162
|
|
|
$delete = $this->confirm("\nDo you really want to delete web assets?", ['default' => true]); |
163
|
|
|
|
164
|
|
|
if ($delete) { |
165
|
|
|
// Try to execute $command |
166
|
|
|
if ($command->execute()) { |
167
|
|
|
$this->stdout("Web assets have been deleted.\n\n"); |
168
|
|
|
} else { |
169
|
|
|
$this->stderr("\n" . $command->getError() . "\n"); |
170
|
|
|
$this->stderr($command->getStdErr()); |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
public function actionTestMail($to) |
176
|
|
|
{ |
177
|
|
|
$mailer = Yii::$app->mailer; |
178
|
|
|
|
179
|
|
|
$message = $mailer->compose(); |
180
|
|
|
$message |
181
|
|
|
->setTo($to) |
182
|
|
|
->setSubject('Test-Mail from ' . getenv('APP_NAME')) |
183
|
|
|
->setTextBody(getenv('APP_TITLE') . ' | ' . getenv('HOSTNAME')); |
184
|
|
|
|
185
|
|
|
if ($message->send()) { |
186
|
|
|
$this->stdout('Mail sent'); |
187
|
|
|
} else { |
188
|
|
|
$this->stderr('Mail sending failed'); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
$this->stdout(PHP_EOL); |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|