chore:适配hyperf3.0

This commit is contained in:
zyimm 2023-07-31 17:17:44 +08:00
parent efc747a8b4
commit 66175b4221
4 changed files with 68 additions and 66 deletions

View File

@ -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": {

View File

@ -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
{

View File

@ -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;
}

View File

@ -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;
}
}
}