Skip to content

Commit

Permalink
add cache flag if translations needs to be reloaded
Browse files Browse the repository at this point in the history
  • Loading branch information
Warxcell committed Jun 17, 2024
1 parent 1cbf7c8 commit f9c8b01
Show file tree
Hide file tree
Showing 10 changed files with 116 additions and 28 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"translation"
],
"require": {
"php": "~7.4 | ^8.0",
"php": "^8.2",
"doctrine/orm": "~2.5",
"symfony/framework-bundle": "*",
"symfony/translation-contracts": "*",
Expand Down
33 changes: 33 additions & 0 deletions src/CacheFlag.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Arxy\TranslationsBundle;

use Psr\Cache\CacheItemInterface;
use Psr\Cache\CacheItemPoolInterface;

final readonly class CacheFlag
{
public function __construct(
private CacheItemPoolInterface $cache,
private string $key = 'translations'
) {
}

public function getVersion(): CacheItemInterface
{
$item = $this->cache->getItem($this->key);
if (!$item->isHit()) {
$item->set(0);
}

return $item;
}

public function increment(CacheItemInterface $item): void
{
$item->set($item->get() + 1);
$this->cache->save($item);
}
}
7 changes: 5 additions & 2 deletions src/DependencyInjection/ArxyTranslationsExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@

namespace Arxy\TranslationsBundle\DependencyInjection;

use Arxy\TranslationsBundle\CacheFlag;
use Arxy\TranslationsBundle\Repository;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;

/**
Expand All @@ -25,9 +27,10 @@ public function load(array $configs, ContainerBuilder $container): void
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);

$loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
$loader->load('services.xml');
$loader = new Loader\PhpFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.php');

$container->setAlias(Repository::class, $config['repository']);
$container->getDefinition(CacheFlag::class)->setArgument('$cache', new Reference($config['cache_flag']));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@

namespace Arxy\TranslationsBundle\DependencyInjection\CompilerPass;

use Arxy\TranslationsBundle\CacheFlag;
use Arxy\TranslationsBundle\Repository;
use Arxy\TranslationsBundle\Translator;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;

class OverrideTranslatorPass implements CompilerPassInterface
final readonly class OverrideTranslatorPass implements CompilerPassInterface
{
/**
* {@inheritdoc}
Expand All @@ -20,6 +21,7 @@ public function process(ContainerBuilder $container): void
$container->getDefinition('translator.default')
->setClass(Translator::class)
->addTag('kernel.reset', ['method' => 'reset'])
->addMethodCall('setRepository', [new Reference(Repository::class)]);
->addMethodCall('setRepository', [new Reference(Repository::class)])
->addMethodCall('setCacheFlag', [new Reference(CacheFlag::class)]);
}
}
3 changes: 2 additions & 1 deletion src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*
* To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html#cookbook-bundles-extension-config-class}
*/
class Configuration implements ConfigurationInterface
final readonly class Configuration implements ConfigurationInterface
{
/**
* {@inheritDoc}
Expand All @@ -37,6 +37,7 @@ public function getConfigTreeBuilder()
*/
$root->children()
->scalarNode('repository')->isRequired()->cannotBeEmpty()->end()
->scalarNode('cache_flag')->isRequired()->cannotBeEmpty()->end()
->end();
// @formatter:on

Expand Down
10 changes: 4 additions & 6 deletions src/Dumper/DatabaseDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,11 @@
/**
* @internal
*/
final class DatabaseDumper implements DumperInterface
final readonly class DatabaseDumper implements DumperInterface
{
private Repository $repository;

public function __construct(Repository $repository)
{
$this->repository = $repository;
public function __construct(
private Repository $repository
) {
}

public function dump(MessageCatalogue $messages, $options = []): void
Expand Down
2 changes: 1 addition & 1 deletion src/Model/TranslationModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
/**
* Lightweight DTO to avoid heavy hydrating of ORM
*/
class TranslationModel implements Translation
final readonly class TranslationModel implements Translation
{
private string $translation;
private string $token;
Expand Down
46 changes: 46 additions & 0 deletions src/Resources/config/services.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

declare(strict_types=1);
/*
* Copyright (C) 2016-2024 Taylor & Hart Limited
* All Rights Reserved.
*
* NOTICE: All information contained herein is, and remains the property
* of Taylor & Hart Limited and its suppliers, if any.
*
* All intellectual and technical concepts contained herein are
* proprietary to Taylor & Hart Limited and its suppliers and may be
* covered by U.K. and foreign patents, patents in process, and are
* protected in full by copyright law. Dissemination of this information
* or reproduction of this material is strictly forbidden unless prior
* written permission is obtained from Taylor & Hart Limited.
*
* ANY REPRODUCTION, MODIFICATION, DISTRIBUTION, PUBLIC PERFORMANCE, OR
* PUBLIC DISPLAY OF OR THROUGH USE OF THIS SOURCE CODE WITHOUT THE
* EXPRESS WRITTEN CONSENT OF RARE PINK LIMITED IS STRICTLY PROHIBITED,
* AND IN VIOLATION OF APPLICABLE LAWS. THE RECEIPT OR POSSESSION OF
* THIS SOURCE CODE AND/OR RELATED INFORMATION DOES NOT CONVEY OR IMPLY
* ANY RIGHTS TO REPRODUCE, DISCLOSE OR DISTRIBUTE ITS CONTENTS, OR TO
* MANUFACTURE, USE, OR SELL ANYTHING THAT IT MAY DESCRIBE, IN WHOLE OR
* IN PART.
*/

namespace Symfony\Component\DependencyInjection\Loader\Configurator;

use Arxy\TranslationsBundle\CacheFlag;
use Arxy\TranslationsBundle\Dumper\DatabaseDumper;
use Arxy\TranslationsBundle\Repository;

return static function (ContainerConfigurator $container) {
$services = $container->services()
->defaults()
->autoconfigure()
->autowire();

$services->set(DatabaseDumper::class)->args([
service(Repository::class),
])
->tag('translation.dumper', ['alias' => 'db']);

$services->set(CacheFlag::class);
};
13 changes: 0 additions & 13 deletions src/Resources/config/services.xml

This file was deleted.

22 changes: 20 additions & 2 deletions src/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@
*/
class Translator extends OriginalTranslator implements ResetInterface
{
private Repository $repository;
private Repository|null $repository = null;

private CacheFlag|null $cacheFlag = null;

private int $version = 0;
private bool $warmUp = false;

/**
Expand All @@ -24,6 +28,14 @@ public function setRepository(Repository $repository): void
$this->repository = $repository;
}

/**
* @required
*/
public function setCacheFlag(CacheFlag $cacheFlag): void
{
$this->cacheFlag = $cacheFlag;
}

protected function loadCatalogue(string $locale): void
{
parent::loadCatalogue($locale);
Expand All @@ -38,7 +50,7 @@ protected function loadCatalogue(string $locale): void

private function fetchTranslations(string $locale): void
{
$translations = $this->repository->findByLocale($locale);
$translations = $this->repository?->findByLocale($locale) ?? [];
$catalogue = $this->catalogues[$locale];
foreach ($translations as $translation) {
$catalogue->set(
Expand Down Expand Up @@ -67,6 +79,11 @@ private function loadFallbackTranslations(MessageCatalogueInterface $catalogue):

public function reset(): void
{
$version = $this->cacheFlag?->getVersion();
if (null !== $version && $version->get() === $this->version) {
return;
}

foreach ($this->catalogues as $locale => $catalogue) {
$translations = $this->repository->findByLocale($locale);
foreach ($translations as $translation) {
Expand All @@ -77,6 +94,7 @@ public function reset(): void
);
}
}
$this->version = $version->get();
}

public function warmUp(string $cacheDir): array
Expand Down

0 comments on commit f9c8b01

Please sign in to comment.