diff --git a/Database/ActiveRecord.php b/Database/ActiveRecord.php index 8c71a6d6..7f5c0b88 100644 --- a/Database/ActiveRecord.php +++ b/Database/ActiveRecord.php @@ -13,6 +13,7 @@ namespace Database; use Exception; use Database\Base\BaseActiveRecord; use ReflectionException; +use Snowflake\Channel; use Snowflake\Exception\ComponentException; use Snowflake\Exception\NotFindClassException; use Snowflake\Snowflake; @@ -130,7 +131,10 @@ class ActiveRecord extends BaseActiveRecord } $className = get_called_class(); - $select = objectPool($className, function () use ($className) { + + /** @var Channel $channel */ + $channel = Snowflake::app()->get('channel'); + $select = $channel->pop($className, function () use ($className) { return new $className(); }); @@ -284,7 +288,9 @@ class ActiveRecord extends BaseActiveRecord */ public function recover() { - objectRecover(get_called_class(), $this); + /** @var Channel $channel */ + $channel = Snowflake::app()->get('channel'); + return $channel->push($this, get_called_class()); } diff --git a/Database/Base/AbstractCollection.php b/Database/Base/AbstractCollection.php index c9db758f..fa7dc3ed 100644 --- a/Database/Base/AbstractCollection.php +++ b/Database/Base/AbstractCollection.php @@ -18,6 +18,7 @@ use JetBrains\PhpStorm\Pure; use ReflectionException; use Snowflake\Abstracts\Component; use Database\ActiveRecord; +use Snowflake\Channel; use Snowflake\Exception\ComponentException; use Snowflake\Exception\NotFindClassException; use Snowflake\Snowflake; @@ -35,7 +36,7 @@ abstract class AbstractCollection extends Component implements \IteratorAggregat */ protected array $_item = []; - protected ?ActiveRecord $model; + protected ActiveRecord|string|null $model; protected ActiveQuery $query; @@ -119,7 +120,10 @@ abstract class AbstractCollection extends Component implements \IteratorAggregat if (is_object($model)) { return $model; } - return objectPool($model, function () use ($model) { + + /** @var Channel $channel */ + $channel = Snowflake::app()->get('channel'); + return $channel->pop($model, function () use ($model) { return new $model(); }); } diff --git a/Database/Base/BaseActiveRecord.php b/Database/Base/BaseActiveRecord.php index c24a0788..8f9ca7b6 100644 --- a/Database/Base/BaseActiveRecord.php +++ b/Database/Base/BaseActiveRecord.php @@ -29,6 +29,7 @@ use Database\Mysql\Columns; use Database\Relation; use Exception; use Snowflake\Abstracts\TraitApplication; +use Snowflake\Channel; use Snowflake\Exception\ComponentException; use Snowflake\Exception\NotFindClassException; use validator\Validator; @@ -1032,8 +1033,9 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess { $className = get_called_class(); - /** @var static $model */ - $model = objectPool($className, function () use ($className) { + /** @var Channel $channel */ + $channel = Snowflake::app()->get('channel'); + $model = $channel->pop($className, function () use ($className) { return new $className(); }); $model->_attributes = $data; diff --git a/Database/Base/CollectionIterator.php b/Database/Base/CollectionIterator.php index 3ba47aaa..d62b3869 100644 --- a/Database/Base/CollectionIterator.php +++ b/Database/Base/CollectionIterator.php @@ -9,6 +9,7 @@ use Database\ActiveQuery; use Database\ActiveRecord; use Exception; use ReflectionException; +use Snowflake\Channel; use Snowflake\Exception\NotFindClassException; use Snowflake\Snowflake; @@ -56,14 +57,18 @@ class CollectionIterator extends \ArrayIterator */ protected function newModel($current): ActiveRecord { + /** @var ActiveRecord|string $model */ $model = $this->model; if (is_object($model)) { $model = get_class($model); } - return objectPool($model, function () use ($model) { - return new $model(); - })->setAttributes($current); + /** @var Channel $channel */ + $channel = Snowflake::app()->get('channel'); + $model = $channel->pop($model, function () use ($model) { + return new $model(); + }); + return $model->setAttributes($current); } diff --git a/Rpc/Client.php b/Rpc/Client.php index 9831d421..f1486064 100644 --- a/Rpc/Client.php +++ b/Rpc/Client.php @@ -6,7 +6,9 @@ namespace Rpc; use Exception; use Snowflake\Abstracts\Component; +use Snowflake\Channel; use Snowflake\Core\Json; +use Snowflake\Snowflake; use Swoole\Coroutine\Client as CClient; @@ -90,7 +92,9 @@ class Client extends Component */ public function getClient(): CClient { - return objectPool(CClient::class, function () { + /** @var Channel $channel */ + $channel = Snowflake::app()->get('channel'); + return $channel->pop(CClient::class, function () { $client = new CClient($this->config['mode'] ?? SWOOLE_SOCK_TCP); $client->set([ 'timeout' => 0.5, diff --git a/System/Channel.php b/System/Channel.php index 3fbcc3c9..92f2dd0e 100644 --- a/System/Channel.php +++ b/System/Channel.php @@ -76,14 +76,16 @@ class Channel extends Component * @return mixed * @throws Exception */ - public function pop(int|float $timeout, string $name, Closure $closure, int $length = 100): mixed + public function pop(string $name, Closure $closure, int|float $timeout = null, int $length = 999): mixed { if (($channel = $this->channelInit($length, $name)) == false) { return $this->addError('Channel is full.'); } - - $data = null; if (!$channel->isEmpty()) { + return $channel->pop(); + } + $data = null; + if ($timeout !== null) { $data = $channel->pop($timeout); } if (empty($data)) { diff --git a/function.php b/function.php index 63bf2c20..faeb412a 100644 --- a/function.php +++ b/function.php @@ -16,7 +16,9 @@ use HttpServer\Service\Websocket; use JetBrains\PhpStorm\Pure; use Snowflake\Abstracts\Config; use Snowflake\Error\Logger; +use Snowflake\Exception\ComponentException; use Snowflake\Exception\ConfigException; +use Snowflake\Exception\NotFindClassException; use Snowflake\Snowflake; use HttpServer\Http\Context; use Snowflake\Core\ArrayAccess; @@ -303,47 +305,20 @@ if (!function_exists('aop')) { /** - * @param string $event + * @param mixed $handler * @param array $params - * @throws Exception - * @throws Exception + * @return mixed + * @throws ReflectionException + * @throws ComponentException + * @throws NotFindClassException */ - function aop(mixed $handler, array $params = []) + function aop(mixed $handler, array $params = []): mixed { return Snowflake::app()->get('aop')->dispatch($handler, ...$params); } } -if (!function_exists('objectPool')) { - - /** - * @param string $className - * @param callable $construct - * @return mixed - * @throws Exception - */ - function objectPool(mixed $className, callable $construct): mixed - { - return Snowflake::app()->getObject()->load($className, $construct); - } -} - -if (!function_exists('objectRecover')) { - - /** - * @param string $className - * @param $object - * @return mixed - * @throws Exception - */ - function objectRecover(mixed $className, $object): void - { - Snowflake::app()->getObject()->release($className, $object); - } -} - - if (!function_exists('instance_load')) { function instance_load()