diff --git a/Database/ActiveQuery.php b/Database/ActiveQuery.php index f21ffe0a..af7c387a 100644 --- a/Database/ActiveQuery.php +++ b/Database/ActiveQuery.php @@ -181,7 +181,7 @@ class ActiveQuery extends Component */ public function page(int $size, callable $callback): Pagination { - $pagination = new Pagination($this); + $pagination = objectPool(Pagination::class, [$this]); $pagination->setOffset(0); $pagination->setLimit($size); $pagination->setCallback($callback); @@ -206,9 +206,8 @@ class ActiveQuery extends Component */ public function all(): Collection|array { - $collect = new Collection($this, $this->modelClass::getDb() - ->createCommand($this->queryBuilder()) - ->all(), $this->modelClass); + $data = $this->modelClass::getDb()->createCommand($this->queryBuilder())->all(); + $collect = objectPool(Collection::class, [$this, $data, $this->modelClass]); if ($this->asArray) { return $collect->toArray(); } diff --git a/Database/ActiveRecord.php b/Database/ActiveRecord.php index 64c5f7f3..3c074319 100644 --- a/Database/ActiveRecord.php +++ b/Database/ActiveRecord.php @@ -126,7 +126,7 @@ class ActiveRecord extends BaseActiveRecord if (empty($attributes)) { return \logger()->addError(FIND_OR_CREATE_MESSAGE, 'mysql'); } - $select = new static(); + $select = objectPool(self::class); $select->attributes = $attributes; if (!$select->save()) { throw new Exception($select->getLastError()); @@ -365,7 +365,7 @@ class ActiveRecord extends BaseActiveRecord $relation = $this->getRelation(); - return new HasOne($modelName, $foreignKey, $value, $relation); + return objectPool(HasOne::class, [$modelName, $foreignKey, $value, $relation]); } @@ -386,7 +386,7 @@ class ActiveRecord extends BaseActiveRecord $relation = $this->getRelation(); - return new HasCount($modelName, $foreignKey, $value, $relation); + return objectPool(HasCount::class, [$modelName, $foreignKey, $value, $relation]); } @@ -407,7 +407,7 @@ class ActiveRecord extends BaseActiveRecord $relation = $this->getRelation(); - return new HasMany($modelName, $foreignKey, $value, $relation); + return objectPool(HasMany::class, [$modelName, $foreignKey, $value, $relation]); } /** @@ -427,7 +427,7 @@ class ActiveRecord extends BaseActiveRecord $relation = $this->getRelation(); - return new HasMany($modelName, $foreignKey, $value, $relation); + return objectPool(HasMany::class, [$modelName, $foreignKey, $value, $relation]); } /** diff --git a/Database/Base/AbstractCollection.php b/Database/Base/AbstractCollection.php index 03f6b819..a21987fc 100644 --- a/Database/Base/AbstractCollection.php +++ b/Database/Base/AbstractCollection.php @@ -107,7 +107,7 @@ abstract class AbstractCollection extends Component implements \IteratorAggregat */ public function getModel(): ActiveRecord { - return Snowflake::app()->getObject()->get($this->model, false); + return objectPool($this->model); } diff --git a/Database/Base/BaseActiveRecord.php b/Database/Base/BaseActiveRecord.php index 4f48776a..23992bd2 100644 --- a/Database/Base/BaseActiveRecord.php +++ b/Database/Base/BaseActiveRecord.php @@ -898,8 +898,7 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess */ public static function populate(array $data): static { - $object = Snowflake::app()->getObject(); - $model = $object->get(static::class); + $model = objectPool(static::class); $model->setAttributes($data); $model->setIsCreate(false); $model->refresh(); diff --git a/Database/Base/CollectionIterator.php b/Database/Base/CollectionIterator.php index a9fd6f45..00a9f7d5 100644 --- a/Database/Base/CollectionIterator.php +++ b/Database/Base/CollectionIterator.php @@ -50,10 +50,7 @@ class CollectionIterator extends \ArrayIterator */ protected function newModel($current): ActiveRecord { - $object = Snowflake::app()->getObject(); - $model = $object->get($this->model, false); - - return $model->setAttributes($current); + return objectPool($this->model)->setAttributes($current); } diff --git a/Database/Collection.php b/Database/Collection.php index 337406ca..2e2693e3 100644 --- a/Database/Collection.php +++ b/Database/Collection.php @@ -198,7 +198,7 @@ class Collection extends AbstractCollection } $_filters[] = $value; } - return new Collection($this->query, $_filters, $this->model); + return objectPool(Collection::class, [$this->query, $_filters, $this->model]); } diff --git a/HttpServer/Route/Dispatch/Dispatch.php b/HttpServer/Route/Dispatch/Dispatch.php index 63ee9fb8..fda7912c 100644 --- a/HttpServer/Route/Dispatch/Dispatch.php +++ b/HttpServer/Route/Dispatch/Dispatch.php @@ -9,6 +9,7 @@ use Closure; use HttpServer\Controller; use HttpServer\Http\Context; use HttpServer\Http\Request; +use Snowflake\Exception\ComponentException; use Snowflake\Snowflake; /** @@ -27,10 +28,11 @@ class Dispatch * @param $handler * @param $request * @return static + * @throws ComponentException */ public static function create($handler, $request): static { - $class = new static(); + $class = objectPool(get_called_class()); $class->handler = $handler; $class->request = $request; if ($handler instanceof Closure) { @@ -53,10 +55,11 @@ class Dispatch /** * 设置作用域 + * @throws ComponentException */ protected function bind() { - $class = $this->bindRequest(new Controller()); + $class = $this->bindRequest(objectPool(Controller::class)); $this->handler = Closure::bind($this->handler, $class); } diff --git a/Kafka/Kafka.php b/Kafka/Kafka.php index 664afe90..b2c52be9 100644 --- a/Kafka/Kafka.php +++ b/Kafka/Kafka.php @@ -10,6 +10,7 @@ use RdKafka\ConsumerTopic; use RdKafka\Exception; use RdKafka\KafkaConsumer; use RdKafka\TopicConf; +use Snowflake\Exception\ComponentException; use Snowflake\Exception\ConfigException; use Snowflake\Snowflake; use Swoole\Coroutine; @@ -98,6 +99,7 @@ class Kafka extends \Snowflake\Process\Process /** * @param $topic * @param $message + * @throws ComponentException */ protected function handlerExecute($topic, $message) { @@ -109,11 +111,11 @@ class Kafka extends \Snowflake\Process\Process if (!class_exists($namespace)) { return; } - $class = Snowflake::createObject($namespace); + $class = objectPool($namespace); if (!($class instanceof ConsumerInterface)) { return; } - $class->onHandler(new Struct($topic, $message)); + $class->onHandler(objectPool(Struct::class, [$topic, $message])); } catch (Throwable $exception) { $this->application->error($exception); } diff --git a/System/Pool/ObjectPool.php b/System/Pool/ObjectPool.php index 93689aac..98904912 100644 --- a/System/Pool/ObjectPool.php +++ b/System/Pool/ObjectPool.php @@ -28,16 +28,16 @@ class ObjectPool extends \Snowflake\Abstracts\Pool /** * @param array $config - * @param bool $isMaster + * @param array $construct * @return mixed * @throws Exception */ - public function get(mixed $config, bool $isMaster = false): mixed + public function load(mixed $config, array $construct = []): mixed { if (is_object($config)) { return $config; } - return $this->getFromChannel(md5($config), $config); + return $this->getFromChannel(md5($config), [$config, $construct]); } @@ -50,8 +50,7 @@ class ObjectPool extends \Snowflake\Abstracts\Pool */ public function createClient(string $name, mixed $config): mixed { - // TODO: Implement createClient() method. - return Snowflake::createObject($config); + return Snowflake::createObject(...$config); } diff --git a/function.php b/function.php index aa1b4e42..23c27664 100644 --- a/function.php +++ b/function.php @@ -218,6 +218,22 @@ if (!function_exists('fire')) { } +if (!function_exists('objectPool')) { + + /** + * @param string $className + * @param array $construct + * @return mixed + * @throws ComponentException + * @throws Exception + */ + function objectPool(mixed $className, array $construct = []): mixed + { + return Snowflake::app()->getObject()->load($className, $construct); + } +} + + if (!function_exists('instance_load')) { function instance_load()