Skip to content

Commit

Permalink
迭代改进 (#2)
Browse files Browse the repository at this point in the history
* 更新 swoole-ide 助手,使用完整命名协程函数替代短命名

* 更新 .gitignore

* 添加 php-cs-fixer

* 修复代码样式

* 修正返回值注释

* 使用 tmp 目录,避免各种权限问题

* 修正 demo 代码样式
  • Loading branch information
NHZEX authored Sep 17, 2021
1 parent a34d389 commit ec83b35
Show file tree
Hide file tree
Showing 30 changed files with 518 additions and 321 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
.idea
/vendor
*.lock
*.log
.gitignore
.php-cs-fixer.cache
42 changes: 42 additions & 0 deletions .php-cs-fixer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

return (new PhpCsFixer\Config())
->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__])
)
;
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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" : {
Expand Down
8 changes: 5 additions & 3 deletions demo/server.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
<?php

declare(strict_types=1);
require dirname(__DIR__) . '/vendor/autoload.php';

use Yurun\Swoole\SharedMemory\Server;

$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,
\Yurun\Swoole\SharedMemory\Store\PriorityQueue::class,
],
];
$server = new Server($options);
$server->run();
$server->run();
8 changes: 4 additions & 4 deletions demo/struct/kv.php
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
<?php

declare(strict_types=1);
require dirname(__DIR__, 2) . '/vendor/autoload.php';

use Yurun\Swoole\SharedMemory\Server;
use Yurun\Swoole\SharedMemory\Client\Client;
use Yurun\Swoole\SharedMemory\Client\Store\KV;

$options = [
// 这个文件必须,而且不能是samba共享文件
'socketFile' => '/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);
Expand Down
7 changes: 4 additions & 3 deletions demo/struct/priority-queue.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
<?php

declare(strict_types=1);
require dirname(__DIR__, 2) . '/vendor/autoload.php';

use Yurun\Swoole\SharedMemory\Server;
use Yurun\Swoole\SharedMemory\Client\Client;
use Yurun\Swoole\SharedMemory\Client\Store\PriorityQueue;

$options = [
// 这个文件必须,而且不能是samba共享文件
'socketFile' => '/swoole-shared-memory.sock',
'socketFile' => '/tmp/swoole-shared-memory.sock',
];

$client = new Client($options);
Expand All @@ -29,7 +30,7 @@

var_dump($instance->count());

while($element = $queue->extract('a'))
while ($element = $queue->extract('a'))
{
var_dump($element);
}
13 changes: 7 additions & 6 deletions demo/struct/queue.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
<?php

declare(strict_types=1);
require dirname(__DIR__, 2) . '/vendor/autoload.php';

use Yurun\Swoole\SharedMemory\Server;
use Yurun\Swoole\SharedMemory\Client\Client;
use Yurun\Swoole\SharedMemory\Client\Store\Queue;

$options = [
// 这个文件必须,而且不能是samba共享文件
'socketFile' => '/swoole-shared-memory.sock',
'socketFile' => '/tmp/swoole-shared-memory.sock',
];

$client = new Client($options);
Expand All @@ -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'));

Expand All @@ -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'));
var_dump('back: ', $queue->back('a'));
14 changes: 7 additions & 7 deletions demo/struct/stack.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
<?php

declare(strict_types=1);
require dirname(__DIR__, 2) . '/vendor/autoload.php';

use Yurun\Swoole\SharedMemory\Server;
use Yurun\Swoole\SharedMemory\Client\Client;
use Yurun\Swoole\SharedMemory\Client\Store\Stack;

$options = [
// 这个文件必须,而且不能是samba共享文件
'socketFile' => '/swoole-shared-memory.sock',
'socketFile' => '/tmp/swoole-shared-memory.sock',
];


$client = new Client($options);
var_dump($client->connect());

Expand All @@ -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'));

Expand All @@ -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'));
var_dump('top: ', $stack->top('a'));
21 changes: 11 additions & 10 deletions demo/swoole.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
<?php

declare(strict_types=1);
require dirname(__DIR__) . '/vendor/autoload.php';

$server = new Swoole\Http\Server('127.0.0.1', 9501);

$server->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,
Expand All @@ -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']);
Expand All @@ -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();
Expand Down
Loading

0 comments on commit ec83b35

Please sign in to comment.