From 13ce2d1e3d024d9decdf9c7ae7df3875d6d6362a Mon Sep 17 00:00:00 2001 From: Toby Zerner Date: Fri, 7 Dec 2018 09:38:08 +1030 Subject: [PATCH] Performance: Actually make use of the translator cache We had added a `storage/locale` directory to our skeleton, but we had forgotten to hook it up with the translator. Enabling caching saves parsing that locale YAML files on every pageload which should be good for performance. The locale cache will be cleared whenever an extension that uses the `Locales` or `LanguagePack` extenders is enabled/disabled. If debug mode is ON, then the caching mechanism will automatically check if any of the loaded YAML files are dirty and update accordingly. --- framework/core/src/Extend/LanguagePack.php | 12 ++++++++- framework/core/src/Extend/Locales.php | 12 ++++++++- framework/core/src/Locale/LocaleManager.php | 15 ++++++++++- .../core/src/Locale/LocaleServiceProvider.php | 27 ++++++++++++++++--- 4 files changed, 60 insertions(+), 6 deletions(-) diff --git a/framework/core/src/Extend/LanguagePack.php b/framework/core/src/Extend/LanguagePack.php index e579bfaee..d51c228e7 100644 --- a/framework/core/src/Extend/LanguagePack.php +++ b/framework/core/src/Extend/LanguagePack.php @@ -18,7 +18,7 @@ use Illuminate\Contracts\Container\Container; use InvalidArgumentException; use RuntimeException; -class LanguagePack implements ExtenderInterface +class LanguagePack implements ExtenderInterface, LifecycleInterface { public function extend(Container $container, Extension $extension = null) { @@ -63,4 +63,14 @@ class LanguagePack implements ExtenderInterface } } } + + public function onEnable(Container $container, Extension $extension) + { + $container->make('flarum.locales')->clearCache(); + } + + public function onDisable(Container $container, Extension $extension) + { + $container->make('flarum.locales')->clearCache(); + } } diff --git a/framework/core/src/Extend/Locales.php b/framework/core/src/Extend/Locales.php index 02a3d52a9..fab28553a 100644 --- a/framework/core/src/Extend/Locales.php +++ b/framework/core/src/Extend/Locales.php @@ -16,7 +16,7 @@ use Flarum\Extension\Extension; use Flarum\Locale\LocaleManager; use Illuminate\Contracts\Container\Container; -class Locales implements ExtenderInterface +class Locales implements ExtenderInterface, LifecycleInterface { protected $directory; @@ -46,4 +46,14 @@ class Locales implements ExtenderInterface ); } } + + public function onEnable(Container $container, Extension $extension) + { + $container->make('flarum.locales')->clearCache(); + } + + public function onDisable(Container $container, Extension $extension) + { + $container->make('flarum.locales')->clearCache(); + } } diff --git a/framework/core/src/Locale/LocaleManager.php b/framework/core/src/Locale/LocaleManager.php index 5fdd4d492..d07b51f73 100644 --- a/framework/core/src/Locale/LocaleManager.php +++ b/framework/core/src/Locale/LocaleManager.php @@ -24,9 +24,15 @@ class LocaleManager protected $css = []; - public function __construct(Translator $translator) + /** + * @var string + */ + protected $cacheDir; + + public function __construct(Translator $translator, string $cacheDir = null) { $this->translator = $translator; + $this->cacheDir = $cacheDir; } public function getLocale(): string @@ -106,4 +112,11 @@ class LocaleManager { $this->translator = $translator; } + + public function clearCache() + { + if ($this->cacheDir) { + array_map('unlink', glob($this->cacheDir.'/*')); + } + } } diff --git a/framework/core/src/Locale/LocaleServiceProvider.php b/framework/core/src/Locale/LocaleServiceProvider.php index b9cf94b68..87150f848 100644 --- a/framework/core/src/Locale/LocaleServiceProvider.php +++ b/framework/core/src/Locale/LocaleServiceProvider.php @@ -13,10 +13,10 @@ namespace Flarum\Locale; use Flarum\Event\ConfigureLocales; use Flarum\Foundation\AbstractServiceProvider; +use Flarum\Foundation\Event\ClearingCache; use Flarum\Settings\SettingsRepositoryInterface; use Illuminate\Contracts\Events\Dispatcher; use Illuminate\Contracts\Translation\Translator as TranslatorContract; -use Symfony\Component\Translation\MessageSelector; use Symfony\Component\Translation\TranslatorInterface; class LocaleServiceProvider extends AbstractServiceProvider @@ -31,6 +31,10 @@ class LocaleServiceProvider extends AbstractServiceProvider $locales->addLocale($this->getDefaultLocale(), 'Default'); $events->dispatch(new ConfigureLocales($locales)); + + $events->listen(ClearingCache::class, function () use ($locales) { + $locales->clearCache(); + }); } /** @@ -38,11 +42,23 @@ class LocaleServiceProvider extends AbstractServiceProvider */ public function register() { - $this->app->singleton(LocaleManager::class); + $this->app->singleton(LocaleManager::class, function () { + return new LocaleManager( + $this->app->make('translator'), + $this->getCacheDir() + ); + }); + $this->app->alias(LocaleManager::class, 'flarum.locales'); $this->app->singleton('translator', function () { - $translator = new Translator($this->getDefaultLocale(), new MessageSelector()); + $translator = new Translator( + $this->getDefaultLocale(), + null, + $this->getCacheDir(), + $this->app->inDebugMode() + ); + $translator->setFallbackLocales(['en']); $translator->addLoader('prefixed_yaml', new PrefixedYamlFileLoader()); @@ -59,4 +75,9 @@ class LocaleServiceProvider extends AbstractServiceProvider return $repo->get('default_locale', 'en'); } + + private function getCacheDir(): string + { + return $this->app->storagePath().'/locale'; + } }