Skip to content

Commit

Permalink
composer cs-fix
Browse files Browse the repository at this point in the history
  • Loading branch information
chaz6chez committed Oct 9, 2024
1 parent 08048fb commit 4f48448
Show file tree
Hide file tree
Showing 41 changed files with 187 additions and 165 deletions.
4 changes: 2 additions & 2 deletions src/CoroutineWebServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
*/
class CoroutineWebServer extends App
{

/**
* 每个连接的协程计数
*
Expand Down Expand Up @@ -159,7 +158,7 @@ public function onMessage($connection, $request, ...$params)
$res = parent::onMessage(...$params);
} finally {
// 计数 --
self::$_connectionCoroutineCount[$connectionId] --;
self::$_connectionCoroutineCount[$connectionId]--;
// 尝试回收
self::unsetConnectionCoroutineCount($connectionId);
// wg完成
Expand All @@ -173,6 +172,7 @@ public function onMessage($connection, $request, ...$params)
: 1);
// 等待
$waitGroup->wait();

return $res;
}
}
2 changes: 0 additions & 2 deletions src/Handlers/DefaultHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@

namespace Workbunny\WebmanCoroutine\Handlers;

use Closure;

/**
* 默认处理器,使用workerman基础事件
*/
Expand Down
22 changes: 22 additions & 0 deletions src/Handlers/HandlerMethods.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace Workbunny\WebmanCoroutine\Handlers;

use Workerman\Worker;

trait HandlerMethods
{
/**
* 为了测试可以mock
*
* @return string
*/
protected static function _getWorkerVersion(): string
{
preg_match('/^(\d+\.\d+\.\d+)/', Worker::VERSION, $matches);

return $matches[1] ?? '0.0.0';
}
}
12 changes: 1 addition & 11 deletions src/Handlers/RippleHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,21 @@

namespace Workbunny\WebmanCoroutine\Handlers;

use Workerman\Worker;
use function Workbunny\WebmanCoroutine\package_installed;

/**
* 基于Ripple插件的协程处理器,支持PHP-fiber
*/
class RippleHandler implements HandlerInterface
{
use HandlerMethods;

/** @inheritdoc */
public static function isAvailable(): bool
{
return version_compare(static::_getWorkerVersion(), '5.0.0', '<') and package_installed('cclilshy/p-ripple-drive');
}

/**
* 为了测试可以mock
*
* @return string
*/
protected static function _getWorkerVersion(): string
{
return Worker::VERSION;
}

/**
* ripple handler无需初始化
*
Expand Down
12 changes: 1 addition & 11 deletions src/Handlers/RippleWorkerman5Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,21 @@

namespace Workbunny\WebmanCoroutine\Handlers;

use Workerman\Worker;
use function Workbunny\WebmanCoroutine\package_installed;

/**
* 基于Ripple插件的协程处理器,支持PHP-fiber
*/
class RippleWorkerman5Handler implements HandlerInterface
{
use HandlerMethods;

/** @inheritdoc */
public static function isAvailable(): bool
{
return version_compare(static::_getWorkerVersion(), '5.0.0', '>=') and package_installed('cclilshy/p-ripple-drive');
}

/**
* 为了测试可以mock
*
* @return string
*/
protected static function _getWorkerVersion(): string
{
return Worker::VERSION;
}

/**
* ripple handler无需初始化
*
Expand Down
7 changes: 4 additions & 3 deletions src/Handlers/SwooleHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,18 @@
namespace Workbunny\WebmanCoroutine\Handlers;

use Swoole\Runtime;
use Workerman\Worker;

/**
* 基于swoole实现的协程处理器
*/
class SwooleHandler implements HandlerInterface
{
use HandlerMethods;

/** @inheritdoc */
public static function isAvailable(): bool
{
return version_compare(Worker::VERSION, '5.0.0', '<') and extension_loaded('swoole');
return version_compare(static::_getWorkerVersion(), '5.0.0', '<') and extension_loaded('swoole');
}

/** @inheritdoc */
Expand All @@ -38,7 +39,7 @@ public static function waitFor(?\Closure $closure = null, float|int $timeout = -
if ($timeout > 0 && microtime(true) - $time >= $timeout) {
return;
}
usleep(max((int)($timeout * 1000 * 1000), 0));
usleep(max((int) ($timeout * 1000 * 1000), 0));
}
}
}
6 changes: 3 additions & 3 deletions src/Handlers/SwooleWorkerman5Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@

namespace Workbunny\WebmanCoroutine\Handlers;

use Workerman\Worker;

/**
* 基于swoole实现的Workerman5.x版本协程处理器
*/
class SwooleWorkerman5Handler extends SwooleHandler
{
use HandlerMethods;

/** @inheritdoc */
public static function isAvailable(): bool
{
return version_compare(Worker::VERSION, '5.0.0', '>=') and extension_loaded('swoole');
return version_compare(static::_getWorkerVersion(), '5.0.0', '>=') and extension_loaded('swoole');
}
}
7 changes: 3 additions & 4 deletions src/Handlers/SwowHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,17 @@

namespace Workbunny\WebmanCoroutine\Handlers;

use Workerman\Worker;

/**
* 基于swow实现的协程处理器
*/
class SwowHandler implements HandlerInterface
{
use HandlerMethods;

/** @inheritdoc */
public static function isAvailable(): bool
{
return !version_compare(Worker::VERSION, '5.0.0', '>=') and extension_loaded('swow');
return !version_compare(static::_getWorkerVersion(), '5.0.0', '>=') and extension_loaded('swow');
}

/**
Expand All @@ -41,7 +40,7 @@ public static function waitFor(?\Closure $closure = null, float|int $timeout = -
if ($timeout > 0 && microtime(true) - $time >= $timeout) {
return;
}
usleep(max((int)($timeout * 1000 * 1000), 0));
usleep(max((int) ($timeout * 1000 * 1000), 0));
}
}
}
6 changes: 3 additions & 3 deletions src/Handlers/SwowWorkerman5Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@

namespace Workbunny\WebmanCoroutine\Handlers;

use Workerman\Worker;

/**
* 基于swow实现的Workerman5.X的协程处理器
*/
class SwowWorkerman5Handler extends SwowHandler
{
use HandlerMethods;

/** @inheritdoc */
public static function isAvailable(): bool
{
return version_compare(Worker::VERSION, '5.0.0', '>=') and extension_loaded('swow');
return version_compare(static::_getWorkerVersion(), '5.0.0', '>=') and extension_loaded('swow');
}
}
3 changes: 2 additions & 1 deletion src/Utils/Channel/Channel.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ public function __call(string $name, array $arguments): mixed
if (!method_exists($this->_interface, $name)) {
throw new \BadMethodCallException("Method $name not exists. ");
}

return $this->_interface->$name(...$arguments);
}
}
}
3 changes: 1 addition & 2 deletions src/Utils/Channel/Handlers/ChannelInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

interface ChannelInterface
{

/**
* 初始化通道
*
Expand Down Expand Up @@ -66,4 +65,4 @@ public function capacity(): int;
* @return void
*/
public function close(): void;
}
}
6 changes: 3 additions & 3 deletions src/Utils/Channel/Handlers/DefaultChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@

class DefaultChannel implements ChannelInterface
{

/** @var \SplQueue|null */
/** @var \SplQueue|null */
protected ?\SplQueue $_queue;

/** @var int */
/** @var int */
protected int $_capacity;

/** @inheritdoc */
Expand All @@ -38,6 +37,7 @@ public function pop(int|float $timeout = -1): mixed
public function push(mixed $data, int|float $timeout = -1): bool
{
$this->_queue->enqueue($data);

return true;
}

Expand Down
6 changes: 3 additions & 3 deletions src/Utils/Channel/Handlers/RippleChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@

class RippleChannel implements ChannelInterface
{

/** @var \SplQueue */
/** @var \SplQueue */
protected \SplQueue $_queue;

/** @var int */
/** @var int */
protected int $_capacity;

/** @inheritdoc */
Expand Down Expand Up @@ -62,6 +61,7 @@ public function push(mixed $data, int|float $timeout = -1): bool
while (1) {
if (!$this->isFull()) {
$this->_queue->enqueue($data);

return true;
} else {
// timeout
Expand Down
7 changes: 3 additions & 4 deletions src/Utils/Channel/Handlers/SwooleChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@

class SwooleChannel implements ChannelInterface
{

/** @var Channel */
/** @var Channel */
protected Channel $_channel;

/** @inheritdoc */
Expand All @@ -30,13 +29,13 @@ public function __destruct()
/** @inheritdoc */
public function pop(int|float $timeout = -1): mixed
{
return $this->_channel->pop((float)$timeout);
return $this->_channel->pop((float) $timeout);
}

/** @inheritdoc */
public function push(mixed $data, int|float $timeout = -1): mixed
{
return $this->_channel->push($data, (float)$timeout);
return $this->_channel->push($data, (float) $timeout);
}

/** @inheritdoc */
Expand Down
5 changes: 2 additions & 3 deletions src/Utils/Channel/Handlers/SwowChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@

class SwowChannel implements ChannelInterface
{

/** @var Channel */
/** @var Channel */
protected Channel $_channel;

/** @inheritdoc */
Expand Down Expand Up @@ -69,6 +68,6 @@ public function capacity(): int
*/
protected function _second2microsecond(int|float $timeout): int
{
return $timeout > 0 ? (int)($timeout * 1000 * 1000) : -1;
return $timeout > 0 ? (int) ($timeout * 1000 * 1000) : -1;
}
}
7 changes: 4 additions & 3 deletions src/Utils/RegisterMethods.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

trait RegisterMethods
{

/**
* 注册
*
Expand All @@ -24,10 +23,13 @@ public static function register(string $key, mixed $value): ?bool
if (!self::$_handlers[$key] ?? null) {
if ($value = self::registerVerify($value)) {
self::$_handlers[$key] = $value;

return true;
}

return false;
}

return null;
}

Expand Down Expand Up @@ -68,7 +70,6 @@ public static function unregister(string $key): ?bool
*/
abstract public static function unregisterExecute(string $key): bool;


/**
* 获取指定handler 或 获取所有
*
Expand All @@ -78,4 +79,4 @@ public static function getHandler(null|string $key): mixed
{
return $key === null ? self::$_handlers : self::$_handlers[$key] ?? null;
}
}
}
Loading

0 comments on commit 4f48448

Please sign in to comment.