Completed
Push — master ( 628ca0...425df1 )
by Tobias
12:24
created

AppController::actionInitModules()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
cc 3
nc 3
nop 0
1
<?php
2
3
namespace app\commands;
4
5
use dmstr\helpers\Metadata;
6
use mikehaertl\shellcommand\Command;
7
use yii\console\Controller;
8
use yii\helpers\VarDumper;
9
use Yii;
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
     * Shows application configuration
25
     * Note that this action shows console configuration.
26
     *
27
     * @param null $key configuration section
28
     */
29
    public function actionConfig($key = null)
30
    {
31
        // get config from global variable (TODO)
32
        $data = $GLOBALS['config'];
33
        if ($key) {
34
            $keys = explode('.', $key);
35
            if (isset($keys[0])) {
36
                $data = $GLOBALS['config'][$keys[0]];
37
            }
38
            if (isset($keys[1])) {
39
                $data = $GLOBALS['config'][$keys[0]][$keys[1]];
40
            }
41
        }
42
        $this->stdout(VarDumper::dumpAsString($data));
43
    }
44
45
    /**
46
     * Shows application environment variables.
47
     */
48
    public function actionEnv()
49
    {
50
        $env = $_ENV;
51
        ksort($env);
52
        $this->stdout(VarDumper::dumpAsString($env));
53
    }
54
55
    /**
56
     * Displays application version from APP_VERSION constant.
57
     */
58
    public function actionVersion()
59
    {
60
        $this->stdout('Application Version: ');
61
        $this->stdout(getenv('APP_NAME') . ' ');
62
        $this->stdout(APP_VERSION);
63
        echo "\n";
64
    }
65
66
    /**
67
     * Initializes application.
68
     */
69
    public function actionSetup()
70
    {
71
        $this->stdout("\n");
72
        $this->stdout("phd application setup\n");
73
        $this->stdout("=====================\n");
74
        $this->stdout("Initializing application\n");
75
76
        $this->interactive = (bool)getenv('APP_INTERACTIVE');
77
78
        $this->stdout("\nDatabase\n");
79
        $this->stdout("--------\n");
80
        $this->run('db/create');
81
        $this->run('migrate/up', ['interactive' => (bool)getenv('APP_INTERACTIVE')]);
82
83
        $this->stdout("\nUser\n");
84
        $this->stdout("----\n");
85
        $adminPassword = $this->prompt(
86
            'Enter admin password',
87
            [
88
                'default' => getenv('APP_ADMIN_PASSWORD') ?: \Yii::$app->security->generateRandomString(8),
89
            ]
90
        );
91
        $this->run('user/create', [getenv('APP_ADMIN_EMAIL'), 'admin', $adminPassword]);
92
93
        $this->stdout('Initializing modules');
94
95
        $this->stdout("\n\nDone.\n");
96
    }
97
98
    /**
99
     * Call init() of non-loaded modules
100
     */
101
    public function actionInitModules()
102
    {
103
        $this->stdout('Initializing modules...' . PHP_EOL);
104
        foreach (Yii::$app->getModules() as $name => $module) {
105
            if (is_array($module)) {
106
                $this->stdout("{$name}");
107
                Yii::$app->getModule($name)->init();
108
                $this->stdout(" [OK]" . PHP_EOL);
109
            }
110
        }
111
        $this->stdout(PHP_EOL . "Done." . PHP_EOL);
112
    }
113
114
    /**
115
     * Clean cache, assets and audit tables
116
     */
117
    public function actionCleanup()
118
    {
119
        $this->stdout("\nCleanup\n");
120
        $this->stdout("-------\n");
121
        $this->run('cache/flush-all');
122
        $this->run('audit/cleanup', ['age' => 30, 'interactive' => (bool)getenv('APP_INTERACTIVE')]);
123
        $this->run('app/clear-assets', ['interactive' => (bool)getenv('APP_INTERACTIVE')]);
124
    }
125
126
    /**
127
     * Clear [application]/web/assets folder.
128
     */
129
    public function actionClearAssets()
130
    {
131
        $assets = \Yii::getAlias('@web/assets');
132
133
        // Matches from 7-8 char folder names, the 8. char is optional
134
        $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]$"';
135
136
        // create $cmd command
137
        $cmd = 'cd "' . $assets . '" && ls | grep -e ' . $matchRegex . ' | xargs rm -rf ';
138
139
        // Set command
140
        $command = new Command($cmd);
141
142
        // Prompt user
143
        $delete = $this->confirm("\nDo you really want to delete web assets?", ['default' => true]);
144
145
        if ($delete) {
146
            // Try to execute $command
147
            if ($command->execute()) {
148
                echo "Web assets have been deleted.\n\n";
149
            } else {
150
                echo "\n" . $command->getError() . "\n";
151
                echo $command->getStdErr();
152
            }
153
        }
154
    }
155
}
156