This commit is contained in:
2021-02-23 17:19:46 +08:00
parent 21bdf32fc3
commit 31d5024663
10 changed files with 41 additions and 26 deletions
+3 -4
View File
@@ -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();
}
+5 -5
View File
@@ -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]);
}
/**
+1 -1
View File
@@ -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);
}
+1 -2
View File
@@ -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();
+1 -4
View File
@@ -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);
}
+1 -1
View File
@@ -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]);
}
+5 -2
View File
@@ -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);
}
+4 -2
View File
@@ -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);
}
+4 -5
View File
@@ -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);
}
+16
View File
@@ -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()