From af7b8057e32ea9a94f6f6c9687d6e5e6d2425e99 Mon Sep 17 00:00:00 2001 From: chaz6chez Date: Mon, 30 Sep 2024 00:57:52 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20channel=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Utils/Channel/Channel.php | 14 +++++++++++++- src/Utils/Channel/Handlers/ChannelInterface.php | 11 +++++++++++ src/Utils/Channel/Handlers/DefaultChannel.php | 2 ++ src/Utils/Channel/Handlers/SwooleChannel.php | 2 ++ src/Utils/Channel/Handlers/SwowChannel.php | 2 ++ src/Utils/RegisterMethods.php | 4 ---- 6 files changed, 30 insertions(+), 5 deletions(-) diff --git a/src/Utils/Channel/Channel.php b/src/Utils/Channel/Channel.php index 7386c9a..a95ba07 100644 --- a/src/Utils/Channel/Channel.php +++ b/src/Utils/Channel/Channel.php @@ -10,16 +10,28 @@ use Workbunny\WebmanCoroutine\Factory; use Workbunny\WebmanCoroutine\Utils\Channel\Handlers\ChannelInterface; use Workbunny\WebmanCoroutine\Utils\Channel\Handlers\DefaultChannel; +use Workbunny\WebmanCoroutine\Utils\Channel\Handlers\SwooleChannel; +use Workbunny\WebmanCoroutine\Utils\Channel\Handlers\SwowChannel; use Workbunny\WebmanCoroutine\Utils\RegisterMethods; class Channel { use RegisterMethods; + /** + * @var ChannelInterface + */ protected ChannelInterface $_interface; + /** + * @var string[] + */ protected static array $_handlers = [ - + Factory::WORKERMAN_SWOW => SwowChannel::class, + Factory::WORKBUNNY_SWOW => SwowChannel::class, + Factory::WORKERMAN_SWOOLE => SwooleChannel::class, + Factory::WORKBUNNY_SWOOLE => SwooleChannel::class, + Factory::RIPPLE_FIBER => DefaultChannel::class, ]; /** diff --git a/src/Utils/Channel/Handlers/ChannelInterface.php b/src/Utils/Channel/Handlers/ChannelInterface.php index 1bb2ad0..8f5d0c8 100644 --- a/src/Utils/Channel/Handlers/ChannelInterface.php +++ b/src/Utils/Channel/Handlers/ChannelInterface.php @@ -10,6 +10,17 @@ interface ChannelInterface { + /** + * 初始化通道 + * + * @param int $capacity 初始容量 + */ + public function __construct(int $capacity = -1); + + /** + * 摧毁通道 + */ + public function __destruct(); /** * 获取一个数据 * diff --git a/src/Utils/Channel/Handlers/DefaultChannel.php b/src/Utils/Channel/Handlers/DefaultChannel.php index ef8b143..8c6700c 100644 --- a/src/Utils/Channel/Handlers/DefaultChannel.php +++ b/src/Utils/Channel/Handlers/DefaultChannel.php @@ -16,12 +16,14 @@ class DefaultChannel implements ChannelInterface /** @var int */ protected int $_capacity; + /** @inheritdoc */ public function __construct(int $capacity = -1) { $this->_queue = new \SplQueue(); $this->_capacity = $capacity; } + /** @inheritdoc */ public function __destruct() { $this->close(); diff --git a/src/Utils/Channel/Handlers/SwooleChannel.php b/src/Utils/Channel/Handlers/SwooleChannel.php index 8e9bcb8..bc7cb40 100644 --- a/src/Utils/Channel/Handlers/SwooleChannel.php +++ b/src/Utils/Channel/Handlers/SwooleChannel.php @@ -15,11 +15,13 @@ class SwooleChannel implements ChannelInterface /** @var Channel|null */ protected ?Channel $_channel; + /** @inheritdoc */ public function __construct(int $capacity = -1) { $this->_channel = new Channel($capacity); } + /** @inheritdoc */ public function __destruct() { $this->close(); diff --git a/src/Utils/Channel/Handlers/SwowChannel.php b/src/Utils/Channel/Handlers/SwowChannel.php index 296ccff..d2ed4e7 100644 --- a/src/Utils/Channel/Handlers/SwowChannel.php +++ b/src/Utils/Channel/Handlers/SwowChannel.php @@ -15,11 +15,13 @@ class SwowChannel implements ChannelInterface /** @var Channel|null */ protected ?Channel $_channel; + /** @inheritdoc */ public function __construct(int $capacity = -1) { $this->_channel = new Channel($capacity); } + /** @inheritdoc */ public function __destruct() { $this->close(); diff --git a/src/Utils/RegisterMethods.php b/src/Utils/RegisterMethods.php index a6e5c6e..5c1f466 100644 --- a/src/Utils/RegisterMethods.php +++ b/src/Utils/RegisterMethods.php @@ -11,10 +11,6 @@ trait RegisterMethods { - /** - * @var array - */ - protected static array $_handlers = []; /** * 注册