From b50221c0e2fb614aa7f41186475457f01129e12b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20N=C3=A9grier?= Date: Thu, 13 Jun 2019 16:00:41 +0200 Subject: [PATCH] Adding configurable namespace separator to have separator compatible with PSR-16. --- .../Cache/Adapter/AbstractAdapter.php | 2 +- .../Cache/Adapter/SimpleCacheAdapter.php | 8 ++++++++ .../Component/Cache/Traits/AbstractTrait.php | 18 +++++++++++++----- 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/src/Symfony/Component/Cache/Adapter/AbstractAdapter.php b/src/Symfony/Component/Cache/Adapter/AbstractAdapter.php index 099c97a4da9d4..70af0c7314cac 100644 --- a/src/Symfony/Component/Cache/Adapter/AbstractAdapter.php +++ b/src/Symfony/Component/Cache/Adapter/AbstractAdapter.php @@ -39,7 +39,7 @@ abstract class AbstractAdapter implements AdapterInterface, LoggerAwareInterface */ protected function __construct($namespace = '', $defaultLifetime = 0) { - $this->namespace = '' === $namespace ? '' : CacheItem::validateKey($namespace).':'; + $this->namespace = '' === $namespace ? '' : CacheItem::validateKey($namespace).static::getNsSeparator(); if (null !== $this->maxIdLength && \strlen($namespace) > $this->maxIdLength - 24) { throw new InvalidArgumentException(sprintf('Namespace must be %d chars max, %d given ("%s")', $this->maxIdLength - 24, \strlen($namespace), $namespace)); } diff --git a/src/Symfony/Component/Cache/Adapter/SimpleCacheAdapter.php b/src/Symfony/Component/Cache/Adapter/SimpleCacheAdapter.php index bdb62a4bb3809..de4dd745cd119 100644 --- a/src/Symfony/Component/Cache/Adapter/SimpleCacheAdapter.php +++ b/src/Symfony/Component/Cache/Adapter/SimpleCacheAdapter.php @@ -75,4 +75,12 @@ protected function doSave(array $values, $lifetime) { return $this->pool->setMultiple($values, 0 === $lifetime ? null : $lifetime); } + + /** + * @return string the namespace separator for cache keys + */ + protected static function getNsSeparator() + { + return '_'; + } } diff --git a/src/Symfony/Component/Cache/Traits/AbstractTrait.php b/src/Symfony/Component/Cache/Traits/AbstractTrait.php index be576098e45eb..83a86a5ee9e2e 100644 --- a/src/Symfony/Component/Cache/Traits/AbstractTrait.php +++ b/src/Symfony/Component/Cache/Traits/AbstractTrait.php @@ -106,7 +106,7 @@ public function clear() { $this->deferred = []; if ($cleared = $this->versioningIsEnabled) { - $namespaceVersion = substr_replace(base64_encode(pack('V', mt_rand())), ':', 5); + $namespaceVersion = substr_replace(base64_encode(pack('V', mt_rand())), static::getNsSeparator(), 5); try { $cleared = $this->doSave(['@'.$this->namespace => $namespaceVersion], 0); } catch (\Exception $e) { @@ -235,13 +235,13 @@ private function getId($key) CacheItem::validateKey($key); if ($this->versioningIsEnabled && '' === $this->namespaceVersion) { - $this->namespaceVersion = '1:'; + $this->namespaceVersion = '1'.static::getNsSeparator(); try { foreach ($this->doFetch(['@'.$this->namespace]) as $v) { $this->namespaceVersion = $v; } - if ('1:' === $this->namespaceVersion) { - $this->namespaceVersion = substr_replace(base64_encode(pack('V', time())), ':', 5); + if ('1'.static::getNsSeparator() === $this->namespaceVersion) { + $this->namespaceVersion = substr_replace(base64_encode(pack('V', time())), static::getNsSeparator(), 5); $this->doSave(['@'.$this->namespace => $this->namespaceVersion], 0); } } catch (\Exception $e) { @@ -252,7 +252,7 @@ private function getId($key) return $this->namespace.$this->namespaceVersion.$key; } if (\strlen($id = $this->namespace.$this->namespaceVersion.$key) > $this->maxIdLength) { - $id = $this->namespace.$this->namespaceVersion.substr_replace(base64_encode(hash('sha256', $key, true)), ':', -(\strlen($this->namespaceVersion) + 22)); + $id = $this->namespace.$this->namespaceVersion.substr_replace(base64_encode(hash('sha256', $key, true)), static::getNsSeparator(), -(\strlen($this->namespaceVersion) + 22)); } return $id; @@ -265,4 +265,12 @@ public static function handleUnserializeCallback($class) { throw new \DomainException('Class not found: '.$class); } + + /** + * @return string the namespace separator for cache keys + */ + protected static function getNsSeparator() + { + return ':'; + } }