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