Skip to content

Commit

Permalink
Merge pull request #12 from Laravel-Lang/1.x
Browse files Browse the repository at this point in the history
Adding route groups with a locale prefix
  • Loading branch information
andrey-helldar authored Jun 17, 2024
2 parents de22d91 + 9986251 commit 5496696
Show file tree
Hide file tree
Showing 17 changed files with 203 additions and 6 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/Facades/LocalizationRoute.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace LaravelLang\Routes\Facades;

use Closure;
use Illuminate\Support\Facades\Facade;
use LaravelLang\Routes\Services\Route;

/**
* @method static void group(Closure $callback)
*/
class LocalizationRoute extends Facade
{
protected static function getFacadeAccessor(): string
{
return Route::class;
}
}
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
4 changes: 2 additions & 2 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 Down
37 changes: 37 additions & 0 deletions src/Services/Route.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace LaravelLang\Routes\Services;

use Closure;
use Illuminate\Support\Facades\Route as BaseRoute;
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\LocalizationByParameterPrefix;
use LaravelLang\Routes\Middlewares\LocalizationBySession;

class Route
{
use RouteParameters;

public function group(Closure $callback): void
{
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);
}
}
8 changes: 8 additions & 0 deletions tests/Concerns/Routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Tests\Concerns;

use Closure;
use LaravelLang\Routes\Facades\LocalizationRoute;
use LaravelLang\Routes\Middlewares\LocalizationByCookie;
use LaravelLang\Routes\Middlewares\LocalizationByHeader;
use LaravelLang\Routes\Middlewares\LocalizationByParameter;
Expand Down Expand Up @@ -63,6 +64,13 @@ public function setUpRoutes(): void
->get('clean/{foo}', $this->jsonResponse())
->name('clean');
});

LocalizationRoute::group(function () {
app('router')
->middleware('web')
->get('group/{foo}', $this->jsonResponse())
->name('via.group');
});
}

protected function jsonResponse(): Closure
Expand Down
19 changes: 19 additions & 0 deletions tests/Feature/Facades/RouteTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

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

test('group', function () {
$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');
});
File renamed without changes.
File renamed without changes.
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');
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 5496696

Please sign in to comment.