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

Updating Translator code with PHP 8.1 features #648

Merged
merged 3 commits into from
Apr 3, 2022
Merged
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@
in `Spiral\Console\SequenceInterface` interface.
- [spiral/files] Added return type `bool` to the method `delete`, added return type `bool` to the method `deleteDirectory`,
added return type `bool` to the method `touch`, added return type `bool` to the method `setPermissions` in `Spiral\Files\FilesInterface`.
- [spiral/translator] Added return type `void` to a methods `setLocales`, `saveLocale` in `Spiral\Translator\Catalogue\CacheInterface`.
- [spiral/translator] Added return type `void` to the method `save` in `Spiral\Translator\CatalogueManagerInterface`.
- [spiral/storage] Added `string|\Stringable` parameter type of `$id` to a methods `getContents`, `getStream`,
`exists`, `getLastModified`, `getSize`, `getMimeType`, `getVisibility` in `Spiral\Storage\Storage\ReadableInterface`.
- [spiral/storage] Added `string|\Stringable` parameter type of `$id` to a methods `create`, `setVisibility`,
Expand Down
52 changes: 12 additions & 40 deletions src/Translator/src/Catalogue.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
<?php

/**
* Spiral Framework.
*
* @license MIT
* @author Anton Titov (Wolfy-J)
*/

declare(strict_types=1);

namespace Spiral\Translator;
Expand All @@ -20,69 +13,48 @@
*/
final class Catalogue implements CatalogueInterface
{
/** @var string */
private $locale;

/** @var array<string, array<string, string>> */
private $data = [];

public function __construct(string $locale, array $data = [])
{
$this->locale = $locale;
$this->data = $data;
}

/**
* @inheritdoc
* @param array<string, array<string, string>> $data
*/
public function __construct(
private readonly string $locale,
private array $data = []
) {
}

public function getLocale(): string
{
return $this->locale;
}

/**
* @inheritdoc
*/
public function getDomains(): array
{
return array_keys($this->data);
return \array_keys($this->data);
}

/**
* @inheritdoc
*/
public function has(string $domain, string $id): bool
{
if (!isset($this->data[$domain])) {
return false;
}

return array_key_exists($id, $this->data[$domain]);
return \array_key_exists($id, $this->data[$domain]);
}

/**
* @inheritdoc
*/
public function get(string $domain, string $id): string
{
if (!$this->has($domain, $id)) {
throw new CatalogueException("Undefined string in domain '{$domain}'");
throw new CatalogueException(\sprintf("Undefined string in domain '%s'", $domain));
}

return $this->data[$domain][$id];
}

/**
* @inheritdoc
*/
public function set(string $domain, string $id, string $translation): void
{
$this->data[$domain][$id] = $translation;
}

/**
* @inheritdoc
*/
public function getData(): array
{
return $this->data;
Expand All @@ -101,9 +73,9 @@ public function mergeFrom(MessageCatalogue $catalogue, bool $follow = true): voi

if ($follow) {
//MessageCatalogue string has higher priority that string stored in memory
$this->data[$domain] = array_merge($messages, $this->data[$domain]);
$this->data[$domain] = \array_merge($messages, $this->data[$domain]);
} else {
$this->data[$domain] = array_merge($this->data[$domain], $messages);
$this->data[$domain] = \array_merge($this->data[$domain], $messages);
}
}
}
Expand Down
11 changes: 2 additions & 9 deletions src/Translator/src/Catalogue/CacheInterface.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
<?php

/**
* Spiral Framework.
*
* @license MIT
* @author Anton Titov (Wolfy-J)
*/

declare(strict_types=1);

namespace Spiral\Translator\Catalogue;
Expand All @@ -16,7 +9,7 @@ interface CacheInterface
/**
* Cache list of available locates.
*/
public function setLocales(?array $locales);
public function setLocales(?array $locales): void;

/**
* Get cached list of locales.
Expand All @@ -26,7 +19,7 @@ public function getLocales(): ?array;
/**
* Store locale data.
*/
public function saveLocale(string $locale, ?array $data);
public function saveLocale(string $locale, ?array $data): void;

/**
* Load cached locale data.
Expand Down
40 changes: 10 additions & 30 deletions src/Translator/src/Catalogue/CatalogueLoader.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
<?php

/**
* Spiral Framework.
*
* @license MIT
* @author Anton Titov (Wolfy-J)
*/

declare(strict_types=1);

namespace Spiral\Translator\Catalogue;
Expand All @@ -16,36 +9,26 @@
use Spiral\Translator\CatalogueInterface;
use Spiral\Translator\Config\TranslatorConfig;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Finder\SplFileInfo;

final class CatalogueLoader implements LoaderInterface
{
use LoggerTrait;

/** @var TranslatorConfig */
private $config;

public function __construct(TranslatorConfig $config)
{
$this->config = $config;
public function __construct(
private readonly TranslatorConfig $config
) {
}

/**
* @inheritdoc
*/
public function hasLocale(string $locale): bool
{
$locale = preg_replace('/[^a-zA-Z_]/', '', mb_strtolower($locale));
$locale = \preg_replace('/[^a-zA-Z_]/', '', \mb_strtolower($locale));

return is_dir($this->config->getLocaleDirectory($locale));
return \is_dir($this->config->getLocaleDirectory($locale));
}

/**
* @inheritdoc
*/
public function getLocales(): array
{
if (!is_dir($this->config->getLocalesDirectory())) {
if (!\is_dir($this->config->getLocalesDirectory())) {
return [];
}

Expand All @@ -61,12 +44,9 @@ public function getLocales(): array
return $locales;
}

/**
* @inheritdoc
*/
public function loadCatalogue(string $locale): CatalogueInterface
{
$locale = preg_replace('/[^a-zA-Z_]/', '', mb_strtolower($locale));
$locale = \preg_replace('/[^a-zA-Z_]/', '', \mb_strtolower($locale));
$catalogue = new Catalogue($locale);

if (!$this->hasLocale($locale)) {
Expand All @@ -78,19 +58,19 @@ public function loadCatalogue(string $locale): CatalogueInterface

foreach ($finder->getIterator() as $file) {
$this->getLogger()->info(
sprintf(
\sprintf(
"found locale domain file '%s'",
$file->getFilename()
),
['file' => $file->getFilename()]
);

//Per application agreement domain name must present in filename
$domain = strstr($file->getFilename(), '.', true);
$domain = \strstr($file->getFilename(), '.', true);

if (!$this->config->hasLoader($file->getExtension())) {
$this->getLogger()->warning(
sprintf(
\sprintf(
"unable to load domain file '%s', no loader found",
$file->getFilename()
),
Expand Down
46 changes: 9 additions & 37 deletions src/Translator/src/Catalogue/CatalogueManager.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
<?php

/**
* Spiral Framework.
*
* @license MIT
* @author Anton Titov (Wolfy-J)
*/

declare(strict_types=1);

namespace Spiral\Translator\Catalogue;
Expand All @@ -20,30 +13,21 @@
*/
final class CatalogueManager implements CatalogueManagerInterface
{
/** @var LoaderInterface */
private $loader;
private array $locales = [];

/**
* @internal
* @var CacheInterface
*/
private $cache;

/** @var array */
private $locales = [];
/** @internal */
private readonly CacheInterface $cache;

/** @var Catalogue[] */
private $catalogues = [];
private array $catalogues = [];

public function __construct(LoaderInterface $loader, CacheInterface $cache = null)
{
$this->loader = $loader;
public function __construct(
private readonly LoaderInterface $loader,
CacheInterface $cache = null
) {
$this->cache = $cache ?? new NullCache();
}

/**
* @inheritdoc
*/
public function getLocales(): array
{
if ($this->locales !== []) {
Expand All @@ -59,9 +43,6 @@ public function getLocales(): array
return $this->locales;
}

/**
* @inheritdoc
*/
public function load(string $locale): CatalogueInterface
{
if (isset($this->catalogues[$locale])) {
Expand All @@ -78,25 +59,16 @@ public function load(string $locale): CatalogueInterface
return $this->catalogues[$locale];
}

/**
* @inheritdoc
*/
public function save(string $locale): void
{
$this->cache->saveLocale($locale, $this->get($locale)->getData());
}

/**
* @inheritdoc
*/
public function has(string $locale): bool
{
return isset($this->catalogues[$locale]) || in_array($locale, $this->getLocales());
return isset($this->catalogues[$locale]) || \in_array($locale, $this->getLocales());
}

/**
* @inheritdoc
*/
public function get(string $locale): CatalogueInterface
{
return $this->load($locale);
Expand Down
7 changes: 0 additions & 7 deletions src/Translator/src/Catalogue/LoaderInterface.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
<?php

/**
* Spiral Framework.
*
* @license MIT
* @author Anton Titov (Wolfy-J)
*/

declare(strict_types=1);

namespace Spiral\Translator\Catalogue;
Expand Down
19 changes: 0 additions & 19 deletions src/Translator/src/Catalogue/NullCache.php
Original file line number Diff line number Diff line change
@@ -1,43 +1,24 @@
<?php

/**
* Spiral Framework.
*
* @license MIT
* @author Anton Titov (Wolfy-J)
*/

declare(strict_types=1);

namespace Spiral\Translator\Catalogue;

final class NullCache implements CacheInterface
{
/**
* @inheritdoc
*/
public function setLocales(?array $locales): void
{
}

/**
* @inheritdoc
*/
public function getLocales(): ?array
{
return null;
}

/**
* @inheritdoc
*/
public function saveLocale(string $locale, ?array $data): void
{
}

/**
* @inheritdoc
*/
public function loadLocale(string $locale): ?array
{
return null;
Expand Down
Loading