diff --git a/.gitignore b/.gitignore index 3255193..8cb2a21 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,6 @@ +.idea /vendor *.lock *.log +.gitignore +.php-cs-fixer.cache \ No newline at end of file diff --git a/.php-cs-fixer.php b/.php-cs-fixer.php new file mode 100644 index 0000000..7f22d2e --- /dev/null +++ b/.php-cs-fixer.php @@ -0,0 +1,42 @@ +setRules([ + '@Symfony' => true, + '@Symfony:risky' => true, + 'php_unit_dedicate_assert' => ['target' => '5.6'], + 'array_syntax' => ['syntax' => 'short'], + 'array_indentation' => true, + 'binary_operator_spaces' => [ + 'operators' => [ + '=>' => 'align_single_space', + ], + ], + 'concat_space' => [ + 'spacing' => 'one', + ], + 'fopen_flags' => false, + 'protected_to_private' => false, + 'native_constant_invocation' => true, + 'combine_nested_dirname' => true, + 'single_quote' => true, + 'single_space_after_construct' => [ + 'constructs' => ['abstract', 'as', 'attribute', 'break', 'case', 'catch', 'class', 'clone', 'comment', 'const', 'const_import', 'continue', 'do', 'echo', 'else', 'elseif', 'extends', 'final', 'finally', 'for', 'foreach', 'function', 'function_import', 'global', 'goto', 'if', 'implements', 'include', 'include_once', 'instanceof', 'insteadof', 'interface', 'match', 'named_argument', 'new', 'open_tag_with_echo', 'php_doc', 'php_open', 'print', 'private', 'protected', 'public', 'require', 'require_once', 'return', 'static', 'throw', 'trait', 'try', 'use', 'use_lambda', 'use_trait', 'var', 'while', 'yield', 'yield_from'], + ], + 'braces' => [ + 'position_after_control_structures' => 'next', + ], + 'single_line_comment_style' => false, + 'phpdoc_to_comment' => false, + 'declare_strict_types' => true, + ]) + ->setRiskyAllowed(true) + ->setFinder( + PhpCsFixer\Finder::create() + ->in(__DIR__ . '/src') + ->in(__DIR__ . '/demo') + ->append([__FILE__]) + ) +; diff --git a/composer.json b/composer.json index 41c3150..841d23d 100644 --- a/composer.json +++ b/composer.json @@ -3,10 +3,10 @@ "type": "library", "license": "MIT", "require": { - }, "require-dev": { - "eaglewu/swoole-ide-helper": "dev-master" + "swoole/ide-helper": "^4.4", + "friendsofphp/php-cs-fixer": "^3.0.2" }, "autoload": { "psr-4" : { diff --git a/demo/server.php b/demo/server.php index 4a63f07..02bae81 100644 --- a/demo/server.php +++ b/demo/server.php @@ -1,12 +1,14 @@ '/swoole-shared-memory.sock', - 'storeTypes' => [ + 'socketFile' => '/tmp/swoole-shared-memory.sock', + 'storeTypes' => [ \Yurun\Swoole\SharedMemory\Store\KV::class, \Yurun\Swoole\SharedMemory\Store\Stack::class, \Yurun\Swoole\SharedMemory\Store\Queue::class, @@ -14,4 +16,4 @@ ], ]; $server = new Server($options); -$server->run(); \ No newline at end of file +$server->run(); diff --git a/demo/struct/kv.php b/demo/struct/kv.php index 2519243..af51037 100644 --- a/demo/struct/kv.php +++ b/demo/struct/kv.php @@ -1,22 +1,22 @@ '/swoole-shared-memory.sock', + 'socketFile' => '/tmp/swoole-shared-memory.sock', ]; - $client = new Client($options); var_dump($client->connect()); $kv = new KV($client); -$obj = new stdClass; +$obj = new stdClass(); $obj->time = date('Y-m-d H:i:s'); $kv->set('a', $obj); diff --git a/demo/struct/priority-queue.php b/demo/struct/priority-queue.php index 4c3fd5d..95b1872 100644 --- a/demo/struct/priority-queue.php +++ b/demo/struct/priority-queue.php @@ -1,13 +1,14 @@ '/swoole-shared-memory.sock', + 'socketFile' => '/tmp/swoole-shared-memory.sock', ]; $client = new Client($options); @@ -29,7 +30,7 @@ var_dump($instance->count()); -while($element = $queue->extract('a')) +while ($element = $queue->extract('a')) { var_dump($element); } diff --git a/demo/struct/queue.php b/demo/struct/queue.php index b07342b..ef0be9f 100644 --- a/demo/struct/queue.php +++ b/demo/struct/queue.php @@ -1,13 +1,14 @@ '/swoole-shared-memory.sock', + 'socketFile' => '/tmp/swoole-shared-memory.sock', ]; $client = new Client($options); @@ -18,7 +19,7 @@ $queue->push('a', microtime(true)); $queue->push('a', microtime(true)); $queue->push('a', microtime(true)); -$queue->push('a', 1,2,3); +$queue->push('a', 1, 2, 3); var_dump($queue->size('a')); @@ -31,12 +32,12 @@ var_dump($instance->count()); -echo 'pop:', PHP_EOL; +echo 'pop:', \PHP_EOL; -while($element = $queue->pop('a')) +while ($element = $queue->pop('a')) { var_dump($element); } var_dump('front: ', $queue->front('a')); -var_dump('back: ', $queue->back('a')); \ No newline at end of file +var_dump('back: ', $queue->back('a')); diff --git a/demo/struct/stack.php b/demo/struct/stack.php index b995d25..77621bf 100644 --- a/demo/struct/stack.php +++ b/demo/struct/stack.php @@ -1,16 +1,16 @@ '/swoole-shared-memory.sock', + 'socketFile' => '/tmp/swoole-shared-memory.sock', ]; - $client = new Client($options); var_dump($client->connect()); @@ -19,7 +19,7 @@ $stack->push('a', microtime(true)); $stack->push('a', microtime(true)); $stack->push('a', microtime(true)); -$stack->push('a', 1,2,3); +$stack->push('a', 1, 2, 3); var_dump($stack->size('a')); @@ -31,11 +31,11 @@ var_dump($instance->count()); -echo 'pop:', PHP_EOL; +echo 'pop:', \PHP_EOL; -while($element = $stack->pop('a')) +while ($element = $stack->pop('a')) { var_dump($element); } -var_dump('top: ', $stack->top('a')); \ No newline at end of file +var_dump('top: ', $stack->top('a')); diff --git a/demo/swoole.php b/demo/swoole.php index 59fc2c5..b62d91f 100644 --- a/demo/swoole.php +++ b/demo/swoole.php @@ -1,18 +1,20 @@ set([ - 'dispatch_mode' => 1, // 这里仅为演示不同 workerId 数据共享而设为 1 + 'dispatch_mode' => 1, // 这里仅为演示不同 workerId 数据共享而设为 1 ]); // 添加一个用户自定义的工作进程,启动 unix socket 服务 -$server->addProcess(new Swoole\Process(function($process) { +$server->addProcess(new Swoole\Process(function ($process) { $options = [ // 这个文件必须,而且不能是samba共享文件 - 'socketFile' => '/swoole-shared-memory.sock', - 'storeTypes' => [ + 'socketFile' => '/tmp/swoole-shared-memory.sock', + 'storeTypes' => [ \Yurun\Swoole\SharedMemory\Store\KV::class, \Yurun\Swoole\SharedMemory\Store\Stack::class, \Yurun\Swoole\SharedMemory\Store\Queue::class, @@ -23,16 +25,16 @@ $server->run(); })); -$server->on('request', function (swoole_http_request $request, swoole_http_response $response) use($server) { +$server->on('request', function (swoole_http_request $request, swoole_http_response $response) use ($server) { $client = new \Yurun\Swoole\SharedMemory\Client\Client([ // 这个文件必须,而且不能是samba共享文件 - 'socketFile' => '/swoole-shared-memory.sock', + 'socketFile' => '/swoole-shared-memory.sock', ]); $client->connect(); $kv = new \Yurun\Swoole\SharedMemory\Client\Store\KV($client); - switch($request->server['path_info']) + switch ($request->server['path_info']) { case '/set': $result = $kv->set($request->get['k'], $request->get['v']); @@ -47,10 +49,9 @@ $response->header('Content-Type', 'application/json'); $response->end(json_encode([ - 'result' => $result, - 'workerId' => $server->worker_id, + 'result' => $result, + 'workerId' => $server->worker_id, ])); - }); $server->start(); diff --git a/src/Client/Client.php b/src/Client/Client.php index 71de6b3..c43f660 100644 --- a/src/Client/Client.php +++ b/src/Client/Client.php @@ -1,14 +1,17 @@ options = $options; - if(!isset($this->options['socketFile'])) + if (!isset($this->options['socketFile'])) { throw new \InvalidArgumentException('If you want to use Swoole Shared Memory, you must set the "socketFile" option'); } @@ -61,29 +64,29 @@ public function __construct($options = []) } /** - * 连接 - * - * @return boolean + * 连接. */ public function connect(): bool { - if($this->connected) + if ($this->connected) { return true; } $this->socket = stream_socket_client('unix://' . $this->socketFile, $errno, $errstr); - if(false === $this->socket) + if (false === $this->socket) { $this->connected = false; + return false; } $this->connected = true; + return true; } public function close() { - if($this->connected) + if ($this->connected) { fclose($this->socket); $this->socket = null; @@ -91,9 +94,7 @@ public function close() } /** - * 是否已连接 - * - * @return boolean + * 是否已连接. */ public function isConnected(): bool { @@ -101,55 +102,55 @@ public function isConnected(): bool } /** - * 发送操作 - * - * @param \Yurun\Swoole\SharedMemory\Message\Operation $operation - * @return boolean + * 发送操作. */ public function send(Operation $operation): bool { - if(!$this->connected || !$this->connect()) + if (!$this->connected || !$this->connect()) { return false; } $data = ($this->serialize)($operation); - $length = strlen($data); + $length = \strlen($data); $data = pack('N', $length) . $data; $length += 4; $result = fwrite($this->socket, $data, $length); - if(false === $result) + if (false === $result) { $this->close(); } + return $length === $result; } /** - * 接收结果 + * 接收结果. * - * @return \Yurun\Swoole\SharedMemory\Message\Result|boolean + * @return \Yurun\Swoole\SharedMemory\Message\Result|bool */ public function recv() { - if(!$this->connected || !$this->connect()) + if (!$this->connected || !$this->connect()) { return false; } $meta = fread($this->socket, 4); - if('' === $meta || false === $meta) + if ('' === $meta || false === $meta) { $this->close(); + return false; } $length = unpack('N', $meta)[1]; $data = fread($this->socket, $length); - if(false === $data || !isset($data[$length - 1])) + if (false === $data || !isset($data[$length - 1])) { $this->close(); + return false; } $result = ($this->unserialize)($data); - if($result instanceof Result) + if ($result instanceof Result) { return $result; } @@ -158,4 +159,4 @@ public function recv() return false; } } -} \ No newline at end of file +} diff --git a/src/Client/Store/Base.php b/src/Client/Store/Base.php index 640d324..ac5ccb4 100644 --- a/src/Client/Store/Base.php +++ b/src/Client/Store/Base.php @@ -1,12 +1,13 @@ client->send($operation); $result = $this->client->recv(); - if(false === $result) + if (false === $result) { return false; } - if(null === $result->throwable) + if (null === $result->throwable) { return $result->result; } @@ -42,26 +44,26 @@ protected function doCall($operation) } /** - * Get 客户端 + * Get 客户端. * * @return \Yurun\Swoole\SharedMemory\Client\Client - */ + */ public function getClient() { return $this->client; } /** - * Set 客户端 + * Set 客户端. * * @param \Yurun\Swoole\SharedMemory\Client\Client $client 客户端 * * @return self - */ + */ public function setClient(\Yurun\Swoole\SharedMemory\Client\Client $client) { $this->client = $client; return $this; } -} \ No newline at end of file +} diff --git a/src/Client/Store/KV.php b/src/Client/Store/KV.php index 0caa51d..41355cf 100644 --- a/src/Client/Store/KV.php +++ b/src/Client/Store/KV.php @@ -1,4 +1,7 @@ doCall(new Operation('KV', 'count')); } - -} \ No newline at end of file +} diff --git a/src/Client/Store/PriorityQueue.php b/src/Client/Store/PriorityQueue.php index a1fc3d6..08555ec 100644 --- a/src/Client/Store/PriorityQueue.php +++ b/src/Client/Store/PriorityQueue.php @@ -1,16 +1,20 @@ doCall(new Operation('PriorityQueue', 'getInstance', [$name])); } - -} \ No newline at end of file +} diff --git a/src/Client/Store/Queue.php b/src/Client/Store/Queue.php index 40b9180..d42bae3 100644 --- a/src/Client/Store/Queue.php +++ b/src/Client/Store/Queue.php @@ -1,4 +1,7 @@ doCall(new Operation('Queue', 'getInstance', [$name])); } - -} \ No newline at end of file +} diff --git a/src/Client/Store/Stack.php b/src/Client/Store/Stack.php index 1e4e707..7cb4f39 100644 --- a/src/Client/Store/Stack.php +++ b/src/Client/Store/Stack.php @@ -1,16 +1,20 @@ doCall(new Operation('Stack', 'getInstance', [$name])); } - -} \ No newline at end of file +} diff --git a/src/Interfaces/IClient.php b/src/Interfaces/IClient.php index b29f85c..c78ca60 100644 --- a/src/Interfaces/IClient.php +++ b/src/Interfaces/IClient.php @@ -1,38 +1,34 @@ operation = $operation; $this->args = $args; } -} \ No newline at end of file +} diff --git a/src/Message/Result.php b/src/Message/Result.php index fb48ae4..5de706d 100644 --- a/src/Message/Result.php +++ b/src/Message/Result.php @@ -1,10 +1,13 @@ result = $result; $this->throwable = $throwable; } -} \ No newline at end of file +} diff --git a/src/OperationParser.php b/src/OperationParser.php index d1797d6..5ad44f4 100644 --- a/src/OperationParser.php +++ b/src/OperationParser.php @@ -1,4 +1,7 @@ $v) + foreach ($storeTypes as $k => $v) { - if(is_numeric($k)) + if (is_numeric($k)) { $refClass = new \ReflectionClass($v); - $this->objects[$refClass->getShortName()] = new $v; + $this->objects[$refClass->getShortName()] = new $v(); } else { - $this->objects[$k] = new $v; + $this->objects[$k] = new $v(); } } } public function parse(Operation $body) { - if(!isset($this->objects[$body->object])) + if (!isset($this->objects[$body->object])) { throw new \RuntimeException(sprintf('Has no object %s', $body->object)); } + return ($this->objects[$body->object])->{$body->operation}(...$body->args); } -} \ No newline at end of file +} diff --git a/src/Server.php b/src/Server.php index 466a0be..22c062d 100644 --- a/src/Server.php +++ b/src/Server.php @@ -1,24 +1,25 @@ options = $options; - if(!isset($this->options['socketFile'])) + if (!isset($this->options['socketFile'])) { throw new \InvalidArgumentException('If you want to use Swoole Shared Memory, you must set the "socketFile" option'); } @@ -84,34 +85,34 @@ public function __construct($options = []) } /** - * 运行服务器 + * 运行服务器. * * @return void */ public function run() { - if(version_compare(SWOOLE_VERSION, '4.1', '>=')) + if (version_compare(\SWOOLE_VERSION, '4.1', '>=')) { \Swoole\Runtime::enableCoroutine(true); } - if(file_exists($this->socketFile)) + if (file_exists($this->socketFile)) { unlink($this->socketFile); } - go(function(){ + \Swoole\Coroutine::create(function () { $this->socket = stream_socket_server('unix://' . $this->socketFile, $errno, $errstr); - if(false === $this->socket) + if (false === $this->socket) { throw new \RuntimeException(sprintf('Create unix socket server failed, errno: %s, errstr: %s', $errno, $errstr)); } - while(true) + while (true) { $conn = stream_socket_accept($this->socket, 0); - if(false === $conn) + if (false === $conn) { continue; } - go(function() use($conn){ + \Swoole\Coroutine::create(function () use ($conn) { $this->parseConn($conn); }); } @@ -119,9 +120,9 @@ public function run() } /** - * 获取配置 + * 获取配置. * - * @return void + * @return array */ public function getOptions() { @@ -129,9 +130,10 @@ public function getOptions() } /** - * 设置配置 + * 设置配置. * * @param array $options + * * @return void */ public function setOptions($options) @@ -140,40 +142,44 @@ public function setOptions($options) } /** - * 处理连接 + * 处理连接. * * @param resource $conn + * * @return void */ private function parseConn($conn) { - while(true) + while (true) { $meta = fread($conn, 4); - if('' === $meta || false === $meta) + if ('' === $meta || false === $meta) { return; } $length = unpack('N', $meta)[1]; $data = fread($conn, $length); - if(false === $data || !isset($data[$length - 1])) + if (false === $data || !isset($data[$length - 1])) { return; } $body = ($this->unserialize)($data); - if(!$body instanceof Operation) + if (!$body instanceof Operation) { return; } - try{ + try + { $result = new Result($this->operationParser->parse($body)); - } catch(\Throwable $th) { + } + catch (\Throwable $th) + { $result = new Result(null, $th); } $resultData = ($this->serialize)($result); - $length = strlen($resultData); + $length = \strlen($resultData); $resultData = pack('N', $length) . $resultData; fwrite($conn, $resultData, $length + 4); } } -} \ No newline at end of file +} diff --git a/src/Store/KV.php b/src/Store/KV.php index 9d55d79..5c9c01f 100644 --- a/src/Store/KV.php +++ b/src/Store/KV.php @@ -1,4 +1,7 @@ data[$name] = $value; + return true; } @@ -29,25 +34,28 @@ public function set($name, $value) * 获取值 * * @param string $name - * @param mixed $default + * @param mixed $default + * * @return mixed */ public function get($name, $default = null) { - return array_key_exists($name, $this->data) ? $this->data[$name] : $default; + return \array_key_exists($name, $this->data) ? $this->data[$name] : $default; } /** * 移除值 * * @param string $name - * @return boolean + * + * @return bool */ public function remove($name) { - if(array_key_exists($name, $this->data)) + if (\array_key_exists($name, $this->data)) { unset($this->data[$name]); + return true; } else @@ -57,35 +65,36 @@ public function remove($name) } /** - * 是否存在 + * 是否存在. * * @param string $name - * @return boolean + * + * @return bool */ public function exists($name) { - return array_key_exists($name, $this->data); + return \array_key_exists($name, $this->data); } /** - * 清除 + * 清除. * - * @return void + * @return bool */ public function clear() { $this->data = []; + return true; } /** - * 获取总的存储数据条数 + * 获取总的存储数据条数. * - * @return iont + * @return int */ public function count() { - return count($this->data); + return \count($this->data); } - -} \ No newline at end of file +} diff --git a/src/Store/PriorityQueue.php b/src/Store/PriorityQueue.php index 7a719ba..1486dbd 100644 --- a/src/Store/PriorityQueue.php +++ b/src/Store/PriorityQueue.php @@ -1,4 +1,7 @@ getInstance($name); + return $queue->isEmpty() ? false : $queue->extract(); } /** - * 返回队列长度 + * 返回队列长度. * * @param string $name + * * @return int */ public function size($name) @@ -60,25 +68,28 @@ public function size($name) } /** - * 清空队列 + * 清空队列. * * @param string $name + * * @return void */ public function clear($name) { - $this->data[$name] = new \Yurun\Swoole\SharedMemory\Struct\PriorityQueue; + $this->data[$name] = new \Yurun\Swoole\SharedMemory\Struct\PriorityQueue(); } /** - * 获取数组 + * 获取数组. * * @param string $name + * * @return array */ public function getArray($name) { $queue = clone $this->getInstance($name); + return iterator_to_array($queue); } @@ -86,15 +97,16 @@ public function getArray($name) * 获取实例对象 * * @param string $name + * * @return \Yurun\Swoole\SharedMemory\Struct\PriorityQueue */ public function getInstance($name) { - if(!isset($this->data[$name])) + if (!isset($this->data[$name])) { - $this->data[$name] = new \Yurun\Swoole\SharedMemory\Struct\PriorityQueue; + $this->data[$name] = new \Yurun\Swoole\SharedMemory\Struct\PriorityQueue(); } + return $this->data[$name]; } - -} \ No newline at end of file +} diff --git a/src/Store/Queue.php b/src/Store/Queue.php index 31cf393..fa124b2 100644 --- a/src/Store/Queue.php +++ b/src/Store/Queue.php @@ -1,4 +1,7 @@ getInstance($name); + return $queue->isEmpty() ? false : $queue->dequeue(); } /** - * 在队列尾部增加元素 + * 在队列尾部增加元素. * * @param string $name - * @param mixed $element - * @return boolean + * @param mixed $element + * + * @return bool */ public function push($name, ...$element) { - $result = true; - foreach($element as $e) + foreach ($element as $e) { - $result &= $this->getInstance($name)->enqueue($e); + $this->getInstance($name)->enqueue($e); } - return $result; + + return true; } /** - * 返回队列长度 + * 返回队列长度. * * @param string $name + * * @return int */ public function size($name) @@ -64,49 +72,56 @@ public function size($name) } /** - * 返回队首元素 + * 返回队首元素. * * @param string $name + * * @return mixed */ public function front($name) { $queue = $this->getInstance($name); + return $queue->isEmpty() ? false : $queue->top(); } /** - * 返回队尾元素 + * 返回队尾元素. * * @param string $name - * @return void + * + * @return mixed */ public function back($name) { $queue = $this->getInstance($name); + return $queue->isEmpty() ? false : $queue->bottom(); } /** - * 清空队列 + * 清空队列. * * @param string $name + * * @return void */ public function clear($name) { - $this->data[$name] = new \SplQueue; + $this->data[$name] = new \SplQueue(); } /** - * 获取数组 + * 获取数组. * * @param string $name + * * @return array */ public function getArray($name) { $queue = clone $this->getInstance($name); + return iterator_to_array($queue); } @@ -114,15 +129,16 @@ public function getArray($name) * 获取实例对象 * * @param string $name + * * @return \SplQueue */ public function getInstance($name) { - if(!isset($this->data[$name])) + if (!isset($this->data[$name])) { - $this->data[$name] = new \SplQueue; + $this->data[$name] = new \SplQueue(); } + return $this->data[$name]; } - -} \ No newline at end of file +} diff --git a/src/Store/Stack.php b/src/Store/Stack.php index ca7f15e..58cd5f4 100644 --- a/src/Store/Stack.php +++ b/src/Store/Stack.php @@ -1,4 +1,7 @@ getInstance($name); + return $stack->isEmpty() ? false : $stack->pop(); } /** - * 在栈底增加元素 + * 在栈底增加元素. * * @param string $name - * @param mixed $element - * @return boolean + * @param mixed $element + * + * @return bool */ public function push($name, ...$element) { - $result = true; - foreach($element as $e) + foreach ($element as $e) { - $result &= $this->getInstance($name)->push($e); + $this->getInstance($name)->push($e); } - return $result; + + return true; } /** - * 返回栈中元素数目 + * 返回栈中元素数目. * * @param string $name + * * @return int */ public function size($name) @@ -64,37 +72,42 @@ public function size($name) } /** - * 返回栈顶元素 + * 返回栈顶元素. * * @param string $name + * * @return mixed */ public function top($name) { $stack = $this->getInstance($name); + return $stack->isEmpty() ? false : $stack->top(); } /** - * 清空栈 + * 清空栈. * * @param string $name + * * @return void */ public function clear($name) { - $this->data[$name] = new \SplStack; + $this->data[$name] = new \SplStack(); } /** - * 获取数组 + * 获取数组. * * @param string $name + * * @return array */ public function getArray($name) { $stack = clone $this->getInstance($name); + return iterator_to_array($stack); } @@ -102,15 +115,16 @@ public function getArray($name) * 获取实例对象 * * @param string $name + * * @return \SplQueue */ public function getInstance($name) { - if(!isset($this->data[$name])) + if (!isset($this->data[$name])) { - $this->data[$name] = new \SplQueue; + $this->data[$name] = new \SplQueue(); } + return $this->data[$name]; } - -} \ No newline at end of file +} diff --git a/src/Struct/PriorityQueue.php b/src/Struct/PriorityQueue.php index ed2be24..05a5078 100644 --- a/src/Struct/PriorityQueue.php +++ b/src/Struct/PriorityQueue.php @@ -1,4 +1,7 @@ $v) + foreach ($array as $p => $v) { $this->insert($v, $p); } } -} \ No newline at end of file +}