-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ec6c8f9
commit 8d362dc
Showing
7 changed files
with
230 additions
and
82 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
<?php | ||
/** | ||
* Created by PhpStorm. | ||
* User: yf | ||
* Date: 2019-01-06 | ||
* Time: 22:58 | ||
*/ | ||
|
||
namespace EasySwoole\Component\Context; | ||
|
||
|
||
use EasySwoole\Component\Context\Exception\ModifyError; | ||
use EasySwoole\Component\Singleton; | ||
use Swoole\Coroutine; | ||
|
||
class ContextManager | ||
{ | ||
use Singleton; | ||
|
||
private $handler = []; | ||
|
||
private $context = []; | ||
|
||
public function handler($key,HandlerInterface $handler):ContextManager | ||
{ | ||
$this->handler[$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'); | ||
} | ||
$cid = $this->getCid($cid); | ||
$this->context[$cid][$key] = $value; | ||
return $this; | ||
} | ||
|
||
public function get($key,$cid = null) | ||
{ | ||
$cid = $this->getCid($cid); | ||
if(isset($this->context[$cid][$key])){ | ||
return $this->context[$cid][$key]; | ||
} | ||
if(isset($this->handler[$key])){ | ||
/** @var HandlerInterface $handler */ | ||
$handler = $this->handler[$key]; | ||
$this->context[$cid][$key] = $handler->onContextCreate(); | ||
return $this->context[$cid][$key]; | ||
} | ||
return null; | ||
} | ||
|
||
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]; | ||
$item = $this->context[$cid][$key]; | ||
return $handler->onDestroy($item); | ||
} | ||
unset($this->context[$cid][$key]); | ||
return true; | ||
}else{ | ||
return false; | ||
} | ||
} | ||
|
||
public function destroy($cid = null) | ||
{ | ||
$cid = $this->getCid($cid); | ||
if(isset($this->context[$cid])){ | ||
$data = $this->context[$cid]; | ||
foreach ($data as $key => $val){ | ||
$this->unset($key,$cid); | ||
} | ||
} | ||
unset($this->context[$cid]); | ||
} | ||
|
||
public function getCid($cid = null):int | ||
{ | ||
if($cid === null){ | ||
return Coroutine::getUid(); | ||
} | ||
return $cid; | ||
} | ||
|
||
public function destroyAll($force = false) | ||
{ | ||
if($force){ | ||
$this->context = []; | ||
}else{ | ||
foreach ($this->context as $cid => $data){ | ||
$this->destroy($cid); | ||
} | ||
} | ||
} | ||
|
||
public function getContextArray($cid = null):?array | ||
{ | ||
$cid = $this->getCid($cid); | ||
if(isset($this->context[$cid])){ | ||
return $this->context[$cid]; | ||
}else{ | ||
return null; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
/** | ||
* Created by PhpStorm. | ||
* User: yf | ||
* Date: 2019-01-06 | ||
* Time: 23:13 | ||
*/ | ||
|
||
namespace EasySwoole\Component\Context\Exception; | ||
|
||
|
||
class ModifyError extends \Exception | ||
{ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
/** | ||
* Created by PhpStorm. | ||
* User: yf | ||
* Date: 2019-01-06 | ||
* Time: 23:02 | ||
*/ | ||
|
||
namespace EasySwoole\Component\Context; | ||
|
||
|
||
interface HandlerInterface | ||
{ | ||
function onContextCreate(); | ||
function onDestroy($context); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
/** | ||
* Created by PhpStorm. | ||
* User: yf | ||
* Date: 2019-01-06 | ||
* Time: 23:27 | ||
*/ | ||
|
||
namespace EasySwoole\Component\Tests; | ||
|
||
|
||
use EasySwoole\Component\Context\HandlerInterface; | ||
use EasySwoole\Utility\Random; | ||
|
||
class ContextHandler implements HandlerInterface | ||
{ | ||
|
||
function onContextCreate() | ||
{ | ||
// TODO: Implement onContextCreate() method. | ||
$stdClass = new \stdClass(); | ||
$stdClass->text = 'handler'; | ||
return $stdClass; | ||
} | ||
|
||
function onDestroy($context) | ||
{ | ||
// TODO: Implement onDestroy() method. | ||
$context->destroy = true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
/** | ||
* Created by PhpStorm. | ||
* User: yf | ||
* Date: 2019-01-06 | ||
* Time: 23:27 | ||
*/ | ||
|
||
namespace EasySwoole\Component\Tests; | ||
|
||
|
||
use EasySwoole\Component\Context\ContextManager; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class ContextTest extends TestCase | ||
{ | ||
function __construct(?string $name = null, array $data = [], string $dataName = '') | ||
{ | ||
ContextManager::getInstance()->handler('handler',new ContextHandler()); | ||
parent::__construct($name, $data, $dataName); | ||
} | ||
|
||
function testHandler() | ||
{ | ||
$object = ContextManager::getInstance()->get('handler'); | ||
$this->assertEquals('handler',$object->text); | ||
ContextManager::getInstance()->destroy(); | ||
$this->assertEquals(true,$object->destroy); | ||
} | ||
|
||
function testSet() | ||
{ | ||
ContextManager::getInstance()->set('key1','key1'); | ||
$this->assertEquals('key1',ContextManager::getInstance()->get('key1')); | ||
} | ||
|
||
function testUnset() | ||
{ | ||
ContextManager::getInstance()->set('key1','key1'); | ||
ContextManager::getInstance()->unset('key1'); | ||
$this->assertEquals(null,ContextManager::getInstance()->get('key1')); | ||
} | ||
|
||
function testDestroy() | ||
{ | ||
ContextManager::getInstance()->destroy(); | ||
$this->assertEquals(null,ContextManager::getInstance()->getContextArray()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters