colly_wyx
2018-04-23 0c4b798032fd08f562570201c0ae6ba1d040cfba
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
<?php
define('PHALAPI_INSTALL', TRUE);
define('D_S', DIRECTORY_SEPARATOR);
 
$step = isset($_GET['step']) ? intval($_GET['step']) : 0;
 
switch ($step) {
    //第一步:环境检测
case 1:
    if (file_exists('_install.lock')) {
        $error = '项目已安装,请不要重复安装,并建议手动删除 ./install 此目录以及目录下的全部文件';
        include dirname(__FILE__) . D_S . '_error.php';
        exit(0);
    }
    //-1:必须但不支持 0:可选但不支持 1:完美支持
    $checkList = array(
        'php'       => array('name' => 'PHP 版本', 'status' => -1, 'tip' => '建议使用PHP 5.3.3及以上版本,否则DI无法支持匿名函数'),
        'pdo'       => array('name' => '数据库模块', 'status' => -1, 'tip' => '建议使用PDO扩展,否则NotORM无法使用PDO进行数据库操作'),
        'memcache'  => array('name' => 'Memcache扩展', 'status' => 0, 'tip' => '无此扩展时,不能使用Memcache缓存'),
        'mcrypt'    => array('name' => 'Mcrypt扩展', 'status' => 0, 'tip' => '无此扩展时,不能使用mcrypt进行加密处理'),
        'runtime'   => array('name' => '目录权限', 'status' => -1, 'tip' => '根目录、日志及配置文件目录若缺少写入权限,则不能写入日记和进行文件缓存以及接下配置无法生效'),
    );
 
    if (version_compare(PHP_VERSION, '5.3.3', '>=')) {
        $checkList['php']['status'] = 1;
    }
    if (class_exists('PDO', false) && extension_loaded('PDO')) {
        $checkList['pdo']['status'] = 1;
    }
    if (class_exists('Memcache', false) && extension_loaded('memcache')) {
        $checkList['memcache']['status'] = 1;
    }
    if (extension_loaded('mcrypt')) {
        $checkList['mcrypt']['status'] = 1;
    }
    $runtimePath = dirname(__FILE__) . implode(D_S, array('', '..', '..', 'Runtime'));
    $runtimePath = file_exists($runtimePath) ? realpath($runtimePath) : $runtimePath;
    $checkList['runtime']['tip'] = $runtimePath . '<br>' . $checkList['runtime']['tip'];
 
    $configPath = dirname(__FILE__) . implode(D_S, array('', '..', '..', 'Config'));
    $configPath = file_exists($configPath) ? realpath($configPath) : $configPath;
    $checkList['runtime']['tip'] = $configPath . '<br>' . $checkList['runtime']['tip'];
 
    $publicPath = dirname(__FILE__) . implode(D_S, array('', '..', '..', 'Public'));
    $publicPath = file_exists($publicPath) ? realpath($publicPath) : $publicPath;
    $checkList['runtime']['tip'] = $publicPath . '<br>' . $checkList['runtime']['tip'];
 
    if (is_writeable($runtimePath) && is_writeable($configPath) && is_writable($publicPath)) {
        $checkList['runtime']['status'] =  1;
    }
 
    include dirname(__FILE__) . D_S . '_step1.php';
    break;
    //第2步:系统配置
case 2:
    include dirname(__FILE__) . D_S . '_step2.php';
    break;
    //第3步:接口请求
case 3:
    if (empty($_POST['doSubmit']) || empty($_POST)) {
        header('Location: ./?step=1');
        exit(0);
    }
 
    //数据库配置文件
    $search = array(
        '{project}',
        '{host}',
        '{name}',
        '{user}',
        '{password}',
        '{port}',
        '{charset}',
        '{prefix}',
    );
    $replace = array(
        strtolower($_POST['project']),
        $_POST['host'],
        $_POST['name'],
        $_POST['user'],
        $_POST['password'],
        $_POST['port'],
        $_POST['charset'],
        $_POST['prefix'],
    );
    $configDbsContent = str_replace($search, $replace, getConfigDbsTpl());
    file_put_contents(
        dirname(__FILE__) . implode(D_S, array('', '..', '..', 'Config', 'dbs.php')),
        $configDbsContent
    );
 
    //Project
    $project = ucwords($_POST['project']);
    $appPath = dirname(__FILE__) . implode(D_S, array('', '..', '..', $project,));
    $demoPath = dirname(__FILE__) . implode(D_S, array('', '..', '..', 'Demo',));
    if (!file_exists($appPath)) {
        //项目目录
        mkdir($appPath . D_S);
        mkdir($appPath . D_S . 'Api');
        mkdir($appPath . D_S . 'Domain');
        mkdir($appPath . D_S . 'Model');
        mkdir($appPath . D_S . 'Common');
 
        copy(
            $demoPath . D_S . 'Api' . D_S . 'Default.php',
            $appPath . D_S . 'Api' . D_S . 'Default.php'
        );
 
        mkdir($appPath . D_S . 'Tests');
        mkdir($appPath . D_S . 'Tests' . D_S . 'Api');
        mkdir($appPath . D_S . 'Tests' . D_S . 'Domain');
        mkdir($appPath . D_S . 'Tests' . D_S . 'Model');
        mkdir($appPath . D_S . 'Tests' . D_S . 'Common');
 
        //单元测试
        copy(
            $demoPath . D_S . 'Tests' . D_S . 'test_env.php',
            $appPath . D_S . 'Tests'  . D_S . 'test_env.php'
        );
        file_put_contents(
            $appPath . D_S . 'Tests'  . D_S . 'test_env.php',
            str_replace('Demo', $project, file_get_contents($appPath . D_S . 'Tests'  . D_S . 'test_env.php'))
        );
 
        copy(
            $demoPath . D_S . 'Tests' . D_S . 'Api' . D_S . 'Api_Default_Test.php',
            $appPath . D_S . 'Tests' . D_S . 'Api' . D_S . 'Api_Default_Test.php'
        );
        copy(
            $demoPath . D_S . 'Tests' . D_S . 'phpunit.xml',
            $appPath . D_S . 'Tests' . D_S . 'phpunit.xml'
        );
 
        //访问入口
        $appPublicPath = dirname(__FILE__) . implode(D_S, array('', '..', strtolower($project), ));
        $demoPublicPath = dirname(__FILE__) . implode(D_S, array('', '..', 'demo',));
 
        mkdir($appPublicPath);
 
        copy(
            $demoPublicPath . D_S . 'checkApiParams.php',
            $appPublicPath . D_S . 'checkApiParams.php'
        );
        copy(
            $demoPublicPath . D_S . 'listAllApis.php',
            $appPublicPath . D_S . 'listAllApis.php'
        );
        copy(
            $demoPublicPath . D_S . 'index.php',
            $appPublicPath . D_S . 'index.php'
        );
 
        // 挂载项目
        foreach (array('checkApiParams.php', 'listAllApis.php', 'index.php') as $publicFile) {
            file_put_contents(
                $appPublicPath . D_S . $publicFile,
                str_replace('Demo', $project, file_get_contents($demoPublicPath . D_S . $publicFile))
            );
        }
    }
 
    @touch('_install.lock');
 
    //请求链接
    $relatePath = substr($_SERVER['REQUEST_URI'], 0, stripos($_SERVER['REQUEST_URI'], '/install/'));
    $apiUrl = 'http://' . $_SERVER['HTTP_HOST'] . '/' . $relatePath . '/' . strtolower($project);
    include dirname(__FILE__) . D_S . '_step3.php';
    break;
default:
    include dirname(__FILE__) . D_S . '_start.php';
}
 
 
function getConfigDbsTpl() {
    $configDbs = <<<EOT
<?php
/**
 * 分库分表的自定义数据库路由配置
 */
 
return array(
    /**
     * DB数据库服务器集群
     */
    'servers' => array(
        'db_{project}' => array(                   //服务器标记
            'host'      => '{host}',               //数据库域名
            'name'      => '{name}',               //数据库名字
            'user'      => '{user}',               //数据库用户名
            'password'  => '{password}',           //数据库密码
            'port'      => '{port}',               //数据库端口
            'charset'   => '{charset}',            //数据库字符集
        ),
    ),
 
    /**
     * 自定义路由表
     */
    'tables' => array(
        //通用路由
        '__default__' => array(
            'prefix' => '{prefix}',
            'key' => 'id',
            'map' => array(
                array('db' => 'db_{project}'),
            ),
        ),
 
        /**
        'demo' => array(                                                //表名
            'prefix' => '{prefix}',                                     //表名前缀
            'key' => 'id',                                              //表主键名
            'map' => array(                                             //表路由配置
                array('db' => 'db_{project}'),                               //单表配置:array('db' => 服务器标记)
                array('start' => 0, 'end' => 2, 'db' => 'db_{project}'),     //分表配置:array('start' => 开始下标, 'end' => 结束下标, 'db' => 服务器标记)
            ),
        ),
         */
    ),
);
 
EOT;
 
    return $configDbs;
}