-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathApp.php
209 lines (182 loc) · 5.95 KB
/
App.php
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
<?php
/**
* App 基础类
*
* @package ultraman\Foundation
* @copyright Copyright (c) 2017, ultraman
* @var 基础服务入口文件
*/
namespace ultraman;
use ultraman\Foundation\DI;
use League\CLImate\CLImate;
use Symfony\Component\Console\Command\Command;
class App
{
/**
* 构造函数 入口加载配置文件进行注入
*
* @param db 数据库配置
* @param main 服务配置项
* @param api 外部接口配置项
*/
protected $climate;
protected $_service;
/**
* 入口文件加载
*/
public function __construct()
{
require dirname(dirname(__FILE__)."/../").'/vendor/autoload.php';
}
/**
* 服务器启动
*/
public function run($params)
{
$this->climate = new CLImate();
$item['service'] = $params[1]??'';
$item['command'] = $params[2]??'';
$item['env'] = $params[3]??'';
if ($item['service']!="-h" && $item['service']!="-t") {
return true;
}
DI::set("port", $params[4]??'');
$this->configure($item['env']);
if ($item['service'] == '--help') {
$this->help();
}
$this->_service = $item['service'];
if ($item['service'] == '-h') {
if (in_array($item['command'], ['start', 'stop', 'reload'])) {
call_user_func([$this, $item['command']]);
} else {
$this->help();
}
}
if ($item['service'] == '-t') {
if (in_array($item['command'], ['start', 'stop', 'reload'])) {
call_user_func([$this, $item['command']]);
} else {
$this->help();
}
}
$this->injection();
}
//启动
public function start()
{
$climate = $this->climate;
$climate->style->addCommand('rage', ['green','bold']);
$climate->br(1)->rage('Server is starting....');
if ($this->_service == '-h') {
$app = new \ultraman\Http\HttpYafServer();
} elseif ($this->_service == '-t') {
$app = new \ultraman\Tcp\SwooleServer();
}
$app->run();
}
//停止
private function stop()
{
$climate = $this->climate;
$climate->style->addCommand('rage', ['green','bold']);
$climate->br(1)->rage('Server is stopping....');
if ($this->_service == '-h') {
$app = new \ultraman\Http\HttpYafServer();
} elseif ($this->_service == '-t') {
$app = new \ultraman\Tcp\SwooleServer();
}
$app->stop();
$climate->br(1)->rage('Server is stopping....ok');
die;
}
//重载服务
public function reload()
{
$climate = $this->climate;
$climate->style->addCommand('rage', ['green','bold']);
$climate->br(1)->rage('Server is reloadping....');
if ($this->_service == '-h') {
$app = new \ultraman\Http\HttpYafServer();
} elseif ($this->_service == '-t') {
$app = new \ultraman\Tcp\SwooleServer();
}
$app->reload();
$climate->br(1)->rage('Server is reloadping....ok');
die;
}
//帮助
public function help()
{
$climate = $this->climate;
$climate->style->addCommand('rage', ['green','bold']);
$climate->br(2)->rage('欢迎使用');
$climate->addArt(dirname(dirname(__FILE__)."/../")."/Foundation/");
$climate->br(2)->draw('ultraman');
$climate->style->addCommand('holler', ['underline', 'green', 'bold']);
$climate->br(2)->holler('帮助');
$climate->br(1)->out('基础命令:<bold><green>php cli (-h|-t) (start|stop|reload) (env) (port)</green></bold> 启动');
$climate->br(1)->out('<bold><green>-h / -t:</green></bold> -h 启动http服务 -t 启动tcp服务');
$climate->br(1)->out('<bold><green>start / stop / reload:</green></bold> start 启动服务 stop 停止服务 reload重载服务');
$climate->br(1)->out('<bold><green>env:</green></bold> 环境区分建议为dev test pre prod');
$climate->br(1)->out('<bold><green>port:</green></bold> 环境端口 可不填写默认读取main.ini配置');
$climate->br(5);
die;
}
//环境注入
private function configure($env='')
{
$config = dirname(dirname(dirname(dirname(__FILE__))));
if ($env != '') {
$conf = dir($config.'/conf');
while (false != ($item = $conf->read())) {
if ($item == '.' || $item == '..') {
continue;
}
unlink($conf->path.'/'.$item);
}
$env = dir($config.'/env/'.$env);
while (false != ($item = $env->read())) {
if ($item == '.' || $item == '..') {
continue;
}
copy($env->path.'/'.$item, $conf->path.'/'.$item);
}
}
$conf = dir($config.'/conf');
while (false != ($item = $conf->read())) {
if ($item == '.' || $item == '..') {
continue;
}
$houzhui = substr(strrchr($item, '.'), 1);
$keys = basename($item, ".".$houzhui);
$params[$keys] = $conf->path.'/'.$item;
}
$this->injection($params);
return true;
}
/**
* 依赖文件注入
*/
public function injection($params=[])
{
foreach ($params as $key => $class) {
$f = pathinfo($class, PATHINFO_EXTENSION);
$conf = "";
if ($f === 'php') {
$conf = @include $class;
if ($conf == "") {
continue;
}
}
if ($f === 'ini') {
$conf = @parse_ini_file($class, true);
if ($conf == "") {
continue;
}
}
DI::set($key, $conf);
}
return true;
}
}