Remove redundant YAML parsing

This commit is contained in:
Pig Fang 2019-09-07 21:23:38 +08:00
parent efd6f44aea
commit 2b0eb3101c
4 changed files with 9 additions and 24 deletions

View File

@ -13,16 +13,12 @@ class JavaScript
/** @var Repository */ /** @var Repository */
protected $cache; protected $cache;
/** @var Yaml */
protected $yaml;
protected $prefix = 'front-end-trans-'; 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->filesystem = $filesystem;
$this->cache = $cache; $this->cache = $cache;
$this->yaml = $yaml;
} }
public function generate(string $locale): string public function generate(string $locale): string
@ -33,7 +29,7 @@ class JavaScript
$compiledModified = intval($this->cache->get($this->prefix.$locale, 0)); $compiledModified = intval($this->cache->get($this->prefix.$locale, 0));
if ($sourceModified > $compiledModified || ! $this->filesystem->exists($compiled)) { 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->filesystem->put($compiled, $content);
$this->cache->put($this->prefix.$locale, $sourceModified); $this->cache->put($this->prefix.$locale, $sourceModified);

View File

@ -4,9 +4,8 @@ namespace App\Services\Translations;
use Illuminate\Contracts\Cache\Repository; use Illuminate\Contracts\Cache\Repository;
use Symfony\Component\Yaml\Yaml as YamlParser; use Symfony\Component\Yaml\Yaml as YamlParser;
use Spatie\TranslationLoader\TranslationLoaders\TranslationLoader;
class Yaml implements TranslationLoader class Yaml
{ {
/** @var Repository */ /** @var Repository */
protected $cache; protected $cache;
@ -16,13 +15,6 @@ class Yaml implements TranslationLoader
$this->cache = $cache; $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 public function loadYaml(string $path): array
{ {
$key = 'yaml-trans-'.md5($path).'-'.filemtime($path); $key = 'yaml-trans-'.md5($path).'-'.filemtime($path);

View File

@ -8,7 +8,6 @@ return [
*/ */
'translation_loaders' => [ 'translation_loaders' => [
Spatie\TranslationLoader\TranslationLoaders\Db::class, Spatie\TranslationLoader\TranslationLoaders\Db::class,
App\Services\Translations\Yaml::class,
], ],
/* /*

View File

@ -2,8 +2,8 @@
namespace Tests; namespace Tests;
use Illuminate\Support\Str;
use Illuminate\Cache\Repository; use Illuminate\Cache\Repository;
use App\Services\Translations\Yaml;
use Illuminate\Filesystem\Filesystem; use Illuminate\Filesystem\Filesystem;
use App\Services\Translations\JavaScript; use App\Services\Translations\JavaScript;
@ -23,7 +23,11 @@ class JavaScriptTest extends TestCase
->once() ->once()
->andReturn(1); ->andReturn(1);
$mock->shouldReceive('put') $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() ->once()
->andReturn(1); ->andReturn(1);
}); });
@ -36,12 +40,6 @@ class JavaScriptTest extends TestCase
->with('front-end-trans-en', 1) ->with('front-end-trans-en', 1)
->once(); ->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')); $this->assertEquals(url('lang/en.js?t=1'), resolve(JavaScript::class)->generate('en'));
} }