From 088d890e1646a143ee95eb17e983e7944b3bcecd Mon Sep 17 00:00:00 2001 From: shimon Date: Sun, 20 Jul 2025 13:59:11 +0300 Subject: [PATCH 01/16] merged bulkUpsert() with upsert --- composer.json | 2 +- composer.lock | 95 ++++++++++++++++++++++++++++++++++++++++----- src/Client.php | 57 ++++++--------------------- tests/MongoTest.php | 9 +++-- 4 files changed, 103 insertions(+), 60 deletions(-) diff --git a/composer.json b/composer.json index caf216322b..1ee3dcb499 100755 --- a/composer.json +++ b/composer.json @@ -30,7 +30,7 @@ "require": { "php": ">=8.0", "ext-mongodb": "*", - "mongodb/mongodb": "^1.21" + "mongodb/mongodb": "2.1.0" }, "require-dev": { "fakerphp/faker": "^1.14", diff --git a/composer.lock b/composer.lock index 52cee61cbf..0fa64a4865 100644 --- a/composer.lock +++ b/composer.lock @@ -4,27 +4,28 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "104758a98a297851d8985a60cbfa65bc", + "content-hash": "6def0d090ef1b5df4a7d2af3ea71fe1c", "packages": [ { "name": "mongodb/mongodb", - "version": "1.21.1", + "version": "2.1.0", "source": { "type": "git", "url": "https://github.com/mongodb/mongo-php-library.git", - "reference": "37bc8df3a67ddf8380704a5ba5dbd00e92ec1f6a" + "reference": "3bbe7ba9578724c7e1f47fcd17c881c0995baaad" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/mongodb/mongo-php-library/zipball/37bc8df3a67ddf8380704a5ba5dbd00e92ec1f6a", - "reference": "37bc8df3a67ddf8380704a5ba5dbd00e92ec1f6a", + "url": "https://api.github.com/repos/mongodb/mongo-php-library/zipball/3bbe7ba9578724c7e1f47fcd17c881c0995baaad", + "reference": "3bbe7ba9578724c7e1f47fcd17c881c0995baaad", "shasum": "" }, "require": { "composer-runtime-api": "^2.0", - "ext-mongodb": "^1.21.0", + "ext-mongodb": "^2.1", "php": "^8.1", - "psr/log": "^1.1.4|^2|^3" + "psr/log": "^1.1.4|^2|^3", + "symfony/polyfill-php85": "^1.32" }, "replace": { "mongodb/builder": "*" @@ -78,9 +79,9 @@ ], "support": { "issues": "https://github.com/mongodb/mongo-php-library/issues", - "source": "https://github.com/mongodb/mongo-php-library/tree/1.21.1" + "source": "https://github.com/mongodb/mongo-php-library/tree/2.1.0" }, - "time": "2025-02-28T17:24:20+00:00" + "time": "2025-05-23T10:48:05+00:00" }, { "name": "psr/log", @@ -131,6 +132,82 @@ "source": "https://github.com/php-fig/log/tree/3.0.2" }, "time": "2024-09-11T13:17:53+00:00" + }, + { + "name": "symfony/polyfill-php85", + "version": "v1.32.0", + "source": { + "type": "git", + "url": "https://github.com/symfony/polyfill-php85.git", + "reference": "6fedf31ce4e3648f4ff5ca58bfd53127d38f05fd" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/polyfill-php85/zipball/6fedf31ce4e3648f4ff5ca58bfd53127d38f05fd", + "reference": "6fedf31ce4e3648f4ff5ca58bfd53127d38f05fd", + "shasum": "" + }, + "require": { + "php": ">=7.2" + }, + "type": "library", + "extra": { + "thanks": { + "url": "https://github.com/symfony/polyfill", + "name": "symfony/polyfill" + } + }, + "autoload": { + "files": [ + "bootstrap.php" + ], + "psr-4": { + "Symfony\\Polyfill\\Php85\\": "" + }, + "classmap": [ + "Resources/stubs" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Nicolas Grekas", + "email": "p@tchwork.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony polyfill backporting some PHP 8.5+ features to lower PHP versions", + "homepage": "https://symfony.com", + "keywords": [ + "compatibility", + "polyfill", + "portable", + "shim" + ], + "support": { + "source": "https://github.com/symfony/polyfill-php85/tree/v1.32.0" + }, + "funding": [ + { + "url": "https://symfony.com/sponsor", + "type": "custom" + }, + { + "url": "https://github.com/fabpot", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", + "type": "tidelift" + } + ], + "time": "2025-05-02T08:40:52+00:00" } ], "packages-dev": [ diff --git a/src/Client.php b/src/Client.php index 0a9b3d11fc..b9adf4531f 100644 --- a/src/Client.php +++ b/src/Client.php @@ -534,18 +534,20 @@ class Client } /** - * Perform multiple upserts in a single update command (wire protocol batch). - * Each operation should have 'filter' and 'update' keys. + * Insert, or update, document(s) with support for bulk operations. + * https://docs.mongodb.com/manual/reference/command/update/#syntax * * @param string $collection - * @param array $operations + * @param array $operations Array of operations, each with 'filter' and 'update' keys * @param array $options + * * @return self * @throws Exception */ - public function bulkUpsert(string $collection, array $operations, array $options = []): self + public function upsert(string $collection, array $operations, array $options = []): self { $updates = []; + foreach ($operations as $op) { $cleanUpdate = []; foreach ($op['update'] as $k => $v) { @@ -554,12 +556,16 @@ class Client } } - $updates[] = [ + $updateOperation = [ 'q' => $op['filter'], 'u' => $cleanUpdate, 'upsert' => true, + 'multi' => isset($op['multi']) ? $op['multi'] : false, ]; + + $updates[] = $updateOperation; } + $this->query( array_merge( [ @@ -572,48 +578,7 @@ class Client return $this; } - /** - * Insert, or update, a document/s. - * https://docs.mongodb.com/manual/reference/command/update/#syntax - * - * @param string $collection - * @param array $where - * @param array $updates - * @param array $options - * - * @return Client - * @throws Exception - */ - public function upsert(string $collection, array $where = [], array $updates = [], array $options = []): self - { - $cleanUpdates = []; - - foreach ($updates as $k => $v) { - if (\is_null($v)) { - continue; - } - $cleanUpdates[$k] = $v; - } - - - $this->query( - array_merge( - [ - 'update' => $collection, - 'updates' => [ - [ - 'q' => ['_uid' => $where['_uid']], - 'u' => ['$set' => $cleanUpdates], - ] - ], - ], - $options - ) - ); - - return $this; - } /** * Find a document/s. diff --git a/tests/MongoTest.php b/tests/MongoTest.php index 3f0b7798cf..1fa38b0538 100644 --- a/tests/MongoTest.php +++ b/tests/MongoTest.php @@ -19,7 +19,7 @@ class MongoTest extends TestCase if (!is_null(self::$db)) { return self::$db; } - + $client = new Client('testing', 'mongo', 27017, 'root', 'example', false); $client->connect(); @@ -110,6 +110,7 @@ class MongoTest extends TestCase public function testCreateDocuments(): array { + $docs = $this->getDatabase()->insertMany( 'movies', [ @@ -226,7 +227,7 @@ class MongoTest extends TestCase } - public function testBulkUpsert() + public function testUpsert() { $this->getDatabase()->insert( 'movies_upsert', @@ -237,8 +238,8 @@ class MongoTest extends TestCase 'counter' => 1 ] ); - - $this->getDatabase()->bulkUpsert('movies_upsert', [ + + $this->getDatabase()->upsert('movies_upsert', [ [ 'filter' => ['name' => 'Gone with the wind'], 'update' => [ From 9c67b64f90f9737c2d10279554c7a856ca586f10 Mon Sep 17 00:00:00 2001 From: shimon Date: Mon, 21 Jul 2025 10:00:47 +0300 Subject: [PATCH 02/16] detect replicaSet --- src/Client.php | 23 +++++++++++++++++++++++ tests/MongoTest.php | 1 + 2 files changed, 24 insertions(+) diff --git a/src/Client.php b/src/Client.php index b9adf4531f..667bc44ff9 100644 --- a/src/Client.php +++ b/src/Client.php @@ -787,4 +787,27 @@ class Client return $cleanedFilters; } + + private ?bool $replicaSet = null; + + /** + * Check if MongoDB is running as a replica set. + * + * @return bool True if this is a replica set, false if standalone + * @throws Exception + */ + public function isReplicaSet(): bool + { + if ($this->replicaSet !== null) { + return $this->replicaSet; + } + + $result = $this->query([ + 'isMaster' => 1, + ], 'admin'); + + $this->replicaSet = property_exists($result, 'setName'); + return $this->replicaSet; + } + } diff --git a/tests/MongoTest.php b/tests/MongoTest.php index 1fa38b0538..9093ac1cfe 100644 --- a/tests/MongoTest.php +++ b/tests/MongoTest.php @@ -262,4 +262,5 @@ class MongoTest extends TestCase self::assertEquals('USA', $documents[1]->country); self::assertEquals('English', $documents[1]->language); } + } From 8fe9907bf75fd46533a7bc0b9689540a72ce7690 Mon Sep 17 00:00:00 2001 From: shimon Date: Tue, 22 Jul 2025 09:45:35 +0300 Subject: [PATCH 03/16] update phpStan --- composer.json | 2 +- composer.lock | 23 +++++++++++------------ src/Client.php | 4 ++-- 3 files changed, 14 insertions(+), 15 deletions(-) diff --git a/composer.json b/composer.json index 1ee3dcb499..e1afb4a741 100755 --- a/composer.json +++ b/composer.json @@ -37,6 +37,6 @@ "phpunit/phpunit": "^9.4", "swoole/ide-helper": "4.8.0", "laravel/pint": "1.2.*", - "phpstan/phpstan": "1.8.*" + "phpstan/phpstan": "2.1.*" } } diff --git a/composer.lock b/composer.lock index 0fa64a4865..9cedc283c2 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": "6def0d090ef1b5df4a7d2af3ea71fe1c", + "content-hash": "eb347e1bd2b8e17ec80b21c30ec72876", "packages": [ { "name": "mongodb/mongodb", @@ -648,20 +648,20 @@ }, { "name": "phpstan/phpstan", - "version": "1.8.11", + "version": "2.1.19", "source": { "type": "git", "url": "https://github.com/phpstan/phpstan.git", - "reference": "46e223dd68a620da18855c23046ddb00940b4014" + "reference": "473a8c30e450d87099f76313edcbb90852f9afdf" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/phpstan/phpstan/zipball/46e223dd68a620da18855c23046ddb00940b4014", - "reference": "46e223dd68a620da18855c23046ddb00940b4014", + "url": "https://api.github.com/repos/phpstan/phpstan/zipball/473a8c30e450d87099f76313edcbb90852f9afdf", + "reference": "473a8c30e450d87099f76313edcbb90852f9afdf", "shasum": "" }, "require": { - "php": "^7.2|^8.0" + "php": "^7.4|^8.0" }, "conflict": { "phpstan/phpstan-shim": "*" @@ -686,8 +686,11 @@ "static analysis" ], "support": { + "docs": "https://phpstan.org/user-guide/getting-started", + "forum": "https://github.com/phpstan/phpstan/discussions", "issues": "https://github.com/phpstan/phpstan/issues", - "source": "https://github.com/phpstan/phpstan/tree/1.8.11" + "security": "https://github.com/phpstan/phpstan/security/policy", + "source": "https://github.com/phpstan/phpstan-src" }, "funding": [ { @@ -697,13 +700,9 @@ { "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" + "time": "2025-07-21T19:58:24+00:00" }, { "name": "phpunit/php-code-coverage", diff --git a/src/Client.php b/src/Client.php index 667bc44ff9..945af8e3ef 100644 --- a/src/Client.php +++ b/src/Client.php @@ -137,7 +137,7 @@ class Client '$db' => $db ?? $this->database, ]); - $sections = BSON\fromPHP($params); + $sections = BSON\fromPHPWithOptions($params); $message = pack('V*', 21 + strlen($sections), $this->id, 0, 2013, 0) . "\0" . $sections; return $this->send($message); } @@ -200,7 +200,7 @@ class Client /** * @var stdClass $result */ - $result = BSON\toPHP(substr($res, 21, $responseLength - 21)); + $result = BSON\toPHPWithOptions(substr($res, 21, $responseLength - 21)); if (property_exists($result, "writeErrors")) { // Throws Utopia\Mongo\Exception From 0353429227a93bd12edcb3270cb146c6560a895f Mon Sep 17 00:00:00 2001 From: shimon Date: Tue, 22 Jul 2025 10:02:34 +0300 Subject: [PATCH 04/16] fix for codeQL --- src/Auth.php | 2 +- src/Client.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Auth.php b/src/Auth.php index 2cc1435882..c2c988f945 100644 --- a/src/Auth.php +++ b/src/Auth.php @@ -10,7 +10,7 @@ class Auth private string $secret; private string $authzid; private string $gs2Header; - private string $cnonce; + private ?string $cnonce = null; private string $firstMessageBare; private string $saltedPassword; private string $authMessage; diff --git a/src/Client.php b/src/Client.php index 945af8e3ef..ac4d3034a3 100644 --- a/src/Client.php +++ b/src/Client.php @@ -137,7 +137,7 @@ class Client '$db' => $db ?? $this->database, ]); - $sections = BSON\fromPHPWithOptions($params); + $sections = MongoDB\BSON\fromPHP($params); $message = pack('V*', 21 + strlen($sections), $this->id, 0, 2013, 0) . "\0" . $sections; return $this->send($message); } @@ -200,7 +200,7 @@ class Client /** * @var stdClass $result */ - $result = BSON\toPHPWithOptions(substr($res, 21, $responseLength - 21)); + $result = MongoDB\BSON\fromPHP(substr($res, 21, $responseLength - 21)); if (property_exists($result, "writeErrors")) { // Throws Utopia\Mongo\Exception From 348cf87abb86fcb9b5ca7d4e51cf6b3c24531cb8 Mon Sep 17 00:00:00 2001 From: shimon Date: Tue, 22 Jul 2025 12:07:31 +0300 Subject: [PATCH 05/16] updating ext to 2.1.1 --- Dockerfile-php-8.3 | 2 +- composer.json | 2 +- composer.lock | 4 ++-- src/Client.php | 17 ++++++++++------- 4 files changed, 14 insertions(+), 11 deletions(-) diff --git a/Dockerfile-php-8.3 b/Dockerfile-php-8.3 index cb031e1f47..d65b1c804e 100644 --- a/Dockerfile-php-8.3 +++ b/Dockerfile-php-8.3 @@ -13,7 +13,7 @@ RUN composer update --ignore-platform-reqs --optimize-autoloader \ FROM appwrite/utopia-base:php-8.3-0.1.0 as compile -ENV PHP_MONGO_VERSION=1.11.1 +ENV PHP_MONGO_VERSION=2.1.1 RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone diff --git a/composer.json b/composer.json index e1afb4a741..8ca49e8158 100755 --- a/composer.json +++ b/composer.json @@ -29,7 +29,7 @@ }, "require": { "php": ">=8.0", - "ext-mongodb": "*", + "ext-mongodb": "2.1.1", "mongodb/mongodb": "2.1.0" }, "require-dev": { diff --git a/composer.lock b/composer.lock index 9cedc283c2..24ee8e730f 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": "eb347e1bd2b8e17ec80b21c30ec72876", + "content-hash": "5c611cfa67003afadc6c7afff9166100", "packages": [ { "name": "mongodb/mongodb", @@ -2317,7 +2317,7 @@ "prefer-lowest": false, "platform": { "php": ">=8.0", - "ext-mongodb": "*" + "ext-mongodb": "^2.1" }, "platform-dev": [], "plugin-api-version": "2.2.0" diff --git a/src/Client.php b/src/Client.php index ac4d3034a3..7947007016 100644 --- a/src/Client.php +++ b/src/Client.php @@ -2,7 +2,8 @@ namespace Utopia\Mongo; -use MongoDB\BSON; +use MongoDB\BSON\Document; +use MongoDB\BSON\ObjectId; use Swoole\Client as SwooleClient; use Swoole\Coroutine\Client as CoroutineClient; use stdClass; @@ -88,6 +89,8 @@ class Client 'authcid' => $user, 'secret' => Auth::encodeCredentials($user, $password) ]); + + } /** @@ -136,8 +139,8 @@ class Client $params = array_merge($command, [ '$db' => $db ?? $this->database, ]); - - $sections = MongoDB\BSON\fromPHP($params); + + $sections = Document::fromPHP($params); $message = pack('V*', 21 + strlen($sections), $this->id, 0, 2013, 0) . "\0" . $sections; return $this->send($message); } @@ -200,8 +203,8 @@ class Client /** * @var stdClass $result */ - $result = MongoDB\BSON\fromPHP(substr($res, 21, $responseLength - 21)); - + $bsonString = substr($res, 21, $responseLength - 21); + $result = Document::fromBSON($bsonString)->toPHP(); if (property_exists($result, "writeErrors")) { // Throws Utopia\Mongo\Exception throw new Exception( @@ -435,7 +438,7 @@ class Client $docObj->{$key} = $value; } - $docObj->_id ??= new BSON\ObjectId(); + $docObj->_id ??= new ObjectId(); $this->query(array_merge([ self::COMMAND_INSERT => $collection, @@ -460,7 +463,7 @@ class Client $docObj->{$key} = $value; } - $docObj->_id ??= new BSON\ObjectId(); + $docObj->_id ??= new ObjectId(); $docObjs[] = $docObj; } From a2435484a087748f9e81d50948e6db9d5ddc3b2f Mon Sep 17 00:00:00 2001 From: shimon Date: Tue, 22 Jul 2025 12:10:23 +0300 Subject: [PATCH 06/16] composer format --- src/Client.php | 7 ++----- tests/MongoTest.php | 8 +++----- 2 files changed, 5 insertions(+), 10 deletions(-) diff --git a/src/Client.php b/src/Client.php index 7947007016..d29bed7473 100644 --- a/src/Client.php +++ b/src/Client.php @@ -89,8 +89,6 @@ class Client 'authcid' => $user, 'secret' => Auth::encodeCredentials($user, $password) ]); - - } /** @@ -139,7 +137,7 @@ class Client $params = array_merge($command, [ '$db' => $db ?? $this->database, ]); - + $sections = Document::fromPHP($params); $message = pack('V*', 21 + strlen($sections), $this->id, 0, 2013, 0) . "\0" . $sections; return $this->send($message); @@ -550,7 +548,7 @@ class Client public function upsert(string $collection, array $operations, array $options = []): self { $updates = []; - + foreach ($operations as $op) { $cleanUpdate = []; foreach ($op['update'] as $k => $v) { @@ -812,5 +810,4 @@ class Client $this->replicaSet = property_exists($result, 'setName'); return $this->replicaSet; } - } diff --git a/tests/MongoTest.php b/tests/MongoTest.php index 9093ac1cfe..aba9be3653 100644 --- a/tests/MongoTest.php +++ b/tests/MongoTest.php @@ -19,7 +19,7 @@ class MongoTest extends TestCase if (!is_null(self::$db)) { return self::$db; } - + $client = new Client('testing', 'mongo', 27017, 'root', 'example', false); $client->connect(); @@ -110,7 +110,6 @@ class MongoTest extends TestCase public function testCreateDocuments(): array { - $docs = $this->getDatabase()->insertMany( 'movies', [ @@ -237,8 +236,8 @@ class MongoTest extends TestCase 'country' => 'UK', 'counter' => 1 ] - ); - + ); + $this->getDatabase()->upsert('movies_upsert', [ [ 'filter' => ['name' => 'Gone with the wind'], @@ -262,5 +261,4 @@ class MongoTest extends TestCase self::assertEquals('USA', $documents[1]->country); self::assertEquals('English', $documents[1]->language); } - } From a50588f81c35d7afc610cbd152baef1672bd633c Mon Sep 17 00:00:00 2001 From: shimon Date: Tue, 22 Jul 2025 12:14:09 +0300 Subject: [PATCH 07/16] phpStan fix --- src/Client.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/Client.php b/src/Client.php index d29bed7473..441d375fa9 100644 --- a/src/Client.php +++ b/src/Client.php @@ -203,6 +203,9 @@ class Client */ $bsonString = substr($res, 21, $responseLength - 21); $result = Document::fromBSON($bsonString)->toPHP(); + if (is_array($result)) { + $result = (object) $result; + } if (property_exists($result, "writeErrors")) { // Throws Utopia\Mongo\Exception throw new Exception( From f9dd89d517863091286c24d09e10e1d8c6c1b285 Mon Sep 17 00:00:00 2001 From: shimon Date: Tue, 22 Jul 2025 12:16:38 +0300 Subject: [PATCH 08/16] Update variable type annotation in Client.php from stdClass to object --- src/Client.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Client.php b/src/Client.php index 441d375fa9..a80a5e7bf0 100644 --- a/src/Client.php +++ b/src/Client.php @@ -198,8 +198,8 @@ class Client (!isset($responseLength)) || ($receivedLength < $responseLength) ); - /** - * @var stdClass $result + /** + * @var object $result */ $bsonString = substr($res, 21, $responseLength - 21); $result = Document::fromBSON($bsonString)->toPHP(); From 101a7b20e02cf8211315f937c9564aecad028ac7 Mon Sep 17 00:00:00 2001 From: shimon Date: Tue, 22 Jul 2025 12:19:10 +0300 Subject: [PATCH 09/16] Remove unused variable annotation in Client.php --- src/Client.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/Client.php b/src/Client.php index a80a5e7bf0..59c53fffa0 100644 --- a/src/Client.php +++ b/src/Client.php @@ -198,9 +198,6 @@ class Client (!isset($responseLength)) || ($receivedLength < $responseLength) ); - /** - * @var object $result - */ $bsonString = substr($res, 21, $responseLength - 21); $result = Document::fromBSON($bsonString)->toPHP(); if (is_array($result)) { From 00b99565dc43b13837c0321a00ad6fac581d2670 Mon Sep 17 00:00:00 2001 From: shimon Date: Tue, 22 Jul 2025 15:10:09 +0300 Subject: [PATCH 10/16] Update PHP MongoDB extension version to 2.1.1 in Dockerfile-php-8.0 --- Dockerfile-php-8.0 | 2 +- Dockerfile-php-8.1 | 2 +- Dockerfile-php-8.2 | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Dockerfile-php-8.0 b/Dockerfile-php-8.0 index 0d3c013269..e19c783032 100755 --- a/Dockerfile-php-8.0 +++ b/Dockerfile-php-8.0 @@ -13,7 +13,7 @@ RUN composer update --ignore-platform-reqs --optimize-autoloader \ FROM appwrite/utopia-base:php-8.0-0.1.0 as compile -ENV PHP_MONGO_VERSION=1.11.1 +ENV PHP_MONGO_VERSION=2.1.1 RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone diff --git a/Dockerfile-php-8.1 b/Dockerfile-php-8.1 index 97bfddca2f..e459721d73 100644 --- a/Dockerfile-php-8.1 +++ b/Dockerfile-php-8.1 @@ -13,7 +13,7 @@ RUN composer update --ignore-platform-reqs --optimize-autoloader \ FROM appwrite/utopia-base:php-8.1-0.1.0 as compile -ENV PHP_MONGO_VERSION=1.11.1 +ENV PHP_MONGO_VERSION=2.1.1 RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone diff --git a/Dockerfile-php-8.2 b/Dockerfile-php-8.2 index 214e3727e0..c3a0f73e57 100644 --- a/Dockerfile-php-8.2 +++ b/Dockerfile-php-8.2 @@ -13,7 +13,7 @@ RUN composer update --ignore-platform-reqs --optimize-autoloader \ FROM appwrite/utopia-base:php-8.2-0.1.0 as compile -ENV PHP_MONGO_VERSION=1.11.1 +ENV PHP_MONGO_VERSION=2.1.1 RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone From 0516d0d325ea54c093f18435e0b11795c1293328 Mon Sep 17 00:00:00 2001 From: shimon Date: Tue, 22 Jul 2025 16:34:49 +0300 Subject: [PATCH 11/16] Update GitHub Actions workflow to use PHP version 8.3 only --- .github/workflows/tests.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 12495e9a9f..8deed9a9bb 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -7,7 +7,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - php-versions: ['8.0', '8.1', '8.2', '8.3'] # add PHP versions as required + php-versions: ['8.3'] # add PHP versions as required steps: - name: Checkout repository From 12a267d6c83ce8102bf05eac87c04390b646143f Mon Sep 17 00:00:00 2001 From: shimon Date: Wed, 23 Jul 2025 08:51:59 +0300 Subject: [PATCH 12/16] Remove unsupported php versions --- Dockerfile-php-8.0 | 57 ---------------------------------------------- Dockerfile-php-8.1 | 57 ---------------------------------------------- Dockerfile-php-8.2 | 57 ---------------------------------------------- 3 files changed, 171 deletions(-) delete mode 100755 Dockerfile-php-8.0 delete mode 100644 Dockerfile-php-8.1 delete mode 100644 Dockerfile-php-8.2 diff --git a/Dockerfile-php-8.0 b/Dockerfile-php-8.0 deleted file mode 100755 index e19c783032..0000000000 --- a/Dockerfile-php-8.0 +++ /dev/null @@ -1,57 +0,0 @@ -FROM composer:2.0 as composer - -ARG TESTING=false -ENV TESTING=$TESTING - -WORKDIR /usr/local/src/ - -COPY composer.lock /usr/local/src/ -COPY composer.json /usr/local/src/ - -RUN composer update --ignore-platform-reqs --optimize-autoloader \ - --no-plugins --no-scripts --prefer-dist - -FROM appwrite/utopia-base:php-8.0-0.1.0 as compile - -ENV PHP_MONGO_VERSION=2.1.1 - -RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone - -RUN \ - apk update \ - && apk add --no-cache postgresql-libs postgresql-dev make automake autoconf gcc g++ git \ - && docker-php-ext-install opcache pgsql pdo_mysql pdo_pgsql \ - && rm -rf /var/cache/apk/* - -## MongoDB Extension -FROM compile AS mongodb -RUN \ - git clone --depth 1 --branch $PHP_MONGO_VERSION https://github.com/mongodb/mongo-php-driver.git \ - && cd mongo-php-driver \ - && git submodule update --init \ - && phpize \ - && ./configure \ - && make && make install - -FROM compile as final - -LABEL maintainer="team@appwrite.io" - -WORKDIR /usr/src/code - -RUN echo extension=mongodb.so >> /usr/local/etc/php/conf.d/mongodb.ini - -RUN mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini" - -RUN echo "opcache.enable_cli=1" >> $PHP_INI_DIR/php.ini - -RUN echo "memory_limit=1024M" >> $PHP_INI_DIR/php.ini - -COPY --from=composer /usr/local/src/vendor /usr/src/code/vendor -COPY --from=mongodb /usr/local/lib/php/extensions/no-debug-non-zts-20200930/mongodb.so /usr/local/lib/php/extensions/no-debug-non-zts-20200930/ - -# Add Source Code -COPY . /usr/src/code - -CMD [ "tail", "-f", "/dev/null" ] - diff --git a/Dockerfile-php-8.1 b/Dockerfile-php-8.1 deleted file mode 100644 index e459721d73..0000000000 --- a/Dockerfile-php-8.1 +++ /dev/null @@ -1,57 +0,0 @@ -FROM composer:2.0 as composer - -ARG TESTING=false -ENV TESTING=$TESTING - -WORKDIR /usr/local/src/ - -COPY composer.lock /usr/local/src/ -COPY composer.json /usr/local/src/ - -RUN composer update --ignore-platform-reqs --optimize-autoloader \ - --no-plugins --no-scripts --prefer-dist - -FROM appwrite/utopia-base:php-8.1-0.1.0 as compile - -ENV PHP_MONGO_VERSION=2.1.1 - -RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone - -RUN \ - apk update \ - && apk add --no-cache postgresql-libs postgresql-dev make automake autoconf gcc g++ git \ - && docker-php-ext-install opcache pgsql pdo_mysql pdo_pgsql \ - && rm -rf /var/cache/apk/* - -## MongoDB Extension -FROM compile AS mongodb -RUN \ - git clone --depth 1 --branch $PHP_MONGO_VERSION https://github.com/mongodb/mongo-php-driver.git \ - && cd mongo-php-driver \ - && git submodule update --init \ - && phpize \ - && ./configure \ - && make && make install - -FROM compile as final - -LABEL maintainer="team@appwrite.io" - -WORKDIR /usr/src/code - -RUN echo extension=mongodb.so >> /usr/local/etc/php/conf.d/mongodb.ini - -RUN mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini" - -RUN echo "opcache.enable_cli=1" >> $PHP_INI_DIR/php.ini - -RUN echo "memory_limit=1024M" >> $PHP_INI_DIR/php.ini - -COPY --from=composer /usr/local/src/vendor /usr/src/code/vendor -COPY --from=mongodb /usr/local/lib/php/extensions/no-debug-non-zts-20210902/mongodb.so /usr/local/lib/php/extensions/no-debug-non-zts-20210902/ - -# Add Source Code -COPY . /usr/src/code - -CMD [ "tail", "-f", "/dev/null" ] - diff --git a/Dockerfile-php-8.2 b/Dockerfile-php-8.2 deleted file mode 100644 index c3a0f73e57..0000000000 --- a/Dockerfile-php-8.2 +++ /dev/null @@ -1,57 +0,0 @@ -FROM composer:2.0 as composer - -ARG TESTING=false -ENV TESTING=$TESTING - -WORKDIR /usr/local/src/ - -COPY composer.lock /usr/local/src/ -COPY composer.json /usr/local/src/ - -RUN composer update --ignore-platform-reqs --optimize-autoloader \ - --no-plugins --no-scripts --prefer-dist - -FROM appwrite/utopia-base:php-8.2-0.1.0 as compile - -ENV PHP_MONGO_VERSION=2.1.1 - -RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone - -RUN \ - apk update \ - && apk add --no-cache postgresql-libs postgresql-dev make automake autoconf gcc g++ git \ - && docker-php-ext-install opcache pgsql pdo_mysql pdo_pgsql \ - && rm -rf /var/cache/apk/* - -## MongoDB Extension -FROM compile AS mongodb -RUN \ - git clone --depth 1 --branch $PHP_MONGO_VERSION https://github.com/mongodb/mongo-php-driver.git \ - && cd mongo-php-driver \ - && git submodule update --init \ - && phpize \ - && ./configure \ - && make && make install - -FROM compile as final - -LABEL maintainer="team@appwrite.io" - -WORKDIR /usr/src/code - -RUN echo extension=mongodb.so >> /usr/local/etc/php/conf.d/mongodb.ini - -RUN mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini" - -RUN echo "opcache.enable_cli=1" >> $PHP_INI_DIR/php.ini - -RUN echo "memory_limit=1024M" >> $PHP_INI_DIR/php.ini - -COPY --from=composer /usr/local/src/vendor /usr/src/code/vendor -COPY --from=mongodb /usr/local/lib/php/extensions/no-debug-non-zts-20220829/mongodb.so /usr/local/lib/php/extensions/no-debug-non-zts-20220829/ - -# Add Source Code -COPY . /usr/src/code - -CMD [ "tail", "-f", "/dev/null" ] - From 757e61da1beacc75423426771bc93b649a4ab000 Mon Sep 17 00:00:00 2001 From: Shimon Newman Date: Wed, 23 Jul 2025 16:50:50 +0300 Subject: [PATCH 13/16] Update src/Client.php Co-authored-by: Jake Barnby --- src/Client.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Client.php b/src/Client.php index 59c53fffa0..3bf79e20d6 100644 --- a/src/Client.php +++ b/src/Client.php @@ -38,7 +38,6 @@ class Client public const COMMAND_DISTINCT = "distinct"; public const COMMAND_MAP_REDUCE = "mapReduce"; - /** * Authentication for connection */ From 03d1ba460813fa703039c2d177cdf6b61c08a19d Mon Sep 17 00:00:00 2001 From: shimon Date: Wed, 23 Jul 2025 17:11:19 +0300 Subject: [PATCH 14/16] Update return type of getCnonce method to nullable string and enhance MongoDB response documentation in Client.php --- src/Auth.php | 2 +- src/Client.php | 17 +++++++++++++++-- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/Auth.php b/src/Auth.php index c2c988f945..ca0aa59bdb 100644 --- a/src/Auth.php +++ b/src/Auth.php @@ -218,7 +218,7 @@ class Auth /** * @return string */ - public function getCnonce(): string + public function getCnonce(): ?string { return $this->cnonce; } diff --git a/src/Client.php b/src/Client.php index 3bf79e20d6..82fc56636e 100644 --- a/src/Client.php +++ b/src/Client.php @@ -197,6 +197,19 @@ class Client (!isset($responseLength)) || ($receivedLength < $responseLength) ); + /* + * The first 21 bytes of the MongoDB wire protocol response consist of: + * - 16 bytes: Standard message header, which includes: + * - messageLength (4 bytes): Total size of the message, including the header. + * - requestID (4 bytes): Identifier for this message. + * - responseTo (4 bytes): The requestID that this message is responding to. + * - opCode (4 bytes): The operation code for the message type (e.g., OP_MSG). + * - 4 bytes: flagBits, which provide additional information about the message. + * - 1 byte: payloadType, indicating the type of the following payload (usually 0 for a BSON document). + * + * These 21 bytes are protocol metadata and precede the actual BSON-encoded document in the response. + */ + $bsonString = substr($res, 21, $responseLength - 21); $result = Document::fromBSON($bsonString)->toPHP(); if (is_array($result)) { @@ -555,10 +568,10 @@ class Client $cleanUpdate[$k] = $v; } } - + $updateOperation = [ 'q' => $op['filter'], - 'u' => $cleanUpdate, + 'u' => $this->toObject($cleanUpdate), 'upsert' => true, 'multi' => isset($op['multi']) ? $op['multi'] : false, ]; From 7c8b6e90224bef1c1e582c9035cfd621bdec0561 Mon Sep 17 00:00:00 2001 From: shimon Date: Wed, 23 Jul 2025 17:12:52 +0300 Subject: [PATCH 15/16] Refactor comment formatting and clean up whitespace in Client.php --- src/Client.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Client.php b/src/Client.php index 82fc56636e..8dc5227f2a 100644 --- a/src/Client.php +++ b/src/Client.php @@ -197,7 +197,7 @@ class Client (!isset($responseLength)) || ($receivedLength < $responseLength) ); - /* + /* * The first 21 bytes of the MongoDB wire protocol response consist of: * - 16 bytes: Standard message header, which includes: * - messageLength (4 bytes): Total size of the message, including the header. @@ -568,7 +568,7 @@ class Client $cleanUpdate[$k] = $v; } } - + $updateOperation = [ 'q' => $op['filter'], 'u' => $this->toObject($cleanUpdate), From 91a780b7a47f1f6869d8c819e927e20983c5ccfe Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Thu, 24 Jul 2025 02:47:53 +1200 Subject: [PATCH 16/16] Update src/Client.php --- src/Client.php | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/Client.php b/src/Client.php index 8dc5227f2a..04696c3e43 100644 --- a/src/Client.php +++ b/src/Client.php @@ -198,17 +198,17 @@ class Client ); /* - * The first 21 bytes of the MongoDB wire protocol response consist of: - * - 16 bytes: Standard message header, which includes: - * - messageLength (4 bytes): Total size of the message, including the header. - * - requestID (4 bytes): Identifier for this message. - * - responseTo (4 bytes): The requestID that this message is responding to. - * - opCode (4 bytes): The operation code for the message type (e.g., OP_MSG). - * - 4 bytes: flagBits, which provide additional information about the message. - * - 1 byte: payloadType, indicating the type of the following payload (usually 0 for a BSON document). - * - * These 21 bytes are protocol metadata and precede the actual BSON-encoded document in the response. - */ + * The first 21 bytes of the MongoDB wire protocol response consist of: + * - 16 bytes: Standard message header, which includes: + * - messageLength (4 bytes): Total size of the message, including the header. + * - requestID (4 bytes): Identifier for this message. + * - responseTo (4 bytes): The requestID that this message is responding to. + * - opCode (4 bytes): The operation code for the message type (e.g., OP_MSG). + * - 4 bytes: flagBits, which provide additional information about the message. + * - 1 byte: payloadType, indicating the type of the following payload (usually 0 for a BSON document). + * + * These 21 bytes are protocol metadata and precede the actual BSON-encoded document in the response. + */ $bsonString = substr($res, 21, $responseLength - 21); $result = Document::fromBSON($bsonString)->toPHP();