From a166837b3b429eb25ad4ca3787d337d9c8702da2 Mon Sep 17 00:00:00 2001 From: fogelito Date: Thu, 27 Oct 2022 15:20:50 +0300 Subject: [PATCH 01/34] mongo readme example --- .travis.yml | 17 ++++ README.md | 91 ++++++++++++++++++- composer.json | 6 ++ composer.lock | 2 +- pint.json | 3 + src/{MongoClient.php => Client.php} | 56 ++++++------ ...ngoClientOptions.php => ClientOptions.php} | 2 +- src/{MongoCommand.php => Command.php} | 2 +- tests/MongoTest.php | 12 +-- 9 files changed, 151 insertions(+), 40 deletions(-) create mode 100644 .travis.yml create mode 100644 pint.json rename src/{MongoClient.php => Client.php} (92%) rename src/{MongoClientOptions.php => ClientOptions.php} (92%) rename src/{MongoCommand.php => Command.php} (97%) diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000000..95bcf3ef6f --- /dev/null +++ b/.travis.yml @@ -0,0 +1,17 @@ +language: php +php: 8.0 +services: + - docker + +notifications: + email: + - team@appwrite.io + +before_script: + - composer install --ignore-platform-reqs + +script: + - docker-compose up -d + - docker-compose exec tests vendor/bin/pint --test + - docker-compose exec tests vendor/bin/phpstan analyse + - docker-compose exec tests vendor/bin/phpunit \ No newline at end of file diff --git a/README.md b/README.md index 1c9b84285e..2b030b1c67 100644 --- a/README.md +++ b/README.md @@ -1 +1,90 @@ -# mongo \ No newline at end of file +# mongo client + +[![Build Status](https://travis-ci.com/utopia-php/system.svg?branch=main)](https://travis-ci.com/utopia-php/mongo) +![Total Downloads](https://img.shields.io/packagist/dt/utopia-php/mongo.svg) +[![Discord](https://img.shields.io/discord/564160730845151244?label=discord)](https://appwrite.io/discord) + +Utopia WebSocket is a simple and lite abstraction layer around a Mongo Databse. +This library is aiming to be as simple and easy to learn and use. +This library is maintained by the [Appwrite team](https://appwrite.io). + +Although this library is part of the [Utopia Framework](https://github.com/utopia-php/framework) project it is dependency free and can be used as standalone with any other PHP project or framework. + +## Getting Started + +Install using composer: +```bash +composer require utopia-php/mongo +``` + +Init in your application: +```php +connect(); + +// drop database +$client->dropDatabase([]); + +// Create a new collection +$client->createCollection('movies'); + +// Get the list of databases +$client->listDatabaseNames()->databases; + +// insert a new document +$document = $client->insert('movies', [ + 'name' => 'Armageddon 1', + 'country' => 'USA', + 'language' => 'English' + ] +); + +$id = (string)$document['_id']; +// Find Document with ObjectId +$client->find('movies', ['_id' => new ObjectId($id)])->cursor->firstBatch ?? []; + +// insert a new document with specific id +$id = 999; +$client->insert('movies', [ + 'name' => 'Armageddon 2', + '_id' => $id, + 'array' => ['USA', 'UK', 'India'], + 'language' => 'English', + 'float' => 9.9, + 'integer' => 9, + 'is_open' => true, + 'date_string' => (new \DateTime())->format('Y-m-d\TH:i:s.vP'), + ] +); + +// Find document by id +$client->find('movies', ['_id' => $id])->cursor->firstBatch ?? []; + +// Find documents by field +$client->find('movies', ['name' => 'Armageddon'])->cursor->firstBatch ?? []; + +// Delete a document +$client->delete('movies', ['_id' => $id]); + +// drop a collections +$client->dropCollection('movies'); + +``` + +## System Requirements + +Utopia Mongo client requires PHP 8.0 or later. We recommend using the latest PHP version whenever possible. + +## Copyright and license + +The MIT License (MIT) [http://www.opensource.org/licenses/mit-license.php](http://www.opensource.org/licenses/mit-license.php) \ No newline at end of file diff --git a/composer.json b/composer.json index 7a6be5604b..dc14c638d2 100755 --- a/composer.json +++ b/composer.json @@ -21,6 +21,12 @@ "autoload-dev": { "psr-4": {"Utopia\\Tests\\": "tests"} }, + "scripts":{ + "test": "phpunit", + "analyse": "vendor/bin/phpstan analyse", + "format": "vendor/bin/pint", + "lint": "vendor/bin/pint --test" + }, "require": { "php": ">=8.0", "ext-mongodb": "*", diff --git a/composer.lock b/composer.lock index 1a9ae6a580..8ab77467f7 100644 --- a/composer.lock +++ b/composer.lock @@ -2257,5 +2257,5 @@ "ext-mongodb": "*" }, "platform-dev": [], - "plugin-api-version": "2.3.0" + "plugin-api-version": "2.2.0" } diff --git a/pint.json b/pint.json new file mode 100644 index 0000000000..ef654c64d1 --- /dev/null +++ b/pint.json @@ -0,0 +1,3 @@ +{ + "preset": "psr12" +} \ No newline at end of file diff --git a/src/MongoClient.php b/src/Client.php similarity index 92% rename from src/MongoClient.php rename to src/Client.php index 263c362b20..f00f091a29 100644 --- a/src/MongoClient.php +++ b/src/Client.php @@ -4,12 +4,12 @@ namespace Utopia\Mongo; use Exception; use MongoDB\BSON; -use Swoole\Client; +use Swoole\Client as SwooleClient; use Swoole\Coroutine\Client as CoroutineClient; use stdClass; use Utopia\Mongo\Exception\Duplicate; -class MongoClient +class Client { /** * Unique identifier for socket connection. @@ -19,12 +19,12 @@ class MongoClient /** * Options for connection. */ - private MongoClientOptions $options; + private ClientOptions $options; /** * Socket (sync or async) client. */ - private Client|CoroutineClient $client; + private SwooleClient|CoroutineClient $client; /** * Authentication for connection @@ -33,17 +33,17 @@ class MongoClient /** * Create a Mongo connection. - * @param MongoClientOptions $options + * @param ClientOptions $options * @param Boolean $useCoroutine */ - public function __construct(MongoClientOptions $options, bool $useCoroutine = true) + public function __construct(ClientOptions $options, bool $useCoroutine = true) { $this->id = uniqid('utopia.mongo.client'); $this->options = $options; $this->client = $useCoroutine ? new CoroutineClient(SWOOLE_SOCK_TCP | SWOOLE_KEEP) - : new Client(SWOOLE_SOCK_TCP | SWOOLE_KEEP); + : new SwooleClient(SWOOLE_SOCK_TCP | SWOOLE_KEEP); $this->auth = new Auth([ 'authcid' => $options->username, @@ -55,7 +55,7 @@ class MongoClient * Connect to Mongo using TCP/IP * and Wire Protocol. */ - public function connect(): MongoClient + public function connect(): Client { if($this->client->isConnected()) return $this; @@ -186,7 +186,7 @@ class MongoClient * Note: Since Mongo creates on the fly, this just returns * an instances of self. */ - public function selectDatabase(): MongoClient + public function selectDatabase(): Client { return $this; } @@ -197,7 +197,7 @@ class MongoClient * Note: Since Mongo creates on the fly, this just returns * an instances of self. */ - public function createDatabase(): MongoClient + public function createDatabase(): Client { return $this; } @@ -228,7 +228,7 @@ class MongoClient return $res->ok === 1.0; } - public function selectCollection($name): MongoClient + public function selectCollection($name): Client { return $this; } @@ -343,7 +343,7 @@ class MongoClient * * @return array */ - public function dropIndexes(string $collection, array $indexes, array $options = []): MongoClient + public function dropIndexes(string $collection, array $indexes, array $options = []): Client { $this->query( array_merge([ @@ -379,7 +379,7 @@ class MongoClient $docObj->_id ??= new BSON\ObjectId(); $this->query(array_merge([ - MongoCommand::INSERT => $collection, + Command::INSERT => $collection, 'documents' => [$docObj], ], $options)); @@ -413,9 +413,9 @@ class MongoClient * @param array $updates * @param array $options * - * @return MongoClient + * @return Client */ - public function update(string $collection, array $where = [], array $updates = [], array $options = []): MongoClient + public function update(string $collection, array $where = [], array $updates = [], array $options = []): Client { $cleanUpdates = []; @@ -427,7 +427,7 @@ class MongoClient $this->query( array_merge([ - MongoCommand::UPDATE => $collection, + Command::UPDATE => $collection, 'updates' => [ [ 'q' => $this->toObject($where), @@ -451,10 +451,10 @@ class MongoClient * @param array $updates * @param array $options * - * @return MongoClient + * @return Client */ - public function upsert(string $collection, array $where = [], array $updates = [], array $options = []): MongoClient + public function upsert(string $collection, array $where = [], array $updates = [], array $options = []): Client { $cleanUpdates = []; @@ -496,7 +496,7 @@ class MongoClient { $result = $this->query( array_merge([ - MongoCommand::FIND => $collection, + Command::FIND => $collection, 'filter' => $this->toObject($filters), ], $options) ); @@ -515,20 +515,18 @@ class MongoClient * @param array $filters * @param array $options * - * @return array + * @return stdClass */ public function findAndModify(string $collection, array $update, bool $remove = false, array $filters = [], array $options = []): stdClass { - $result = $this->query( + return $this->query( array_merge([ - MongoCommand::FIND_AND_MODIFY => $collection, + Command::FIND_AND_MODIFY => $collection, 'filter' => $this->toObject($filters), 'remove' => $remove, 'update' => $update, ], $options) ); - - return $result; } /** @@ -544,7 +542,7 @@ class MongoClient public function getMore(int $cursorId, string $collection, int $batchSize = 25): stdClass { return $this->query([ - MongoCommand::GET_MORE => $cursorId, + Command::GET_MORE => $cursorId, 'collection' => $collection, 'batchSize' => $batchSize ]); @@ -567,7 +565,7 @@ class MongoClient return $this->query( array_merge( [ - MongoCommand::DELETE => $collection, + Command::DELETE => $collection, 'deletes' => [ $this->toObject( array_merge( @@ -612,13 +610,11 @@ class MongoClient */ public function aggregate(string $collection, array $pipeline): stdClass { - $result = $this->query([ - MongoCommand::AGGREGATE => $collection, + return $this->query([ + Command::AGGREGATE => $collection, 'pipeline' => $pipeline, 'cursor' => $this->toObject([]), ]); - - return $result; } /** diff --git a/src/MongoClientOptions.php b/src/ClientOptions.php similarity index 92% rename from src/MongoClientOptions.php rename to src/ClientOptions.php index 6ec803bd6b..78879a7fcc 100644 --- a/src/MongoClientOptions.php +++ b/src/ClientOptions.php @@ -2,7 +2,7 @@ namespace Utopia\Mongo; -class MongoClientOptions +class ClientOptions { /** * Creates an object that contains the connection diff --git a/src/MongoCommand.php b/src/Command.php similarity index 97% rename from src/MongoCommand.php rename to src/Command.php index 11a8ba110b..f70450155e 100644 --- a/src/MongoCommand.php +++ b/src/Command.php @@ -2,7 +2,7 @@ namespace Utopia\Mongo; -class MongoCommand +class Command { /** * Defines commands Mongo uses over wire protocol. diff --git a/tests/MongoTest.php b/tests/MongoTest.php index 62694e9350..bc16f2696e 100644 --- a/tests/MongoTest.php +++ b/tests/MongoTest.php @@ -5,20 +5,20 @@ namespace Utopia\Tests; use MongoDB\BSON\ObjectId; use PHPUnit\Framework\TestCase; use Utopia\Mongo\Exception\Duplicate; -use Utopia\Mongo\MongoClient; -use Utopia\Mongo\MongoClientOptions; +use Utopia\Mongo\Client; +use Utopia\Mongo\ClientOptions; class MongoTest extends TestCase { - static ?MongoClient $db = null; + static ?Client $db = null; - static function getDatabase(): MongoClient + static function getDatabase(): Client { if (!is_null(self::$db)) { return self::$db; } - $options = new MongoClientOptions( + $options = new ClientOptions( 'utopia_testing', 'mongo', 27017, @@ -26,7 +26,7 @@ class MongoTest extends TestCase 'example' ); - $client = new MongoClient($options, false); + $client = new Client($options, false); $client->connect(); self::$db = $client; From faab4d0cb7532d8979461e9fd1b9172f6886a213 Mon Sep 17 00:00:00 2001 From: fogelito Date: Thu, 27 Oct 2022 15:31:56 +0300 Subject: [PATCH 02/34] mongo readme example --- README.md | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/README.md b/README.md index 2b030b1c67..aeb427a776 100644 --- a/README.md +++ b/README.md @@ -4,12 +4,10 @@ ![Total Downloads](https://img.shields.io/packagist/dt/utopia-php/mongo.svg) [![Discord](https://img.shields.io/discord/564160730845151244?label=discord)](https://appwrite.io/discord) -Utopia WebSocket is a simple and lite abstraction layer around a Mongo Databse. +Utopia WebSocket is a simple and lite abstraction layer around a Mongo Database. This library is aiming to be as simple and easy to learn and use. This library is maintained by the [Appwrite team](https://appwrite.io). -Although this library is part of the [Utopia Framework](https://github.com/utopia-php/framework) project it is dependency free and can be used as standalone with any other PHP project or framework. - ## Getting Started Install using composer: From cfdb47fa0baf4266459b2a22372ed713fd198065 Mon Sep 17 00:00:00 2001 From: Shmuel Fogel Date: Thu, 27 Oct 2022 16:02:10 +0300 Subject: [PATCH 03/34] Update README.md Co-authored-by: Eldad A. Fux --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index aeb427a776..5cd25fa7b2 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# mongo client +# Mongo Line Protocol Client [![Build Status](https://travis-ci.com/utopia-php/system.svg?branch=main)](https://travis-ci.com/utopia-php/mongo) ![Total Downloads](https://img.shields.io/packagist/dt/utopia-php/mongo.svg) From f4672ed24f905eca66224d495fec86bb0d681f4a Mon Sep 17 00:00:00 2001 From: fogelito Date: Thu, 27 Oct 2022 16:06:40 +0300 Subject: [PATCH 04/34] mongo readme example --- README.md | 2 +- src/Client.php | 35 +++++++++++++++++++++++++---------- src/Command.php | 23 ----------------------- 3 files changed, 26 insertions(+), 34 deletions(-) delete mode 100644 src/Command.php diff --git a/README.md b/README.md index 5cd25fa7b2..46c8aa2866 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ ![Total Downloads](https://img.shields.io/packagist/dt/utopia-php/mongo.svg) [![Discord](https://img.shields.io/discord/564160730845151244?label=discord)](https://appwrite.io/discord) -Utopia WebSocket is a simple and lite abstraction layer around a Mongo Database. +Mongo Line Protocol Client is a simple and lite abstraction layer around a Mongo Database. This library is aiming to be as simple and easy to learn and use. This library is maintained by the [Appwrite team](https://appwrite.io). diff --git a/src/Client.php b/src/Client.php index f00f091a29..03c62e6623 100644 --- a/src/Client.php +++ b/src/Client.php @@ -26,6 +26,23 @@ class Client */ private SwooleClient|CoroutineClient $client; + /** + * Defines commands Mongo uses over wire protocol. + */ + public const CREATE = "create"; + public const DELETE = "delete"; + public const FIND = "find"; + public const FIND_AND_MODIFY = "findAndModify"; + public const GET_LAST_ERROR = "getLastError"; + public const GET_MORE = "getMore"; + public const INSERT = "insert"; + public const RESET_ERROR = "resetError"; + public const UPDATE = "update"; + public const COUNT = "count"; + public const AGGREGATE = "aggregate"; + public const DISTINCT = "distinct"; + public const MAP_REDUCE = "mapReduce"; + /** * Authentication for connection */ @@ -379,7 +396,7 @@ class Client $docObj->_id ??= new BSON\ObjectId(); $this->query(array_merge([ - Command::INSERT => $collection, + self::INSERT => $collection, 'documents' => [$docObj], ], $options)); @@ -427,7 +444,7 @@ class Client $this->query( array_merge([ - Command::UPDATE => $collection, + self::UPDATE => $collection, 'updates' => [ [ 'q' => $this->toObject($where), @@ -494,15 +511,13 @@ class Client */ public function find(string $collection, array $filters = [], array $options = []): stdClass { - $result = $this->query( + return $this->query( array_merge([ - Command::FIND => $collection, + self::FIND => $collection, 'filter' => $this->toObject($filters), ], $options) ); - return $result; - } /** @@ -521,7 +536,7 @@ class Client { return $this->query( array_merge([ - Command::FIND_AND_MODIFY => $collection, + self::FIND_AND_MODIFY => $collection, 'filter' => $this->toObject($filters), 'remove' => $remove, 'update' => $update, @@ -542,7 +557,7 @@ class Client public function getMore(int $cursorId, string $collection, int $batchSize = 25): stdClass { return $this->query([ - Command::GET_MORE => $cursorId, + self::GET_MORE => $cursorId, 'collection' => $collection, 'batchSize' => $batchSize ]); @@ -565,7 +580,7 @@ class Client return $this->query( array_merge( [ - Command::DELETE => $collection, + self::DELETE => $collection, 'deletes' => [ $this->toObject( array_merge( @@ -611,7 +626,7 @@ class Client public function aggregate(string $collection, array $pipeline): stdClass { return $this->query([ - Command::AGGREGATE => $collection, + self::AGGREGATE => $collection, 'pipeline' => $pipeline, 'cursor' => $this->toObject([]), ]); diff --git a/src/Command.php b/src/Command.php deleted file mode 100644 index f70450155e..0000000000 --- a/src/Command.php +++ /dev/null @@ -1,23 +0,0 @@ - Date: Thu, 27 Oct 2022 16:11:56 +0300 Subject: [PATCH 05/34] add throws --- src/Client.php | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/src/Client.php b/src/Client.php index 03c62e6623..831311f5cb 100644 --- a/src/Client.php +++ b/src/Client.php @@ -72,7 +72,7 @@ class Client * Connect to Mongo using TCP/IP * and Wire Protocol. */ - public function connect(): Client + public function connect(): self { if($this->client->isConnected()) return $this; @@ -104,6 +104,7 @@ class Client * @param array $command * @param string|null $db * @return stdClass|array|int + * @throws Exception */ public function query(array $command, ?string $db = null): stdClass|array|int { @@ -117,7 +118,7 @@ class Client } /** - * Send a syncronous command to connection. + * Send a synchronous command to connection. */ public function blocking(string $cmd): stdClass|array|int { @@ -203,7 +204,7 @@ class Client * Note: Since Mongo creates on the fly, this just returns * an instances of self. */ - public function selectDatabase(): Client + public function selectDatabase(): self { return $this; } @@ -214,7 +215,7 @@ class Client * Note: Since Mongo creates on the fly, this just returns * an instances of self. */ - public function createDatabase(): Client + public function createDatabase(): self { return $this; } @@ -237,6 +238,7 @@ class Client * @param array $options * @param string|null $db * @return bool + * @throws Exception */ public function dropDatabase(array $options = [], ?string $db = null): bool { @@ -245,7 +247,7 @@ class Client return $res->ok === 1.0; } - public function selectCollection($name): Client + public function selectCollection($name): self { return $this; } @@ -281,6 +283,7 @@ class Client * @param string $name * @param array $options * @return bool + * @throws Exception */ public function dropCollection(string $name, array $options = []): bool { @@ -299,6 +302,7 @@ class Client * @param array $options * * @return stdClass + * @throws Exception */ public function listCollectionNames(array $filter = [], array $options = []): stdClass { @@ -325,6 +329,7 @@ class Client * @param array $options * * @return boolean + * @throws Exception */ public function createIndexes(string $collection, array $indexes, array $options = []): bool { @@ -358,9 +363,10 @@ class Client * @param array $indexes * @param array $options * - * @return array + * @return Client + * @throws Exception */ - public function dropIndexes(string $collection, array $indexes, array $options = []): Client + public function dropIndexes(string $collection, array $indexes, array $options = []): self { $this->query( array_merge([ @@ -431,8 +437,9 @@ class Client * @param array $options * * @return Client + * @throws Exception */ - public function update(string $collection, array $where = [], array $updates = [], array $options = []): Client + public function update(string $collection, array $where = [], array $updates = [], array $options = []): self { $cleanUpdates = []; @@ -469,9 +476,10 @@ class Client * @param array $options * * @return Client + * @throws Exception */ - public function upsert(string $collection, array $where = [], array $updates = [], array $options = []): Client + public function upsert(string $collection, array $where = [], array $updates = [], array $options = []): self { $cleanUpdates = []; @@ -508,6 +516,7 @@ class Client * @param array $options * * @return stdClass + * @throws Exception */ public function find(string $collection, array $filters = [], array $options = []): stdClass { @@ -531,6 +540,7 @@ class Client * @param array $options * * @return stdClass + * @throws Exception */ public function findAndModify(string $collection, array $update, bool $remove = false, array $filters = [], array $options = []): stdClass { @@ -553,6 +563,7 @@ class Client * @param int batchSize * * @return stdClass + * @throws Exception */ public function getMore(int $cursorId, string $collection, int $batchSize = 25): stdClass { @@ -574,6 +585,7 @@ class Client * @param array $options * * @return int + * @throws Exception */ public function delete(string $collection, array $filters = [], int $limit = 1, array $deleteOptions = [], array $options = []): int { @@ -606,6 +618,7 @@ class Client * @param array $options * * @return int + * @throws Exception */ public function count(string $collection, array $filters, array $options): int { @@ -622,6 +635,7 @@ class Client * @param array $pipeline * * @return stdClass + * @throws Exception */ public function aggregate(string $collection, array $pipeline): stdClass { From f3b915515209cc6cc5c68f783b3d3b100a99d11e Mon Sep 17 00:00:00 2001 From: fogelito Date: Thu, 27 Oct 2022 16:41:58 +0300 Subject: [PATCH 06/34] remove Options Class --- src/Client.php | 51 +++++++++++++++++++++++++++++++------------ src/ClientOptions.php | 19 ---------------- tests/MongoTest.php | 11 +--------- 3 files changed, 38 insertions(+), 43 deletions(-) delete mode 100644 src/ClientOptions.php diff --git a/src/Client.php b/src/Client.php index 831311f5cb..c5dd59da55 100644 --- a/src/Client.php +++ b/src/Client.php @@ -16,11 +16,6 @@ class Client */ private string $id; - /** - * Options for connection. - */ - private ClientOptions $options; - /** * Socket (sync or async) client. */ @@ -48,35 +43,63 @@ class Client */ private Auth $auth; + /** + * Default Database Name + */ + private string $database; + + /** + * Database $host + */ + private string $host; + + /** + * Database $port + */ + private int $port; + /** * Create a Mongo connection. - * @param ClientOptions $options + * @param string $database + * @param string $host + * @param int $port + * @param string $user + * @param string $password * @param Boolean $useCoroutine */ - public function __construct(ClientOptions $options, bool $useCoroutine = true) - { + public function __construct( + string $database, + string $host, + int $port, + string $user, + string $password, + bool $useCoroutine = true + ){ $this->id = uniqid('utopia.mongo.client'); - $this->options = $options; + $this->database = $database; + $this->host = $host; + $this->port = $port; $this->client = $useCoroutine ? new CoroutineClient(SWOOLE_SOCK_TCP | SWOOLE_KEEP) : new SwooleClient(SWOOLE_SOCK_TCP | SWOOLE_KEEP); $this->auth = new Auth([ - 'authcid' => $options->username, - 'secret' => Auth::encodeCredentials($options->username, $options->password) + 'authcid' => $user, + 'secret' => Auth::encodeCredentials($user, $password) ]); } /** * Connect to Mongo using TCP/IP * and Wire Protocol. + * @throws Exception */ public function connect(): self { if($this->client->isConnected()) return $this; - $this->client->connect($this->options->host, $this->options->port); + $this->client->connect($this->host, $this->port); [$payload, $db] = $this->auth->start(); $res = $this->query($payload, $db); @@ -109,7 +132,7 @@ class Client public function query(array $command, ?string $db = null): stdClass|array|int { $params = array_merge($command, [ - '$db' => $db ?? $this->options->name, + '$db' => $db ?? $this->database, ]); $sections = BSON\fromPHP($params); @@ -242,7 +265,7 @@ class Client */ public function dropDatabase(array $options = [], ?string $db = null): bool { - $db ??= $this->options->name; + $db ??= $this->database; $res = $this->query(array_merge(["dropDatabase" => 1], $options), $db); return $res->ok === 1.0; } diff --git a/src/ClientOptions.php b/src/ClientOptions.php deleted file mode 100644 index 78879a7fcc..0000000000 --- a/src/ClientOptions.php +++ /dev/null @@ -1,19 +0,0 @@ -connect(); self::$db = $client; From 8c0febdbe26d4c2cd1ecd1ad2a2b828ad1bacf30 Mon Sep 17 00:00:00 2001 From: fogelito Date: Thu, 27 Oct 2022 16:46:42 +0300 Subject: [PATCH 07/34] tests --- tests/MongoTest.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/tests/MongoTest.php b/tests/MongoTest.php index 2304cb717f..3f4f736bee 100644 --- a/tests/MongoTest.php +++ b/tests/MongoTest.php @@ -2,6 +2,7 @@ namespace Utopia\Tests; +use Exception; use MongoDB\BSON\ObjectId; use PHPUnit\Framework\TestCase; use Utopia\Mongo\Exception\Duplicate; @@ -11,6 +12,9 @@ class MongoTest extends TestCase { static ?Client $db = null; + /** + * @throws Exception + */ static function getDatabase(): Client { if (!is_null(self::$db)) { @@ -101,7 +105,4 @@ class MongoTest extends TestCase } - - - } From f7848d1407d3487d0124fa854308cdeb82e96915 Mon Sep 17 00:00:00 2001 From: fogelito Date: Thu, 27 Oct 2022 16:53:15 +0300 Subject: [PATCH 08/34] CHANGE COMMAND NAMES --- src/Client.php | 44 +++++++++++++++++++++++--------------------- 1 file changed, 23 insertions(+), 21 deletions(-) diff --git a/src/Client.php b/src/Client.php index c5dd59da55..057fb04265 100644 --- a/src/Client.php +++ b/src/Client.php @@ -24,19 +24,20 @@ class Client /** * Defines commands Mongo uses over wire protocol. */ - public const CREATE = "create"; - public const DELETE = "delete"; - public const FIND = "find"; - public const FIND_AND_MODIFY = "findAndModify"; - public const GET_LAST_ERROR = "getLastError"; - public const GET_MORE = "getMore"; - public const INSERT = "insert"; - public const RESET_ERROR = "resetError"; - public const UPDATE = "update"; - public const COUNT = "count"; - public const AGGREGATE = "aggregate"; - public const DISTINCT = "distinct"; - public const MAP_REDUCE = "mapReduce"; + + public const COMMAND_CREATE = "create"; + public const COMMAND_DELETE = "delete"; + public const COMMAND_FIND = "find"; + public const COMMAND_FIND_AND_MODIFY = "findAndModify"; + public const COMMAND_GET_LAST_ERROR = "getLastError"; + public const COMMAND_GET_MORE = "getMore"; + public const COMMAND_INSERT = "insert"; + public const COMMAND_RESET_ERROR = "resetError"; + public const COMMAND_UPDATE = "update"; + public const COMMAND_COUNT = "count"; + public const COMMAND_AGGREGATE = "aggregate"; + public const COMMAND_DISTINCT = "distinct"; + public const COMMAND_MAP_REDUCE = "mapReduce"; /** * Authentication for connection @@ -425,7 +426,7 @@ class Client $docObj->_id ??= new BSON\ObjectId(); $this->query(array_merge([ - self::INSERT => $collection, + self::COMMAND_INSERT => $collection, 'documents' => [$docObj], ], $options)); @@ -433,11 +434,12 @@ class Client } /** - * Retreive the last inserted document. + * Retrieve the last inserted document. * * @param string $collection * * @return array + * @throws Exception */ public function lastInsertedDocument(string $collection): array @@ -474,7 +476,7 @@ class Client $this->query( array_merge([ - self::UPDATE => $collection, + self::COMMAND_UPDATE => $collection, 'updates' => [ [ 'q' => $this->toObject($where), @@ -545,7 +547,7 @@ class Client { return $this->query( array_merge([ - self::FIND => $collection, + self::COMMAND_FIND => $collection, 'filter' => $this->toObject($filters), ], $options) ); @@ -569,7 +571,7 @@ class Client { return $this->query( array_merge([ - self::FIND_AND_MODIFY => $collection, + self::COMMAND_FIND_AND_MODIFY => $collection, 'filter' => $this->toObject($filters), 'remove' => $remove, 'update' => $update, @@ -591,7 +593,7 @@ class Client public function getMore(int $cursorId, string $collection, int $batchSize = 25): stdClass { return $this->query([ - self::GET_MORE => $cursorId, + self::COMMAND_GET_MORE => $cursorId, 'collection' => $collection, 'batchSize' => $batchSize ]); @@ -615,7 +617,7 @@ class Client return $this->query( array_merge( [ - self::DELETE => $collection, + self::COMMAND_DELETE => $collection, 'deletes' => [ $this->toObject( array_merge( @@ -663,7 +665,7 @@ class Client public function aggregate(string $collection, array $pipeline): stdClass { return $this->query([ - self::AGGREGATE => $collection, + self::COMMAND_AGGREGATE => $collection, 'pipeline' => $pipeline, 'cursor' => $this->toObject([]), ]); From a4c0eaf04ce7ed0eaf5334f760414614838f0aa2 Mon Sep 17 00:00:00 2001 From: fogelito Date: Thu, 27 Oct 2022 17:01:06 +0300 Subject: [PATCH 09/34] readme code change --- README.md | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/README.md b/README.md index 46c8aa2866..2033f70a2a 100644 --- a/README.md +++ b/README.md @@ -19,15 +19,7 @@ Init in your application: ```php connect(); // drop database From 8a5c0f597dc6b271070fecec7ac7babb6c42d6e1 Mon Sep 17 00:00:00 2001 From: Shmuel Fogel Date: Thu, 27 Oct 2022 17:10:35 +0300 Subject: [PATCH 10/34] Update README.md Co-authored-by: Eldad A. Fux --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 2033f70a2a..dba6d60b14 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Mongo Line Protocol Client +# Non-Blocking PHP Line Protocol Client for MongoDB [![Build Status](https://travis-ci.com/utopia-php/system.svg?branch=main)](https://travis-ci.com/utopia-php/mongo) ![Total Downloads](https://img.shields.io/packagist/dt/utopia-php/mongo.svg) From 1e1c3de9a044b573fdc847f748bf9a246ae64e73 Mon Sep 17 00:00:00 2001 From: Shmuel Fogel Date: Thu, 27 Oct 2022 17:11:11 +0300 Subject: [PATCH 11/34] Update README.md Co-authored-by: Eldad A. Fux --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index dba6d60b14..eec0d0064b 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ ![Total Downloads](https://img.shields.io/packagist/dt/utopia-php/mongo.svg) [![Discord](https://img.shields.io/discord/564160730845151244?label=discord)](https://appwrite.io/discord) -Mongo Line Protocol Client is a simple and lite abstraction layer around a Mongo Database. +A non-blocking PHP client based on the line protocol for MongoDB. Designed to work well in async PHP environments like [Swoole](https://github.com/swoole/swoole-src) This library is aiming to be as simple and easy to learn and use. This library is maintained by the [Appwrite team](https://appwrite.io). From 3932c605852e2d4e51ff0fee9613e18eb6b07e35 Mon Sep 17 00:00:00 2001 From: fogelito Date: Thu, 27 Oct 2022 17:32:24 +0300 Subject: [PATCH 12/34] add pint --- composer.json | 3 +- composer.lock | 80 ++++++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 75 insertions(+), 8 deletions(-) diff --git a/composer.json b/composer.json index dc14c638d2..2535aaa4fe 100755 --- a/composer.json +++ b/composer.json @@ -35,6 +35,7 @@ "require-dev": { "fakerphp/faker": "^1.14", "phpunit/phpunit": "^9.4", - "swoole/ide-helper": "4.8.0" + "swoole/ide-helper": "4.8.0", + "laravel/pint": "1.2.*" } } diff --git a/composer.lock b/composer.lock index 8ab77467f7..c1551e2ef1 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "83cb82fa3af1f298f22d6e1b341453d4", + "content-hash": "1553dd6ec345f3e12b374bb03e72baa1", "packages": [ { "name": "composer/package-versions-deprecated", @@ -424,6 +424,72 @@ }, "time": "2022-07-20T13:12:54+00:00" }, + { + "name": "laravel/pint", + "version": "v1.2.0", + "source": { + "type": "git", + "url": "https://github.com/laravel/pint.git", + "reference": "1d276e4c803397a26cc337df908f55c2a4e90d86" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/laravel/pint/zipball/1d276e4c803397a26cc337df908f55c2a4e90d86", + "reference": "1d276e4c803397a26cc337df908f55c2a4e90d86", + "shasum": "" + }, + "require": { + "ext-json": "*", + "ext-mbstring": "*", + "ext-tokenizer": "*", + "ext-xml": "*", + "php": "^8.0" + }, + "require-dev": { + "friendsofphp/php-cs-fixer": "^3.11.0", + "illuminate/view": "^9.27", + "laravel-zero/framework": "^9.1.3", + "mockery/mockery": "^1.5.0", + "nunomaduro/larastan": "^2.2", + "nunomaduro/termwind": "^1.14.0", + "pestphp/pest": "^1.22.1" + }, + "bin": [ + "builds/pint" + ], + "type": "project", + "autoload": { + "psr-4": { + "App\\": "app/", + "Database\\Seeders\\": "database/seeders/", + "Database\\Factories\\": "database/factories/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Nuno Maduro", + "email": "enunomaduro@gmail.com" + } + ], + "description": "An opinionated code formatter for PHP.", + "homepage": "https://laravel.com", + "keywords": [ + "format", + "formatter", + "lint", + "linter", + "php" + ], + "support": { + "issues": "https://github.com/laravel/pint/issues", + "source": "https://github.com/laravel/pint" + }, + "time": "2022-09-13T15:07:15+00:00" + }, { "name": "myclabs/deep-copy", "version": "1.11.0", @@ -652,16 +718,16 @@ }, { "name": "phpunit/php-code-coverage", - "version": "9.2.17", + "version": "9.2.18", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/php-code-coverage.git", - "reference": "aa94dc41e8661fe90c7316849907cba3007b10d8" + "reference": "12fddc491826940cf9b7e88ad9664cf51f0f6d0a" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/aa94dc41e8661fe90c7316849907cba3007b10d8", - "reference": "aa94dc41e8661fe90c7316849907cba3007b10d8", + "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/12fddc491826940cf9b7e88ad9664cf51f0f6d0a", + "reference": "12fddc491826940cf9b7e88ad9664cf51f0f6d0a", "shasum": "" }, "require": { @@ -717,7 +783,7 @@ ], "support": { "issues": "https://github.com/sebastianbergmann/php-code-coverage/issues", - "source": "https://github.com/sebastianbergmann/php-code-coverage/tree/9.2.17" + "source": "https://github.com/sebastianbergmann/php-code-coverage/tree/9.2.18" }, "funding": [ { @@ -725,7 +791,7 @@ "type": "github" } ], - "time": "2022-08-30T12:24:04+00:00" + "time": "2022-10-27T13:35:33+00:00" }, { "name": "phpunit/php-file-iterator", From 141d94fafaa0f63a0a6b179f84aeafe703568564 Mon Sep 17 00:00:00 2001 From: fogelito Date: Thu, 27 Oct 2022 17:51:49 +0300 Subject: [PATCH 13/34] add phpstan --- composer.json | 3 ++- composer.lock | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 62 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 2535aaa4fe..f32e5ee4ef 100755 --- a/composer.json +++ b/composer.json @@ -36,6 +36,7 @@ "fakerphp/faker": "^1.14", "phpunit/phpunit": "^9.4", "swoole/ide-helper": "4.8.0", - "laravel/pint": "1.2.*" + "laravel/pint": "1.2.*", + "phpstan/phpstan": "1.8.*" } } diff --git a/composer.lock b/composer.lock index c1551e2ef1..eef19b4a29 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "1553dd6ec345f3e12b374bb03e72baa1", + "content-hash": "ba5d5c5efdbd54c4c986193c090b1760", "packages": [ { "name": "composer/package-versions-deprecated", @@ -716,6 +716,65 @@ }, "time": "2022-02-21T01:04:05+00:00" }, + { + "name": "phpstan/phpstan", + "version": "1.8.11", + "source": { + "type": "git", + "url": "https://github.com/phpstan/phpstan.git", + "reference": "46e223dd68a620da18855c23046ddb00940b4014" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/phpstan/phpstan/zipball/46e223dd68a620da18855c23046ddb00940b4014", + "reference": "46e223dd68a620da18855c23046ddb00940b4014", + "shasum": "" + }, + "require": { + "php": "^7.2|^8.0" + }, + "conflict": { + "phpstan/phpstan-shim": "*" + }, + "bin": [ + "phpstan", + "phpstan.phar" + ], + "type": "library", + "autoload": { + "files": [ + "bootstrap.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "description": "PHPStan - PHP Static Analysis Tool", + "keywords": [ + "dev", + "static analysis" + ], + "support": { + "issues": "https://github.com/phpstan/phpstan/issues", + "source": "https://github.com/phpstan/phpstan/tree/1.8.11" + }, + "funding": [ + { + "url": "https://github.com/ondrejmirtes", + "type": "github" + }, + { + "url": "https://github.com/phpstan", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/phpstan/phpstan", + "type": "tidelift" + } + ], + "time": "2022-10-24T15:45:13+00:00" + }, { "name": "phpunit/php-code-coverage", "version": "9.2.18", From e736514d38a390b8d67bfd78c8133bfc81585ed4 Mon Sep 17 00:00:00 2001 From: fogelito Date: Thu, 27 Oct 2022 18:37:13 +0300 Subject: [PATCH 14/34] Change Exception --- src/Client.php | 9 +++------ src/Exception.php | 6 ++++++ src/Exception/Duplicate.php | 6 ------ tests/MongoTest.php | 7 +++---- 4 files changed, 12 insertions(+), 16 deletions(-) create mode 100644 src/Exception.php delete mode 100644 src/Exception/Duplicate.php diff --git a/src/Client.php b/src/Client.php index 057fb04265..2541ba0ef3 100644 --- a/src/Client.php +++ b/src/Client.php @@ -2,12 +2,10 @@ namespace Utopia\Mongo; -use Exception; use MongoDB\BSON; use Swoole\Client as SwooleClient; use Swoole\Coroutine\Client as CoroutineClient; use stdClass; -use Utopia\Mongo\Exception\Duplicate; class Client { @@ -172,7 +170,6 @@ class Client /** * Receive a message from connection. - * @throws Duplicate * @throws Exception */ private function receive(): stdClass|array|int @@ -200,7 +197,7 @@ class Client $result = BSON\toPHP(substr($res, 21, $responseLength - 21)); if (property_exists($result, "writeErrors")) { - throw new Duplicate($result->writeErrors[0]->errmsg); + throw new Exception($result->writeErrors[0]->errmsg); } if (property_exists($result, "n") && $result->ok === 1.0) { @@ -283,14 +280,14 @@ class Client * @param string $name * @param array $options * @return bool - * @throws Duplicate + * @throws Exception */ public function createCollection(string $name, array $options = []): bool { $list = $this->listCollectionNames(["name" => $name]); if (\count($list->cursor->firstBatch) > 0) { - throw new Duplicate('Collection Exists'); + throw new Exception('Collection Exists'); } $res = $this->query(array_merge([ diff --git a/src/Exception.php b/src/Exception.php new file mode 100644 index 0000000000..ddc141f147 --- /dev/null +++ b/src/Exception.php @@ -0,0 +1,6 @@ +getDatabase()->createCollection('movies')); self::assertTrue($this->getDatabase()->dropCollection('movies')); self::assertTrue($this->getDatabase()->createCollection('movies')); - self::expectException(Duplicate::class); + self::expectException(Exception::class); self::assertTrue($this->getDatabase()->createCollection('movies')); } From 283d2274d6846b30cf9aed2db438d0f17f95f4ea Mon Sep 17 00:00:00 2001 From: fogelito Date: Thu, 27 Oct 2022 18:46:08 +0300 Subject: [PATCH 15/34] Change Exception --- src/Client.php | 7 ++++++- tests/MongoTest.php | 12 ++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/Client.php b/src/Client.php index 2541ba0ef3..1ea91d0090 100644 --- a/src/Client.php +++ b/src/Client.php @@ -197,7 +197,12 @@ class Client $result = BSON\toPHP(substr($res, 21, $responseLength - 21)); if (property_exists($result, "writeErrors")) { - throw new Exception($result->writeErrors[0]->errmsg); + // Throws a Utopia\Mongo\Exception with Code Error + + throw new Exception( + $result->writeErrors[0]->errmsg, + $result->writeErrors[0]->code + ); } if (property_exists($result, "n") && $result->ok === 1.0) { diff --git a/tests/MongoTest.php b/tests/MongoTest.php index e12e3cc5dd..02d3e87ae5 100644 --- a/tests/MongoTest.php +++ b/tests/MongoTest.php @@ -92,6 +92,18 @@ class MongoTest extends TestCase ] ); + $doc = $this->getDatabase()->insert('movies', [ + '_id' => $id, + 'array' => ['USA', 'UK', 'India'], + 'language' => 'English', + 'float' => 9.9, + 'integer' => 9, + 'is_open' => true, + 'date_string' => (new \DateTime())->format('Y-m-d\TH:i:s.vP'), + 'date_object' => new \DateTime() + ] + ); + $doc = $this->getDatabase()->find('movies', ['_id' => $id])->cursor->firstBatch ?? []; $doc = $doc[0]; From 2395fe9aea99821d19bf261945970742d578682c Mon Sep 17 00:00:00 2001 From: fogelito Date: Thu, 27 Oct 2022 18:56:19 +0300 Subject: [PATCH 16/34] Changes from wess pr --- src/Client.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Client.php b/src/Client.php index 1ea91d0090..72b1d93a1b 100644 --- a/src/Client.php +++ b/src/Client.php @@ -432,11 +432,11 @@ class Client 'documents' => [$docObj], ], $options)); - return $this->lastInsertedDocument($collection); + return $this->toArray($docObj); } /** - * Retrieve the last inserted document. + * Retrieve the last lastDocument * * @param string $collection * @@ -444,7 +444,7 @@ class Client * @throws Exception */ - public function lastInsertedDocument(string $collection): array + public function lastDocument(string $collection): array { $result = $this->find($collection, [], [ 'sort' => ['_id' => -1], From e93d1fd1b8b0dae24afc0a76f9f309d58a3b8abb Mon Sep 17 00:00:00 2001 From: fogelito Date: Thu, 27 Oct 2022 19:04:31 +0300 Subject: [PATCH 17/34] toArray generic --- src/Client.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Client.php b/src/Client.php index 72b1d93a1b..8287701efa 100644 --- a/src/Client.php +++ b/src/Client.php @@ -2,6 +2,7 @@ namespace Utopia\Mongo; +use DateTime; use MongoDB\BSON; use Swoole\Client as SwooleClient; use Swoole\Coroutine\Client as CoroutineClient; @@ -694,11 +695,10 @@ class Client /** * Convert an object (stdClass) to an assoc array. * - * @param stdClass|array|string|null $obj - * + * @param mixed $obj * @return array|null */ - public function toArray(stdClass|array|string|null $obj): array|null + public function toArray(mixed $obj): ?array { if(\is_null($obj)) return null; From 217ff61fe2ed767f1f84d8d890f0cf8d9a500206 Mon Sep 17 00:00:00 2001 From: fogelito Date: Thu, 27 Oct 2022 19:04:53 +0300 Subject: [PATCH 18/34] remove use datetime --- src/Client.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Client.php b/src/Client.php index 8287701efa..755737310e 100644 --- a/src/Client.php +++ b/src/Client.php @@ -2,7 +2,6 @@ namespace Utopia\Mongo; -use DateTime; use MongoDB\BSON; use Swoole\Client as SwooleClient; use Swoole\Coroutine\Client as CoroutineClient; From b760f9bf8ac915313180a22a90caaaadf63f62ad Mon Sep 17 00:00:00 2001 From: fogelito Date: Thu, 27 Oct 2022 19:09:36 +0300 Subject: [PATCH 19/34] expectExceptionCode --- tests/MongoTest.php | 21 ++++++--------------- 1 file changed, 6 insertions(+), 15 deletions(-) diff --git a/tests/MongoTest.php b/tests/MongoTest.php index 02d3e87ae5..c435faca6a 100644 --- a/tests/MongoTest.php +++ b/tests/MongoTest.php @@ -79,9 +79,8 @@ class MongoTest extends TestCase ] ); - $id = 999; $doc = $this->getDatabase()->insert('movies', [ - '_id' => $id, + '_id' => 999, 'array' => ['USA', 'UK', 'India'], 'language' => 'English', 'float' => 9.9, @@ -92,19 +91,7 @@ class MongoTest extends TestCase ] ); - $doc = $this->getDatabase()->insert('movies', [ - '_id' => $id, - 'array' => ['USA', 'UK', 'India'], - 'language' => 'English', - 'float' => 9.9, - 'integer' => 9, - 'is_open' => true, - 'date_string' => (new \DateTime())->format('Y-m-d\TH:i:s.vP'), - 'date_object' => new \DateTime() - ] - ); - - $doc = $this->getDatabase()->find('movies', ['_id' => $id])->cursor->firstBatch ?? []; + $doc = $this->getDatabase()->find('movies', ['_id' => 999])->cursor->firstBatch ?? []; $doc = $doc[0]; self::assertTrue($doc->is_open); @@ -114,6 +101,10 @@ class MongoTest extends TestCase self::assertIsString($doc->date_string); self::assertIsObject($doc->date_object); // Todo: This is not working can't retrieve the object back + self::expectException(Exception::class); + self::expectExceptionCode(11000); + $this->getDatabase()->insert('movies', ['_id' => 999, 'b' => 'Duplication']); + } } From 22a0b15d059b2a21d8688e0106f43b3e3129c77c Mon Sep 17 00:00:00 2001 From: fogelito Date: Wed, 2 Nov 2022 15:24:25 +0200 Subject: [PATCH 20/34] toObject wess fix --- src/Client.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Client.php b/src/Client.php index 755737310e..d95602be75 100644 --- a/src/Client.php +++ b/src/Client.php @@ -685,7 +685,7 @@ class Client $obj = new stdClass(); foreach ($dict as $k => $v) { - $obj->{$k} = $v; + $obj->{$k} = is_array($v) ? $this->toObject($v) : $v; } return $obj; From 09834889e31ef28ee8e37f7a0ba353cb9aa04c21 Mon Sep 17 00:00:00 2001 From: Torsten Dittmann Date: Wed, 2 Nov 2022 17:59:32 +0100 Subject: [PATCH 21/34] feat: draft for adapter pattern --- composer.lock | 14 +++++++------- src/Adapter.php | 15 +++++++++++++++ src/Adapter/SwooleClient.php | 11 +++++++++++ src/Adapter/SwooleCoroutineClient.php | 10 ++++++++++ src/Client.php | 9 +++------ 5 files changed, 46 insertions(+), 13 deletions(-) create mode 100644 src/Adapter.php create mode 100644 src/Adapter/SwooleClient.php create mode 100644 src/Adapter/SwooleCoroutineClient.php diff --git a/composer.lock b/composer.lock index eef19b4a29..93d5777530 100644 --- a/composer.lock +++ b/composer.lock @@ -1095,16 +1095,16 @@ }, { "name": "phpunit/phpunit", - "version": "9.5.25", + "version": "9.5.26", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/phpunit.git", - "reference": "3e6f90ca7e3d02025b1d147bd8d4a89fd4ca8a1d" + "reference": "851867efcbb6a1b992ec515c71cdcf20d895e9d2" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/3e6f90ca7e3d02025b1d147bd8d4a89fd4ca8a1d", - "reference": "3e6f90ca7e3d02025b1d147bd8d4a89fd4ca8a1d", + "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/851867efcbb6a1b992ec515c71cdcf20d895e9d2", + "reference": "851867efcbb6a1b992ec515c71cdcf20d895e9d2", "shasum": "" }, "require": { @@ -1177,7 +1177,7 @@ ], "support": { "issues": "https://github.com/sebastianbergmann/phpunit/issues", - "source": "https://github.com/sebastianbergmann/phpunit/tree/9.5.25" + "source": "https://github.com/sebastianbergmann/phpunit/tree/9.5.26" }, "funding": [ { @@ -1193,7 +1193,7 @@ "type": "tidelift" } ], - "time": "2022-09-25T03:44:45+00:00" + "time": "2022-10-28T06:00:21+00:00" }, { "name": "psr/container", @@ -2382,5 +2382,5 @@ "ext-mongodb": "*" }, "platform-dev": [], - "plugin-api-version": "2.2.0" + "plugin-api-version": "2.3.0" } diff --git a/src/Adapter.php b/src/Adapter.php new file mode 100644 index 0000000000..521b01ca51 --- /dev/null +++ b/src/Adapter.php @@ -0,0 +1,15 @@ +id = uniqid('utopia.mongo.client'); $this->database = $database; $this->host = $host; $this->port = $port; - - $this->client = $useCoroutine - ? new CoroutineClient(SWOOLE_SOCK_TCP | SWOOLE_KEEP) - : new SwooleClient(SWOOLE_SOCK_TCP | SWOOLE_KEEP); + $this->client = $adapter; $this->auth = new Auth([ 'authcid' => $user, From 653dc48feb713b8708b4c4654f04024914a23db1 Mon Sep 17 00:00:00 2001 From: fogelito Date: Wed, 2 Nov 2022 19:18:16 +0200 Subject: [PATCH 22/34] reverting --- src/Client.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Client.php b/src/Client.php index d95602be75..755737310e 100644 --- a/src/Client.php +++ b/src/Client.php @@ -685,7 +685,7 @@ class Client $obj = new stdClass(); foreach ($dict as $k => $v) { - $obj->{$k} = is_array($v) ? $this->toObject($v) : $v; + $obj->{$k} = $v; } return $obj; From b68a170c76bf92eec92c6be34a2eaa1789953b38 Mon Sep 17 00:00:00 2001 From: wess Date: Wed, 2 Nov 2022 13:22:34 -0400 Subject: [PATCH 23/34] Adds filter cleaning for keywords and, or, nor --- src/Client.php | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/Client.php b/src/Client.php index 755737310e..abeda06e2f 100644 --- a/src/Client.php +++ b/src/Client.php @@ -547,6 +547,8 @@ class Client */ public function find(string $collection, array $filters = [], array $options = []): stdClass { + $filters = $this->cleanFilters($filters); + return $this->query( array_merge([ self::COMMAND_FIND => $collection, @@ -712,4 +714,26 @@ class Client return [$obj]; } + + private function cleanFilters($filters):array { + foreach($filters as $k => $v) { + $value = $v; + + if(in_array($k, ['$and', '$or', '$nor']) && is_array($v)) { + $values = []; + + foreach($v as $item) { + if(is_null($item)) continue; + + $values[] = is_array($item) ? $this->toObject($item) : $item; + } + + $value = $values; + } + + $cleanedFilters[$k] = $value; + } + + return $cleanedFilters; + } } \ No newline at end of file From 64866393649f039011a806217b298d29cb844b3d Mon Sep 17 00:00:00 2001 From: wess Date: Wed, 2 Nov 2022 13:25:39 -0400 Subject: [PATCH 24/34] Adds predefined variable for results --- src/Client.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Client.php b/src/Client.php index abeda06e2f..e8e61d49be 100644 --- a/src/Client.php +++ b/src/Client.php @@ -548,7 +548,7 @@ class Client public function find(string $collection, array $filters = [], array $options = []): stdClass { $filters = $this->cleanFilters($filters); - + return $this->query( array_merge([ self::COMMAND_FIND => $collection, @@ -716,6 +716,8 @@ class Client } private function cleanFilters($filters):array { + $cleanFilters = []; + foreach($filters as $k => $v) { $value = $v; From e2704f0a58f33cd845eb8aefd1cabc104525e313 Mon Sep 17 00:00:00 2001 From: wess Date: Wed, 2 Nov 2022 13:29:09 -0400 Subject: [PATCH 25/34] Typo --- src/Client.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Client.php b/src/Client.php index e8e61d49be..f7e80e97e0 100644 --- a/src/Client.php +++ b/src/Client.php @@ -716,7 +716,7 @@ class Client } private function cleanFilters($filters):array { - $cleanFilters = []; + $cleanedFilters = []; foreach($filters as $k => $v) { $value = $v; From 9f2e3028c22c49a710a922fadeea4b6d6dc49a32 Mon Sep 17 00:00:00 2001 From: fogelito Date: Thu, 3 Nov 2022 16:41:02 +0200 Subject: [PATCH 26/34] reverting --- src/Adapter/SwooleClient.php | 19 +++++++++++++++++++ src/Adapter/SwooleCoroutineClient.php | 19 +++++++++++++++++++ src/Client.php | 17 ++++++++++------- tests/MongoTest.php | 1 + 4 files changed, 49 insertions(+), 7 deletions(-) diff --git a/src/Adapter/SwooleClient.php b/src/Adapter/SwooleClient.php index dbb44470f0..c0f9386f0e 100644 --- a/src/Adapter/SwooleClient.php +++ b/src/Adapter/SwooleClient.php @@ -8,4 +8,23 @@ use Utopia\Mongo\Adapter; class SwooleClient extends Adapter { + public function isConnected(): bool + { + // TODO: Implement isConnected() method. + } + + public function connect(string $host, int $port = null, float $timeout = null): bool + { + // TODO: Implement connect() method. + } + + public function send(mixed $data): int|bool + { + // TODO: Implement send() method. + } + + public function recv(int $timeout = null): string|bool + { + // TODO: Implement recv() method. + } } diff --git a/src/Adapter/SwooleCoroutineClient.php b/src/Adapter/SwooleCoroutineClient.php index 9dd62dbd89..783c0dfe93 100644 --- a/src/Adapter/SwooleCoroutineClient.php +++ b/src/Adapter/SwooleCoroutineClient.php @@ -7,4 +7,23 @@ use Utopia\Mongo\Adapter; class SwooleCoroutineClient extends Adapter { + public function isConnected(): bool + { + // TODO: Implement isConnected() method. + } + + public function connect(string $host, int $port = null, float $timeout = null): bool + { + // TODO: Implement connect() method. + } + + public function send(mixed $data): int|bool + { + // TODO: Implement send() method. + } + + public function recv(int $timeout = null): string|bool + { + // TODO: Implement recv() method. + } } \ No newline at end of file diff --git a/src/Client.php b/src/Client.php index e724a348c0..74d7d48304 100644 --- a/src/Client.php +++ b/src/Client.php @@ -17,7 +17,7 @@ class Client /** * Socket (sync or async) client. */ - private Adapter $client; + private SwooleClient|CoroutineClient $client; /** * Defines commands Mongo uses over wire protocol. @@ -72,13 +72,16 @@ class Client int $port, string $user, string $password, - Adapter $adapter + bool $useCoroutine = true ){ $this->id = uniqid('utopia.mongo.client'); $this->database = $database; $this->host = $host; $this->port = $port; - $this->client = $adapter; + + $this->client = $useCoroutine + ? new CoroutineClient(SWOOLE_SOCK_TCP | SWOOLE_KEEP) + : new SwooleClient(SWOOLE_SOCK_TCP | SWOOLE_KEEP); $this->auth = new Auth([ 'authcid' => $user, @@ -215,7 +218,7 @@ class Client } if ($result->ok === 1.0) { - return $result; + return $result; } return $result->cursor->firstBatch; @@ -714,13 +717,13 @@ class Client private function cleanFilters($filters):array { $cleanedFilters = []; - + foreach($filters as $k => $v) { $value = $v; if(in_array($k, ['$and', '$or', '$nor']) && is_array($v)) { $values = []; - + foreach($v as $item) { if(is_null($item)) continue; @@ -735,4 +738,4 @@ class Client return $cleanedFilters; } -} \ No newline at end of file +} diff --git a/tests/MongoTest.php b/tests/MongoTest.php index c435faca6a..273a48ada7 100644 --- a/tests/MongoTest.php +++ b/tests/MongoTest.php @@ -4,6 +4,7 @@ namespace Utopia\Tests; use MongoDB\BSON\ObjectId; use PHPUnit\Framework\TestCase; +use Utopia\Mongo\Adapter\SwooleClient; use Utopia\Mongo\Client; use Utopia\Mongo\Exception; From 89ab5f8adea32473b829e01ba46cbc76b4acb42d Mon Sep 17 00:00:00 2001 From: fogelito Date: Fri, 4 Nov 2022 00:21:21 +0200 Subject: [PATCH 27/34] composer.lock --- composer.lock | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.lock b/composer.lock index 93d5777530..ce1e3b48fa 100644 --- a/composer.lock +++ b/composer.lock @@ -2382,5 +2382,5 @@ "ext-mongodb": "*" }, "platform-dev": [], - "plugin-api-version": "2.3.0" + "plugin-api-version": "2.2.0" } From d8fdcda057ad8ae2f8bb394686e574a1ef5e8c33 Mon Sep 17 00:00:00 2001 From: fogelito Date: Fri, 4 Nov 2022 00:34:43 +0200 Subject: [PATCH 28/34] upgrade version to 5 --- docker-compose.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docker-compose.yml b/docker-compose.yml index 351fe8a02e..554a98dea4 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -14,7 +14,7 @@ services: - "8882:8882" mongo: - image: mongo:4.4 + image: mongo:5 container_name: utopia-mongo-1 networks: - database From 0dedc23251642b4322365228370627ee10ad8060 Mon Sep 17 00:00:00 2001 From: fogelito Date: Fri, 4 Nov 2022 00:53:10 +0200 Subject: [PATCH 29/34] coroutine = true --- tests/MongoTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/MongoTest.php b/tests/MongoTest.php index 273a48ada7..5aa34f3458 100644 --- a/tests/MongoTest.php +++ b/tests/MongoTest.php @@ -21,7 +21,7 @@ class MongoTest extends TestCase return self::$db; } - $client = new Client('testing', 'mongo', 27017, 'root', 'example', false); + $client = new Client('testing', 'mongo', 27017, 'root', 'example', true); $client->connect(); self::$db = $client; From 06647ce7fc014803581c591efec7c9c1c3af904f Mon Sep 17 00:00:00 2001 From: fogelito Date: Fri, 4 Nov 2022 00:53:37 +0200 Subject: [PATCH 30/34] coroutine = true --- tests/MongoTest.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/MongoTest.php b/tests/MongoTest.php index 5aa34f3458..3db6261015 100644 --- a/tests/MongoTest.php +++ b/tests/MongoTest.php @@ -21,6 +21,7 @@ class MongoTest extends TestCase return self::$db; } + // todo: revert true to false $client = new Client('testing', 'mongo', 27017, 'root', 'example', true); $client->connect(); From ea31c8c1afcb0af7cf9fa1dde5eced68ac9537d0 Mon Sep 17 00:00:00 2001 From: fogelito Date: Mon, 7 Nov 2022 18:30:44 +0200 Subject: [PATCH 31/34] revert $useCoroutine --- tests/MongoTest.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/MongoTest.php b/tests/MongoTest.php index 3db6261015..273a48ada7 100644 --- a/tests/MongoTest.php +++ b/tests/MongoTest.php @@ -21,8 +21,7 @@ class MongoTest extends TestCase return self::$db; } - // todo: revert true to false - $client = new Client('testing', 'mongo', 27017, 'root', 'example', true); + $client = new Client('testing', 'mongo', 27017, 'root', 'example', false); $client->connect(); self::$db = $client; From 5596830d2103709b02f566fb4d84bb5acdbb4823 Mon Sep 17 00:00:00 2001 From: fogelito Date: Mon, 7 Nov 2022 19:44:15 +0200 Subject: [PATCH 32/34] php stan add .neon file --- phpstan.neon | 4 ++++ pint.json | 5 ++++- src/Adapter/SwooleClient.php | 1 - 3 files changed, 8 insertions(+), 2 deletions(-) create mode 100644 phpstan.neon diff --git a/phpstan.neon b/phpstan.neon new file mode 100644 index 0000000000..e0629c9276 --- /dev/null +++ b/phpstan.neon @@ -0,0 +1,4 @@ +parameters: + paths: + - src + level: 6 \ No newline at end of file diff --git a/pint.json b/pint.json index ef654c64d1..940cc35bf1 100644 --- a/pint.json +++ b/pint.json @@ -1,3 +1,6 @@ { - "preset": "psr12" + "preset": "psr12", + "exclude": [ + "tests/" + ] } \ No newline at end of file diff --git a/src/Adapter/SwooleClient.php b/src/Adapter/SwooleClient.php index c0f9386f0e..9dce323acd 100644 --- a/src/Adapter/SwooleClient.php +++ b/src/Adapter/SwooleClient.php @@ -2,7 +2,6 @@ namespace Utopia\Mongo\Adapter; -use Swoole\Client; use Utopia\Mongo\Adapter; class SwooleClient extends Adapter From 8b5f9cab339d0ecd76b59de0db67a00948492f82 Mon Sep 17 00:00:00 2001 From: fogelito Date: Tue, 8 Nov 2022 11:09:34 +0200 Subject: [PATCH 33/34] pint check --- pint.json | 5 +--- src/Adapter/SwooleClient.php | 1 - src/Adapter/SwooleCoroutineClient.php | 3 +- src/Auth.php | 4 +-- src/Client.php | 42 +++++++++++++++++---------- src/Exception.php | 3 +- tests/MongoTest.php | 19 +++++++----- 7 files changed, 43 insertions(+), 34 deletions(-) diff --git a/pint.json b/pint.json index 940cc35bf1..ef654c64d1 100644 --- a/pint.json +++ b/pint.json @@ -1,6 +1,3 @@ { - "preset": "psr12", - "exclude": [ - "tests/" - ] + "preset": "psr12" } \ No newline at end of file diff --git a/src/Adapter/SwooleClient.php b/src/Adapter/SwooleClient.php index 9dce323acd..c1a86f24ac 100644 --- a/src/Adapter/SwooleClient.php +++ b/src/Adapter/SwooleClient.php @@ -6,7 +6,6 @@ use Utopia\Mongo\Adapter; class SwooleClient extends Adapter { - public function isConnected(): bool { // TODO: Implement isConnected() method. diff --git a/src/Adapter/SwooleCoroutineClient.php b/src/Adapter/SwooleCoroutineClient.php index 783c0dfe93..07d5f5eca2 100644 --- a/src/Adapter/SwooleCoroutineClient.php +++ b/src/Adapter/SwooleCoroutineClient.php @@ -6,7 +6,6 @@ use Utopia\Mongo\Adapter; class SwooleCoroutineClient extends Adapter { - public function isConnected(): bool { // TODO: Implement isConnected() method. @@ -26,4 +25,4 @@ class SwooleCoroutineClient extends Adapter { // TODO: Implement recv() method. } -} \ No newline at end of file +} diff --git a/src/Auth.php b/src/Auth.php index 352a296207..047959bc39 100644 --- a/src/Auth.php +++ b/src/Auth.php @@ -285,8 +285,8 @@ class Auth /** * @return string */ - static function encodeCredentials($username, $password): string + public static function encodeCredentials($username, $password): string { return \md5(\utf8_encode($username . ':mongo:' . $password)); } -} \ No newline at end of file +} diff --git a/src/Client.php b/src/Client.php index 74d7d48304..d224c655fd 100644 --- a/src/Client.php +++ b/src/Client.php @@ -73,7 +73,7 @@ class Client string $user, string $password, bool $useCoroutine = true - ){ + ) { $this->id = uniqid('utopia.mongo.client'); $this->database = $database; $this->host = $host; @@ -96,7 +96,9 @@ class Client */ public function connect(): self { - if($this->client->isConnected()) return $this; + if ($this->client->isConnected()) { + return $this; + } $this->client->connect($this->host, $this->port); [$payload, $db] = $this->auth->start(); @@ -332,7 +334,6 @@ class Client */ public function listCollectionNames(array $filter = [], array $options = []): stdClass { - $qry = array_merge( [ "listCollections" => 1.0, @@ -420,7 +421,9 @@ class Client $docObj = new stdClass(); foreach ($document as $key => $value) { - if(\is_null($value)) continue; + if (\is_null($value)) { + continue; + } $docObj->{$key} = $value; } @@ -468,11 +471,12 @@ class Client */ public function update(string $collection, array $where = [], array $updates = [], array $options = []): self { - $cleanUpdates = []; - foreach($updates as $k => $v) { - if(\is_null($v)) continue; + foreach ($updates as $k => $v) { + if (\is_null($v)) { + continue; + } $cleanUpdates[$k] = $v; } @@ -510,8 +514,10 @@ class Client { $cleanUpdates = []; - foreach($updates as $k => $v) { - if(\is_null($v)) continue; + foreach ($updates as $k => $v) { + if (\is_null($v)) { + continue; + } $cleanUpdates[$k] = $v; } @@ -555,7 +561,6 @@ class Client 'filter' => $this->toObject($filters), ], $options) ); - } /** @@ -701,7 +706,9 @@ class Client */ public function toArray(mixed $obj): ?array { - if(\is_null($obj)) return null; + if (\is_null($obj)) { + return null; + } if (is_object($obj) || is_array($obj)) { $ret = (array) $obj; @@ -715,17 +722,20 @@ class Client return [$obj]; } - private function cleanFilters($filters):array { + private function cleanFilters($filters): array + { $cleanedFilters = []; - foreach($filters as $k => $v) { + foreach ($filters as $k => $v) { $value = $v; - if(in_array($k, ['$and', '$or', '$nor']) && is_array($v)) { + if (in_array($k, ['$and', '$or', '$nor']) && is_array($v)) { $values = []; - foreach($v as $item) { - if(is_null($item)) continue; + foreach ($v as $item) { + if (is_null($item)) { + continue; + } $values[] = is_array($item) ? $this->toObject($item) : $item; } diff --git a/src/Exception.php b/src/Exception.php index ddc141f147..7d0cf47979 100644 --- a/src/Exception.php +++ b/src/Exception.php @@ -1,6 +1,7 @@ getDatabase()->insert('movies', [ + $doc = $this->getDatabase()->insert( + 'movies', + [ 'name' => 'Armageddon', 'country' => 'USA', 'language' => 'English' @@ -73,14 +74,18 @@ class MongoTest extends TestCase $id = (string)$doc['_id']; self::assertEquals(24, strlen($id)); - $doc = $this->getDatabase()->insert('movies', [ + $doc = $this->getDatabase()->insert( + 'movies', + [ 'name' => 300, 'country' => 'USA', 'language' => 'English' ] ); - $doc = $this->getDatabase()->insert('movies', [ + $doc = $this->getDatabase()->insert( + 'movies', + [ '_id' => 999, 'array' => ['USA', 'UK', 'India'], 'language' => 'English', @@ -105,7 +110,5 @@ class MongoTest extends TestCase self::expectException(Exception::class); self::expectExceptionCode(11000); $this->getDatabase()->insert('movies', ['_id' => 999, 'b' => 'Duplication']); - } - } From 49189d1928c27e4fa2de284e6e5e6b0c83eb0f6e Mon Sep 17 00:00:00 2001 From: fogelito Date: Tue, 8 Nov 2022 12:43:02 +0200 Subject: [PATCH 34/34] phpstan updates --- phpstan.neon | 2 +- src/Auth.php | 48 ++++++++++++++++++++++-------------------------- src/Client.php | 8 +++++--- 3 files changed, 28 insertions(+), 30 deletions(-) diff --git a/phpstan.neon b/phpstan.neon index e0629c9276..b6f7d44b32 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -1,4 +1,4 @@ parameters: paths: - src - level: 6 \ No newline at end of file + level: 4 \ No newline at end of file diff --git a/src/Auth.php b/src/Auth.php index 047959bc39..2cc1435882 100644 --- a/src/Auth.php +++ b/src/Auth.php @@ -15,14 +15,15 @@ class Auth private string $saltedPassword; private string $authMessage; + /** + * @throws \Exception + */ public function __construct(array $options) { - $this->authcid = isset($options['authcid']) ? $options['authcid'] : ''; - $this->secret = isset($options['secret']) ? $options['secret'] : ''; - $this->authzid = isset($options['authzid']) ? $options['authzid'] : ''; - $this->service = isset($options['service']) ? $options['service'] : ''; - - $this->nonce = base64_encode(random_bytes(32)); + $this->authcid = $options['authcid'] ?? ''; + $this->secret = $options['secret'] ?? ''; + $this->authzid = $options['authzid'] ?? ''; + $this->cnonce = base64_encode(random_bytes(32)); } public function start(): array @@ -65,10 +66,10 @@ class Auth } /** - * @param mixed $challenge - * @return string + * @param mixed|null $challenge + * @return string|bool */ - public function createResponse($challenge = null): string + public function createResponse(mixed $challenge = null): string|bool { $authcid = $this->formatName($this->authcid); if (empty($authcid)) { @@ -156,11 +157,11 @@ class Auth $finalMessage = $channelBinding . ',r=' . $nonce; $saltedPassword = $this->hi($password, $salt, $i); $this->saltedPassword = $saltedPassword; - $clientKey = $this->hmac($saltedPassword, "Client Key", true); - $storedKey = $this->hash($clientKey, true); + $clientKey = $this->hmac($saltedPassword, "Client Key"); + $storedKey = $this->hash($clientKey); $authMessage = $this->firstMessageBare . ',' . $challenge . ',' . $finalMessage; $this->authMessage = $authMessage; - $clientSignature = $this->hmac($storedKey, $authMessage, true); + $clientSignature = $this->hmac($storedKey, $authMessage); $clientProof = $clientKey ^ $clientSignature; $proof = ',p=' . base64_encode($clientProof); @@ -178,10 +179,10 @@ class Auth private function hi(string $str, string $salt, int $i): string { $int1 = "\0\0\0\1"; - $ui = $this->hmac($str, $salt . $int1, true); + $ui = $this->hmac($str, $salt . $int1); $result = $ui; for ($k = 1; $k < $i; $k++) { - $ui = $this->hmac($str, $ui, true); + $ui = $this->hmac($str, $ui); $result = $result ^ $ui; } return $result; @@ -208,8 +209,8 @@ class Auth $verifier = $matches[1]; $proposedServerSignature = base64_decode($verifier); - $serverKey = $this->hmac($this->saltedPassword, "Server Key", true); - $serverSignature = $this->hmac($serverKey, $this->authMessage, true); + $serverKey = $this->hmac($this->saltedPassword, "Server Key"); + $serverSignature = $this->hmac($serverKey, $this->authMessage); return $proposedServerSignature === $serverSignature; } @@ -239,14 +240,7 @@ class Auth } /** - * @return string - */ - public function getHashAlgo(): string - { - return $this->hashAlgo; - } - - /** + * @param $data * @return string */ private function hash($data): string @@ -255,11 +249,13 @@ class Auth } /** + * @param $key + * @param $str * @return string */ - private function hmac($key, $str, $raw): string + private function hmac($key, $str): string { - return hash_hmac('sha1', $str, $key, $raw); + return hash_hmac('sha1', $str, $key, true); } /** diff --git a/src/Client.php b/src/Client.php index d224c655fd..562d2d7a12 100644 --- a/src/Client.php +++ b/src/Client.php @@ -125,7 +125,7 @@ class Client /** * Send a BSON packed query to connection. * - * @param array $command + * @param array $command * @param string|null $db * @return stdClass|array|int * @throws Exception @@ -196,6 +196,9 @@ class Client (!isset($responseLength)) || ($receivedLength < $responseLength) ); + /** + * @var stdClass $result + */ $result = BSON\toPHP(substr($res, 21, $responseLength - 21)); if (property_exists($result, "writeErrors")) { @@ -594,8 +597,7 @@ class Client * * @param int $cursorId * @param string $collection - * @param int batchSize - * + * @param int $batchSize * @return stdClass * @throws Exception */