From 66175b422195fc282f40743ea90b55233a0d4834 Mon Sep 17 00:00:00 2001 From: zyimm Date: Mon, 31 Jul 2023 17:17:44 +0800 Subject: [PATCH] =?UTF-8?q?chore:=E9=80=82=E9=85=8Dhyperf3.0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- composer.json | 10 +-- src/ConfigProvider.php | 2 +- src/Exception/MongoDBException.php | 3 +- src/MongodbConnection.php | 119 +++++++++++++++-------------- 4 files changed, 68 insertions(+), 66 deletions(-) diff --git a/composer.json b/composer.json index 1b6b2bf..7e4be5c 100644 --- a/composer.json +++ b/composer.json @@ -8,12 +8,12 @@ } ], "require": { - "php": ">=7.2", - "hyperf/framework": "^2.2.0", + "php": ">=8.0", + "hyperf/framework": "3.*", "psr/container": "^1.0", - "hyperf/pool": "^2.2", - "hyperf/utils": "^2.2.0", - "hyperf/di": "^2.2.0", + "hyperf/pool": "3.*", + "hyperf/utils": "3.*", + "hyperf/di": "3.*", "ext-mongodb": "*" }, "autoload": { diff --git a/src/ConfigProvider.php b/src/ConfigProvider.php index 9b5f88e..4fa3735 100644 --- a/src/ConfigProvider.php +++ b/src/ConfigProvider.php @@ -2,7 +2,7 @@ namespace Hyperf\Mongodb; -!defined('BASE_PATH') && define('BASE_PATH', dirname(__DIR__, 1)); +!defined('BASE_PATH') && define('BASE_PATH', dirname(__DIR__)); class ConfigProvider { diff --git a/src/Exception/MongoDBException.php b/src/Exception/MongoDBException.php index 28ac30b..13acfd0 100644 --- a/src/Exception/MongoDBException.php +++ b/src/Exception/MongoDBException.php @@ -10,7 +10,8 @@ class MongoDBException extends Exception { public function __construct($message = "", $code = 0, Throwable $previous = null) { - $this->code = empty($code) ? __LINE__ : $code; + parent::__construct($message, $code, $previous); + $this->code = empty($code) ? __LINE__ : $code; $this->message = $message; } diff --git a/src/MongodbConnection.php b/src/MongodbConnection.php index 1ac489f..31aa3b2 100644 --- a/src/MongodbConnection.php +++ b/src/MongodbConnection.php @@ -248,32 +248,7 @@ class MongodbConnection extends Connection implements ConnectionInterface // 查询数据 $data = $result = []; //每次最多返回10条记录 - if (!isset($options['limit']) || (int) $options['limit'] <= 0) { - $options['limit'] = $limit; - } - if (!isset($options['skip']) || (int) $options['skip'] <= 0) { - $options['skip'] = $currentPage * $limit; - } - try { - $query = new Query($filter, $options); - $cursor = $this->connection->executeQuery($this->config['db'].'.'.$namespace, $query); - foreach ($cursor as $document) { - $document = (array) $document; - $document['_id'] = (string) $document['_id']; - $data[] = $document; - } - $result['totalCount'] = $this->execCount($namespace, $filter); - $result['currentPage'] = $currentPage; - $result['perPage'] = $limit; - $result['list'] = $data; - } catch (\Exception $e) { - throw new MongoDBException($e->getFile().$e->getLine().$e->getMessage()); - } catch (Exception $e) { - throw new MongoDBException($e->getFile().$e->getLine().$e->getMessage()); - } finally { - $this->pool->release($this); - return $result; - } + return $this->limitTen($options, $limit, $currentPage, $filter, $namespace, $data, $result); } /** @@ -357,13 +332,7 @@ class MongodbConnection extends Connection implements ConnectionInterface // 查询数据 $result = []; try { - $query = new Query($filter, $options); - $cursor = $this->connection->executeQuery($this->config['db'].'.'.$namespace, $query); - foreach ($cursor as $document) { - $document = (array) $document; - $document['_id'] = (string) $document['_id']; - $result[] = $document; - } + $result = $this->getArr($filter, $options, $namespace, $result); } catch (\Exception $e) { throw new MongoDBException($e->getFile().$e->getLine().$e->getMessage()); } catch (Exception $e) { @@ -399,32 +368,7 @@ class MongodbConnection extends Connection implements ConnectionInterface $data = []; $result = []; //每次最多返回10条记录 - if (!isset($options['limit']) || (int) $options['limit'] <= 0) { - $options['limit'] = $limit; - } - if (!isset($options['skip']) || (int) $options['skip'] <= 0) { - $options['skip'] = $currentPage * $limit; - } - try { - $query = new Query($filter, $options); - $cursor = $this->connection->executeQuery($this->config['db'].'.'.$namespace, $query); - foreach ($cursor as $document) { - $document = (array) $document; - $document['_id'] = (string) $document['_id']; - $data[] = $document; - } - $result['totalCount'] = $this->execCount($namespace, $filter); - $result['currentPage'] = $currentPage; - $result['perPage'] = $limit; - $result['list'] = $data; - } catch (\Exception $e) { - throw new MongoDBException($e->getFile().$e->getLine().$e->getMessage()); - } catch (Exception $e) { - throw new MongoDBException($e->getFile().$e->getLine().$e->getMessage()); - } finally { - $this->pool->release($this); - return $result; - } + return $this->limitTen($options, $limit, $currentPage, $filter, $namespace, $data, $result); } /** @@ -752,4 +696,61 @@ class MongodbConnection extends Connection implements ConnectionInterface return $count; } } + + /** + * @param array $filter + * @param array $options + * @param string $namespace + * @param array $result + * + * @return array + * @throws Exception + */ + public function getArr(array $filter, array $options, string $namespace, array $result): array + { + $query = new Query($filter, $options); + $cursor = $this->connection->executeQuery($this->config['db'].'.'.$namespace, $query); + foreach ($cursor as $document) { + $document = (array)$document; + $document['_id'] = (string)$document['_id']; + $result[] = $document; + } + return $result; + } + + /** + * @param array $options + * @param int $limit + * @param int $currentPage + * @param array $filter + * @param string $namespace + * @param array $data + * @param array $result + * + * @return array + * @throws MongoDBException + */ + public function limitTen(array $options, int $limit, int $currentPage, array $filter, string $namespace, array $data, array $result): array + { + if (!isset($options['limit']) || (int)$options['limit'] <= 0) { + $options['limit'] = $limit; + } + if (!isset($options['skip']) || (int)$options['skip'] <= 0) { + $options['skip'] = $currentPage * $limit; + } + try { + $data = $this->getArr($filter, $options, $namespace, $data); + $result['totalCount'] = $this->execCount($namespace, $filter); + $result['currentPage'] = $currentPage; + $result['perPage'] = $limit; + $result['list'] = $data; + } catch (\Exception $e) { + throw new MongoDBException($e->getFile().$e->getLine().$e->getMessage()); + } catch (Exception $e) { + throw new MongoDBException($e->getFile().$e->getLine().$e->getMessage()); + } finally { + $this->pool->release($this); + return $result; + } + } }