diff --git a/composer.json b/composer.json index a2bcfa06..e210bd59 100644 --- a/composer.json +++ b/composer.json @@ -35,12 +35,12 @@ "php": "~8.1.0 || ~8.2.0 || ~8.3.0", "ext-intl": "*", "laminas/laminas-servicemanager": "^3.21.0", - "laminas/laminas-stdlib": "^3.0" + "laminas/laminas-stdlib": "^3.0", + "psr/simple-cache": "^1.0 || ^2.0 || ^3.0" }, "require-dev": { "laminas/laminas-cache": "^3.12.1", "laminas/laminas-cache-storage-adapter-memory": "^2.3.0", - "laminas/laminas-cache-storage-deprecated-factory": "^1.2", "laminas/laminas-coding-standard": "~2.5.0", "laminas/laminas-config": "^3.9.0", "laminas/laminas-eventmanager": "^3.13", diff --git a/composer.lock b/composer.lock index 5ac4465a..624eb957 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": "31ac363ec8bf003ee6a61b444948f2b2", + "content-hash": "34e420973a6a579c4fd6fb930fc5f68b", "packages": [ { "name": "laminas/laminas-servicemanager", @@ -202,6 +202,57 @@ "source": "https://github.com/php-fig/container/tree/1.1.2" }, "time": "2021-11-05T16:50:12+00:00" + }, + { + "name": "psr/simple-cache", + "version": "1.0.1", + "source": { + "type": "git", + "url": "https://github.com/php-fig/simple-cache.git", + "reference": "408d5eafb83c57f6365a3ca330ff23aa4a5fa39b" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/php-fig/simple-cache/zipball/408d5eafb83c57f6365a3ca330ff23aa4a5fa39b", + "reference": "408d5eafb83c57f6365a3ca330ff23aa4a5fa39b", + "shasum": "" + }, + "require": { + "php": ">=5.3.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0.x-dev" + } + }, + "autoload": { + "psr-4": { + "Psr\\SimpleCache\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "PHP-FIG", + "homepage": "http://www.php-fig.org/" + } + ], + "description": "Common interfaces for simple caching", + "keywords": [ + "cache", + "caching", + "psr", + "psr-16", + "simple-cache" + ], + "support": { + "source": "https://github.com/php-fig/simple-cache/tree/master" + }, + "time": "2017-10-23T01:57:42+00:00" } ], "packages-dev": [ @@ -1098,65 +1149,6 @@ ], "time": "2023-10-18T09:43:33+00:00" }, - { - "name": "laminas/laminas-cache-storage-deprecated-factory", - "version": "1.2.0", - "source": { - "type": "git", - "url": "https://github.com/laminas/laminas-cache-storage-deprecated-factory.git", - "reference": "276ffbb6c01400fb071d8f6167650e6112d2de2f" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/laminas/laminas-cache-storage-deprecated-factory/zipball/276ffbb6c01400fb071d8f6167650e6112d2de2f", - "reference": "276ffbb6c01400fb071d8f6167650e6112d2de2f", - "shasum": "" - }, - "require": { - "laminas/laminas-cache": "^3.0", - "laminas/laminas-servicemanager": "^3.7", - "laminas/laminas-stdlib": "^3.6", - "php": "~8.0.0 || ~8.1.0 || ~8.2.0 || ~8.3.0", - "webmozart/assert": "^1.10" - }, - "require-dev": { - "laminas/laminas-cache-storage-adapter-apcu": "^2.4", - "laminas/laminas-cache-storage-adapter-blackhole": "^2.2", - "laminas/laminas-cache-storage-adapter-ext-mongodb": "^2.3", - "laminas/laminas-cache-storage-adapter-filesystem": "^2.3", - "laminas/laminas-cache-storage-adapter-memcached": "^2.4", - "laminas/laminas-cache-storage-adapter-memory": "^2.2", - "laminas/laminas-cache-storage-adapter-redis": "^2.5", - "laminas/laminas-cache-storage-adapter-session": "^2.3", - "laminas/laminas-coding-standard": "2.3", - "laminas/laminas-serializer": "^2.14", - "phpunit/phpunit": "^9.5.27", - "psalm/plugin-phpunit": "^0.18.4", - "vimeo/psalm": "^5.4" - }, - "type": "library", - "autoload": { - "psr-4": { - "Laminas\\Cache\\": "src/" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "BSD-3-Clause" - ], - "description": "Temporary storage adapter factory for fluent migration to laminas-cache v3 when working with laminas components which depend on laminas-cache", - "support": { - "issues": "https://github.com/laminas/laminas-cache-storage-deprecated-factory/issues", - "source": "https://github.com/laminas/laminas-cache-storage-deprecated-factory/tree/1.2.0" - }, - "funding": [ - { - "url": "https://funding.communitybridge.org/projects/laminas-project", - "type": "community_bridge" - } - ], - "time": "2024-01-08T14:30:59+00:00" - }, { "name": "laminas/laminas-coding-standard", "version": "2.5.0", @@ -2910,57 +2902,6 @@ }, "time": "2021-07-14T16:46:02+00:00" }, - { - "name": "psr/simple-cache", - "version": "1.0.1", - "source": { - "type": "git", - "url": "https://github.com/php-fig/simple-cache.git", - "reference": "408d5eafb83c57f6365a3ca330ff23aa4a5fa39b" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/php-fig/simple-cache/zipball/408d5eafb83c57f6365a3ca330ff23aa4a5fa39b", - "reference": "408d5eafb83c57f6365a3ca330ff23aa4a5fa39b", - "shasum": "" - }, - "require": { - "php": ">=5.3.0" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "1.0.x-dev" - } - }, - "autoload": { - "psr-4": { - "Psr\\SimpleCache\\": "src/" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "PHP-FIG", - "homepage": "http://www.php-fig.org/" - } - ], - "description": "Common interfaces for simple caching", - "keywords": [ - "cache", - "caching", - "psr", - "psr-16", - "simple-cache" - ], - "support": { - "source": "https://github.com/php-fig/simple-cache/tree/master" - }, - "time": "2017-10-23T01:57:42+00:00" - }, { "name": "sebastian/cli-parser", "version": "2.0.1", diff --git a/docs/book/translator/caching.md b/docs/book/translator/caching.md index fdea25ce..4c53b017 100755 --- a/docs/book/translator/caching.md +++ b/docs/book/translator/caching.md @@ -4,35 +4,21 @@ In production, it makes sense to cache your translations. This not only saves you from loading and parsing the individual formats each time, but also guarantees an optimized loading procedure. -> MISSING: **Installation Requirements** -> The cache support of laminas-i18n depends on the [laminas-cache](https://docs.laminas.dev/laminas-cache/) component, so be sure to have it installed before getting started: -> -> ```bash -> $ composer require laminas/laminas-cache -> ``` -> -> Version 3 of laminas-cache removed support for factories required by this component, so if your application requires laminas-cache version 3 or later, you will also need to install `laminas-cache-storage-deprecated-factory` -> -> ```bash -> $ composer require laminas/laminas-cache-storage-deprecated-factory -> ``` - ## Enable Caching -To enable caching, pass a `Laminas\Cache\Storage\Adapter` to the `setCache()` +To enable caching, pass a `Psr\SimpleCache\CacheInterface` to the `setCache()` method. +The following example is based on the use of the +[laminas-cache](https://docs.laminas.dev/laminas-cache/) component. + ```php $translator = new Laminas\I18n\Translator\Translator(); -$cache = Laminas\Cache\StorageFactory::factory([ - 'adapter' => [ - 'name' => Laminas\Cache\Storage\Adapter\Filesystem::class, - 'options' => [ - 'cache_dir' => __DIR__ . '/cache', - ], - ], +$cacheStorage = new Laminas\Cache\Storage\Adapter\Filesystem([ + 'cache_dir' => __DIR__ . '/cache', ]); -$translator->setCache($cache); +$cache = new Laminas\Cache\Psr\SimpleCache\SimpleCacheDecorator($cacheStorage); +$translator->setCache($cache);`` ``` The explanation of creating a cache and using different adapters for caching diff --git a/docs/book/translator/factory.md b/docs/book/translator/factory.md index 7b5dec25..9dc5e6e2 100755 --- a/docs/book/translator/factory.md +++ b/docs/book/translator/factory.md @@ -115,35 +115,20 @@ $translator->getPluginManager()->setService( ### Using a Cache Instance +The following example is based on the use of the +[laminas-cache](https://docs.laminas.dev/laminas-cache/) component. + ```php -$cache = Laminas\Cache\StorageFactory::factory([ - 'adapter' => [ - 'name' => Laminas\Cache\Storage\Adapter\Filesystem::class, - 'options' => [ - 'cache_dir' => __DIR__ . '/cache', - ], - ], +$translator = new Laminas\I18n\Translator\Translator(); +$cacheStorage = new Laminas\Cache\Storage\Adapter\Filesystem([ + 'cache_dir' => __DIR__ . '/cache', ]); +$cache = new Laminas\Cache\Psr\SimpleCache\SimpleCacheDecorator($cacheStorage); $translator = Laminas\I18n\Translator\Translator::factory([ 'cache' => $cache, ]); ``` -### Using Cache Configuration - -```php -$translator = Laminas\I18n\Translator\Translator::factory([ - 'cache' => [ - 'adapter' => [ - 'name' => Laminas\Cache\Storage\Adapter\Filesystem::class, - 'options' => [ - 'cache_dir' => __DIR__ . '/cache', - ], - ], - ], -]); -``` - ## Enable EventManager ```php diff --git a/src/Translator/Translator.php b/src/Translator/Translator.php index 597d2395..5406550a 100644 --- a/src/Translator/Translator.php +++ b/src/Translator/Translator.php @@ -2,8 +2,6 @@ namespace Laminas\I18n\Translator; -use Laminas\Cache; -use Laminas\Cache\Storage\StorageInterface as CacheStorage; use Laminas\EventManager\Event; use Laminas\EventManager\EventManager; use Laminas\EventManager\EventManagerInterface; @@ -13,6 +11,7 @@ use Laminas\ServiceManager\ServiceManager; use Laminas\Stdlib\ArrayUtils; use Locale; +use Psr\SimpleCache\CacheInterface; use Traversable; use function array_shift; @@ -84,7 +83,7 @@ class Translator implements TranslatorInterface /** * Translation cache. * - * @var CacheStorage|null + * @var CacheInterface|null */ protected $cache; @@ -220,11 +219,7 @@ public static function factory($options) // cache if (isset($options['cache'])) { - if ($options['cache'] instanceof CacheStorage) { - $translator->setCache($options['cache']); - } else { - $translator->setCache(Cache\StorageFactory::factory($options['cache'])); - } + $translator->setCache($options['cache']); } // event manager enabled @@ -290,7 +285,7 @@ public function getFallbackLocale() * * @return $this */ - public function setCache(?CacheStorage $cache = null) + public function setCache(?CacheInterface $cache = null) { $this->cache = $cache; @@ -300,7 +295,7 @@ public function setCache(?CacheStorage $cache = null) /** * Returns the set cache * - * @return CacheStorage|null The set cache + * @return CacheInterface|null The set cache */ public function getCache() { @@ -561,7 +556,7 @@ public function addRemoteTranslations($type, $textDomain = 'default') */ public function getCacheId($textDomain, $locale) { - return 'Laminas_I18n_Translator_Messages_' . md5($textDomain . $locale); + return 'Laminas_I18n_Translator_Msg_' . md5($textDomain . $locale); } /** @@ -576,7 +571,7 @@ public function clearCache($textDomain, $locale) if (null === ($cache = $this->getCache())) { return false; } - return $cache->removeItem($this->getCacheId($textDomain, $locale)); + return $cache->delete($this->getCacheId($textDomain, $locale)); } /** @@ -597,7 +592,7 @@ protected function loadMessages($textDomain, $locale) if (null !== ($cache = $this->getCache())) { $cacheId = $this->getCacheId($textDomain, $locale); - if (null !== ($result = $cache->getItem($cacheId))) { + if (null !== ($result = $cache->get($cacheId))) { $this->messages[$textDomain][$locale] = $result; return; @@ -631,7 +626,7 @@ protected function loadMessages($textDomain, $locale) } if ($cache !== null) { - $cache->setItem($cacheId, $this->messages[$textDomain][$locale]); + $cache->set($cacheId, $this->messages[$textDomain][$locale]); } } diff --git a/test/Translator/TranslatorTest.php b/test/Translator/TranslatorTest.php index 768c2ac6..d60ab2b6 100644 --- a/test/Translator/TranslatorTest.php +++ b/test/Translator/TranslatorTest.php @@ -4,8 +4,8 @@ namespace LaminasTest\I18n\Translator; -use Laminas\Cache\Storage\StorageInterface; -use Laminas\Cache\StorageFactory as CacheFactory; +use Laminas\Cache\Psr\SimpleCache\SimpleCacheDecorator; +use Laminas\Cache\Storage\Adapter\Memory; use Laminas\EventManager\Event; use Laminas\EventManager\EventInterface; use Laminas\I18n\Translator\TextDomain; @@ -124,6 +124,8 @@ public function testTranslationFromDifferentSourceTypes(): void public function testFactoryCreatesTranslatorWithCache(): void { + $cache = new SimpleCacheDecorator(new Memory()); + $translator = Translator::factory([ 'locale' => 'de_DE', 'patterns' => [ @@ -133,13 +135,11 @@ public function testFactoryCreatesTranslatorWithCache(): void 'pattern' => 'translation-%s.php', ], ], - 'cache' => [ - 'adapter' => 'memory', - ], + 'cache' => $cache, ]); self::assertInstanceOf(Translator::class, $translator); - self::assertInstanceOf(StorageInterface::class, $translator->getCache()); + $this->assertSame($cache, $translator->getCache()); } public function testDefaultLocale(): void @@ -172,10 +172,10 @@ public function testTranslate(): void public function testTranslationsLoadedFromCache(): void { - $cache = CacheFactory::factory(['adapter' => 'memory']); + $cache = new SimpleCacheDecorator(new Memory()); $this->translator->setCache($cache); - $cache->addItem( + $cache->set( $this->translator->getCacheId('default', 'en_EN'), new TextDomain(['foo' => 'bar']) ); @@ -185,7 +185,7 @@ public function testTranslationsLoadedFromCache(): void public function testTranslationsAreStoredInCache(): void { - $cache = CacheFactory::factory(['adapter' => 'memory']); + $cache = new SimpleCacheDecorator(new Memory()); $this->translator->setCache($cache); $loader = new TestLoader(); @@ -198,7 +198,7 @@ public function testTranslationsAreStoredInCache(): void self::assertEquals('bar', $this->translator->translate('foo')); - $item = $cache->getItem($this->translator->getCacheId('default', 'en_EN')); + $item = $cache->get($this->translator->getCacheId('default', 'en_EN')); self::assertInstanceOf(TextDomain::class, $item); self::assertEquals('bar', $item['foo']); } @@ -208,19 +208,18 @@ public function testTranslationsAreClearedFromCache(): void $textDomain = 'default'; $locale = 'en_EN'; - $cache = CacheFactory::factory(['adapter' => 'memory']); + $cache = new SimpleCacheDecorator(new Memory()); $this->translator->setCache($cache); - $cache->addItem( + $cache->set( $this->translator->getCacheId($textDomain, $locale), new TextDomain(['foo' => 'bar']) ); self::assertTrue($this->translator->clearCache($textDomain, $locale)); - $item = $cache->getItem($this->translator->getCacheId($textDomain, $locale), $success); - self::assertNull($item); - self::assertFalse($success); + $item = $cache->get($this->translator->getCacheId($textDomain, $locale)); + $this->assertNull($item); } public function testClearCacheReturnsFalseIfNoCacheIsPresent(): void