From 5706605c8ff51f3f633a2e7941e650a5a232cb84 Mon Sep 17 00:00:00 2001 From: yf <291323003@qq.com> Date: Fri, 24 May 2019 14:44:52 +0800 Subject: [PATCH] bug fix --- README.md | 9 +++------ composer.json | 12 ++++++------ src/Render.php | 14 ++++++++++++-- src/RenderProcess.php | 40 ++++++++++++++++++++++++---------------- 4 files changed, 45 insertions(+), 30 deletions(-) diff --git a/README.md b/README.md index af548de..1d45885 100644 --- a/README.md +++ b/README.md @@ -104,14 +104,11 @@ class SmartyRender implements RenderInterface } // 配置渲染器 -$config = new Config(); -$config->setRender(new SmartyRender()); -$render = new Render($config); - +Render::getInstance()->getConfig()->setRender(new SmartyRender()); // 启动Swoole(在框架中使用时可以省略) $http = new swoole_http_server("0.0.0.0", 9501); -$http->on("request", function (\Swoole\Http\Request $request, \Swoole\Http\Response $response)use($render) { - $content = $render->render('smarty.tpl',['time'=>time()]); // 调用渲染器进行渲染 +$http->on("request", function (\Swoole\Http\Request $request, \Swoole\Http\Response $response){ + $content = Render::getInstance()->render('smarty.tpl',['time'=>time(),'engine'=>'smarty']); // 调用渲染器进行渲染 $response->end($content); }); $render->attachServer($http); diff --git a/composer.json b/composer.json index 8750dfb..3aabe9d 100644 --- a/composer.json +++ b/composer.json @@ -18,15 +18,15 @@ ], "require": { "easyswoole/spl": "^1.0", - "easyswoole/component": "^1.5", - "phpunit/phpunit": "^8.1", - "smarty/smarty": "~3.1", - "league/plates": "3.*", - "duncan3dc/blade": "^4.5" + "easyswoole/component": "^1.5" }, "require-dev": { "easyswoole/swoole-ide-helper": "^1.1", - "topthink/think-template": "^2.0" + "topthink/think-template": "^2.0", + "easyswoole/phpunit": "^1.0", + "smarty/smarty": "~3.1", + "league/plates": "3.*", + "duncan3dc/blade": "^4.5" }, "autoload": { "psr-4": { diff --git a/src/Render.php b/src/Render.php index 57e2c3a..00cc584 100644 --- a/src/Render.php +++ b/src/Render.php @@ -6,12 +6,22 @@ +use EasySwoole\Component\Singleton; + class Render { + use Singleton; + protected $config; - function __construct(Config $config) + + function __construct() + { + $this->config = new Config(); + } + + public function getConfig():Config { - $this->config = $config; + return $this->config; } function attachServer(\swoole_server $server) diff --git a/src/RenderProcess.php b/src/RenderProcess.php index 0a744fe..dfd61dc 100644 --- a/src/RenderProcess.php +++ b/src/RenderProcess.php @@ -25,25 +25,33 @@ public function run($arg) } while (1){ $reply = null; - $conn = stream_socket_accept($socket,-1); - if($conn){ - stream_set_timeout($conn,2); - $header = fread($conn,4); - $allLength = Protocol::packDataLength($header); - $data = fread($conn,$allLength ); - if(strlen($data) == $allLength){ - $data = unserialize($data); - try{ - $reply = $arg->getRender()->render($data['template'],$data['data'],$data['options']); - }catch (\Throwable $throwable){ - $reply = $arg->getRender()->onException($throwable); - }finally{ - $arg->getRender()->afterRender($reply,$data['template'],$data['data'],$data['options']); + $read = [$socket]; + $write = null; + $except = null; + $result = stream_select($read, $write, $except, 1); + if ($result > 0) { + $conn = stream_socket_accept($socket, 1); + if($conn){ + stream_set_timeout($conn,2); + $header = fread($conn,4); + $allLength = Protocol::packDataLength($header); + $data = fread($conn,$allLength ); + if(strlen($data) == $allLength){ + $data = unserialize($data); + try{ + $reply = $arg->getRender()->render($data['template'],$data['data'],$data['options']); + }catch (\Throwable $throwable){ + $reply = $arg->getRender()->onException($throwable); + }finally{ + $arg->getRender()->afterRender($reply,$data['template'],$data['data'],$data['options']); + } } + fwrite($conn,Protocol::pack(serialize($reply))); + fclose($conn); } + }else{ + usleep(1); } - fwrite($conn,Protocol::pack(serialize($reply))); - fclose($conn); } }