From fa0dec32d0e1f587f0425501e6d6f9a3708464e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20B=C3=B6sing?= <2189546+boesing@users.noreply.github.com> Date: Mon, 20 Jul 2020 23:59:02 +0200 Subject: [PATCH 1/7] Remove old PHP versions from travis MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Maximilian Bösing <2189546+boesing@users.noreply.github.com> --- .travis.yml | 32 -------------------------------- 1 file changed, 32 deletions(-) diff --git a/.travis.yml b/.travis.yml index 821f993..ae2c5c7 100644 --- a/.travis.yml +++ b/.travis.yml @@ -17,38 +17,6 @@ env: matrix: fast_finish: true include: - - php: 5.6 - env: - - DEPS=lowest - - EXTMONGODB_DEPS="mongodb/mongodb:~1.4.0" - - php: 5.6 - env: - - DEPS=latest - - EXTMONGODB_DEPS="mongodb/mongodb:~1.4.0" - - php: 7.0 - env: - - DEPS=lowest - - EXTMONGODB_DEPS="mongodb/mongodb:~1.4.0" - - php: 7.0 - env: - - DEPS=latest - - EXTMONGODB_DEPS="mongodb/mongodb:~1.4.0" - - php: 7.1 - env: - - DEPS=lowest - - EXTMONGODB_DEPS="mongodb/mongodb:~1.4.0" - - php: 7.1 - env: - - DEPS=latest - - EXTMONGODB_DEPS="mongodb/mongodb:~1.4.0" - - php: 7.2 - env: - - DEPS=lowest - - EXTMONGODB_DEPS="mongodb/mongodb:~1.4.0" - - php: 7.2 - env: - - DEPS=latest - - EXTMONGODB_DEPS="mongodb/mongodb:~1.4.0" - php: 7.3 env: - DEPS=lowest From d8620ced74f94b2143ffd4712f97faa89c2b225c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20B=C3=B6sing?= <2189546+boesing@users.noreply.github.com> Date: Mon, 20 Jul 2020 23:59:50 +0200 Subject: [PATCH 2/7] Added test to resolve `BSONArray` and `BSONDocument` values MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Maximilian Bösing <2189546+boesing@users.noreply.github.com> --- test/unit/ExtMongoDbTest.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/test/unit/ExtMongoDbTest.php b/test/unit/ExtMongoDbTest.php index aac69e7..3b0dd05 100644 --- a/test/unit/ExtMongoDbTest.php +++ b/test/unit/ExtMongoDbTest.php @@ -67,4 +67,18 @@ public function testSetOptionsNotMongoDbOptions() $this->assertInstanceOf(ExtMongoDbOptions::class, $this->_storage->getOptions()); } + + public function testSetItemWithNestedArraysWillReturnNestedArrayValue() + { + $value = [ + 'foo' => [ + 'bar' => [ + 'baz' => [], + ], + ], + ]; + + $this->assertTrue($this->_storage->setItem('test', $value)); + $this->assertSame($value, $this->_storage->getItem('test')); + } } From 42212f0306b468391f2a2446285d5fd6fc4aa424 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20B=C3=B6sing?= <2189546+boesing@users.noreply.github.com> Date: Tue, 21 Jul 2020 00:00:06 +0200 Subject: [PATCH 3/7] Resolve `BSONArray` and `BSONDocument` values MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Maximilian Bösing <2189546+boesing@users.noreply.github.com> --- src/ExtMongoDb.php | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/src/ExtMongoDb.php b/src/ExtMongoDb.php index 6ed95ea..53d9630 100644 --- a/src/ExtMongoDb.php +++ b/src/ExtMongoDb.php @@ -8,6 +8,7 @@ namespace Laminas\Cache\Storage\Adapter; +use ArrayObject; use Laminas\Cache\Exception; use Laminas\Cache\Storage\Capabilities; use Laminas\Cache\Storage\FlushableInterface; @@ -161,7 +162,12 @@ protected function internalGetItem(& $normalizedKey, & $success = null, & $casTo $success = true; - return $casToken = $result['value']; + $value = $result['value']; + if ($value instanceof ArrayObject) { + $value = $this->arrayObjectToArray($value); + } + + return $casToken = $value; } /** @@ -282,4 +288,22 @@ private function fetchFromCollection(& $normalizedKey) throw new Exception\RuntimeException($e->getMessage(), $e->getCode(), $e); } } + + /** + * @return array + */ + private function arrayObjectToArray(ArrayObject $value): array + { + $converted = $value->getArrayCopy(); + + foreach ($converted as $index => $nested) { + if (! $nested instanceof ArrayObject) { + continue; + } + + $converted[$index] = $this->arrayObjectToArray($nested); + } + + return $converted; + } } From d9210f487944c51527e5d58aaf7614f0106624b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20B=C3=B6sing?= <2189546+boesing@users.noreply.github.com> Date: Tue, 21 Jul 2020 00:03:34 +0200 Subject: [PATCH 4/7] Handle already dropped collection MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Maximilian Bösing <2189546+boesing@users.noreply.github.com> --- src/ExtMongoDb.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/ExtMongoDb.php b/src/ExtMongoDb.php index 53d9630..d394e65 100644 --- a/src/ExtMongoDb.php +++ b/src/ExtMongoDb.php @@ -223,6 +223,10 @@ protected function internalRemoveItem(& $normalizedKey) public function flush() { $result = $this->getMongoCollection()->drop(); + if ($result instanceof stdClass) { + $result = (array) $result; + } + return ((float) 1) === $result['ok']; } From 692230c0cef91d8cab9967155e794370a22d3ecf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20B=C3=B6sing?= <2189546+boesing@users.noreply.github.com> Date: Tue, 21 Jul 2020 00:22:52 +0200 Subject: [PATCH 5/7] Add conflict with mongodb <1.6 and fixed expire handling MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Maximilian Bösing <2189546+boesing@users.noreply.github.com> --- composer.json | 3 +++ src/ExtMongoDb.php | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index cf01d70..d7a0dc6 100644 --- a/composer.json +++ b/composer.json @@ -46,6 +46,9 @@ "test": "phpunit --colors=always", "test-coverage": "phpunit --colors=always --coverage-clover clover.xml" }, + "conflict": { + "mongodb/mongodb": "<1.6" + }, "support": { "issues": "https://github.com/laminas/laminas-cache-storage-adapter-ext-mongodb/issues", "forum": "https://discourse.laminas.dev/", diff --git a/src/ExtMongoDb.php b/src/ExtMongoDb.php index d394e65..84bc899 100644 --- a/src/ExtMongoDb.php +++ b/src/ExtMongoDb.php @@ -146,7 +146,7 @@ protected function internalGetItem(& $normalizedKey, & $success = null, & $casTo )); } - if ($result['expires']->sec < (new MongoDate())) { + if ($result['expires']->toDateTime() < (new MongoDate())->toDateTime()) { $this->internalRemoveItem($normalizedKey); return; } From 30955352c88f389fb585f2cab23d398ec2cc8618 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20B=C3=B6sing?= <2189546+boesing@users.noreply.github.com> Date: Tue, 21 Jul 2020 00:23:22 +0200 Subject: [PATCH 6/7] Remove `db` handling in resource manager and receive proper db name MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Maximilian Bösing <2189546+boesing@users.noreply.github.com> --- src/ExtMongoDbResourceManager.php | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/src/ExtMongoDbResourceManager.php b/src/ExtMongoDbResourceManager.php index d85f939..c345edc 100644 --- a/src/ExtMongoDbResourceManager.php +++ b/src/ExtMongoDbResourceManager.php @@ -51,8 +51,7 @@ public function setResource($id, $resource) { if ($resource instanceof Collection) { $this->resources[$id] = [ - 'db' => (string) $resource->db, - 'db_instance' => $resource->db, + 'db' => $resource->getDatabaseName(), 'collection' => (string) $resource, 'collection_instance' => $resource, ]; @@ -88,14 +87,12 @@ public function getResource($id) $resource = $this->resources[$id]; if (! isset($resource['collection_instance'])) { try { - if (! isset($resource['db_instance'])) { - if (! isset($resource['client_instance'])) { - $resource['client_instance'] = new Client( - isset($resource['server']) ? $resource['server'] : null, - isset($resource['connection_options']) ? $resource['connection_options'] : [], - isset($resource['driver_options']) ? $resource['driver_options'] : [] - ); - } + if (! isset($resource['client_instance'])) { + $resource['client_instance'] = new Client( + isset($resource['server']) ? $resource['server'] : null, + isset($resource['connection_options']) ? $resource['connection_options'] : [], + isset($resource['driver_options']) ? $resource['driver_options'] : [] + ); } $collection = $resource['client_instance']->selectCollection( @@ -123,7 +120,6 @@ public function setServer($id, $server) $this->resources[$id]['server'] = (string) $server; unset($this->resources[$id]['client_instance']); - unset($this->resources[$id]['db_instance']); unset($this->resources[$id]['collection_instance']); } @@ -151,7 +147,6 @@ public function setConnectionOptions($id, array $connectionOptions) $this->resources[$id]['connection_options'] = $connectionOptions; unset($this->resources[$id]['client_instance']); - unset($this->resources[$id]['db_instance']); unset($this->resources[$id]['collection_instance']); } @@ -181,7 +176,6 @@ public function setDriverOptions($id, array $driverOptions) $this->resources[$id]['driver_options'] = $driverOptions; unset($this->resources[$id]['client_instance']); - unset($this->resources[$id]['db_instance']); unset($this->resources[$id]['collection_instance']); } @@ -208,7 +202,6 @@ public function setDatabase($id, $database) { $this->resources[$id]['db'] = (string) $database; - unset($this->resources[$id]['db_instance']); unset($this->resources[$id]['collection_instance']); } From ac293a860174cd4102ebb29b9415321f4d44a551 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20B=C3=B6sing?= <2189546+boesing@users.noreply.github.com> Date: Tue, 21 Jul 2020 00:49:01 +0200 Subject: [PATCH 7/7] Avoid deprecation warnings for PHP 7.4 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Maximilian Bösing <2189546+boesing@users.noreply.github.com> --- src/ExtMongoDb.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/ExtMongoDb.php b/src/ExtMongoDb.php index 84bc899..8925c0b 100644 --- a/src/ExtMongoDb.php +++ b/src/ExtMongoDb.php @@ -16,7 +16,9 @@ use MongoDB\Client; use MongoDB\Collection; use MongoDB\Driver\Exception\Exception as MongoDriverException; +use MongoDB\Model\BSONDocument; use stdClass; +use function property_exists; /** * Cache storage adapter for ext-mongodb @@ -152,7 +154,7 @@ protected function internalGetItem(& $normalizedKey, & $success = null, & $casTo } } - if (! array_key_exists('value', $result)) { + if (! property_exists($result, 'value')) { throw new Exception\RuntimeException(sprintf( "The found item _id '%s' for key '%s' is not a valid cache item: missing the field 'value'", (string) $result['_id'], @@ -280,7 +282,7 @@ protected function internalGetMetadata(& $normalizedKey) * * @param string $normalizedKey * - * @return array|null + * @return BSONDocument|null * * @throws Exception\RuntimeException */