2021-02-22 17:44:24 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace Snowflake\Pool;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
use Exception;
|
|
|
|
|
use ReflectionException;
|
2021-02-23 17:30:10 +08:00
|
|
|
use Snowflake\Event;
|
2021-02-23 19:00:22 +08:00
|
|
|
use Snowflake\Exception\ComponentException;
|
2021-02-22 17:44:24 +08:00
|
|
|
use Snowflake\Exception\NotFindClassException;
|
|
|
|
|
use Snowflake\Snowflake;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class ObjectPool
|
|
|
|
|
* @package Snowflake\Pool
|
|
|
|
|
*/
|
|
|
|
|
class ObjectPool extends \Snowflake\Abstracts\Pool
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
2021-02-23 16:56:34 +08:00
|
|
|
/**
|
|
|
|
|
* set pool max length
|
|
|
|
|
*/
|
|
|
|
|
public function init()
|
|
|
|
|
{
|
|
|
|
|
$this->max = 100;
|
|
|
|
|
}
|
2021-02-23 00:40:51 +08:00
|
|
|
|
|
|
|
|
|
2021-02-23 16:56:34 +08:00
|
|
|
/**
|
|
|
|
|
* @param array $config
|
2021-02-23 17:19:46 +08:00
|
|
|
* @param array $construct
|
2021-02-23 16:56:34 +08:00
|
|
|
* @return mixed
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
2021-02-23 17:19:46 +08:00
|
|
|
public function load(mixed $config, array $construct = []): mixed
|
2021-02-23 16:56:34 +08:00
|
|
|
{
|
|
|
|
|
if (is_object($config)) {
|
|
|
|
|
return $config;
|
|
|
|
|
}
|
2021-02-23 19:00:22 +08:00
|
|
|
return $this->getFromChannel($name = md5($config), [$config, $construct]);
|
2021-02-23 16:56:34 +08:00
|
|
|
}
|
2021-02-23 00:38:33 +08:00
|
|
|
|
|
|
|
|
|
2021-02-23 16:56:34 +08:00
|
|
|
/**
|
|
|
|
|
* @param string $name
|
|
|
|
|
* @param mixed $config
|
|
|
|
|
* @return mixed
|
|
|
|
|
* @throws ReflectionException
|
|
|
|
|
* @throws NotFindClassException
|
|
|
|
|
*/
|
|
|
|
|
public function createClient(string $name, mixed $config): mixed
|
|
|
|
|
{
|
2021-02-23 17:19:46 +08:00
|
|
|
return Snowflake::createObject(...$config);
|
2021-02-23 16:56:34 +08:00
|
|
|
}
|
2021-02-23 00:38:33 +08:00
|
|
|
|
|
|
|
|
|
2021-02-23 16:56:34 +08:00
|
|
|
/**
|
|
|
|
|
* @param string $name
|
|
|
|
|
* @param $object
|
|
|
|
|
*/
|
|
|
|
|
public function release(string $name, mixed $object)
|
|
|
|
|
{
|
2021-02-23 19:09:27 +08:00
|
|
|
$newObject = clone $object;
|
|
|
|
|
if (method_exists($newObject, 'clean')) {
|
|
|
|
|
$newObject->clean();
|
2021-02-23 19:00:22 +08:00
|
|
|
}
|
2021-02-23 19:09:27 +08:00
|
|
|
$this->push($name, $newObject);
|
2021-02-23 16:56:34 +08:00
|
|
|
}
|
2021-02-22 17:44:24 +08:00
|
|
|
|
|
|
|
|
}
|