From 90aca5b19a2616b2a32094ad4e097c06203bd807 Mon Sep 17 00:00:00 2001 From: yf <291323003@qq.com> Date: Sat, 19 Jan 2019 11:38:50 +0800 Subject: [PATCH] fix context manager bug --- ...erface.php => ContextHandlerInterface.php} | 2 +- src/Context/ContextManager.php | 23 ++++++++++--------- ...tHandler.php => ContextContextHandler.php} | 4 ++-- tests/ContextTest.php | 2 +- 4 files changed, 16 insertions(+), 15 deletions(-) rename src/Context/{HandlerInterface.php => ContextHandlerInterface.php} (85%) rename tests/{ContextHandler.php => ContextContextHandler.php} (80%) diff --git a/src/Context/HandlerInterface.php b/src/Context/ContextHandlerInterface.php similarity index 85% rename from src/Context/HandlerInterface.php rename to src/Context/ContextHandlerInterface.php index a38fdbe..0edcea6 100644 --- a/src/Context/HandlerInterface.php +++ b/src/Context/ContextHandlerInterface.php @@ -9,7 +9,7 @@ namespace EasySwoole\Component\Context; -interface HandlerInterface +interface ContextHandlerInterface { function onContextCreate(); function onDestroy($context); diff --git a/src/Context/ContextManager.php b/src/Context/ContextManager.php index 61b22f3..25dde53 100644 --- a/src/Context/ContextManager.php +++ b/src/Context/ContextManager.php @@ -17,22 +17,22 @@ class ContextManager { use Singleton; - private $handler = []; + private $contextHandler = []; private $context = []; private $deferList = []; - public function handler($key,HandlerInterface $handler):ContextManager + public function registerContextHandler($key, ContextHandlerInterface $handler):ContextManager { - $this->handler[$key] = $handler; + $this->contextHandler[$key] = $handler; return $this; } public function set($key,$value,$cid = null):ContextManager { - if(isset($this->handler[$key])){ - throw new ModifyError('key is already been register for handler'); + if(isset($this->contextHandler[$key])){ + throw new ModifyError('key is already been register for context handler'); } $cid = $this->getCid($cid); $this->context[$cid][$key] = $value; @@ -45,9 +45,9 @@ public function get($key,$cid = null) if(isset($this->context[$cid][$key])){ return $this->context[$cid][$key]; } - if(isset($this->handler[$key])){ - /** @var HandlerInterface $handler */ - $handler = $this->handler[$key]; + if(isset($this->contextHandler[$key])){ + /** @var ContextHandlerInterface $handler */ + $handler = $this->contextHandler[$key]; $this->context[$cid][$key] = $handler->onContextCreate(); return $this->context[$cid][$key]; } @@ -58,10 +58,11 @@ public function unset($key,$cid = null) { $cid = $this->getCid($cid); if(isset($this->context[$cid][$key])){ - if(isset($this->handler[$key])){ - /** @var HandlerInterface $handler */ - $handler = $this->handler[$key]; + if(isset($this->contextHandler[$key])){ + /** @var ContextHandlerInterface $handler */ + $handler = $this->contextHandler[$key]; $item = $this->context[$cid][$key]; + unset($this->context[$cid][$key]); return $handler->onDestroy($item); } unset($this->context[$cid][$key]); diff --git a/tests/ContextHandler.php b/tests/ContextContextHandler.php similarity index 80% rename from tests/ContextHandler.php rename to tests/ContextContextHandler.php index ac8cd90..509497c 100644 --- a/tests/ContextHandler.php +++ b/tests/ContextContextHandler.php @@ -9,10 +9,10 @@ namespace EasySwoole\Component\Tests; -use EasySwoole\Component\Context\HandlerInterface; +use EasySwoole\Component\Context\ContextHandlerInterface; use EasySwoole\Utility\Random; -class ContextHandler implements HandlerInterface +class ContextContextHandler implements ContextHandlerInterface { function onContextCreate() diff --git a/tests/ContextTest.php b/tests/ContextTest.php index 7241d00..d831eed 100644 --- a/tests/ContextTest.php +++ b/tests/ContextTest.php @@ -16,7 +16,7 @@ class ContextTest extends TestCase { function __construct(?string $name = null, array $data = [], string $dataName = '') { - ContextManager::getInstance()->handler('handler',new ContextHandler()); + ContextManager::getInstance()->registerContextHandler('handler',new ContextContextHandler()); parent::__construct($name, $data, $dataName); }