-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #224 from conedevelopment/settings
Settings
- Loading branch information
Showing
11 changed files
with
525 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
]; | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
database/migrations/2024_09_02_111321_create_root_settings_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace Cone\Root\Interfaces\Models; | ||
|
||
interface Setting | ||
{ | ||
// | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Oops, something went wrong.