diff --git a/Database/ActiveQuery.php b/Database/ActiveQuery.php index af7c387a..f6349daf 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 = objectPool(Pagination::class, [$this]); + $pagination = new Pagination($this); $pagination->setOffset(0); $pagination->setLimit($size); $pagination->setCallback($callback); @@ -207,7 +207,7 @@ class ActiveQuery extends Component public function all(): Collection|array { $data = $this->modelClass::getDb()->createCommand($this->queryBuilder())->all(); - $collect = objectPool(Collection::class, [$this, $data, $this->modelClass]); + $collect = new Collection($this, $data, $this->modelClass); if ($this->asArray) { return $collect->toArray(); } diff --git a/Database/ActiveRecord.php b/Database/ActiveRecord.php index 3c074319..a75699da 100644 --- a/Database/ActiveRecord.php +++ b/Database/ActiveRecord.php @@ -315,11 +315,6 @@ class ActiveRecord extends BaseActiveRecord */ public function toArray(): array { - $class = $this; - Coroutine::defer(function () use ($class) { - $object = Snowflake::app()->getObject(); - $object->release(get_called_class(), $class); - }); $data = $this->_attributes; foreach ($this->getAnnotation() as $key => $item) { if (!isset($data[$key])) { @@ -327,7 +322,18 @@ class ActiveRecord extends BaseActiveRecord } $data[$key] = call_user_func([$this, $item[1]], $data[$key]); } - return array_merge($data, $this->runRelate()); + $data = array_merge($data, $this->runRelate()); + $this->recover(); + return $data; + } + + + /** + * @throws ComponentException + */ + public function recover() + { + objectRecover(get_called_class(), $this); } diff --git a/HttpServer/Route/Dispatch/Dispatch.php b/HttpServer/Route/Dispatch/Dispatch.php index b750bbb3..e717e3e9 100644 --- a/HttpServer/Route/Dispatch/Dispatch.php +++ b/HttpServer/Route/Dispatch/Dispatch.php @@ -26,15 +26,17 @@ class Dispatch protected mixed $request; + /** * @param $handler * @param $request * @return static - * @throws ComponentException + * @throws NotFindClassException + * @throws ReflectionException */ public static function create($handler, $request): static { - $class = objectPool(get_called_class()); + $class = new static(); $class->handler = $handler; $class->request = $request; if ($handler instanceof Closure) { diff --git a/Kafka/Kafka.php b/Kafka/Kafka.php index b2c52be9..bfaffe28 100644 --- a/Kafka/Kafka.php +++ b/Kafka/Kafka.php @@ -115,7 +115,10 @@ class Kafka extends \Snowflake\Process\Process if (!($class instanceof ConsumerInterface)) { return; } - $class->onHandler(objectPool(Struct::class, [$topic, $message])); + $class->onHandler($Struct = objectPool(Struct::class, [$topic, $message])); + + objectRecover($namespace, $class); + objectRecover(Struct::class, $Struct); } catch (Throwable $exception) { $this->application->error($exception); } diff --git a/System/Pool/ObjectPool.php b/System/Pool/ObjectPool.php index 3a733c03..7c320a38 100644 --- a/System/Pool/ObjectPool.php +++ b/System/Pool/ObjectPool.php @@ -38,9 +38,7 @@ class ObjectPool extends \Snowflake\Abstracts\Pool if (is_object($config)) { return $config; } - $object = $this->getFromChannel($name = md5($config), [$config, $construct]); - listen(Event::EVENT_AFTER_REQUEST, [$this, 'release'], [$name, $object]); - return $object; + return $this->getFromChannel($name = md5($config), [$config, $construct]); } diff --git a/function.php b/function.php index 23c27664..c6050753 100644 --- a/function.php +++ b/function.php @@ -233,6 +233,20 @@ if (!function_exists('objectPool')) { } } +if (!function_exists('objectRecover')) { + + /** + * @param string $className + * @param $object + * @return mixed + * @throws ComponentException + */ + function objectRecover(mixed $className, $object): void + { + Snowflake::app()->getObject()->release($className, $object); + } +} + if (!function_exists('instance_load')) {