Skip to content

Commit

Permalink
context
Browse files Browse the repository at this point in the history
  • Loading branch information
kiss291323003 committed Jan 6, 2019
1 parent ec6c8f9 commit 8d362dc
Show file tree
Hide file tree
Showing 7 changed files with 230 additions and 82 deletions.
82 changes: 0 additions & 82 deletions src/Context.php

This file was deleted.

112 changes: 112 additions & 0 deletions src/Context/ContextManager.php
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;
}
}
}
15 changes: 15 additions & 0 deletions src/Context/Exception/ModifyError.php
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
{

}
16 changes: 16 additions & 0 deletions src/Context/HandlerInterface.php
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);
}
31 changes: 31 additions & 0 deletions tests/ContextHandler.php
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;
}
}
49 changes: 49 additions & 0 deletions tests/ContextTest.php
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());
}
}
7 changes: 7 additions & 0 deletions tests/PoolTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@

class PoolTest extends TestCase
{
function __construct(?string $name = null, array $data = [], string $dataName = '')
{
//cli下关闭pool的自动定时检查
PoolManager::getInstance()->getDefaultConfig()->setIntervalCheckTime(0);
parent::__construct($name, $data, $dataName);
}

function testNormalClass()
{
$pool = PoolManager::getInstance()->getPool(PoolObject::class);
Expand Down

0 comments on commit 8d362dc

Please sign in to comment.