diff --git a/src/Events/LocaleHasBeenSetEvent.php b/src/Events/LocaleHasBeenSetEvent.php new file mode 100644 index 0000000..0fd123b --- /dev/null +++ b/src/Events/LocaleHasBeenSetEvent.php @@ -0,0 +1,20 @@ +route()?->hasParameter($this->names()->parameter) + return ! $this->hasParameter($request) || ! $this->trim($this->detect($request)) || ! $this->isInstalled($this->detect($request)); } + protected function hasParameter(Request $request): bool + { + return (bool) $request->route()?->hasParameter($this->names()->parameter); + } + protected function parameters(Request $request): array { return array_merge($request->route()?->parameters() ?? [], [ diff --git a/src/Middlewares/Middleware.php b/src/Middlewares/Middleware.php index 82288e2..ff0b580 100644 --- a/src/Middlewares/Middleware.php +++ b/src/Middlewares/Middleware.php @@ -6,11 +6,12 @@ use Closure; use Illuminate\Http\Request; +use LaravelLang\Locales\Data\LocaleData; +use LaravelLang\Locales\Facades\Locales; use LaravelLang\Routes\Concerns\KeyNames; -use LaravelLang\Routes\Services\Resolver; +use LaravelLang\Routes\Events\LocaleHasBeenSetEvent; use function app; -use function is_string; use function trim; abstract class Middleware @@ -22,17 +23,20 @@ abstract protected function detect(Request $request): bool|float|int|string|null public function __invoke(Request $request, Closure $next) { if ($locale = $this->getLocale($request)) { - $this->setLocale($locale); + $this->setLocale($locale->code); + $this->event($locale); } return $next($request); } - protected function getLocale(Request $request): string + protected function getLocale(Request $request): ?LocaleData { - return Resolver::locale( - $this->detect($request) - ); + if ($locale = $this->trim($this->detect($request))) { + return Locales::get($locale); + } + + return null; } protected function setLocale(string $locale): void @@ -40,8 +44,13 @@ protected function setLocale(string $locale): void app()->setLocale($locale); } - protected function trim(?string $locale): ?string + protected function event(LocaleData $locale): void + { + LocaleHasBeenSetEvent::dispatch($locale); + } + + protected function trim(bool|float|int|string|null $locale): string { - return is_string($locale) ? trim($locale) : null; + return trim((string) $locale); } } diff --git a/src/Services/Resolver.php b/src/Services/Resolver.php deleted file mode 100644 index a0fcd04..0000000 --- a/src/Services/Resolver.php +++ /dev/null @@ -1,15 +0,0 @@ -code ?? Locales::getDefault()->code; - } -} diff --git a/tests/Concerns/Routes.php b/tests/Concerns/Routes.php index c388d87..9f1d08c 100644 --- a/tests/Concerns/Routes.php +++ b/tests/Concerns/Routes.php @@ -51,6 +51,17 @@ public function setUpRoutes(): void ->middleware(LocalizationBySession::class) ->get('session/{foo}', $this->jsonResponse()) ->name('via.session'); + + app('router') + ->middleware([ + // LocalizationByParameter::class, + LocalizationByParameterWithRedirect::class, + // LocalizationByHeader::class, + // LocalizationByCookie::class, + // LocalizationBySession::class, + ]) + ->get('clean/{foo}', $this->jsonResponse()) + ->name('clean'); }); } diff --git a/tests/Feature/CookieTest.php b/tests/Feature/CookieTest.php index ec77e25..39048a0 100644 --- a/tests/Feature/CookieTest.php +++ b/tests/Feature/CookieTest.php @@ -15,6 +15,8 @@ ->getJson(route('via.cookie', compact('foo'))) ->assertSuccessful() ->assertJsonPath($foo, LocaleValue::TranslationFrench); + + assertEventDispatched(); })->with('main-locales'); test('aliased locale', function (string $locale) { @@ -25,6 +27,8 @@ ->getJson(route('via.cookie', compact('foo'))) ->assertSuccessful() ->assertJsonPath($foo, LocaleValue::TranslationGerman); + + assertEventDispatched(); })->with('aliased-locales'); test('empty locale', function (int|string|null $locale) { @@ -35,6 +39,8 @@ ->getJson(route('via.cookie', compact('foo'))) ->assertSuccessful() ->assertJsonPath($foo, LocaleValue::TranslationFrench); + + assertEventNotDispatched(); })->with('empty-locales'); test('uninstalled locale', function (string $locale) { @@ -45,6 +51,8 @@ ->getJson(route('via.cookie', compact('foo'))) ->assertSuccessful() ->assertJsonPath($foo, LocaleValue::TranslationFrench); + + assertEventDispatched(); })->with('uninstalled-locales'); test('unknown locale', function (int|string $locale) { @@ -55,4 +63,6 @@ ->getJson(route('via.cookie', compact('foo'))) ->assertSuccessful() ->assertJsonPath($foo, LocaleValue::TranslationFrench); + + assertEventDispatched(); })->with('unknown-locales'); diff --git a/tests/Feature/HeaderTest.php b/tests/Feature/HeaderTest.php index e55d965..1d8ed58 100644 --- a/tests/Feature/HeaderTest.php +++ b/tests/Feature/HeaderTest.php @@ -15,6 +15,8 @@ ]) ->assertSuccessful() ->assertJsonPath($foo, LocaleValue::TranslationFrench); + + assertEventDispatched(); })->with('main-locales'); test('aliased locale', function (string $locale) { @@ -25,6 +27,8 @@ ]) ->assertSuccessful() ->assertJsonPath($foo, LocaleValue::TranslationGerman); + + assertEventDispatched(); })->with('aliased-locales'); test('empty locale', function (int|string|null $locale) { @@ -35,6 +39,8 @@ ]) ->assertSuccessful() ->assertJsonPath($foo, LocaleValue::TranslationFrench); + + assertEventNotDispatched(); })->with('empty-locales'); test('uninstalled locale', function (string $locale) { @@ -45,6 +51,8 @@ ]) ->assertSuccessful() ->assertJsonPath($foo, LocaleValue::TranslationFrench); + + assertEventDispatched(); })->with('uninstalled-locales'); test('unknown locale', function (int|string $locale) { @@ -55,4 +63,6 @@ ]) ->assertSuccessful() ->assertJsonPath($foo, LocaleValue::TranslationFrench); + + assertEventDispatched(); })->with('unknown-locales'); diff --git a/tests/Feature/ParameterRedirectTest.php b/tests/Feature/ParameterRedirectTest.php index ccb0ed6..0322474 100644 --- a/tests/Feature/ParameterRedirectTest.php +++ b/tests/Feature/ParameterRedirectTest.php @@ -12,6 +12,8 @@ getJson(route('via.parameter.redirect', compact('foo', 'locale'))) ->assertSuccessful() ->assertJsonPath($foo, LocaleValue::TranslationFrench); + + assertEventDispatched(); })->with('main-locales'); test('aliased locale', function (string $locale) { @@ -20,6 +22,8 @@ getJson(route('via.parameter.redirect', compact('foo', 'locale'))) ->assertSuccessful() ->assertJsonPath($foo, LocaleValue::TranslationGerman); + + assertEventDispatched(); })->with('aliased-locales'); test('empty locale', function (int|string|null $locale) { @@ -30,6 +34,8 @@ 'locale' => LocaleValue::LocaleMain, 'foo' => $foo, ]); + + assertEventNotDispatched(); })->with('empty-locales'); test('uninstalled locale', function (string $locale) { @@ -40,6 +46,8 @@ 'locale' => LocaleValue::LocaleMain, 'foo' => $foo, ]); + + assertEventNotDispatched(); })->with('uninstalled-locales'); test('unknown locale', function (int|string $locale) { @@ -50,6 +58,8 @@ 'locale' => LocaleValue::LocaleMain, 'foo' => $foo, ]); + + assertEventNotDispatched(); })->with('unknown-locales'); test('not named', function (int|string|null $locale) { @@ -58,4 +68,6 @@ getJson(url('not-named/redirect/' . $foo . '/' . $locale)) ->assertSuccessful() ->assertJsonPath($foo, LocaleValue::TranslationFrench); + + assertEventNotDispatched(); })->with('empty-locales'); diff --git a/tests/Feature/ParameterTest.php b/tests/Feature/ParameterTest.php index fc25f0d..14d359a 100644 --- a/tests/Feature/ParameterTest.php +++ b/tests/Feature/ParameterTest.php @@ -12,6 +12,8 @@ getJson(route('via.parameter', compact('foo', 'locale'))) ->assertSuccessful() ->assertJsonPath($foo, LocaleValue::TranslationFrench); + + assertEventDispatched(); })->with('main-locales'); test('aliased locale', function (string $locale) { @@ -20,6 +22,8 @@ getJson(route('via.parameter', compact('foo', 'locale'))) ->assertSuccessful() ->assertJsonPath($foo, LocaleValue::TranslationGerman); + + assertEventDispatched(); })->with('aliased-locales'); test('empty locale', function (int|string|null $locale) { @@ -28,6 +32,8 @@ getJson(route('via.parameter', compact('foo', 'locale'))) ->assertSuccessful() ->assertJsonPath($foo, LocaleValue::TranslationFrench); + + assertEventNotDispatched(); })->with('empty-locales'); test('uninstalled locale', function (string $locale) { @@ -36,6 +42,8 @@ getJson(route('via.parameter', compact('foo', 'locale'))) ->assertSuccessful() ->assertJsonPath($foo, LocaleValue::TranslationFrench); + + assertEventDispatched(); })->with('uninstalled-locales'); test('unknown locale', function (int|string $locale) { @@ -44,4 +52,6 @@ getJson(route('via.parameter', compact('foo', 'locale'))) ->assertSuccessful() ->assertJsonPath($foo, LocaleValue::TranslationFrench); + + assertEventDispatched(); })->with('unknown-locales'); diff --git a/tests/Feature/SessionTest.php b/tests/Feature/SessionTest.php index 1854fb0..17612a2 100644 --- a/tests/Feature/SessionTest.php +++ b/tests/Feature/SessionTest.php @@ -14,6 +14,8 @@ ->getJson(route('via.session', compact('foo'))) ->assertSuccessful() ->assertJsonPath($foo, LocaleValue::TranslationFrench); + + assertEventDispatched(); })->with('main-locales'); test('aliased locale', function (string $locale) { @@ -23,6 +25,8 @@ ->getJson(route('via.session', compact('foo'))) ->assertSuccessful() ->assertJsonPath($foo, LocaleValue::TranslationGerman); + + assertEventDispatched(); })->with('aliased-locales'); test('empty locale', function (int|string|null $locale) { @@ -32,6 +36,8 @@ ->getJson(route('via.session', compact('foo'))) ->assertSuccessful() ->assertJsonPath($foo, LocaleValue::TranslationFrench); + + assertEventNotDispatched(); })->with('empty-locales'); test('uninstalled locale', function (string $locale) { @@ -41,6 +47,8 @@ ->getJson(route('via.session', compact('foo'))) ->assertSuccessful() ->assertJsonPath($foo, LocaleValue::TranslationFrench); + + assertEventDispatched(); })->with('uninstalled-locales'); test('unknown locale', function (int|string $locale) { @@ -50,4 +58,6 @@ ->getJson(route('via.session', compact('foo'))) ->assertSuccessful() ->assertJsonPath($foo, LocaleValue::TranslationFrench); + + assertEventDispatched(); })->with('unknown-locales'); diff --git a/tests/Helpers/AssertEvent.php b/tests/Helpers/AssertEvent.php new file mode 100644 index 0000000..8a151d7 --- /dev/null +++ b/tests/Helpers/AssertEvent.php @@ -0,0 +1,16 @@ +in('Feature'); +use Illuminate\Support\Facades\Event; -// expect()->extend('toBeOne', function () { -// return $this->toBe(1); -// }); +uses(Tests\TestCase::class) + ->beforeEach(fn () => Event::fake()) + ->in('Feature');