Completed
Pull Request — master (#204)
by
unknown
06:57
created

AppController::actionClearAssets()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 26
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 26
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 12
nc 3
nop 0
1
<?php
2
3
namespace app\commands;
4
5
/*
6
 * @link http://www.diemeisterei.de/
7
 *
8
 * @copyright Copyright (c) 2014 diemeisterei GmbH, Stuttgart
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
use dektrium\user\Finder;
15
use mikehaertl\shellcommand\Command;
16
use yii\console\Controller;
17
18
/**
19
 * Task runner command for development.
20
 *
21
 * @author Tobias Munk <[email protected]>
22
 */
23
class AppController extends Controller
24
{
25
    public $defaultAction = 'version';
26
27
    /**
28
     * Displays application version from ENV variable (read from version file).
29
     */
30
    public function actionVersion()
31
    {
32
        $this->stdout(\Yii::$app->id.' version '.APP_VERSION);
33
        $this->stdout("\n");
34
    }
35
36
    /**
37
     * Setup admin user (create, update password, confirm).
38
     */
39
    public function actionSetupAdminUser()
40
    {
41
        $finder = \Yii::$container->get(Finder::className());
42
        $admin = $finder->findUserByUsername('admin');
43
        if ($admin === null) {
44
            $email = $this->prompt(
45
                'E-Mail for application admin user:',
46
                ['default' => getenv('APP_ADMIN_EMAIL')]
47
            );
48
            $this->action('user/create', [$email, 'admin']);
49
            $password = $this->prompt(
50
                'Password for application admin user:',
51
                ['default' => getenv('APP_ADMIN_PASSWORD')]
52
            );
53
        } else {
54
            $password = $this->prompt(
55
                'Update password for application admin user (leave empty to skip):'
56
            );
57
        }
58
        if ($password) {
59
            $this->action('user/password', ['admin', $password]);
60
        }
61
        sleep(1); // confirmation may not succeed without a short pause
62
        $this->action('user/confirm', ['admin']);
63
    }
64
65
    /**
66
     * Clear [application]/web/assets folder.
67
     */
68
    public function actionClearAssets()
69
    {
70
        $assets = \Yii::getAlias('@web/assets');
71
72
        // Matches from 7-8 char folder names, the 8. char is optional
73
        $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]$"';
74
75
        // create $cmd command
76
        $cmd = 'cd "'.$assets.'" && ls | grep -e '.$matchRegex.' | xargs rm -rf ';
77
78
        // Set command
79
        $command = new Command($cmd);
80
81
        // Prompt user
82
        $delete = $this->confirm("\nDo you really want to delete web assets?", true);
83
84
        if ($delete) {
85
            // Try to execute $command
86
            if ($command->execute()) {
87
                echo "Web assets have been deleted.\n\n";
88
            } else {
89
                echo "\n".$command->getError()."\n";
90
                echo $command->getStdErr();
91
            }
92
        }
93
    }
94
95
    /**
96
     * Generate application and required vendor documentation.
97
     */
98
    public function actionGenerateDocs()
99
    {
100
        if ($this->confirm('Regenerate documentation files into ./docs-html', true)) {
101
102
            // array with commands
103
            $commands = [];
104
            $commands[] = 'vendor/bin/apidoc guide --interactive=0 docs web/apidocs';
105
            $commands[] = 'vendor/bin/apidoc api --interactive=0 --exclude=runtime/,tests/ src,vendor/schmunk42 web/apidocs';
106
            $commands[] = 'vendor/bin/apidoc guide --interactive=0 docs web/apidocs';
107
108
            foreach ($commands as $command) {
109
                $cmd = new Command($command);
110
                if ($cmd->execute()) {
111
                    echo $cmd->getOutput();
112
                } else {
113
                    echo $cmd->getOutput();
114
                    echo $cmd->getStdErr();
115
                    echo $cmd->getError();
116
                }
117
            }
118
        }
119
    }
120
121
    /**
122
     * @param string $command
123
     */
124
    protected function action($command, $params = [])
125
    {
126
        echo "\nRunning action '$command'...\n";
127
        \Yii::$app->runAction($command, $params);
128
    }
129
}
130