Skip to content

Commit

Permalink
fix context manager bug
Browse files Browse the repository at this point in the history
  • Loading branch information
kiss291323003 committed Jan 19, 2019
1 parent b532467 commit 90aca5b
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
namespace EasySwoole\Component\Context;


interface HandlerInterface
interface ContextHandlerInterface
{
function onContextCreate();
function onDestroy($context);
Expand Down
23 changes: 12 additions & 11 deletions src/Context/ContextManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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];
}
Expand All @@ -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]);
Expand Down
4 changes: 2 additions & 2 deletions tests/ContextHandler.php → tests/ContextContextHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion tests/ContextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down

0 comments on commit 90aca5b

Please sign in to comment.