Skip to content

Commit

Permalink
bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
kiss291323003 committed May 24, 2019
1 parent c5fc1df commit 5706605
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 30 deletions.
9 changes: 3 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
12 changes: 6 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
14 changes: 12 additions & 2 deletions src/Render.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
40 changes: 24 additions & 16 deletions src/RenderProcess.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down

0 comments on commit 5706605

Please sign in to comment.