Skip to content

Commit

Permalink
适配动态创建pool
Browse files Browse the repository at this point in the history
  • Loading branch information
kiss291323003 committed Jan 28, 2019
1 parent 483dc82 commit 575558a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 10 deletions.
1 change: 1 addition & 0 deletions src/Pool/AbstractPool.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ public function getObj(float $timeout = null, int $beforeUseTryTimes = 3)
$status = $obj->beforeUse();
if ($status === false) {
$this->unsetObj($obj);
$this->inuse--;
//重新进入对象获取
return $this->getObj($timeout, $beforeUseTryTimes - 1);
}
Expand Down
27 changes: 17 additions & 10 deletions src/Pool/PoolManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class PoolManager

private $pool = [];
private $defaultConfig;
private $classMap = [];

function __construct()
{
Expand Down Expand Up @@ -51,45 +52,51 @@ function register(string $className, $maxNum = 20):?PoolConf
*/
function getPool(string $className,?callable $createCall = null):?AbstractPool
{
$key = $this->generateKey($className);
//检查是否存在动态map
if(isset($this->classMap[$className])){
$key = $this->classMap[$className];
}else{
$key = $this->generateKey($className);
}
if(isset($this->pool[$key])){
$item = $this->pool[$key];
if($item instanceof AbstractPool){
return $item;
}else if($item instanceof PoolConf){
$className = $item->getClass();
/** @var AbstractPool $obj */
$obj = new $className($item);
$this->pool[$key] = $obj;
return $obj;
}
}else{
//尝试注册。
if(class_exists($this->register($className)) && $this->register($className)){
return $this->getPool($className);
}else{
//先尝试动态注册
if(!$this->register($className)){
$config = clone $this->defaultConfig;
$config->setClass($className);
$pool = new class($config,$createCall) extends AbstractPool{
$temp = new class($config,$createCall) extends AbstractPool{
protected $createCall;
public function __construct(PoolConf $conf,$createCall)
{
$this->createCall = $createCall;
parent::__construct($conf);
}

protected function createObject()
{
// TODO: Implement createObject() method.
if(is_callable($this->createCall)){
return call_user_func($this->createCall);
}else{
$className = $this->getPoolConfig()->getClass();
return new $className;
$class = $this->getPoolConfig()->getClass();
return new $class;
}
}
};
$this->pool[$key] = $pool;
return $pool;
$this->classMap[get_class($temp)] = $key;
$this->pool[$key] = $temp;
}
return $this->getPool($className);
}
return null;
}
Expand Down
4 changes: 4 additions & 0 deletions tests/PoolTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,9 @@ function testNormalClass2()
$pool->recycleObj($obj);
$this->assertEquals($pool->status()['created'],1);
$this->assertEquals($hash1,$hash2);

$pool::invoke(function (PoolObject $object){
$this->assertEquals(PoolObject::class,$object->fuck());
});
}
}

0 comments on commit 575558a

Please sign in to comment.