Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Settings #224

Merged
merged 6 commits into from
Oct 13, 2024
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
27 changes: 27 additions & 0 deletions database/factories/SettingFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Cone\Root\Database\Factories;

use Cone\Root\Models\Setting;
use Illuminate\Database\Eloquent\Factories\Factory;

class SettingFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var class-string<\Cone\Root\Models\Setting>
*/
protected $model = Setting::class;

/**
* Define the model's default state.
*/
public function definition(): array
{
return [
'key' => $this->faker->slug(1),
'value' => mt_rand(10, 1000),
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('root_settings', static function (Blueprint $table): void {
$table->id();
$table->string('key')->unique();
$table->text('value')->nullable();
$table->timestamps();
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('root_settings');
}
};
8 changes: 8 additions & 0 deletions src/Interfaces/Models/Setting.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Cone\Root\Interfaces\Models;

interface Setting
{
//
}
11 changes: 11 additions & 0 deletions src/Interfaces/Settings/Registry.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Cone\Root\Interfaces\Settings;

interface Registry
{
/**
* Get the repository instance.
*/
public function getRepository(): Repository;
}
63 changes: 63 additions & 0 deletions src/Interfaces/Settings/Repository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

namespace Cone\Root\Interfaces\Settings;

use Cone\Root\Models\Setting;

interface Repository
{
/**
* Get the setting model.
*/
public function model(): Setting;

/**
* Set the value cast.
*/
public function cast(string $key, string $type): void;

/**
* Merge the casts.
*/
public function mergeCasts(array $casts): void;

/**
* Remove the given casts.
*/
public function removeCasts(string|array $keys): void;

/**
* Remove the given casts.
*/
public function clearCasts(): void;

/**
* Get the value casts.
*/
public function getCasts(): array;

/**
* Get the value for the given key.
*/
public function get(string $key, mixed $default = null, bool $fresh = false): mixed;

/**
* Set the value for the given key.
*/
public function set(string $key, mixed $value): mixed;

/**
* Delete the given keys.
*/
public function delete(string|array $keys): void;

/**
* Flush the cache.
*/
public function flush(): void;

/**
* Get all the settings.
*/
public function all(): array;
}
62 changes: 62 additions & 0 deletions src/Models/Setting.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace Cone\Root\Models;

use Cone\Root\Database\Factories\SettingFactory;
use Cone\Root\Interfaces\Models\Setting as Contract;
use Cone\Root\Traits\InteractsWithProxy;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Setting extends Model implements Contract
{
use HasFactory;
use InteractsWithProxy;

/**
* The attributes that are mass assignable.
*
* @var array<string>
*/
protected $fillable = [
'key',
'value',
];

/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'root_settings';

/**
* Get the proxied interface.
*/
public static function getProxiedInterface(): string
{
return Contract::class;
}

/**
* Create a new factory instance for the model.
*/
protected static function newFactory(): SettingFactory
{
return SettingFactory::new();
}

/**
* Cast the value attribute to the given type.
*/
public function castValue(?string $type = null): static
{
if (! is_null($type)) {
$this->casts['value'] = $type;
} else {
unset($this->casts['value']);
}

return $this;
}
}
7 changes: 7 additions & 0 deletions src/Root.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Closure;
use Cone\Root\Interfaces\Breadcrumbs\Registry as Breadcrumbs;
use Cone\Root\Interfaces\Navigation\Registry as Navigation;
use Cone\Root\Interfaces\Settings\Registry as Settings;
use Cone\Root\Models\User;
use Cone\Root\Resources\Resources;
use Cone\Root\Widgets\Widgets;
Expand Down Expand Up @@ -55,6 +56,11 @@ class Root
*/
public readonly Breadcrumbs $breadcrumbs;

/**
* The settings instance.
*/
public readonly Settings $settings;

/**
* The auth resolver.
*/
Expand All @@ -75,6 +81,7 @@ public function __construct(Application $app)
$this->widgets = new Widgets;
$this->navigation = $app->make(Navigation::class);
$this->breadcrumbs = $app->make(Breadcrumbs::class);
$this->settings = $app->make(Settings::class);
$this->timezone = $app['config']->get('app.timezone');
}

Expand Down
3 changes: 3 additions & 0 deletions src/RootServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,11 @@ class RootServiceProvider extends ServiceProvider
Interfaces\Models\Medium::class => Models\Medium::class,
Interfaces\Models\Meta::class => Models\Meta::class,
Interfaces\Models\Notification::class => Models\Notification::class,
Interfaces\Models\Setting::class => Models\Setting::class,
Interfaces\Models\User::class => Models\User::class,
Interfaces\Navigation\Registry::class => Navigation\Registry::class,
Interfaces\Settings\Registry::class => Settings\Registry::class,
Interfaces\Settings\Repository::class => Settings\Repository::class,
];

/**
Expand Down
38 changes: 38 additions & 0 deletions src/Settings/Registry.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Cone\Root\Settings;

use Cone\Root\Interfaces\Settings\Registry as Contract;
use Cone\Root\Interfaces\Settings\Repository;

class Registry implements Contract
{
/**
* The repository instance.
*/
protected Repository $repository;

/**
* Create a new registry instance.
*/
public function __construct(Repository $repository)
{
$this->repository = $repository;
}

/**
* Get the repository instance.
*/
public function getRepository(): Repository
{
return $this->repository;
}

/**
* Dynamically call the given method.
*/
public function __call(string $name, array $arguments): mixed
{
return call_user_func_array([$this->repository, $name], $arguments);
}
}
Loading
Loading