Skip to content
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

PSR-16 standard compliance #120

Draft
wants to merge 7 commits into
base: 2.27.x
Choose a base branch
from
Draft
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
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
163 changes: 52 additions & 111 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 8 additions & 22 deletions docs/book/translator/caching.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
29 changes: 7 additions & 22 deletions docs/book/translator/factory.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 9 additions & 14 deletions src/Translator/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -13,6 +11,7 @@
use Laminas\ServiceManager\ServiceManager;
use Laminas\Stdlib\ArrayUtils;
use Locale;
use Psr\SimpleCache\CacheInterface;
use Traversable;

use function array_shift;
Expand Down Expand Up @@ -84,7 +83,7 @@ class Translator implements TranslatorInterface
/**
* Translation cache.
*
* @var CacheStorage|null
* @var CacheInterface|null
*/
protected $cache;

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -290,7 +285,7 @@ public function getFallbackLocale()
*
* @return $this
*/
public function setCache(?CacheStorage $cache = null)
public function setCache(?CacheInterface $cache = null)
{
$this->cache = $cache;

Expand All @@ -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()
{
Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -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));
}

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

Expand Down
Loading