Skip to content

Commit

Permalink
Merge pull request #8 from Laravel-Lang/1.x
Browse files Browse the repository at this point in the history
Added `LocaleHasBeenSetEvent` event
  • Loading branch information
andrey-helldar authored Jun 10, 2024
2 parents b98a830 + 04f469f commit f5a5bd8
Show file tree
Hide file tree
Showing 12 changed files with 127 additions and 29 deletions.
20 changes: 20 additions & 0 deletions src/Events/LocaleHasBeenSetEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace LaravelLang\Routes\Events;

use Illuminate\Foundation\Events\Dispatchable;
use LaravelLang\Locales\Data\LocaleData;

/**
* @method static void dispatch(LocaleData $locale)
*/
class LocaleHasBeenSetEvent
{
use Dispatchable;

public function __construct(
public LocaleData $locale
) {}
}
7 changes: 6 additions & 1 deletion src/Middlewares/LocalizationByParameterWithRedirect.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,16 @@ protected function detect(Request $request): bool|float|int|string|null

protected function invalidParameter(Request $request): ?bool
{
return ! $request->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() ?? [], [
Expand Down
27 changes: 18 additions & 9 deletions src/Middlewares/Middleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -22,26 +23,34 @@ 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
{
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);
}
}
15 changes: 0 additions & 15 deletions src/Services/Resolver.php

This file was deleted.

11 changes: 11 additions & 0 deletions tests/Concerns/Routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
}

Expand Down
10 changes: 10 additions & 0 deletions tests/Feature/CookieTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -55,4 +63,6 @@
->getJson(route('via.cookie', compact('foo')))
->assertSuccessful()
->assertJsonPath($foo, LocaleValue::TranslationFrench);

assertEventDispatched();
})->with('unknown-locales');
10 changes: 10 additions & 0 deletions tests/Feature/HeaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
])
->assertSuccessful()
->assertJsonPath($foo, LocaleValue::TranslationFrench);

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

test('aliased locale', function (string $locale) {
Expand All @@ -25,6 +27,8 @@
])
->assertSuccessful()
->assertJsonPath($foo, LocaleValue::TranslationGerman);

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

test('empty locale', function (int|string|null $locale) {
Expand All @@ -35,6 +39,8 @@
])
->assertSuccessful()
->assertJsonPath($foo, LocaleValue::TranslationFrench);

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

test('uninstalled locale', function (string $locale) {
Expand All @@ -45,6 +51,8 @@
])
->assertSuccessful()
->assertJsonPath($foo, LocaleValue::TranslationFrench);

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

test('unknown locale', function (int|string $locale) {
Expand All @@ -55,4 +63,6 @@
])
->assertSuccessful()
->assertJsonPath($foo, LocaleValue::TranslationFrench);

assertEventDispatched();
})->with('unknown-locales');
12 changes: 12 additions & 0 deletions tests/Feature/ParameterRedirectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -30,6 +34,8 @@
'locale' => LocaleValue::LocaleMain,
'foo' => $foo,
]);

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

test('uninstalled locale', function (string $locale) {
Expand All @@ -40,6 +46,8 @@
'locale' => LocaleValue::LocaleMain,
'foo' => $foo,
]);

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

test('unknown locale', function (int|string $locale) {
Expand All @@ -50,6 +58,8 @@
'locale' => LocaleValue::LocaleMain,
'foo' => $foo,
]);

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

test('not named', function (int|string|null $locale) {
Expand All @@ -58,4 +68,6 @@
getJson(url('not-named/redirect/' . $foo . '/' . $locale))
->assertSuccessful()
->assertJsonPath($foo, LocaleValue::TranslationFrench);

assertEventNotDispatched();
})->with('empty-locales');
10 changes: 10 additions & 0 deletions tests/Feature/ParameterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -44,4 +52,6 @@
getJson(route('via.parameter', compact('foo', 'locale')))
->assertSuccessful()
->assertJsonPath($foo, LocaleValue::TranslationFrench);

assertEventDispatched();
})->with('unknown-locales');
10 changes: 10 additions & 0 deletions tests/Feature/SessionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -50,4 +58,6 @@
->getJson(route('via.session', compact('foo')))
->assertSuccessful()
->assertJsonPath($foo, LocaleValue::TranslationFrench);

assertEventDispatched();
})->with('unknown-locales');
16 changes: 16 additions & 0 deletions tests/Helpers/AssertEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

use Illuminate\Support\Facades\Event;
use LaravelLang\Routes\Events\LocaleHasBeenSetEvent;

function assertEventDispatched(): void
{
Event::assertDispatched(LocaleHasBeenSetEvent::class);
}

function assertEventNotDispatched(): void
{
Event::assertNotDispatched(LocaleHasBeenSetEvent::class);
}
8 changes: 4 additions & 4 deletions tests/Pest.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

uses(Tests\TestCase::class)->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');

0 comments on commit f5a5bd8

Please sign in to comment.