adrema/app/Setting/SettingFactory.php

49 lines
1.3 KiB
PHP
Raw Normal View History

2022-10-20 02:15:28 +02:00
<?php
namespace App\Setting;
2023-06-01 12:04:31 +02:00
use App\Setting\Contracts\Indexable;
use App\Setting\Contracts\Storeable;
2022-10-20 02:15:28 +02:00
use Illuminate\Routing\Router;
class SettingFactory
{
/**
* @var array<int, class-string<LocalSettings>>
*/
private array $settings = [];
/**
2023-06-01 12:04:31 +02:00
* @param class-string $setting
2022-10-20 02:15:28 +02:00
*/
public function register(string $setting): void
{
$this->settings[] = $setting;
2023-06-01 12:04:31 +02:00
if (new $setting() instanceof Indexable) {
app(Router::class)->middleware(['web', 'auth:web', SettingMiddleware::class])->get($setting::url(), $setting::indexAction());
}
if (new $setting() instanceof Storeable) {
app(Router::class)->middleware(['web', 'auth:web', SettingMiddleware::class])->post($setting::url(), $setting::storeAction());
}
2022-10-20 02:15:28 +02:00
if (1 === count($this->settings)) {
app(Router::class)->redirect('/setting', '/setting/'.$setting::slug());
}
}
/**
* @return array<int, array{url: string, is_active: bool}>
*/
public function getShare(): array
{
return collect($this->settings)->map(fn ($setting) => [
'url' => $setting::url(),
'is_active' => request()->fullUrlIs(url($setting::url())),
'title' => $setting::title(),
])
->toArray();
}
}