-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate
executable file
·153 lines (130 loc) · 3.37 KB
/
update
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/usr/bin/env php
<?php
const MODULE_BLACKS = [
'/\./',
'/\.\./',
'/\.(.+)$/',
'/3rd/',
'/tmp/',
'/logs/',
'/run/',
'/doc/'
];
function IsBlack($path)
{
foreach (MODULE_BLACKS as $b) {
if (preg_match($b, $path)) {
return true;
}
}
return false;
}
function ListModules()
{
$iter = opendir('./');
while (($each = readdir($iter)) != false) {
if (!is_dir($each))
continue;
if (IsBlack($each))
continue;
$ret[] = $each;
}
return $ret;
}
// 遍历根文件夹,获取业务模块列表
$MODULES = ListModules();
// 构造自动加载类
spl_autoload_register(function ($name) {
$cmps = explode('\\', $name);
for ($i = 0, $l = count($cmps) - 1; $i < $l; ++$i) {
$cmps[$i] = strtolower($cmps[$i]);
}
$path = implode(DIRECTORY_SEPARATOR, $cmps);
include_once '.' . DIRECTORY_SEPARATOR . $path . '.php';
return true;
});
// 处理 app.php 获得配置信息
function GetConfig()
{
return include "./app.php";
}
$CONFIG = GetConfig();
// 更新每个模块的Index文件
function UpdateIndex($module)
{
// 不自动更新框架的入口
if ($module == 'nnt')
return;
// 读取目标模块手动设置的Application对象
$cnt = file_get_contents("./$module/index.php");
preg_match('/\$app = new (.+)\(/', $cnt, $result);
$appclazz = $result[1]; // 手动定制的入口app类型
// 构造模板
$cnt = '<?php
use Phalcon\Loader;
use Phalcon\Db\Adapter\Pdo\Factory as DbFactory;
define(\'MODULE_DIR\', __DIR__ . \'/\');
define(\'APP_DIR\', dirname(__DIR__) . \'/\');
$loader = new Loader();
$loader->registerNamespaces([
';
// 插入模块ns
global $MODULES;
foreach ($MODULES as $each) {
$Each = ucfirst($each);
$dir[] = " '$Each' => APP_DIR . '$each'";
if ($each == 'nnt') {
$dir[] = " '$Each\Core' => APP_DIR . '$each/core'";
$dir[] = " '$Each\Store' => APP_DIR . '$each/store'";
$dir[] = " '$Each\Sdks' => APP_DIR . '$each/sdks'";
}
$dir[] = " '$Each\Db' => APP_DIR . '$each/db'";
$dir[] = " '$Each\Model' => APP_DIR . '$each/model'";
$dir[] = " '$Each\Controller' => APP_DIR . '$each/controller'";
}
$cnt .= implode(",\n", $dir) . "\n]);";
$cnt .= '
$loader->registerDirs([
MODULE_DIR . \'controller\',
MODULE_DIR . \'model\'
]);
$loader->register();
$di = new \Nnt\Controller\Factory();
';
// 输出配置
$cfg[] = '
$di->setShared(\'config\', function () {
return include \'config/config.php\';
});';
global $CONFIG;
foreach ($CONFIG as $k => $v) {
switch ($k) {
case 'database':
{
$cfg[] = '
$di->setShared(\'db\', function () {
return DbFactory::load($this->getConfig()->database);
});';
}
break;
case 'redis':
{
$cfg[] = '
$di->setShared(\'redis\', function () {
return new \Nnt\Store\KvRedis($this->getConfig()->redis);
});';
}
break;
}
}
$cnt .= implode("\n", $cfg);
// 输出app
$cnt .= "
\$app = new $appclazz(\$di);
\$app->run();
";
file_put_contents("./$module/index.php", $cnt);
}
foreach ($MODULES as $mod) {
UpdateIndex($mod);
}