Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/1.x' into 1-to-2
Browse files Browse the repository at this point in the history
  • Loading branch information
dbu committed May 6, 2024
2 parents 78cf8b9 + 92ab6d6 commit 254bc3b
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 13 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
Changelog
=========

2.0.1
-----

* Fixed cache key sanitize for PSR-16 cache.
* Fixed not found detection for PSR-16 cache.

2.0.0
-----

Expand All @@ -24,6 +30,12 @@ Changelog
1.x
===

1.13.0
------

* Fixed cache key sanitize for PSR-16 cache.
* Fixed not found detection for PSR-16 cache.

1.12.0
------

Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"psr/log": "^1 || ^2 || ^3",
"phpcr/phpcr-api-tests": "2.1.25",
"phpunit/phpunit": "^9.0",
"psr/simple-cache": "^1.0 || ^2.0 || ^3.0",
"symfony/cache": "^5.4 || ^6.2 || ^7.0",
"phpstan/phpstan": "^1.10"
},
Expand Down
16 changes: 10 additions & 6 deletions src/Jackalope/Transport/DoctrineDBAL/CachedClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,12 @@ public function __construct(FactoryInterface $factory, Connection $conn, array $
}
}
$this->caches = $caches;
$this->keySanitizer = static function (string $cacheKey): string {
return str_replace(' ', '_', $cacheKey);
$this->keySanitizer = static function ($cacheKey) {
return str_replace(
['%', '.'],
['_', '|'],
\urlencode($cacheKey)
);
};
}

Expand Down Expand Up @@ -219,7 +223,7 @@ public function getNode(string $path): \stdClass
$cacheKey = "nodes: $path, ".$this->workspaceName;
$cacheKey = $this->sanitizeKey($cacheKey);

if (false !== ($result = $this->caches['nodes']->get($cacheKey))) {
if (null !== ($result = $this->caches['nodes']->get($cacheKey))) {
if ('ItemNotFoundException' === $result) {
throw new ItemNotFoundException("Item '$path' not found in workspace '$this->workspaceName'");
}
Expand Down Expand Up @@ -354,7 +358,7 @@ public function getNodePathForIdentifier($uuid, $workspace = null): string
$cacheKey = "nodes by uuid: $uuid, $this->workspaceName";
$cacheKey = $this->sanitizeKey($cacheKey);

if (false !== ($result = $this->caches['nodes']->get($cacheKey))) {
if (null !== ($result = $this->caches['nodes']->get($cacheKey))) {
if ('ItemNotFoundException' === $result) {
throw new ItemNotFoundException("no item found with uuid $uuid");
}
Expand Down Expand Up @@ -407,7 +411,7 @@ public function getReferences($path, $name = null): array
$cacheKey = "nodes references: $path, $name, ".$this->workspaceName;
$cacheKey = $this->sanitizeKey($cacheKey);

if (false !== ($result = $this->caches['nodes']->get($cacheKey))) {
if (null !== ($result = $this->caches['nodes']->get($cacheKey))) {
return $result;
}

Expand Down Expand Up @@ -449,7 +453,7 @@ public function query(Query $query): array
$cacheKey = "query: {$query->getStatement()}, {$query->getLimit()}, {$query->getOffset()}, {$query->getLanguage()}, ".$this->workspaceName;
$cacheKey = $this->sanitizeKey($cacheKey);

if (false !== ($result = $this->caches['query']->get($cacheKey))) {
if (null !== ($result = $this->caches['query']->get($cacheKey))) {
return $result;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Jackalope\Transport\DoctrineDBAL;

use Doctrine\DBAL\Connection;
use Jackalope\Factory;
use Symfony\Component\Cache\Adapter\ArrayAdapter;
use Symfony\Component\Cache\Psr16Cache;

class CachedClientFunctionalTest extends ClientTest
{
protected function getClient(Connection $conn)

Check failure on line 12 in tests/Jackalope/Transport/DoctrineDBAL/CachedClientFunctionalTest.php

View workflow job for this annotation

GitHub Actions / PHPStan tests

Return type mixed of method Jackalope\Transport\DoctrineDBAL\CachedClientFunctionalTest::getClient() is not covariant with return type Jackalope\Transport\TransportInterface of method Jackalope\Test\FunctionalTestCase::getClient().
{
$nodeCacheAdapter = new ArrayAdapter();
$nodeCache = new Psr16Cache($nodeCacheAdapter);

$metaCacheAdapter = new ArrayAdapter();
$metaCache = new Psr16Cache($metaCacheAdapter);

return new CachedClient(new Factory(), $conn, [
'nodes' => $nodeCache,
'meta' => $metaCache,
]);
}
}
17 changes: 10 additions & 7 deletions tests/Jackalope/Transport/DoctrineDBAL/CachedClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,24 @@ public function testCacheHit()
{
$cache = new \stdClass();
$cache->foo = 'bar';
$this->cache->set('nodes:_/test,_tests', $cache);
$this->cache->set('nodes_3A+_2Ftest_2C+tests', $cache);
$this->assertEquals($cache, $this->transport->getNode('/test'));
}

/**
* The default key sanitizer replaces spaces with underscores.
* The default key sanitizer keeps the cache key compatible with PSR16
*/
public function testDefaultKeySanitizer(): void
{
/** @var CachedClient $cachedClient */
$cachedClient = $this->transport;
$cachedClient->getNodeTypes();
$client = $this->getClient($this->getConnection());
$reflection = new \ReflectionClass($client);
$keySanitizerProperty = $reflection->getProperty('keySanitizer');
$keySanitizerProperty->setAccessible(true);
$defaultKeySanitizer = $keySanitizerProperty->getValue($client);

$result = $defaultKeySanitizer(' :{}().@/"\\'); // not allowed PSR16 keys

$this->assertTrue($this->cache->has('node_types'));
$this->assertTrue($this->cache->has('nodetypes:_a:0:{}'));
$this->assertEquals('+_3A_7B_7D_28_29|_40_2F_22_5C', $result);
}

public function testCustomKeySanitizer(): void
Expand Down

0 comments on commit 254bc3b

Please sign in to comment.