Skip to content

Commit dad5e29

Browse files
authored
Merge pull request #5 from xudianyang/master
+
2 parents 49a3f5a + 6024f72 commit dad5e29

10 files changed

Lines changed: 190 additions & 144 deletions

File tree

README.md

Lines changed: 64 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,35 +23,80 @@ PHP微服务框架即“Micro Service Framework For PHP”,是Camera360社区
2323
* 支持独立进程的定时器
2424
* 支持独立配置进程
2525

26-
## 安装
26+
## 环境要求
27+
28+
- Linux,FreeBSD,MacOS(有兼容问题)
29+
- Linux内核版本2.3.32以上(支持epoll)
30+
- PHP-7.0及以上版本(生产环境建议使用PHP-7.1)
31+
- gcc-4.4以上版本
32+
- [swoole-1.9.15](https://github.com/swoole/swoole-src/archive/v1.9.15.tar.gz)及以上版本(暂不支持Swoole-2.0)
33+
- [hiredis-0.13.3](https://github.com/redis/hiredis/archive/v0.13.3.tar.gz)
34+
- [yac](https://github.com/laruence/yac/archive/yac-2.0.2.tar.gz)
35+
- [phpredis](http://pecl.php.net/get/redis-3.1.2.tgz)
36+
- composer
2737

28-
推荐安装方式,编辑项目的composer.json,加入依赖`pinguo/php-msf`
2938

30-
```json
31-
{
32-
"require": {
33-
"pinguo/php-msf": "dev-master"
34-
}
35-
}
39+
## 快速起步
40+
41+
```bash
42+
$>curl -sS https://cdn.rawgit.com/pinguo/php-msf-docker/ee580877/installer.php | php
3643
```
3744

38-
或者
45+
`installer.php`会检查运行环境,根据你的自定义配置,自动创建项目模板,composer安装依赖,启动服务。
46+
47+
如果一切顺利,运行到最后你将看到如下的输出:
3948

4049
```bash
41-
$composer require pinguo/php-msf
50+
[2017-09-06 16:08:34] Run composer install success
51+
[2017-09-06 16:08:34] Congratulations, all are installed successfully!
52+
[2017-09-06 16:08:34] You can, visit http://127.0.0.1:8990/Welcome for test
53+
_______ ____
54+
________ / /_ ____ ____ ___ _____/ __/
55+
___/ __ \/ __ \/ __ \______/ __ `__ \/ ___/ /_
56+
__/ /_/ / / / / /_/ /_____/ / / / / (__ ) __/
57+
_/ .___/_/ /_/ .___/ /_/ /_/ /_/____/_/
58+
/_/ /_/ Camera360 Open Source TM
59+
[2017-09-06 16:08:34] Swoole Version: 1.9.18
60+
[2017-09-06 16:08:34] PHP Version: 7.1.8
61+
[2017-09-06 16:08:34] Application ENV: docker
62+
[2017-09-06 16:08:34] Listen Addr: 0.0.0.0
63+
[2017-09-06 16:08:34] Listen Port: 8990
4264
```
4365

44-
更多参考[DEMO](https://github.com/pinguo/php-msf-demo)
66+
访问测试:
4567

46-
## 环境要求
68+
```bash
69+
$>curl http://127.0.0.1:8990/Welcome
70+
hello world!
71+
```
4772

48-
- Linux,FreeBSD,MacOS(有兼容问题)
49-
- Linux内核版本2.3.32以上(支持epoll)
50-
- PHP-7.0及以上版本(推荐使用PHP-7.1)
51-
- gcc-4.4以上版本
52-
- [swoole-1.9.15](https://github.com/swoole/swoole-src/archive/v1.9.15.tar.gz)及以上版本(暂不支持Swoole-2.0)
53-
- [hiredis-0.13.3](https://github.com/redis/hiredis/archive/v0.13.3.tar.gz)
54-
- composer
73+
注意端口,如果你不是8990,你需要修改,然后访问测试。
74+
75+
76+
调试模式
77+
78+
```bash
79+
$>./server.php start
80+
```
81+
82+
Daemon模式
83+
84+
```bash
85+
$>./server.php start -d
86+
```
87+
88+
89+
停止服务
90+
91+
```bash
92+
$>./server.php stop
93+
```
94+
95+
重启服务
96+
97+
```bash
98+
$>./server.php restart
99+
```
55100

56101
## 定位
57102

composer.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
"pinguo/php-exception": "dev-master",
1414
"pinguo/plates": "dev-master",
1515
"hassankhan/config": "~0.10",
16-
"alcaeus/mongo-php-adapter": "^1.0",
1716
"flexihash/flexihash": "^2.0.0"
1817
},
1918
"autoload": {

src/Base/Output.php

Lines changed: 75 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -164,41 +164,82 @@ public function set(&$request, &$response = null)
164164
*/
165165
public function setStatusHeader($code = 200)
166166
{
167-
$this->response->status($code);
167+
if (empty(self::$codes[$code])) {
168+
$code = 500;
169+
}
170+
171+
if (!empty($this->response)) {
172+
$this->response->status($code);
173+
}
174+
168175
return $this;
169176
}
170177

171178
/**
172-
* 响应json格式数据
179+
* 设置响应的Content-Type报头
180+
*
181+
* @param string $mime_type Content-Type的值,如application/json;charset=utf-8
182+
* @return $this
183+
*/
184+
public function setContentType($mime_type)
185+
{
186+
$this->setHeader('Content-Type', $mime_type);
187+
return $this;
188+
}
189+
190+
/**
191+
* 设置响应的其他HTTP报头
192+
*
193+
* @param string $key HTTP报头Key
194+
* @param string $value HTTP报头Value
195+
* @return $this
196+
*/
197+
public function setHeader($key, $value)
198+
{
199+
$this->response->header($key, $value);
200+
return $this;
201+
}
202+
203+
/**
204+
* 设置HTTP响应的cookie信息,与PHP的setcookie()参数一致
205+
*
206+
* @param string $key Cookie名称
207+
* @param string $value Cookie值
208+
* @param int $expire 有效时间
209+
* @param string $path 有效路径
210+
* @param string $domain 有效域名
211+
* @param bool $secure Cookie是否仅仅通过安全的HTTPS连接传给客户端
212+
* @param bool $httponly 设置成TRUE,Cookie仅可通过HTTP协议访问
213+
* @return $this
214+
*/
215+
public function setCookie(
216+
$key,
217+
$value = '',
218+
$expire = 0,
219+
$path = '/',
220+
$domain = '',
221+
$secure = false,
222+
$httponly = false
223+
) {
224+
$this->response->cookie($key, $value, $expire, $path, $domain, $secure, $httponly);
225+
return $this;
226+
}
227+
228+
/**
229+
* 响应原始数据
173230
*
174231
* @param mixed|null $data 响应数据
175-
* @param string $message 响应提示
176232
* @param int $status 响应HTTP状态码
177-
* @param callable|null $callback jsonp参数名
178233
*/
179-
public function outputJson($data = null, $message = '', $status = 200, $callback = null)
234+
public function output($data = null, $status = 200)
180235
{
181236
$this->getContext()->getLog()->pushLog('status', $status);
182237

183-
$result = [
184-
'data' => $data,
185-
'status' => $status,
186-
'message' => empty($message) ? 'success' : $message,
187-
'serverTime' => microtime(true),
188-
];
189-
190238
switch ($this->controller->requestType) {
191239
case Marco::HTTP_REQUEST:
192-
$callback = $this->getCallback($callback);
193-
if (!is_null($callback)) {
194-
$output = $callback . '(' . json_encode($result) . ');';
195-
} else {
196-
$output = json_encode($result);
197-
}
198-
199240
if (!empty($this->response)) {
200-
$this->setContentType('application/json; charset=UTF-8');
201-
$this->end($output);
241+
$this->setStatusHeader($status);
242+
$this->end($data);
202243
}
203244
break;
204245
case Marco::TCP_REQUEST:
@@ -207,6 +248,19 @@ public function outputJson($data = null, $message = '', $status = 200, $callback
207248
}
208249
}
209250

251+
/**
252+
* 响应json格式数据
253+
*
254+
* @param mixed|null $data 响应数据
255+
* @param int $status 响应HTTP状态码
256+
*/
257+
public function outputJson($data = null, $status = 200)
258+
{
259+
$this->setContentType('application/json; charset=UTF-8');
260+
$data = json_encode($data);
261+
$this->output($data, $status);
262+
}
263+
210264
/**
211265
* 通过模板引擎响应输出HTML
212266
*
@@ -247,80 +301,6 @@ public function outputView(array $data, $view = null)
247301
$this->end($response);
248302
}
249303

250-
/**
251-
* 获取jsonp的callback名称
252-
*
253-
* @param string $callback jsonp的callback参数名称
254-
* @return string
255-
*/
256-
public function getCallback($callback)
257-
{
258-
$input = $this->getContext()->getInput();
259-
if (is_null($callback) && (!empty($input->postGet('callback')) ||
260-
!empty($input->postGet('cb')) ||
261-
!empty($input->postGet('jsonpCallback')))
262-
) {
263-
$callback = !empty($input->postGet('callback'))
264-
? $input->postGet('callback')
265-
: !empty($input->postGet('cb'))
266-
? $input->postGet('cb')
267-
: $input->postGet('jsonpCallback');
268-
}
269-
270-
return $callback;
271-
}
272-
273-
/**
274-
* 设置响应的Content-Type报头
275-
*
276-
* @param string $mime_type Content-Type的值,如application/json;charset=utf-8
277-
* @return $this
278-
*/
279-
public function setContentType($mime_type)
280-
{
281-
$this->setHeader('Content-Type', $mime_type);
282-
return $this;
283-
}
284-
285-
/**
286-
* 设置响应的其他HTTP报头
287-
*
288-
* @param string $key HTTP报头Key
289-
* @param string $value HTTP报头Value
290-
* @return $this
291-
*/
292-
public function setHeader($key, $value)
293-
{
294-
$this->response->header($key, $value);
295-
return $this;
296-
}
297-
298-
/**
299-
* 设置HTTP响应的cookie信息,与PHP的setcookie()参数一致
300-
*
301-
* @param string $key Cookie名称
302-
* @param string $value Cookie值
303-
* @param int $expire 有效时间
304-
* @param string $path 有效路径
305-
* @param string $domain 有效域名
306-
* @param bool $secure Cookie是否仅仅通过安全的HTTPS连接传给客户端
307-
* @param bool $httponly 设置成TRUE,Cookie仅可通过HTTP协议访问
308-
* @return $this
309-
*/
310-
public function setCookie(
311-
$key,
312-
$value = '',
313-
$expire = 0,
314-
$path = '/',
315-
$domain = '',
316-
$secure = false,
317-
$httponly = false
318-
) {
319-
$this->response->cookie($key, $value, $expire, $path, $domain, $secure, $httponly);
320-
return $this;
321-
}
322-
323-
324304
/**
325305
* 结束HTTP请求,发送响应体
326306
*

src/Client/Http/Client.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,10 @@ public function goDnsLookup($url = '', $timeout = 0, $headers = [])
214214
*/
215215
public function goPost($url = '', $data = [], $timeout = 30000, $headers = [])
216216
{
217+
if (empty($data)) {
218+
throw new Exception('post data is empty');
219+
}
220+
217221
if (!($this->client instanceof \swoole_http_client)) {
218222
throw new Exception('You must complete the DNS query first, Such as $client->goDnsLookup()');
219223
}
@@ -273,6 +277,10 @@ public function goGet($url = '', $query = null, $timeout = 30000, $headers = [])
273277
*/
274278
public function goSinglePost($url = '', $data = [], $timeout = 30000, $headers = [])
275279
{
280+
if (empty($data)) {
281+
throw new Exception('post data is empty');
282+
}
283+
276284
if (empty($this->urlData)) {
277285
$this->urlData = self::parseUrl($url);
278286
} else {
@@ -386,6 +394,11 @@ public function goConcurrent(array $requests)
386394
$this->getContext()->getLog()->error('$requests[' . $key . '] method field not valid, must be GET or POST, And Case Sensitive');
387395
continue;
388396
}
397+
398+
if ($method == 'POST' && empty($request['data'])) {
399+
$this->getContext()->getLog()->error('$requests[' . $key . '] post data is empty');
400+
continue;
401+
}
389402
}
390403

391404
if (!isset($request['timeout'])) {

src/Client/RpcClient.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -222,11 +222,11 @@ public function remoteHttpCall($method, array $args)
222222
}
223223

224224
if ($response && !empty($response['body']) && ($jsonRes = json_decode($response['body'], true))) {
225-
if ($jsonRes['status'] !== 200) {
225+
if ($response['statusCode'] !== 200) {
226226
$this->getContext()->getLog()->warning(dump($response, false, true));
227227
return false;
228228
} else {
229-
return $jsonRes['data'];
229+
return $jsonRes;
230230
}
231231
} else {
232232
$this->getContext()->getLog()->warning(dump($response, false, true));
@@ -279,11 +279,11 @@ public static function goConcurrent(array $multiRpc)
279279
$responses = yield $context->getObject(Client::class)->goConcurrent($requests);
280280
foreach ($responses as $key => $response) {
281281
if ($response && !empty($response['body']) && ($jsonRes = json_decode($response['body'], true))) {
282-
if ($jsonRes['status'] !== 200) {
282+
if ($response['statusCode'] !== 200) {
283283
$results[$key] = false;
284284
$context->getContext()->getLog()->warning(dump($response, false, true));
285285
} else {
286-
$results[$key] = $jsonRes['data'];
286+
$results[$key] = $jsonRes;
287287
}
288288
} else {
289289
$results[$key] = false;

0 commit comments

Comments
 (0)