From a1fe19f465c9b176059d0198ce96ecdaf7ec30ed Mon Sep 17 00:00:00 2001 From: Andy Miller Date: Mon, 27 Oct 2025 12:31:15 -0600 Subject: [PATCH] replace doctrine/cache with symfony/cache Signed-off-by: Andy Miller --- CHANGELOG.md | 8 +- README.md | 2 +- composer.json | 1 - composer.lock | 96 +---- system/src/Doctrine/Common/Cache/Cache.php | 79 +++++ .../Doctrine/Common/Cache/CacheProvider.php | 329 ++++++++++++++++++ .../Doctrine/Common/Cache/ClearableCache.php | 25 ++ .../Doctrine/Common/Cache/FilesystemCache.php | 81 +---- .../Doctrine/Common/Cache/FlushableCache.php | 22 ++ .../Common/Cache/MultiDeleteCache.php | 26 ++ .../Doctrine/Common/Cache/MultiGetCache.php | 27 ++ .../Common/Cache/MultiOperationCache.php | 16 + .../Doctrine/Common/Cache/MultiPutCache.php | 28 ++ system/src/Grav/Common/Cache.php | 17 +- .../Common/Cache/SymfonyCacheProvider.php | 171 +++++++++ 15 files changed, 748 insertions(+), 180 deletions(-) create mode 100644 system/src/Doctrine/Common/Cache/Cache.php create mode 100644 system/src/Doctrine/Common/Cache/CacheProvider.php create mode 100644 system/src/Doctrine/Common/Cache/ClearableCache.php create mode 100644 system/src/Doctrine/Common/Cache/FlushableCache.php create mode 100644 system/src/Doctrine/Common/Cache/MultiDeleteCache.php create mode 100644 system/src/Doctrine/Common/Cache/MultiGetCache.php create mode 100644 system/src/Doctrine/Common/Cache/MultiOperationCache.php create mode 100644 system/src/Doctrine/Common/Cache/MultiPutCache.php create mode 100644 system/src/Grav/Common/Cache/SymfonyCacheProvider.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 85ece5eab..1c0ba6f77 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +# v1.8.0-beta.18 +## mm/dd/2025 + +1. [](#improved) + * Replaced legacy Doctrine cache dependency with Symfony-backed provider while keeping compatibility layer + # v1.8.0-beta.17 ## 10/23/2025 @@ -5,7 +11,7 @@ * Reworked `Monolog3` ship for better compatibility * Latest vendor libraries * Don't crash if `getManifest()` is not available - + # v1.8.0-beta.16 ## 10/20/2025 diff --git a/README.md b/README.md index 105d80071..026f39ca0 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ The underlying architecture of Grav is designed to use well-established and _bes * [Markdown](https://en.wikipedia.org/wiki/Markdown): for easy content creation * [YAML](https://yaml.org): for simple configuration * [Parsedown](https://parsedown.org/): for fast Markdown and Markdown Extra support -* [Doctrine Cache](https://www.doctrine-project.org/projects/doctrine-orm/en/latest/reference/caching.html): layer for performance +* [Symfony Cache](https://symfony.com/doc/current/components/cache.html): backend layer for performance * [Pimple Dependency Injection Container](https://github.com/silexphp/Pimple): for extensibility and maintainability * [Symfony Event Dispatcher](https://symfony.com/doc/current/components/event_dispatcher/introduction.html): for plugin event handling * [Symfony Console](https://symfony.com/doc/current/components/console/introduction.html): for CLI interface diff --git a/composer.json b/composer.json index 36f2d7b81..078ef4414 100644 --- a/composer.json +++ b/composer.json @@ -39,7 +39,6 @@ "symfony/http-client": "^6.4 || ^7.0", "twig/twig": "3.x-dev", "monolog/monolog": "^3.0", - "doctrine/cache": "^2.2", "doctrine/collections": "^2.2", "pimple/pimple": "~3.5.0", "nyholm/psr7-server": "^1.1", diff --git a/composer.lock b/composer.lock index 8d1ee7067..a2be6b60d 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": "02fd3ae5867b77e79ecaaa2b1b6a13fa", + "content-hash": "1dabba74120393d13e2396f85cae8c38", "packages": [ { "name": "antoligy/dom-string-iterators", @@ -203,100 +203,6 @@ ], "time": "2025-08-20T19:15:30+00:00" }, - { - "name": "doctrine/cache", - "version": "2.2.0", - "source": { - "type": "git", - "url": "https://github.com/doctrine/cache.git", - "reference": "1ca8f21980e770095a31456042471a57bc4c68fb" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/doctrine/cache/zipball/1ca8f21980e770095a31456042471a57bc4c68fb", - "reference": "1ca8f21980e770095a31456042471a57bc4c68fb", - "shasum": "" - }, - "require": { - "php": "~7.1 || ^8.0" - }, - "conflict": { - "doctrine/common": ">2.2,<2.4" - }, - "require-dev": { - "cache/integration-tests": "dev-master", - "doctrine/coding-standard": "^9", - "phpunit/phpunit": "^7.5 || ^8.5 || ^9.5", - "psr/cache": "^1.0 || ^2.0 || ^3.0", - "symfony/cache": "^4.4 || ^5.4 || ^6", - "symfony/var-exporter": "^4.4 || ^5.4 || ^6" - }, - "type": "library", - "autoload": { - "psr-4": { - "Doctrine\\Common\\Cache\\": "lib/Doctrine/Common/Cache" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Guilherme Blanco", - "email": "guilhermeblanco@gmail.com" - }, - { - "name": "Roman Borschel", - "email": "roman@code-factory.org" - }, - { - "name": "Benjamin Eberlei", - "email": "kontakt@beberlei.de" - }, - { - "name": "Jonathan Wage", - "email": "jonwage@gmail.com" - }, - { - "name": "Johannes Schmitt", - "email": "schmittjoh@gmail.com" - } - ], - "description": "PHP Doctrine Cache library is a popular cache implementation that supports many different drivers such as redis, memcache, apc, mongodb and others.", - "homepage": "https://www.doctrine-project.org/projects/cache.html", - "keywords": [ - "abstraction", - "apcu", - "cache", - "caching", - "couchdb", - "memcached", - "php", - "redis", - "xcache" - ], - "support": { - "issues": "https://github.com/doctrine/cache/issues", - "source": "https://github.com/doctrine/cache/tree/2.2.0" - }, - "funding": [ - { - "url": "https://www.doctrine-project.org/sponsorship.html", - "type": "custom" - }, - { - "url": "https://www.patreon.com/phpdoctrine", - "type": "patreon" - }, - { - "url": "https://tidelift.com/funding/github/packagist/doctrine%2Fcache", - "type": "tidelift" - } - ], - "abandoned": true, - "time": "2022-05-20T20:07:39+00:00" - }, { "name": "doctrine/collections", "version": "2.3.0", diff --git a/system/src/Doctrine/Common/Cache/Cache.php b/system/src/Doctrine/Common/Cache/Cache.php new file mode 100644 index 000000000..f78094aaa --- /dev/null +++ b/system/src/Doctrine/Common/Cache/Cache.php @@ -0,0 +1,79 @@ +namespace = (string) $namespace; + $this->namespaceVersion = null; + } + + /** + * Retrieves the namespace that prefixes all cache ids. + * + * @return string + */ + public function getNamespace() + { + return $this->namespace; + } + + /** + * {@inheritdoc} + */ + public function fetch($id) + { + return $this->doFetch($this->getNamespacedId($id)); + } + + /** + * {@inheritdoc} + */ + public function fetchMultiple(array $keys) + { + if (empty($keys)) { + return []; + } + + // note: the array_combine() is in place to keep an association between our $keys and the $namespacedKeys + $namespacedKeys = array_combine($keys, array_map([$this, 'getNamespacedId'], $keys)); + $items = $this->doFetchMultiple($namespacedKeys); + $foundItems = []; + + // no internal array function supports this sort of mapping: needs to be iterative + // this filters and combines keys in one pass + foreach ($namespacedKeys as $requestedKey => $namespacedKey) { + if (! isset($items[$namespacedKey]) && ! array_key_exists($namespacedKey, $items)) { + continue; + } + + $foundItems[$requestedKey] = $items[$namespacedKey]; + } + + return $foundItems; + } + + /** + * {@inheritdoc} + */ + public function saveMultiple(array $keysAndValues, $lifetime = 0) + { + $namespacedKeysAndValues = []; + foreach ($keysAndValues as $key => $value) { + $namespacedKeysAndValues[$this->getNamespacedId($key)] = $value; + } + + return $this->doSaveMultiple($namespacedKeysAndValues, $lifetime); + } + + /** + * {@inheritdoc} + */ + public function contains($id) + { + return $this->doContains($this->getNamespacedId($id)); + } + + /** + * {@inheritdoc} + */ + public function save($id, $data, $lifeTime = 0) + { + return $this->doSave($this->getNamespacedId($id), $data, $lifeTime); + } + + /** + * {@inheritdoc} + */ + public function deleteMultiple(array $keys) + { + return $this->doDeleteMultiple(array_map([$this, 'getNamespacedId'], $keys)); + } + + /** + * {@inheritdoc} + */ + public function delete($id) + { + return $this->doDelete($this->getNamespacedId($id)); + } + + /** + * {@inheritdoc} + */ + public function getStats() + { + return $this->doGetStats(); + } + + /** + * {@inheritDoc} + */ + public function flushAll() + { + return $this->doFlush(); + } + + /** + * {@inheritDoc} + */ + public function deleteAll() + { + $namespaceCacheKey = $this->getNamespaceCacheKey(); + $namespaceVersion = $this->getNamespaceVersion() + 1; + + if ($this->doSave($namespaceCacheKey, $namespaceVersion)) { + $this->namespaceVersion = $namespaceVersion; + + return true; + } + + return false; + } + + /** + * Prefixes the passed id with the configured namespace value. + * + * @param string $id The id to namespace. + * + * @return string The namespaced id. + */ + private function getNamespacedId(string $id): string + { + $namespaceVersion = $this->getNamespaceVersion(); + + return sprintf('%s[%s][%s]', $this->namespace, $id, $namespaceVersion); + } + + /** + * Returns the namespace cache key. + */ + private function getNamespaceCacheKey(): string + { + return sprintf(self::DOCTRINE_NAMESPACE_CACHEKEY, $this->namespace); + } + + /** + * Returns the namespace version. + */ + private function getNamespaceVersion(): int + { + if ($this->namespaceVersion !== null) { + return $this->namespaceVersion; + } + + $namespaceCacheKey = $this->getNamespaceCacheKey(); + $this->namespaceVersion = (int) $this->doFetch($namespaceCacheKey) ?: 1; + + return $this->namespaceVersion; + } + + /** + * Default implementation of doFetchMultiple. Each driver that supports multi-get should overwrite it. + * + * @param string[] $keys Array of keys to retrieve from cache + * + * @return mixed[] Array of values retrieved for the given keys. + */ + protected function doFetchMultiple(array $keys) + { + $returnValues = []; + + foreach ($keys as $key) { + $item = $this->doFetch($key); + if ($item === false && ! $this->doContains($key)) { + continue; + } + + $returnValues[$key] = $item; + } + + return $returnValues; + } + + /** + * Fetches an entry from the cache. + * + * @param string $id The id of the cache entry to fetch. + * + * @return mixed|false The cached data or FALSE, if no cache entry exists for the given id. + */ + abstract protected function doFetch($id); + + /** + * Tests if an entry exists in the cache. + * + * @param string $id The cache id of the entry to check for. + * + * @return bool TRUE if a cache entry exists for the given cache id, FALSE otherwise. + */ + abstract protected function doContains($id); + + /** + * Default implementation of doSaveMultiple. Each driver that supports multi-put should override it. + * + * @param mixed[] $keysAndValues Array of keys and values to save in cache + * @param int $lifetime The lifetime. If != 0, sets a specific lifetime for these + * cache entries (0 => infinite lifeTime). + * + * @return bool TRUE if the operation was successful, FALSE if it wasn't. + */ + protected function doSaveMultiple(array $keysAndValues, $lifetime = 0) + { + $success = true; + + foreach ($keysAndValues as $key => $value) { + if ($this->doSave($key, $value, $lifetime)) { + continue; + } + + $success = false; + } + + return $success; + } + + /** + * Puts data into the cache. + * + * @param string $id The cache id. + * @param string $data The cache entry/data. + * @param int $lifeTime The lifetime. If != 0, sets a specific lifetime for this + * cache entry (0 => infinite lifeTime). + * + * @return bool TRUE if the entry was successfully stored in the cache, FALSE otherwise. + */ + abstract protected function doSave($id, $data, $lifeTime = 0); + + /** + * Default implementation of doDeleteMultiple. Each driver that supports multi-delete should override it. + * + * @param string[] $keys Array of keys to delete from cache + * + * @return bool TRUE if the operation was successful, FALSE if it wasn't + */ + protected function doDeleteMultiple(array $keys) + { + $success = true; + + foreach ($keys as $key) { + if ($this->doDelete($key)) { + continue; + } + + $success = false; + } + + return $success; + } + + /** + * Deletes a cache entry. + * + * @param string $id The cache id. + * + * @return bool TRUE if the cache entry was successfully deleted, FALSE otherwise. + */ + abstract protected function doDelete($id); + + /** + * Flushes all cache entries. + * + * @return bool TRUE if the cache entries were successfully flushed, FALSE otherwise. + */ + abstract protected function doFlush(); + + /** + * Retrieves cached information from the data store. + * + * @return mixed[]|null An associative array with server's statistics if available, NULL otherwise. + */ + abstract protected function doGetStats(); +} diff --git a/system/src/Doctrine/Common/Cache/ClearableCache.php b/system/src/Doctrine/Common/Cache/ClearableCache.php new file mode 100644 index 000000000..7d9edfcaa --- /dev/null +++ b/system/src/Doctrine/Common/Cache/ClearableCache.php @@ -0,0 +1,25 @@ +pool = new FilesystemAdapter('', 0, $directory); + parent::__construct(new FilesystemAdapter('', 0, $directory)); } - - /** - * {@inheritdoc} - */ - protected function doFetch($id) - { - $item = $this->pool->getItem(rawurlencode($id)); - - return $item->isHit() ? $item->get() : false; - } - - /** - * {@inheritdoc} - * - * @return bool - */ - protected function doContains($id) - { - return $this->pool->hasItem(rawurlencode($id)); - } - - /** - * {@inheritdoc} - * - * @return bool - */ - protected function doSave($id, $data, $lifeTime = 0) - { - $item = $this->pool->getItem(rawurlencode($id)); - - if (0 < $lifeTime) { - $item->expiresAfter($lifeTime); - } - - return $this->pool->save($item->set($data)); - } - - /** - * {@inheritdoc} - * - * @return bool - */ - protected function doDelete($id) - { - return $this->pool->deleteItem(rawurlencode($id)); - } - - /** - * {@inheritdoc} - * - * @return bool - */ - protected function doFlush() - { - return $this->pool->clear(); - } - - /** - * {@inheritdoc} - * - * @return array|null - */ - protected function doGetStats() - { - return null; - } - } diff --git a/system/src/Doctrine/Common/Cache/FlushableCache.php b/system/src/Doctrine/Common/Cache/FlushableCache.php new file mode 100644 index 000000000..c2b407d15 --- /dev/null +++ b/system/src/Doctrine/Common/Cache/FlushableCache.php @@ -0,0 +1,22 @@ + infinite lifeTime). + * + * @return bool TRUE if the operation was successful, FALSE if it wasn't. + */ + public function saveMultiple(array $keysAndValues, $lifetime = 0); +} diff --git a/system/src/Grav/Common/Cache.php b/system/src/Grav/Common/Cache.php index 692d27cbd..39c0a8b41 100644 --- a/system/src/Grav/Common/Cache.php +++ b/system/src/Grav/Common/Cache.php @@ -11,11 +11,11 @@ namespace Grav\Common; use DirectoryIterator; use Doctrine\Common\Cache\CacheProvider; -use Doctrine\Common\Cache\Psr6\DoctrineProvider; use Exception; use Grav\Common\Config\Config; use Grav\Common\Filesystem\Folder; use Grav\Common\Scheduler\Scheduler; +use Grav\Common\Cache\SymfonyCacheProvider; use LogicException; use Psr\SimpleCache\CacheInterface; use RocketTheme\Toolbox\Event\Event; @@ -34,7 +34,7 @@ use function is_array; /** * The GravCache object is used throughout Grav to store and retrieve cached data. - * It uses Symfony library (adding backward compatibility to Doctrine Cache) and supports a variety of caching mechanisms. Those include: + * It uses Symfony cache pools (while exposing the historic Doctrine cache API for backward compatibility) and supports a variety of caching mechanisms. Those include: * * APCu * RedisCache @@ -390,12 +390,12 @@ class Cache extends Getters $adapter = $this->getCacheAdapter(); } - $cache = DoctrineProvider::wrap($adapter); - if (!$cache instanceof CacheProvider) { - throw new \RuntimeException('Internal error'); + $driver = new SymfonyCacheProvider($adapter); + if ($adapter === $this->adapter) { + $driver->setNamespace($this->key); } - return $cache; + return $driver; } /** @@ -493,7 +493,10 @@ class Cache extends Getters public function setKey($key) { $this->key = $key; - $this->driver->setNamespace($this->key); + if ($this->driver instanceof CacheProvider) { + $this->driver->setNamespace($this->key); + } + $this->simpleCache = null; } /** diff --git a/system/src/Grav/Common/Cache/SymfonyCacheProvider.php b/system/src/Grav/Common/Cache/SymfonyCacheProvider.php new file mode 100644 index 000000000..ccf4881ed --- /dev/null +++ b/system/src/Grav/Common/Cache/SymfonyCacheProvider.php @@ -0,0 +1,171 @@ +adapter = $adapter; + } + + /** + * Expose the underlying Symfony cache pool for callers needing direct access. + */ + public function getAdapter(): AdapterInterface + { + return $this->adapter; + } + + /** + * {@inheritdoc} + */ + protected function doFetch($id) + { + try { + $item = $this->adapter->getItem($this->encode($id)); + } catch (InvalidArgumentException) { + return false; + } + + return $item->isHit() ? $item->get() : false; + } + + /** + * {@inheritdoc} + */ + protected function doFetchMultiple(array $keys) + { + if (!$keys) { + return []; + } + + $encoded = array_map([$this, 'encode'], $keys); + + try { + $items = $this->adapter->getItems($encoded); + } catch (InvalidArgumentException) { + return []; + } + + $results = []; + foreach ($items as $encodedKey => $item) { + if ($item->isHit()) { + $results[$encodedKey] = $item->get(); + } + } + + return $results; + } + + /** + * {@inheritdoc} + */ + protected function doContains($id) + { + try { + return $this->adapter->hasItem($this->encode($id)); + } catch (InvalidArgumentException) { + return false; + } + } + + /** + * {@inheritdoc} + */ + protected function doSave($id, $data, $lifeTime = 0) + { + try { + $item = $this->adapter->getItem($this->encode($id)); + } catch (InvalidArgumentException) { + return false; + } + + if ($lifeTime > 0) { + $item->expiresAfter($lifeTime); + } + + return $this->adapter->save($item->set($data)); + } + + /** + * {@inheritdoc} + */ + protected function doSaveMultiple(array $keysAndValues, $lifetime = 0) + { + if (!$keysAndValues) { + return true; + } + + $success = true; + foreach ($keysAndValues as $key => $value) { + if (!$this->doSave($key, $value, $lifetime)) { + $success = false; + } + } + + return $success; + } + + /** + * {@inheritdoc} + */ + protected function doDelete($id) + { + try { + return $this->adapter->deleteItem($this->encode($id)); + } catch (InvalidArgumentException) { + return false; + } + } + + /** + * {@inheritdoc} + */ + protected function doDeleteMultiple(array $keys) + { + if (!$keys) { + return true; + } + + try { + return $this->adapter->deleteItems(array_map([$this, 'encode'], $keys)); + } catch (InvalidArgumentException) { + return false; + } + } + + /** + * {@inheritdoc} + */ + protected function doFlush() + { + return $this->adapter->clear(); + } + + /** + * {@inheritdoc} + */ + protected function doGetStats() + { + return null; + } + + private function encode(string $id): string + { + return rawurlencode($id); + } +}