Skip to content

Commit ad3c9bd

Browse files
committed
add cache flag if translations needs to be reloaded
1 parent 1cbf7c8 commit ad3c9bd

File tree

10 files changed

+116
-28
lines changed

10 files changed

+116
-28
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"translation"
1313
],
1414
"require": {
15-
"php": "~7.4 | ^8.0",
15+
"php": "^8.2",
1616
"doctrine/orm": "~2.5",
1717
"symfony/framework-bundle": "*",
1818
"symfony/translation-contracts": "*",

src/CacheFlag.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Arxy\TranslationsBundle;
6+
7+
use Psr\Cache\CacheItemInterface;
8+
use Psr\Cache\CacheItemPoolInterface;
9+
10+
final readonly class CacheFlag
11+
{
12+
public function __construct(
13+
private CacheItemPoolInterface $cache,
14+
private string $key = 'translations'
15+
) {
16+
}
17+
18+
public function getVersion(): CacheItemInterface
19+
{
20+
$item = $this->cache->getItem($this->key);
21+
if (!$item->isHit()) {
22+
$item->set(0);
23+
}
24+
25+
return $item;
26+
}
27+
28+
public function increment(CacheItemInterface $item): void
29+
{
30+
$item->set($item->get() + 1);
31+
$this->cache->save($item);
32+
}
33+
}

src/DependencyInjection/ArxyTranslationsExtension.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44

55
namespace Arxy\TranslationsBundle\DependencyInjection;
66

7+
use Arxy\TranslationsBundle\CacheFlag;
78
use Arxy\TranslationsBundle\Repository;
89
use Symfony\Component\Config\FileLocator;
910
use Symfony\Component\DependencyInjection\ContainerBuilder;
1011
use Symfony\Component\DependencyInjection\Loader;
12+
use Symfony\Component\DependencyInjection\Reference;
1113
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
1214

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

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

3133
$container->setAlias(Repository::class, $config['repository']);
34+
$container->getDefinition(CacheFlag::class)->setArgument('$cache', new Reference($config['cache_flag']));
3235
}
3336
}

src/DependencyInjection/CompilerPass/OverrideTranslatorPass.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44

55
namespace Arxy\TranslationsBundle\DependencyInjection\CompilerPass;
66

7+
use Arxy\TranslationsBundle\CacheFlag;
78
use Arxy\TranslationsBundle\Repository;
89
use Arxy\TranslationsBundle\Translator;
910
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
1011
use Symfony\Component\DependencyInjection\ContainerBuilder;
1112
use Symfony\Component\DependencyInjection\Reference;
1213

13-
class OverrideTranslatorPass implements CompilerPassInterface
14+
final readonly class OverrideTranslatorPass implements CompilerPassInterface
1415
{
1516
/**
1617
* {@inheritdoc}
@@ -20,6 +21,7 @@ public function process(ContainerBuilder $container): void
2021
$container->getDefinition('translator.default')
2122
->setClass(Translator::class)
2223
->addTag('kernel.reset', ['method' => 'reset'])
23-
->addMethodCall('setRepository', [new Reference(Repository::class)]);
24+
->addMethodCall('setRepository', [new Reference(Repository::class)])
25+
->addMethodCall('setCacheFlag', [new Reference(CacheFlag::class)]);
2426
}
2527
}

src/DependencyInjection/Configuration.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
*
1515
* To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html#cookbook-bundles-extension-config-class}
1616
*/
17-
class Configuration implements ConfigurationInterface
17+
final readonly class Configuration implements ConfigurationInterface
1818
{
1919
/**
2020
* {@inheritDoc}
@@ -37,6 +37,7 @@ public function getConfigTreeBuilder()
3737
*/
3838
$root->children()
3939
->scalarNode('repository')->isRequired()->cannotBeEmpty()->end()
40+
->scalarNode('cache_flag')->isRequired()->cannotBeEmpty()->end()
4041
->end();
4142
// @formatter:on
4243

src/Dumper/DatabaseDumper.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,11 @@
1111
/**
1212
* @internal
1313
*/
14-
final class DatabaseDumper implements DumperInterface
14+
final readonly class DatabaseDumper implements DumperInterface
1515
{
16-
private Repository $repository;
17-
18-
public function __construct(Repository $repository)
19-
{
20-
$this->repository = $repository;
16+
public function __construct(
17+
private Repository $repository
18+
) {
2119
}
2220

2321
public function dump(MessageCatalogue $messages, $options = []): void

src/Model/TranslationModel.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
/**
88
* Lightweight DTO to avoid heavy hydrating of ORM
99
*/
10-
class TranslationModel implements Translation
10+
final readonly class TranslationModel implements Translation
1111
{
1212
private string $translation;
1313
private string $token;

src/Resources/config/services.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/*
5+
* Copyright (C) 2016-2024 Taylor & Hart Limited
6+
* All Rights Reserved.
7+
*
8+
* NOTICE: All information contained herein is, and remains the property
9+
* of Taylor & Hart Limited and its suppliers, if any.
10+
*
11+
* All intellectual and technical concepts contained herein are
12+
* proprietary to Taylor & Hart Limited and its suppliers and may be
13+
* covered by U.K. and foreign patents, patents in process, and are
14+
* protected in full by copyright law. Dissemination of this information
15+
* or reproduction of this material is strictly forbidden unless prior
16+
* written permission is obtained from Taylor & Hart Limited.
17+
*
18+
* ANY REPRODUCTION, MODIFICATION, DISTRIBUTION, PUBLIC PERFORMANCE, OR
19+
* PUBLIC DISPLAY OF OR THROUGH USE OF THIS SOURCE CODE WITHOUT THE
20+
* EXPRESS WRITTEN CONSENT OF RARE PINK LIMITED IS STRICTLY PROHIBITED,
21+
* AND IN VIOLATION OF APPLICABLE LAWS. THE RECEIPT OR POSSESSION OF
22+
* THIS SOURCE CODE AND/OR RELATED INFORMATION DOES NOT CONVEY OR IMPLY
23+
* ANY RIGHTS TO REPRODUCE, DISCLOSE OR DISTRIBUTE ITS CONTENTS, OR TO
24+
* MANUFACTURE, USE, OR SELL ANYTHING THAT IT MAY DESCRIBE, IN WHOLE OR
25+
* IN PART.
26+
*/
27+
28+
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
29+
30+
use Arxy\TranslationsBundle\CacheFlag;
31+
use Arxy\TranslationsBundle\Dumper\DatabaseDumper;
32+
use Arxy\TranslationsBundle\Repository;
33+
34+
return static function (ContainerConfigurator $container) {
35+
$services = $container->services()
36+
->defaults()
37+
->autoconfigure()
38+
->autowire();
39+
40+
$services->set(DatabaseDumper::class)->args([
41+
service(Repository::class),
42+
])
43+
->tag('translation.dumper', ['alias' => 'db']);
44+
45+
$services->set(CacheFlag::class);
46+
};

src/Resources/config/services.xml

Lines changed: 0 additions & 13 deletions
This file was deleted.

src/Translator.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@
1313
*/
1414
class Translator extends OriginalTranslator implements ResetInterface
1515
{
16-
private Repository $repository;
16+
private Repository|null $repository = null;
17+
18+
private CacheFlag|null $cacheFlag = null;
19+
20+
private int $version = 0;
1721
private bool $warmUp = false;
1822

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

31+
/**
32+
* @required
33+
*/
34+
public function setCacheFlag(CacheFlag $cacheFlag): void
35+
{
36+
$this->cacheFlag = $cacheFlag;
37+
}
38+
2739
protected function loadCatalogue(string $locale): void
2840
{
2941
parent::loadCatalogue($locale);
@@ -38,7 +50,7 @@ protected function loadCatalogue(string $locale): void
3850

3951
private function fetchTranslations(string $locale): void
4052
{
41-
$translations = $this->repository->findByLocale($locale);
53+
$translations = $this->repository?->findByLocale($locale) ?? [];
4254
$catalogue = $this->catalogues[$locale];
4355
foreach ($translations as $translation) {
4456
$catalogue->set(
@@ -67,6 +79,11 @@ private function loadFallbackTranslations(MessageCatalogueInterface $catalogue):
6779

6880
public function reset(): void
6981
{
82+
$version = $this->cacheFlag?->getVersion();
83+
if ($version?->get() === $this->version) {
84+
return;
85+
}
86+
7087
foreach ($this->catalogues as $locale => $catalogue) {
7188
$translations = $this->repository->findByLocale($locale);
7289
foreach ($translations as $translation) {
@@ -77,6 +94,7 @@ public function reset(): void
7794
);
7895
}
7996
}
97+
$this->version = $version->get();
8098
}
8199

82100
public function warmUp(string $cacheDir): array

0 commit comments

Comments
 (0)