From 2b0eb3101c04985ae29d52510772fc0a2b8bc7e6 Mon Sep 17 00:00:00 2001 From: Pig Fang Date: Sat, 7 Sep 2019 21:23:38 +0800 Subject: [PATCH] Remove redundant YAML parsing --- app/Services/Translations/JavaScript.php | 8 ++------ app/Services/Translations/Yaml.php | 10 +--------- config/translation-loader.php | 1 - .../TranslationsTest/JavaScriptTest.php | 14 ++++++-------- 4 files changed, 9 insertions(+), 24 deletions(-) diff --git a/app/Services/Translations/JavaScript.php b/app/Services/Translations/JavaScript.php index ef52204f..ea8c9dc5 100644 --- a/app/Services/Translations/JavaScript.php +++ b/app/Services/Translations/JavaScript.php @@ -13,16 +13,12 @@ class JavaScript /** @var Repository */ protected $cache; - /** @var Yaml */ - protected $yaml; - protected $prefix = 'front-end-trans-'; - public function __construct(Filesystem $filesystem, Repository $cache, Yaml $yaml) + public function __construct(Filesystem $filesystem, Repository $cache) { $this->filesystem = $filesystem; $this->cache = $cache; - $this->yaml = $yaml; } public function generate(string $locale): string @@ -33,7 +29,7 @@ class JavaScript $compiledModified = intval($this->cache->get($this->prefix.$locale, 0)); if ($sourceModified > $compiledModified || ! $this->filesystem->exists($compiled)) { - $content = 'blessing.i18n='.json_encode($this->yaml->loadYaml($source), JSON_UNESCAPED_UNICODE); + $content = 'blessing.i18n = '.json_encode(trans('front-end'), JSON_UNESCAPED_UNICODE); $this->filesystem->put($compiled, $content); $this->cache->put($this->prefix.$locale, $sourceModified); diff --git a/app/Services/Translations/Yaml.php b/app/Services/Translations/Yaml.php index 47b23b84..7648b410 100644 --- a/app/Services/Translations/Yaml.php +++ b/app/Services/Translations/Yaml.php @@ -4,9 +4,8 @@ namespace App\Services\Translations; use Illuminate\Contracts\Cache\Repository; use Symfony\Component\Yaml\Yaml as YamlParser; -use Spatie\TranslationLoader\TranslationLoaders\TranslationLoader; -class Yaml implements TranslationLoader +class Yaml { /** @var Repository */ protected $cache; @@ -16,13 +15,6 @@ class Yaml implements TranslationLoader $this->cache = $cache; } - public function loadTranslations(string $locale, string $group): array - { - $path = resource_path("lang/$locale/$group.yml"); - - return file_exists($path) ? $this->loadYaml($path) : []; - } - public function loadYaml(string $path): array { $key = 'yaml-trans-'.md5($path).'-'.filemtime($path); diff --git a/config/translation-loader.php b/config/translation-loader.php index 09b975ba..50f5b9ba 100644 --- a/config/translation-loader.php +++ b/config/translation-loader.php @@ -8,7 +8,6 @@ return [ */ 'translation_loaders' => [ Spatie\TranslationLoader\TranslationLoaders\Db::class, - App\Services\Translations\Yaml::class, ], /* diff --git a/tests/ServicesTest/TranslationsTest/JavaScriptTest.php b/tests/ServicesTest/TranslationsTest/JavaScriptTest.php index ebcd1f4e..5267169c 100644 --- a/tests/ServicesTest/TranslationsTest/JavaScriptTest.php +++ b/tests/ServicesTest/TranslationsTest/JavaScriptTest.php @@ -2,8 +2,8 @@ namespace Tests; +use Illuminate\Support\Str; use Illuminate\Cache\Repository; -use App\Services\Translations\Yaml; use Illuminate\Filesystem\Filesystem; use App\Services\Translations\JavaScript; @@ -23,7 +23,11 @@ class JavaScriptTest extends TestCase ->once() ->andReturn(1); $mock->shouldReceive('put') - ->with(public_path('lang/en.js'), 'blessing.i18n={"a":"b"}') + ->withArgs(function ($path, $content) { + $this->assertEquals(public_path('lang/en.js'), $path); + $this->assertTrue(Str::startsWith($content, 'blessing.i18n')); + return true; + }) ->once() ->andReturn(1); }); @@ -36,12 +40,6 @@ class JavaScriptTest extends TestCase ->with('front-end-trans-en', 1) ->once(); }); - $this->mock(Yaml::class, function ($mock) { - $mock->shouldReceive('loadYaml') - ->with(resource_path('lang/en/front-end.yml')) - ->once() - ->andReturn(['a' => 'b']); - }); $this->assertEquals(url('lang/en.js?t=1'), resolve(JavaScript::class)->generate('en')); }