Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 3 additions & 14 deletions app/Actions/LocaleAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace App\Actions;

use App\Enums\LocaleEnum;
use App\Enums\SessionKeyEnum;

class LocaleAction
Expand All @@ -13,19 +12,9 @@ public function __construct(

public function setLocale(): string
{
$locale = $this->validate() ? $this->locale : LocaleEnum::DE->value;
session()->put(SessionKeyEnum::LANGUAGE->value, $this->locale);
app()->setLocale($this->locale);

session()->put(SessionKeyEnum::LANGUAGE->value, $locale);
app()->setLocale($locale);

return $locale;
}

private function validate(): bool
{
return in_array($this->locale, [
LocaleEnum::DE->value,
LocaleEnum::EN->value,
]);
return $this->locale;
}
}
103 changes: 64 additions & 39 deletions app/Actions/PageAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,83 +2,108 @@

namespace App\Actions;

use App\DTO\PageDTO;
use App\Models\News;
use App\Models\Page;
use App\Models\Product;
use App\Models\Reference;
use App\Models\Service;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;

class PageAction
{
private string $prefix;

public function __construct(
private ?string $key = null,
private ?string $locale = null,
private ?string $routeName = null,
private mixed $routeParameters = [],
private ?Collection $referencePages = null,
) {
$this->locale = $locale ?? app()->getLocale();
$this->prefix = 'cached_page_'.$this->locale.'_';
}

public function default(): ?Page
public function default(): ?PageDTO
{
Str::slug($key = $this->prefix.$this->key);
$page = $this->findPage();

if (! $page) {
return null;
}

return Cache::remember($key, 86400, fn () => $this->findPage());
return new PageDTO(
locale: $page->locale,
routeKey: $page->key,
routeName: Str::slug($page->locale).'.'.$this->routeName,
title: $page->title,
description: $page->description,
image: $page->image,
lastModificationDate: $page->updated_at ?? now(),
routeParameters: $this->routeParameters,
referencePages: $this->referencePages
);
}

public function news(News $news): ?Page
public function news(News $news, bool $withReferences = false, ?string $locale = null): PageDTO
{
return $this->createFakePage(
key: Str::slug($this->prefix.'news_'.$news->id),
return new PageDTO(
locale: $locale ?? $news->locale->value,
routeKey: 'news.show',
routeName: Str::slug(title: $locale ?? $news->locale->value).'.news.show',
title: $news->title,
description: $news->teaser,
image: $news->image
image: $news->image,
lastModificationDate: $news->updated_at ?? now(),
routeParameters: ['locale' => $news->locale, 'news' => $news],
referencePages: $withReferences ? $news->references->map(function (Reference $reference) {
$reference->load(['target']);

return self::news(news: $reference->target, withReferences: false, locale: $reference->reference_locale);
}) : null,
);
}

public function products(Product $product): ?Page
public function product(Product $product, bool $withReferences = false, ?string $locale = null): PageDTO
{
return $this->createFakePage(
key: Str::slug($this->prefix.'products_'.$product->id),
return new PageDTO(
locale: $locale ?? $product->locale->value,
routeKey: 'products.show',
routeName: Str::slug(title: $locale ?? $product->locale->value).'.products.show',
title: $product->name,
description: $product->teaser,
image: $product->image
image: $product->image,
lastModificationDate: $product->updated_at ?? now(),
routeParameters: ['locale' => $product->locale, 'product' => $product],
referencePages: $withReferences ? $product->references->map(function (Reference $reference) {
$reference->load(['target']);

return self::product(product: $reference->target, withReferences: false, locale: $reference->reference_locale);
}) : null,
);
}

public function services(Service $service): ?Page
public function service(Service $service, bool $withReferences = false, ?string $locale = null): PageDTO
{
return $this->createFakePage(
key: Str::slug($this->prefix.'services_'.$service->id),
return new PageDTO(
locale: $locale ?? $service->locale->value,
routeKey: 'services.show',
routeName: Str::slug(title: $locale ?? $service->locale->value).'.services.show',
title: $service->name,
description: $service->teaser,
image: $service->image,
);
}

private function createFakePage(
string $key,
string $title,
string $description,
mixed $image
): ?Page {
return Cache::rememberForever($key, function () use ($title, $description, $image) {
lastModificationDate: $service->updated_at ?? now(),
routeParameters: ['locale' => $service->locale, 'service' => $service],
referencePages: $withReferences ? $service->references->map(function (Reference $reference) {
$reference->load(['target']);

$fakePage = new Page;
$fakePage->locale = app()->getLocale();
$fakePage->robots = 'index,follow';
$fakePage->title = $title;
$fakePage->description = $description;
$fakePage->image = $image;

return $fakePage;
});
return self::service(service: $reference->target, withReferences: false, locale: $reference->reference_locale);
}) : null,
);
}

private function findPage(): ?Page
{
return Page::where('locale', $this->locale)->where('key', $this->key)->first();
return Page::where('locale', $this->locale)
->where('key', $this->routeName)
->first();
}
}
27 changes: 27 additions & 0 deletions app/DTO/PageDTO.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace App\DTO;

use Illuminate\Support\Carbon;
use Illuminate\Support\Collection;

class PageDTO
{
public function __construct(
public string $locale,
public string $routeKey,
public string $routeName,
public string $title,
public Carbon $lastModificationDate,
public string $robots = 'noindex,nofollow',
public mixed $routeParameters = [],
public ?string $description = null,
public ?string $image = null,
public ?Collection $referencePages = null,
) {}

public function url(): string
{
return route($this->routeName, $this->routeParameters, true);
}
}
3 changes: 0 additions & 3 deletions app/Enums/EnvironmentEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@

namespace App\Enums;

use App\Traits\HasNovaEnumLabel;
use Filament\Support\Contracts\HasLabel;

enum EnvironmentEnum: string implements HasLabel
{
use HasNovaEnumLabel;

case LOCAL = 'local';
case STAGING = 'staging';
case PRODUCTION = 'production';
Expand Down
3 changes: 0 additions & 3 deletions app/Enums/GuardEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@

namespace App\Enums;

use App\Traits\HasNovaEnumLabel;
use Filament\Support\Contracts\HasLabel;

enum GuardEnum: string implements HasLabel
{
use HasNovaEnumLabel;

case WEB = 'web';

public function getLabel(): string
Expand Down
7 changes: 2 additions & 5 deletions app/Enums/LocaleEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,18 @@

namespace App\Enums;

use App\Traits\HasNovaEnumLabel;
use Filament\Support\Contracts\HasLabel;

enum LocaleEnum: string implements HasLabel
{
use HasNovaEnumLabel;

case DE = 'de_CH';
case EN = 'en_CH';

public function getLabel(): string
{
return match ($this) {
LocaleEnum::DE => __('German'),
LocaleEnum::EN => __('English'),
LocaleEnum::DE => __('DE'),
LocaleEnum::EN => __('EN'),
};
}
}
3 changes: 0 additions & 3 deletions app/Enums/RoleEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@

namespace App\Enums;

use App\Traits\HasNovaEnumLabel;
use Filament\Support\Contracts\HasLabel;

enum RoleEnum: string implements HasLabel
{
use HasNovaEnumLabel;

case ADMINISTRATOR = 'administrator';
case USER = 'user';
case API = 'api';
Expand Down
3 changes: 0 additions & 3 deletions app/Enums/SessionKeyEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@

namespace App\Enums;

use App\Traits\HasNovaEnumLabel;
use Filament\Support\Contracts\HasLabel;

enum SessionKeyEnum: string implements HasLabel
{
use HasNovaEnumLabel;

case LANGUAGE = 'language';

public function getLabel(): string
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/AboutUs/AboutUsIndexController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class AboutUsIndexController extends Controller
public function __invoke(): View
{
return view('app.about-us.index')->with([
'page' => (new PageAction('about-us.index'))->default(),
'page' => (new PageAction(locale: null, routeName: 'about-us.index'))->default(),
]);
}
}
2 changes: 1 addition & 1 deletion app/Http/Controllers/Contact/ContactIndexController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class ContactIndexController extends Controller
public function __invoke(): View
{
return view('app.contact.index')->with([
'page' => (new PageAction('contact.index'))->default(),
'page' => (new PageAction(locale: null, routeName: 'contact.index'))->default(),
]);
}
}
2 changes: 1 addition & 1 deletion app/Http/Controllers/Jobs/JobsIndexController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class JobsIndexController extends Controller
public function __invoke(): View
{
return view('app.jobs.index')->with([
'page' => (new PageAction('jobs.index'))->default(),
'page' => (new PageAction(locale: null, routeName: 'jobs.index'))->default(),
]);
}
}
2 changes: 1 addition & 1 deletion app/Http/Controllers/Legal/ImprintIndexController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class ImprintIndexController extends Controller
public function __invoke(): View
{
return view('app.legal.imprint.index')->with([
'page' => (new PageAction('legal.imprint.index'))->default(),
'page' => (new PageAction(locale: null, routeName: 'legal.imprint.index'))->default(),
]);
}
}
2 changes: 1 addition & 1 deletion app/Http/Controllers/Legal/PrivacyIndexController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class PrivacyIndexController extends Controller
public function __invoke(): View
{
return view('app.legal.privacy.index')->with([
'page' => (new PageAction('legal.privacy.index'))->default(),
'page' => (new PageAction(locale: null, routeName: 'legal.privacy.index'))->default(),
]);
}
}
2 changes: 1 addition & 1 deletion app/Http/Controllers/Legal/TermsIndexController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class TermsIndexController extends Controller
public function __invoke(): View
{
return view('app.legal.terms.index')->with([
'page' => (new PageAction('legal.terms.index'))->default(),
'page' => (new PageAction(locale: null, routeName: 'legal.terms.index'))->default(),
]);
}
}
26 changes: 20 additions & 6 deletions app/Http/Controllers/Locale/LocaleUpdateController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,41 @@
namespace App\Http\Controllers\Locale;

use App\Actions\LocaleAction;
use App\Enums\LocaleEnum;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Str;
use Illuminate\Validation\Rules\Enum;
use Spatie\ResponseCache\Commands\ClearCommand;

class LocaleUpdateController extends Controller
{
/**
* Display the user's profile form.
*/
public function __invoke(string $locale)
public function __invoke(Request $request)
{
$validated = $request->validate([
'language' => ['required', new Enum(LocaleEnum::class)],
]);

$locale = Arr::get($validated, 'language');

$locale = (new LocaleAction($locale))->setLocale();

$previousUrl = url()->previous();

$previousRoute = app('router')->getRoutes()->match(request()->create($previousUrl));
$previousRouteName = Str::after($previousRoute?->getName() ?? '', '.');

$routeParameters = $previousRoute?->parameters();
$route = Route::getRoutes()->match(request()->create($previousUrl));
$routeName = Str::after($route->getName(), '.');
$routeParameters = $route->parameters();

$localeSlug = Str::slug($locale);

return redirect()->route($localeSlug.'.'.$previousRouteName, $routeParameters);
Artisan::call(ClearCommand::class);

return redirect()->route("{$localeSlug}.{$routeName}", $routeParameters);
}
}
2 changes: 1 addition & 1 deletion app/Http/Controllers/Media/MediaIndexController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class MediaIndexController extends Controller
public function __invoke(): View
{
return view('app.media.index')->with([
'page' => (new PageAction('media.index'))->default(),
'page' => (new PageAction(locale: null, routeName: 'media.index'))->default(),
]);
}
}
Loading