Skip to content

Commit

Permalink
Revert "格式化代码,使代码符合PSR2规范"
Browse files Browse the repository at this point in the history
This reverts commit 6f0d97d.
  • Loading branch information
kiss291323003 committed Jan 15, 2019
1 parent 8d1e60e commit c8c8b3f
Show file tree
Hide file tree
Showing 31 changed files with 417 additions and 519 deletions.
7 changes: 4 additions & 3 deletions src/AbstractInterface/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

namespace EasySwoole\EasySwoole\AbstractInterface;


use EasySwoole\EasySwoole\Swoole\EventRegister;
use EasySwoole\Http\Request;
use EasySwoole\Http\Response;
Expand All @@ -18,7 +19,7 @@ public static function initialize();

public static function mainServerCreate(EventRegister $register);

public static function onRequest(Request $request, Response $response):bool;
public static function onRequest(Request $request,Response $response):bool ;

public static function afterRequest(Request $request, Response $response):void;
}
public static function afterRequest(Request $request,Response $response):void;
}
27 changes: 14 additions & 13 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

namespace EasySwoole\EasySwoole;


use EasySwoole\Component\Singleton;
use EasySwoole\Spl\SplArray;
use Swoole\Table;
Expand All @@ -25,28 +26,28 @@ public function __construct()
* 用于存储动态配置,不适合存储大量\大长度的的配置下,仅仅建议用于开关存储
*/
$this->swooleTable = new \swoole_table(1024);
$this->swooleTable->column('value', Table::TYPE_STRING, 512);
$this->swooleTable->column('value',Table::TYPE_STRING,512);
$this->swooleTable->create();
}

public function setDynamicConf($key, $val)
function setDynamicConf($key,$val)
{
$this->swooleTable->set($key, [
'value' => serialize($val),
$this->swooleTable->set($key,[
'value'=> serialize($val)
]);
}

public function getDynamicConf($key)
function getDynamicConf($key)
{
$data = $this->swooleTable->get($key);
if (!empty($data)) {
if(!empty($data)){
return unserialize($data['value']);
} else {
}else{
return null;
}
}

public function delDynamicConf($key)
function delDynamicConf($key)
{
$this->swooleTable->del($key);
}
Expand All @@ -68,7 +69,7 @@ public function getConf($keyPath = '')
* 设置配置项
* 在server启动以后,无法动态的去添加,修改配置信息(进程数据独立)
* @param string $keyPath 配置项名称 支持点语法
* @param mixed $data 配置项数据
* @param mixed $data 配置项数据
*/
public function setConf($keyPath, $data): void
{
Expand Down Expand Up @@ -96,7 +97,7 @@ public function load(array $conf): void
/**
* 载入一个文件的配置项
* @param string $filePath 配置文件路径
* @param bool $merge 是否将内容合并入主配置
* @param bool $merge 是否将内容合并入主配置
* @author : evalor <[email protected]>
*/
public function loadFile($filePath, $merge = false)
Expand All @@ -116,15 +117,15 @@ public function loadFile($filePath, $merge = false)

public function loadEnv(string $file)
{
if (file_exists($file)) {
if(file_exists($file)){
$data = require $file;
$this->conf->loadArray($data);
} else {
}else{
throw new \Exception("config file : {$file} is miss");
}
}

public function clear()
function clear()
{
$this->conf = new SplArray();
}
Expand Down
42 changes: 21 additions & 21 deletions src/Console/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,33 +18,33 @@ class Client
private $port;
private $client = null;

public function __construct($host, $port)
function __construct($host,$port)
{
$this->host = $host;
$this->port = $port;
}

public function close():bool
function close():bool
{
if ($this->client instanceof \swoole_client && $this->client->isConnected()) {
if($this->client instanceof \swoole_client && $this->client->isConnected()){
$this->client->close();
$this->client = null;
return true;
} elseif ($this->client instanceof \swoole_client) {
}else if($this->client instanceof \swoole_client){
//若服务端主动断开的时候
$this->client = null;
return true;
} else {
}else{
return false;
}
}

public function getClient():?\swoole_client
function getClient():?\swoole_client
{
return $this->client;
}

public function connect():bool
function connect():bool
{
$this->close();

Expand All @@ -58,23 +58,23 @@ public function connect():bool
'package_max_length' => 1024*1024
]
);
$this->client->on("connect", function (\swoole_client $cli) {
$this->client->on("connect", function(\swoole_client $cli) {
fwrite(STDOUT, "connect to tcp://{$this->host}:{$this->port} succeed \n");
});

$this->client->on("close", function ($cli) {
$this->client->on("close", function($cli){
$this->close();
fwrite(STDOUT, "tcp://{$this->host}:{$this->port} disconnect \n");
fwrite(STDOUT,"tcp://{$this->host}:{$this->port} disconnect \n");
swoole_event_del(STDIN);
});

$this->client->on("error", function ($cli) {
$this->client->on("error", function($cli){
$this->close();
fwrite(STDOUT, "connection tcp://{$this->host}:{$this->port} error \n");
fwrite(STDOUT,"connection tcp://{$this->host}:{$this->port} error \n");
swoole_event_del(STDIN);
});

$this->client->on("receive", function ($cli, $data) {
$this->client->on("receive", function($cli, $data) {
$str = TcpParser::unpack($data);
echo $str . PHP_EOL;
});
Expand All @@ -83,28 +83,28 @@ public function connect():bool

public function sendCommand(string $commandLine):bool
{
if ($this->client instanceof \swoole_client && $this->client->isConnected()) {
if($this->client instanceof \swoole_client && $this->client->isConnected()){
$commandList = $this->commandParser($commandLine);
$sendArr = [
'action'=>array_shift($commandList),
'args'=>$commandList
];
$this->client->send(TcpParser::pack(json_encode($sendArr, JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES)));
$this->client->send(TcpParser::pack(json_encode($sendArr,JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES)));
return true;
} else {
}else{
return false;
}
}

private function commandParser(string $data):array
{
$list = explode(' ', $data);
$list = explode(' ',$data);
$ret = [];
foreach ($list as $item) {
if (!empty($item)) {
array_push($ret, $item);
foreach ($list as $item){
if(!empty($item)){
array_push($ret,$item);
}
}
return $ret;
}
}
}
8 changes: 4 additions & 4 deletions src/Console/CommandContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function set($key, CommandInterface $command)
$this->container[$key] = $command;
}

public function get($key): ?CommandInterface
function get($key): ?CommandInterface
{
if (isset($this->container[$key])) {
return $this->container[$key];
Expand All @@ -37,7 +37,7 @@ public function get($key): ?CommandInterface
* @return array
* @author: eValor < [email protected] >
*/
public function getCommandList()
function getCommandList()
{
return array_keys($this->container);
}
Expand All @@ -49,7 +49,7 @@ public function getCommandList()
* @param Response $response
* @author: eValor < [email protected] >
*/
public function hook($actionName, Caller $caller, Response $response)
function hook($actionName, Caller $caller, Response $response)
{
$call = CommandContainer::getInstance()->get($actionName);
if ($call instanceof CommandInterface) {
Expand All @@ -58,4 +58,4 @@ public function hook($actionName, Caller $caller, Response $response)
$response->setMessage("action {$actionName} miss");
}
}
}
}
7 changes: 4 additions & 3 deletions src/Console/CommandInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@

namespace EasySwoole\EasySwoole\Console;


use EasySwoole\Socket\Bean\Caller;
use EasySwoole\Socket\Bean\Response;

interface CommandInterface
{
public function exec(Caller $caller, Response $response);
public function help(Caller $caller, Response $response);
}
public function exec(Caller $caller,Response $response);
public function help(Caller $caller,Response $response);
}
10 changes: 5 additions & 5 deletions src/Console/DefaultCommand/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,24 +34,24 @@ public function exec(Caller $caller, Response $response)
$fd = $caller->getClient()->getFd();
$args = $caller->getArgs();
if (Config::getInstance()->getConf('CONSOLE.AUTH') == array_shift($args)) {
TableManager::getInstance()->get(TcpService::$swooleTableName)->set($fd, [
TableManager::getInstance()->get(TcpService::$__swooleTableName)->set($fd, [
'isAuth' => 1,
'tryTimes' => 0
]);
$response->setMessage('auth succeed');
} else {
$info = TableManager::getInstance()->get(TcpService::$swooleTableName)->get($fd);
$info = TableManager::getInstance()->get(TcpService::$__swooleTableName)->get($fd);
if (!empty($info)) {
if ($info['tryTimes'] > 5) {
$response->setStatus(Response::STATUS_RESPONSE_AND_CLOSE);
} else {
TableManager::getInstance()->get(TcpService::$swooleTableName)->set($fd, [
TableManager::getInstance()->get(TcpService::$__swooleTableName)->set($fd, [
'isAuth' => 0,
'tryTimes' => $info['tryTimes'] + 1
]);
}
} else {
TableManager::getInstance()->get(TcpService::$swooleTableName)->set($fd, [
TableManager::getInstance()->get(TcpService::$__swooleTableName)->set($fd, [
'isAuth' => 0,
'tryTimes' => 1
]);
Expand All @@ -77,4 +77,4 @@ public function help(Caller $caller, Response $response)
HELP;
$response->setMessage($help);
}
}
}
4 changes: 2 additions & 2 deletions src/Console/DefaultCommand/Help.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,6 @@ public function help(Caller $caller, Response $response)
{$allCommand}
HELP;
$response->setMessage($help);
$response->setMessage($help);
}
}
}
Loading

0 comments on commit c8c8b3f

Please sign in to comment.