8000 [11.x] Optimize `loadTranslationsFrom` function for simplicity and clarity by selcukcukur · Pull Request #54407 · laravel/framework · GitHub
[go: up one dir, main page]

Skip to content

[11.x] Optimize loadTranslationsFrom function for simplicity and clarity #54407

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Jan 30, 2025
12 changes: 6 additions & 6 deletions src/Illuminate/Support/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,17 +236,17 @@ protected function loadViewComponentsAs($prefix, array $components)
}

/**
* Register a translation file namespace.
* Register a translation file namespace or path.
*
* @param string $path
* @param string $namespace
* @param string|null $namespace
* @return void
*/
protected function loadTranslationsFrom($path, $namespace)
protected function loadTranslationsFrom($path, $namespace = null)
{
$this->callAfterResolving('translator', function ($translator) use ($path, $namespace) {
$translator->addNamespace($namespace, $path);
});
$this->callAfterResolving('translator', fn ($translator) => is_null($namespace)
? $translator->addPath($path)
: $translator->addNamespace($namespace, $path));
}

/**
Expand Down
10 changes: 10 additions & 0 deletions src/Illuminate/Translation/FileLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,16 @@ public function addJsonPath($path)
$this->jsonPaths[] = $path;
}

/**
* Get an array of all the registered paths to translation files.
*
* @return array
*/
public function paths()
{
return $this->paths;
}

/**
* Get an array of all the registered paths to JSON translation files.
*
Expand Down
38 changes: 38 additions & 0 deletions tests/Support/SupportServiceProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Config\Repository as Config;
use Illuminate\Foundation\Application;
use Illuminate\Support\ServiceProvider;
use Illuminate\Translation\Translator;
use Mockery as m;
use PHPUnit\Framework\TestCase;

Expand Down Expand Up @@ -160,6 +161,36 @ 10000 @ public function testPublishesMigrations()

$this->assertNotContains('source/tagged/five', ServiceProvider::publishableMigrationPaths());
}

public function testLoadTranslationsFromWithoutNamespace()
{
$translator = m::mock(Translator::class);
$translator->shouldReceive('addPath')->once()->with(__DIR__.'/translations');

$this->app->shouldReceive('afterResolving')->once()->with('translator', m::on(function ($callback) use ($translator) {
$callback($translator);

return true;
}));

$provider = new ServiceProviderForTestingOne($this->app);
$provider->loadTranslationsFrom(__DIR__.'/translations');
}

public function testLoadTranslationsFromWithNamespace()
{
$translator = m::mock(Translator::class);
$translator->shouldReceive('addNamespace')->once()->with('namespace', __DIR__.'/translations');

$this->app->shouldReceive('afterResolving')->once()->with('translator', m::on(function ($callback) use ($translator) {
$callback($translator);

return true;
}));

$provider = new ServiceProviderForTestingOne($this->app);
$provider->loadTranslationsFrom(__DIR__.'/translations', 'namespace');
}
}

class ServiceProviderForTestingOne extends ServiceProvider
Expand All @@ -179,6 +210,13 @@ public function boot()
$this->publishesMigrations(['source/tagged/three' => 'destination/tagged/three'], 'tag_three');
$this->publishesMigrations(['source/tagged/multiple_two' => 'destination/tagged/multiple_two'], ['tag_four', 'tag_five']);
}

public function loadTranslationsFrom($path, $namespace = null)
{
$this->callAfterResolving('translator', fn ($translator) => is_null($namespace)
? $translator->addPath($path)
: $translator->addNamespace($namespace, $path));
}
}

class ServiceProviderForTestingTwo extends ServiceProvider
Expand Down
73 changes: 73 additions & 0 deletions tests/Translation/TranslationFileLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,69 @@ protected function tearDown(): void
m::close();
}

public function testLoadMethodLoadsTranslationsFromAddedPath()
{
$files = m::mock(Filesystem::class);
$loader = new FileLoader($files, __DIR__);
$loader->addPath(__DIR__.'/another');

$files->shouldReceive('exists')->once()->with(__DIR__.'/en/messages.php')->andReturn(true);
$files->shouldReceive('getRequire')->once()->with(__DIR__.'/en/messages.php')->andReturn(['foo' => 'bar']);

$files->shouldReceive('exists')->once()->with(__DIR__.'/another/en/messages.php')->andReturn(true);
$files->shouldReceive('getRequire')->once()->with(__DIR__.'/another/en/messages.php')->andReturn(['baz' => 'backagesplash']);

$this->assertEquals(['foo' => 'bar', 'baz' => 'backagesplash'], $loader->load('en', 'messages'));
}

public function testLoadMethodHandlesMissingAddedPath()
{
$files = m::mock(Filesystem::class);
$loader = new FileLoader($files, __DIR__);
$loader->addPath(__DIR__.'/missing');

$files->shouldReceive('exists')->once()->with(__DIR__.'/en/messages.php')->andReturn(true);
$files->shouldReceive('getRequire')->once()->with(__DIR__.'/en/messages.php')->andReturn(['foo' => 'bar']);

$files->shouldReceive('exists')->once()->with(__DIR__.'/missing/en/messages.php')->andReturn(false);

$this->assertEquals(['foo' => 'bar'], $loader->load('en', 'messages'));
}

public function testLoadMethodOverwritesExistingKeysFromAddedPath()
{
$files = m::mock(Filesystem::class);
$loader = new FileLoader($files, __DIR__);
$loader->addPath(__DIR__.'/another');

$files->shouldReceive('exists')->once()->with(__DIR__.'/en/messages.php')->andReturn(true);
$files->shouldReceive('getRequire')->once()->with(__DIR__.'/en/messages.php')->andReturn(['foo' => 'bar']);

$files->shouldReceive('exists')->once()->with(__DIR__.'/another/en/messages.php')->andReturn(true);
$files->shouldReceive('getRequire')->once()->with(__DIR__.'/another/en/messages.php')->andReturn(['foo' => 'baz']);

$this->assertEquals(['foo' => 'baz'], $loader->load('en', 'messages'));
}

public function testLoadMethodLoadsTranslationsFromMultipleAddedPaths()
{
$files = m::mock(Filesystem::class);
$loader = new FileLoader($files, __DIR__);
$loader->addPath(__DIR__.'/another');
$loader->addPath(__DIR__.'/yet-another');

$files->shouldReceive('exists')->once()->with(__DIR__.'/en/messages.php')->andReturn(true);
$files->shouldReceive('getRequire')->once()->with(__DIR__.'/en/messages.php')->andReturn(['foo' => 'bar']);

$files->shouldReceive('exists')->once()->with(__DIR__.'/another/en/messages.php')->andReturn(true);
$files->shouldReceive('getRequire')->once()->with(__DIR__.'/another/en/messages.php')->andReturn(['baz' => 'backagesplash']);

$files->shouldReceive('exists')->once()->with(__DIR__.'/yet-another/en/messages.php')->andReturn(true);
$files->shouldReceive('getRequire')->once()->with(__DIR__.'/yet-another/en/messages.php')->andReturn(['qux' => 'quux']);

$this->assertEquals(['foo' => 'bar', 'baz' => 'backagesplash', 'qux' => 'quux'], $loader->load('en', 'messages'));
}

public function testLoadMethodWithoutNamespacesProperlyCallsLoader()
{
$loader = new FileLoader($files = m::mock(Filesystem::class), __DIR__);
Expand Down Expand Up @@ -152,4 +215,14 @@ public function testAllAddedJsonPathsReturnProperly()
$loader->addJsonPath($path2);
$this->assertEquals([$path1, $path2], $loader->jsonPaths());
}

public function testAllAddedPathsReturnProperly()
{
$loader = new FileLoader(m::mock(Filesystem::class), __DIR__);
$path1 = __DIR__.'/another';
$path2 = __DIR__.'/another2';
$loader->addPath($path1);
$loader->addPath($path2);
$this->assertEquals([$path1, $path2], array_slice($loader->paths(), 1));
}
}
0