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