Passed
Push — 6.0 ( 41ffa0...e525de )
by liu
02:38
created

Build::buildCommon()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 3
nc 4
nop 1
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
// +----------------------------------------------------------------------
1 ignored issue
show
Coding Style introduced by
You must use "/**" style comments for a file comment
Loading history...
3
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
4
// +----------------------------------------------------------------------
5
// | Copyright (c) 2006~2015 http://thinkphp.cn All rights reserved.
6
// +----------------------------------------------------------------------
7
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
8
// +----------------------------------------------------------------------
9
// | Author: yunwuxin <[email protected]>
10
// +----------------------------------------------------------------------
11
12
namespace think\console\command;
13
14
use think\console\Command;
15
use think\console\Input;
16
use think\console\input\Option;
17
use think\console\Output;
18
19
class Build extends Command
1 ignored issue
show
Coding Style introduced by
Missing class doc comment
Loading history...
20
{
21
22
    /**
23
     * 应用基础目录
24
     * @var string
25
     */
26
    protected $basePath;
27
28
    /**
29
     * 应用目录
30
     * @var string
31
     */
32
    protected $appPath;
33
34
    /**
35
     * {@inheritdoc}
36
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
37
    protected function configure()
38
    {
39
        $this->setName('build')
40
            ->setDefinition([
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
41
                new Option('config', null, Option::VALUE_OPTIONAL, "build.php path"),
42
                new Option('app', null, Option::VALUE_OPTIONAL, "app name"),
43
            ])
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
44
            ->setDescription('Build Application Dirs');
45
    }
46
47
    protected function execute(Input $input, Output $output)
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
48
    {
49
        $this->basePath = $this->app->getBasePath();
50
        $this->appPath  = $this->app->getAppPath();
51
52
        if ($input->hasOption('app')) {
53
            $this->buildApp($input->getOption('app'));
0 ignored issues
show
Bug introduced by
It seems like $input->getOption('app') can also be of type think\console\input\Option; however, parameter $app of think\console\command\Build::buildApp() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

53
            $this->buildApp(/** @scrutinizer ignore-type */ $input->getOption('app'));
Loading history...
54
            $output->writeln("Successed");
55
            return;
56
        }
57
58
        if ($input->hasOption('config')) {
59
            $build = include $input->getOption('config');
60
        } else {
61
            $build = include $this->appPath . 'build.php';
62
        }
63
64
        if (empty($build)) {
65
            $output->writeln("Build Config Is Empty");
66
            return;
67
        }
68
69
        $this->build($build);
70
        $output->writeln("Successed");
71
    }
72
73
    /**
74
     * 根据配置文件创建应用和文件
75
     * @access protected
76
     * @param  array  $config 配置列表
77
     * @return void
78
     */
79
    protected function build(array $config): void
80
    {
81
        // 创建子目录和文件
82
        foreach ($config as $app => $list) {
83
            $this->buildApp(is_numeric($app) ? '' : $app, $list);
84
        }
85
    }
86
87
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $app should have a doc-comment as per coding-style.
Loading history...
88
     * 创建应用
89
     * @access protected
90
     * @param  string $name 应用名
0 ignored issues
show
Coding Style introduced by
Doc comment for parameter $name does not match actual variable name $app
Loading history...
Coding Style introduced by
Expected 10 spaces after parameter name; 1 found
Loading history...
91
     * @param  array  $list 文件列表
0 ignored issues
show
Coding Style introduced by
Expected 10 spaces after parameter name; 1 found
Loading history...
92
     * @param  string $rootNamespace 应用类库命名空间
93
     * @return void
94
     */
95
    protected function buildApp(string $app, array $list = [], string $rootNamespace = 'app'): void
96
    {
97
        if (!is_dir($this->basePath . $app)) {
98
            // 创建应用目录
99
            mkdir($this->basePath . $app);
100
        }
101
102
        $appPath   = $this->basePath . ($app ? $app . DIRECTORY_SEPARATOR : '');
103
        $namespace = $rootNamespace . ($app ? '\\' . $app : '');
104
105
        // 创建配置文件和公共文件
106
        $this->buildCommon($app);
107
        // 创建模块的默认页面
108
        $this->buildHello($app, $namespace);
109
110
        foreach ($list as $path => $file) {
111
            if ('__dir__' == $path) {
112
                // 生成子目录
113
                foreach ($file as $dir) {
114
                    $this->checkDirBuild($appPath . $dir);
115
                }
116
            } elseif ('__file__' == $path) {
117
                // 生成(空白)文件
118
                foreach ($file as $name) {
119
                    if (!is_file($appPath . $name)) {
120
                        file_put_contents($appPath . $name, 'php' == pathinfo($name, PATHINFO_EXTENSION) ? "<?php\n" : '');
121
                    }
122
                }
123
            } else {
124
                // 生成相关MVC文件
125
                foreach ($file as $val) {
126
                    $val      = trim($val);
127
                    $filename = $appPath . $path . DIRECTORY_SEPARATOR . $val . '.php';
128
                    $space    = $namespace . '\\' . $path;
129
                    $class    = $val;
130
                    switch ($path) {
131
                        case 'controller': // 控制器
1 ignored issue
show
Coding Style introduced by
Line indented incorrectly; expected 20 spaces, found 24
Loading history...
132
                            if ($this->app->config->get('route.controller_suffix')) {
1 ignored issue
show
Coding Style introduced by
Line indented incorrectly; expected 24 spaces, found 28
Loading history...
133
                                $filename = $appPath . $path . DIRECTORY_SEPARATOR . $val . 'Controller.php';
134
                                $class    = $val . 'Controller';
135
                            }
1 ignored issue
show
Coding Style introduced by
Line indented incorrectly; expected 24 spaces, found 28
Loading history...
136
                            $content = "<?php\nnamespace {$space};\n\nuse think\Controller;\n\nclass {$class} extends Controller\n{\n\n}";
137
                            break;
138
                        case 'model': // 模型
1 ignored issue
show
Coding Style introduced by
Line indented incorrectly; expected 20 spaces, found 24
Loading history...
139
                            $content = "<?php\nnamespace {$space};\n\nuse think\Model;\n\nclass {$class} extends Model\n{\n\n}";
140
                            break;
141
                        case 'view': // 视图
1 ignored issue
show
Coding Style introduced by
Line indented incorrectly; expected 20 spaces, found 24
Loading history...
142
                            $filename = $appPath . $path . DIRECTORY_SEPARATOR . $val . '.html';
143
                            $this->checkDirBuild(dirname($filename));
144
                            $content = '';
145
                            break;
146
                        default:
1 ignored issue
show
Coding Style introduced by
Line indented incorrectly; expected 20 spaces, found 24
Loading history...
147
                            // 其他文件
148
                            $content = "<?php\nnamespace {$space};\n\nclass {$class}\n{\n\n}";
149
                    }
150
151
                    if (!is_file($filename)) {
152
                        file_put_contents($filename, $content);
153
                    }
154
                }
155
            }
156
        }
157
    }
158
159
    /**
160
     * 创建应用的欢迎页面
161
     * @access protected
162
     * @param  string $appName 应用名
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter name; 1 found
Loading history...
163
     * @param  string $namespace 应用类库命名空间
164
     * @return void
165
     */
166
    protected function buildHello(string $appName, string $namespace): void
167
    {
168
        $suffix   = $this->app->config->get('route.controller_suffix') ? 'Controller' : '';
169
        $filename = $this->basePath . ($appName ? $appName . DIRECTORY_SEPARATOR : '') . 'controller' . DIRECTORY_SEPARATOR . 'Index' . $suffix . '.php';
170
        if (!is_file($filename)) {
171
            $content = file_get_contents($this->app->getThinkPath() . 'tpl' . DIRECTORY_SEPARATOR . 'default_index.tpl');
172
            $content = str_replace(['{$app}', '{layer}', '{$suffix}'], [$namespace, 'controller', $suffix], $content);
173
            $this->checkDirBuild(dirname($filename));
174
175
            file_put_contents($filename, $content);
176
        }
177
    }
178
179
    /**
180
     * 创建应用的公共文件
181
     * @access protected
182
     * @param  string $appName 应用名称
183
     * @return void
184
     */
185
    protected function buildCommon(string $appName): void
186
    {
187
        $filename = $this->basePath . ($appName ? $appName . DIRECTORY_SEPARATOR : '') . 'common.php';
188
189
        if (!is_file($filename)) {
190
            file_put_contents($filename, "<?php\n");
191
        }
192
    }
193
194
    /**
195
     * 创建目录
196
     * @access protected
197
     * @param  string $dirname 目录名称
198
     * @return void
199
     */
200
    protected function checkDirBuild(string $dirname): void
201
    {
202
        if (!is_dir($dirname)) {
203
            mkdir($dirname, 0755, true);
204
        }
205
    }
206
}
207