Skip to content

Commit

Permalink
Merge pull request #19 from Laravel-Lang/1.x
Browse files Browse the repository at this point in the history
Added automatic forwarding of the localization parameter when calling group methods
  • Loading branch information
andrey-helldar authored Jul 6, 2024
2 parents a2c1941 + 6cbe0a7 commit d7fcbe8
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,43 @@
namespace LaravelLang\Routes;

use Closure;
use Illuminate\Foundation\Application;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\ServiceProvider as BaseServiceProvider;
use LaravelLang\Routes\Facades\LocalizationRoute;
use LaravelLang\Routes\Services\UrlGenerator;

class ServiceProvider extends BaseServiceProvider
{
public function boot(): void
{
$this->registerGroup();
$this->urlGenerator();
}

protected function registerGroup(): void
{
Route::macro('localizedGroup', fn (Closure $callback) => LocalizationRoute::group($callback));
}

protected function urlGenerator(): void
{
$this->app->singleton('url', function ($app) {
$routes = $app['router']->getRoutes();

$app->instance('routes', $routes);

return new UrlGenerator(
$routes,
$app->rebinding('request', $this->requestRebinder()),
$app['config']['app.asset_url']
);
});
}

protected function requestRebinder(): Closure
{
return fn (Application $app, Request $request) => $app['url']->setRequest($request);
}
}
47 changes: 47 additions & 0 deletions src/Services/UrlGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

namespace LaravelLang\Routes\Services;

use Illuminate\Routing\UrlGenerator as BaseGenerator;
use Illuminate\Support\Str;
use LaravelLang\Config\Facades\Config;

use function app;
use function array_merge;
use function is_null;

class UrlGenerator extends BaseGenerator
{
public function route($name, $parameters = [], $absolute = true): string
{
if ($this->isLocalizedGroupRoute($name) && ! is_null($route = $this->routes->getByName($name))) {
return $this->toRoute($route, $this->resolveLocalizedParameters($parameters), $absolute);
}

return parent::route($name, $parameters, $absolute);
}

protected function isLocalizedGroupRoute(string $name): bool
{
return Str::startsWith($name, $this->localizedRoutePrefix());
}

protected function resolveLocalizedParameters(array $parameters): array
{
return array_merge([
$this->localizeRouteParameter() => app()->getLocale(),
], $parameters);
}

protected function localizedRoutePrefix(): string
{
return Config::shared()->routes->namePrefix;
}

protected function localizeRouteParameter(): string
{
return Config::shared()->routes->names->parameter;
}
}
26 changes: 26 additions & 0 deletions tests/Unit/Services/UrlGeneratorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

use LaravelLang\Config\Facades\Config;
use Tests\Constants\LocaleValue;

test('localized route generation', function () {
$name = Config::shared()->routes->namePrefix;
$locale = LocaleValue::LocaleMain;
$fallback = LocaleValue::LocaleAliasParent;

expect(route($name . 'via.group.facade', ['foo' => 'bar']))
->toEndWith("localhost/$locale/group/facade/bar");

expect(route($name . 'via.group.macro', ['foo' => 'bar', 'locale' => $fallback]))
->toEndWith("localhost/$fallback/group/macro/bar");
});

test('non-localized route generation', function () {
expect(route('via.group.facade', ['foo' => 'bar']))
->toEndWith('localhost/group/facade/bar');

expect(route('via.group.macro', ['foo' => 'bar']))
->toEndWith('localhost/group/macro/bar');
});

0 comments on commit d7fcbe8

Please sign in to comment.