Skip to content

remove old php versions #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 0 additions & 32 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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/",
Expand Down
38 changes: 34 additions & 4 deletions src/ExtMongoDb.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,17 @@

namespace Laminas\Cache\Storage\Adapter;

use ArrayObject;
use Laminas\Cache\Exception;
use Laminas\Cache\Storage\Capabilities;
use Laminas\Cache\Storage\FlushableInterface;
use MongoDB\BSON\UTCDateTime as MongoDate;
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
Expand Down Expand Up @@ -145,13 +148,13 @@ protected function internalGetItem(& $normalizedKey, & $success = null, & $casTo
));
}

if ($result['expires']->sec < (new MongoDate())) {
if ($result['expires']->toDateTime() < (new MongoDate())->toDateTime()) {
$this->internalRemoveItem($normalizedKey);
return;
}
}

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'],
Expand All @@ -161,7 +164,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;
}

/**
Expand Down Expand Up @@ -217,6 +225,10 @@ protected function internalRemoveItem(& $normalizedKey)
public function flush()
{
$result = $this->getMongoCollection()->drop();
if ($result instanceof stdClass) {
$result = (array) $result;
}

return ((float) 1) === $result['ok'];
}

Expand Down Expand Up @@ -270,7 +282,7 @@ protected function internalGetMetadata(& $normalizedKey)
*
* @param string $normalizedKey
*
* @return array|null
* @return BSONDocument|null
*
* @throws Exception\RuntimeException
*/
Expand All @@ -282,4 +294,22 @@ private function fetchFromCollection(& $normalizedKey)
throw new Exception\RuntimeException($e->getMessage(), $e->getCode(), $e);
}
}

/**
* @return array<int|string,mixed>
*/
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;
}
}
21 changes: 7 additions & 14 deletions src/ExtMongoDbResourceManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
];
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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']);
}

Expand Down Expand Up @@ -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']);
}

Expand Down Expand Up @@ -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']);
}

Expand All @@ -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']);
}

Expand Down
14 changes: 14 additions & 0 deletions test/unit/ExtMongoDbTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}
}