Skip to content

Commit

Permalink
Added work with route groups
Browse files Browse the repository at this point in the history
  • Loading branch information
andrey-helldar committed Jun 17, 2024
1 parent e3563e5 commit 9986251
Show file tree
Hide file tree
Showing 11 changed files with 160 additions and 22 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@
"dragon-code/support": "^6.13",
"illuminate/config": "^10.0 || ^11.0",
"illuminate/http": "^10.0 || ^11.0",
"illuminate/routing": "^10.0 || ^11.0",
"illuminate/support": "^10.0 || ^11.0",
"laravel-lang/config": "^1.4.2",
"laravel-lang/config": "^1.5",
"laravel-lang/locales": "^2.8"
},
"require-dev": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use LaravelLang\Config\Data\Shared\RouteNameData;
use LaravelLang\Routes\Helpers\Name;

trait KeyNames
trait RouteParameters
{
public function names(): RouteNameData
{
Expand Down
20 changes: 20 additions & 0 deletions src/Helpers/Route.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace LaravelLang\Routes\Helpers;

use LaravelLang\Config\Facades\Config;

class Route
{
public static function prefix(): string
{
return Config::shared()->routes->namePrefix;
}

public static function redirect(): bool
{
return Config::shared()->routes->redirect;
}
}
34 changes: 34 additions & 0 deletions src/Middlewares/LocalizationByParameterPrefix.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace LaravelLang\Routes\Middlewares;

use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use LaravelLang\Routes\Helpers\Route;

class LocalizationByParameterPrefix extends LocalizationByParameterWithRedirect
{
protected function parameters(Request $request): array
{
return Arr::except($request->route()?->parameters() ?? [], [
$this->names()->parameter,
]);
}

protected function routeName(Request $request): ?string
{
if ($name = $request->route()?->getName()) {
return Str::after($name, $this->routePrefix());
}

return null;
}

protected function routePrefix(): string
{
return Route::prefix();
}
}
4 changes: 2 additions & 2 deletions src/Middlewares/LocalizationByParameterWithRedirect.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use LaravelLang\Locales\Facades\Locales;
use LaravelLang\Routes\Concerns\KeyNames;
use LaravelLang\Routes\Concerns\RouteParameters;

use function array_merge;
use function in_array;
use function response;

class LocalizationByParameterWithRedirect extends Middleware
{
use KeyNames;
use RouteParameters;

public function __invoke(Request $request, Closure $next)
{
Expand Down
6 changes: 5 additions & 1 deletion src/Middlewares/LocalizationBySession.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ class LocalizationBySession extends Middleware
{
protected function detect(Request $request): bool|float|int|string|null
{
return $request->getSession()->get($this->names()->session);
if ($request->hasSession()) {
return $request->getSession()->get($this->names()->session);
}

return null;
}
}
15 changes: 12 additions & 3 deletions src/Middlewares/Middleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@
use Illuminate\Http\Request;
use LaravelLang\Locales\Data\LocaleData;
use LaravelLang\Locales\Facades\Locales;
use LaravelLang\Routes\Concerns\KeyNames;
use LaravelLang\Routes\Concerns\RouteParameters;
use LaravelLang\Routes\Events\LocaleHasBeenSetEvent;

use function app;
use function trim;

abstract class Middleware
{
use KeyNames;
use RouteParameters;

abstract protected function detect(Request $request): bool|float|int|string|null;

Expand All @@ -27,7 +27,7 @@ public function __invoke(Request $request, Closure $next)
$this->event($locale);
}

return $next($request);
return $next($this->forgetParameter($request));
}

protected function getLocale(Request $request): ?LocaleData
Expand All @@ -53,4 +53,13 @@ protected function trim(bool|float|int|string|null $locale): string
{
return trim((string) $locale);
}

protected function forgetParameter(Request $request): Request
{
$request->route()->forgetParameter(
$this->names()->parameter
);

return $request;
}
}
23 changes: 13 additions & 10 deletions src/Services/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,32 @@

use Closure;
use Illuminate\Support\Facades\Route as BaseRoute;
use LaravelLang\Routes\Concerns\KeyNames;
use LaravelLang\Routes\Concerns\RouteParameters;
use LaravelLang\Routes\Helpers\Route as RouteName;
use LaravelLang\Routes\Middlewares\LocalizationByCookie;
use LaravelLang\Routes\Middlewares\LocalizationByHeader;
use LaravelLang\Routes\Middlewares\LocalizationByParameter;
use LaravelLang\Routes\Middlewares\LocalizationByParameterPrefix;
use LaravelLang\Routes\Middlewares\LocalizationBySession;

use function sprintf;

class Route
{
use KeyNames;
use RouteParameters;

public function group(Closure $callback): void
{
BaseRoute::prefix(sprintf('{%s}', $this->names()->parameter))
->name($this->names()->parameter . '.')
->middleware(LocalizationByParameter::class)
->group($callback);

BaseRoute::middleware([
LocalizationByCookie::class,
LocalizationByHeader::class,
LocalizationBySession::class,
])->group($callback);

BaseRoute::prefix('{' . $this->names()->parameter . '}')
->name(RouteName::prefix())
->middleware([
LocalizationByParameterPrefix::class,
LocalizationByCookie::class,
LocalizationByHeader::class,
LocalizationBySession::class,
])->group($callback);
}
}
2 changes: 1 addition & 1 deletion tests/Concerns/Routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public function setUpRoutes(): void
LocalizationRoute::group(function () {
app('router')
->middleware('web')
->get('grouped', $this->jsonResponse())
->get('group/{foo}', $this->jsonResponse())
->name('via.group');
});
}
Expand Down
15 changes: 12 additions & 3 deletions tests/Feature/Facades/RouteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,18 @@

declare(strict_types=1);

use LaravelLang\Routes\Helpers\Name;
use LaravelLang\Routes\Helpers\Route as RouteName;
use Tests\Constants\LocaleValue;

test('group', function () {
expect(app('router')->has('via.group'))->toBeTrue();
expect(app('router')->has(Name::parameter() . '.via.group'))->toBeTrue();
$foo = 'test';

expect(route('via.group', compact('foo')))
->toBeString()
->toBe('http://localhost/group/test');

expect(route(RouteName::prefix() . 'via.group', [
'locale' => LocaleValue::LocaleMain,
'foo' => $foo,
]))->toBeString()->toBe('http://localhost/fr/group/test');
});
58 changes: 58 additions & 0 deletions tests/Feature/Middlewares/ParameterPrefixTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

declare(strict_types=1);

use LaravelLang\Routes\Helpers\Route as RouteName;
use Tests\Constants\LocaleValue;

use function Pest\Laravel\getJson;

test('main without prefix', function () {
$foo = 'test';

getJson(route('via.group', compact('foo')))
->assertSuccessful()
->assertJsonPath($foo, LocaleValue::TranslationFrench);
});

test('main locale', function (string $locale) {
$foo = 'test';

getJson(route(RouteName::prefix() . 'via.group', compact('foo', 'locale')))
->assertSuccessful()
->assertJsonPath($foo, LocaleValue::TranslationFrench);

assertEventDispatched();
})->with('main-locales');

test('aliased locale', function (string $locale) {
$foo = 'test';

getJson(route(RouteName::prefix() . 'via.group', compact('foo', 'locale')))
->assertSuccessful()
->assertJsonPath($foo, LocaleValue::TranslationGerman);

assertEventDispatched();
})->with('aliased-locales');

test('uninstalled locale', function (string $locale) {
$foo = 'test';

getJson(route(RouteName::prefix() . 'via.group', compact('foo', 'locale')))
->assertRedirectToRoute('via.group', [
'foo' => $foo,
]);

assertEventNotDispatched();
})->with('uninstalled-locales');

test('unknown locale', function (int|string $locale) {
$foo = 'test';

getJson(route(RouteName::prefix() . 'via.group', compact('foo', 'locale')))
->assertRedirectToRoute('via.group', [
'foo' => $foo,
]);

assertEventNotDispatched();
})->with('unknown-locales');

0 comments on commit 9986251

Please sign in to comment.