1
|
|
|
<?php |
2
|
|
|
// +---------------------------------------------------------------------- |
|
|
|
|
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 |
|
|
|
|
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
|
|
|
*/ |
|
|
|
|
37
|
|
|
protected function configure() |
38
|
|
|
{ |
39
|
|
|
$this->setName('build') |
40
|
|
|
->setDefinition([ |
|
|
|
|
41
|
|
|
new Option('config', null, Option::VALUE_OPTIONAL, "build.php path"), |
42
|
|
|
new Option('app', null, Option::VALUE_OPTIONAL, "app name"), |
43
|
|
|
]) |
|
|
|
|
44
|
|
|
->setDescription('Build Application Dirs'); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
protected function execute(Input $input, Output $output) |
|
|
|
|
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')); |
|
|
|
|
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
|
|
|
/** |
|
|
|
|
88
|
|
|
* 创建应用 |
89
|
|
|
* @access protected |
90
|
|
|
* @param string $name 应用名 |
|
|
|
|
91
|
|
|
* @param array $list 文件列表 |
|
|
|
|
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': // 控制器 |
|
|
|
|
132
|
|
|
if ($this->app->config->get('route.controller_suffix')) { |
|
|
|
|
133
|
|
|
$filename = $appPath . $path . DIRECTORY_SEPARATOR . $val . 'Controller.php'; |
134
|
|
|
$class = $val . 'Controller'; |
135
|
|
|
} |
|
|
|
|
136
|
|
|
$content = "<?php\nnamespace {$space};\n\nuse think\Controller;\n\nclass {$class} extends Controller\n{\n\n}"; |
137
|
|
|
break; |
138
|
|
|
case 'model': // 模型 |
|
|
|
|
139
|
|
|
$content = "<?php\nnamespace {$space};\n\nuse think\Model;\n\nclass {$class} extends Model\n{\n\n}"; |
140
|
|
|
break; |
141
|
|
|
case 'view': // 视图 |
|
|
|
|
142
|
|
|
$filename = $appPath . $path . DIRECTORY_SEPARATOR . $val . '.html'; |
143
|
|
|
$this->checkDirBuild(dirname($filename)); |
144
|
|
|
$content = ''; |
145
|
|
|
break; |
146
|
|
|
default: |
|
|
|
|
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 应用名 |
|
|
|
|
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
|
|
|
|