Skip to content

Commit

Permalink
Settings
Browse files Browse the repository at this point in the history
  • Loading branch information
alexvenga committed Dec 11, 2022
1 parent 5c7f770 commit 11af114
Show file tree
Hide file tree
Showing 12 changed files with 32 additions and 61 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Easy Laravel grouped settings (stored in MYSQL) package with MoonShine Laravel Admin GUI
# Easy laravel cached settings (stored in MYSQL) package with MoonShine Laravel Admin GUI

[![Latest Version on Packagist](https://img.shields.io/packagist/v/visual-ideas/laravel-site-settings.svg?style=flat-square)](https://packagist.org/packages/visual-ideas/laravel-site-settings)
[![Total Downloads](https://img.shields.io/packagist/dt/visual-ideas/laravel-site-settings.svg?style=flat-square)](https://packagist.org/packages/visual-ideas/laravel-site-settings)
Expand Down Expand Up @@ -52,9 +52,11 @@ You can use this package as default laravel config() function!

or Blade directive @lssconfig
```php
@lssconfig('group.setting')
@settings('group.setting')
```

For PHPStorm you can set this blade directive with [This instruction](https://www.jetbrains.com/help/phpstorm/blade-page.html)

## Usage with MoonShine Laravel Admin
Please see [MoonShine](https://moonshine.cutcode.ru/)

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"VI\\LaravelSiteSettings\\LaravelSiteSettingsProvider"
],
"aliases": {
"LSS": "VI\\LaravelSiteSettings\\Facades\\LaravelSiteSettings"
"Settings": "VI\\LaravelSiteSettings\\Facades\\LaravelSiteSettings"
}
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@
*/
public function up()
{
Schema::create('laravel_site_setting_groups', function (Blueprint $table) {
Schema::create('setting_groups', function (Blueprint $table) {
$table->id();
$table->string('slug', 190)->unique();
$table->string('name', 190)->nullable();
$table->timestamps();
});

\VI\LaravelSiteSettings\Models\LaravelSiteSettingGroup::create(['slug' =>'default', 'name' =>'Default']);
\VI\LaravelSiteSettings\Models\SettingGroup::create(['slug' =>'default', 'name' =>'Default']);
}

/**
Expand All @@ -30,6 +30,6 @@ public function up()
*/
public function down()
{
Schema::dropIfExists('laravel_site_setting_groups');
Schema::dropIfExists('setting_groups');
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@
*/
public function up()
{
Schema::create('laravel_site_settings', function (Blueprint $table) {
Schema::create('settings', function (Blueprint $table) {
$table->id();

$table->foreignId('laravel_site_setting_group_id')
->default(1)
$table->foreignId('setting_group_id')
->constrained()
->cascadeOnDelete()
->cascadeOnUpdate();
Expand All @@ -26,7 +25,7 @@ public function up()
$table->string('value', 190);
$table->timestamps();

$table->unique('laravel_site_setting_group_id', 'slug');
$table->unique('setting_group_id', 'slug');
});
}

Expand All @@ -37,6 +36,6 @@ public function up()
*/
public function down()
{
Schema::dropIfExists('laravel_site_settings');
Schema::dropIfExists('settings');
}
};
2 changes: 1 addition & 1 deletion src/Facades/LaravelSiteSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ class LaravelSiteSettings extends Facade
{
protected static function getFacadeAccessor()
{
return 'LssConfig';
return 'settings';
}
}
24 changes: 2 additions & 22 deletions src/LaravelSiteSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Illuminate\Contracts\Config\Repository as ConfigContract;
use Illuminate\Support\Arr;
use VI\LaravelSiteSettings\Models\LaravelSiteSettingGroup;
use VI\LaravelSiteSettings\Models\SettingGroup;

class LaravelSiteSettings implements ConfigContract
{
Expand All @@ -15,7 +15,7 @@ public function __construct()
{

$this->items = cache()->rememberForever(config('laravelsitesettings.cache_key'), function () {
return LaravelSiteSettingGroup::all()
return SettingGroup::all()
->load('settings')
->keyBy('slug')
->map(function ($group) {
Expand Down Expand Up @@ -46,9 +46,6 @@ public function has($key): bool
*/
public function get($key, $default = null): mixed
{
if (is_array($key)) {
return $this->getMany($key);
}

return Arr::get($this->items, $key, $default);
}
Expand Down Expand Up @@ -114,21 +111,4 @@ public function push($key, $value): void
$this->set($key, $array);
}

/**
* Get many configuration values.
*
* @param array $keys
* @return array
*/
public function getMany(array $keys): array
{
$config = [];

foreach ($keys as $key => $default) {
$config[$key] = Arr::get($this->items, $key, $default);
}

return $config;
}

}
14 changes: 7 additions & 7 deletions src/LaravelSiteSettingsProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@

use Illuminate\Support\Facades\Blade;
use Illuminate\Support\ServiceProvider;
use VI\LaravelSiteSettings\Models\LaravelSiteSetting;
use VI\LaravelSiteSettings\Models\LaravelSiteSettingGroup;
use VI\LaravelSiteSettings\Models\Setting;
use VI\LaravelSiteSettings\Models\SettingGroup;
use VI\LaravelSiteSettings\Observers\LSSObserver;

class LaravelSiteSettingsProvider extends ServiceProvider
{

public function register()
{
$this->app->bind('LssConfig', function ($app) {
$this->app->bind('settings', function ($app) {
return new LaravelSiteSettings();
});

Expand All @@ -38,12 +38,12 @@ public function boot()

}

Blade::directive('lssconfig', function (...$expression) {
return "<?php echo lssconfig($expression); ?>";
Blade::directive('settings', function ($expression) {
return "<?php echo settings($expression); ?>";
});

LaravelSiteSetting::observe(LSSObserver::class);
LaravelSiteSettingGroup::observe(LSSObserver::class);
Setting::observe(LSSObserver::class);
SettingGroup::observe(LSSObserver::class);

}

Expand Down
13 changes: 4 additions & 9 deletions src/Models/LaravelSiteSetting.php → src/Models/Setting.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,20 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class LaravelSiteSetting extends Model
class Setting extends Model
{
protected $guarded = [];

protected $fillable = [
'laravel_site_setting_group_id',
'setting_group_id',
'slug',
'name',
'value',
];

public function laravelSiteSettingGroup(): BelongsTo
public function settingsGroup(): BelongsTo
{
return $this->belongsTo(LaravelSiteSettingGroup::class);
}

public function group(): BelongsTo
{
return $this->laravelSiteSettingGroup();
return $this->belongsTo(SettingGroup::class);
}

public function getNameHumanAttribute(): string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;

class LaravelSiteSettingGroup extends Model
class SettingGroup extends Model
{
protected $guarded = [];

Expand All @@ -14,14 +14,9 @@ class LaravelSiteSettingGroup extends Model
'name',
];

public function laravel_site_settings(): HasMany
{
return $this->hasMany(LaravelSiteSetting::class);
}

public function settings(): HasMany
{
return $this->laravel_site_settings();
return $this->hasMany(Setting::class);
}

public function getNameHumanAttribute(): string
Expand Down
4 changes: 2 additions & 2 deletions src/helpers.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php


if (!function_exists('lss_config')) {
if (!function_exists('settings')) {
/**
* Get / set the specified configuration value.
*
Expand All @@ -11,7 +11,7 @@
* @param mixed $default
* @return mixed|\Illuminate\Config\Repository
*/
function lss_config($key = null, $default = null)
function settings($key = null, $default = null)
{
if (is_null($key)) {
return app('LssConfig')->all();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class LaravelSiteSettingGroupResource extends Resource
return [
ID::make()->sortable(),
Text::make('Slug', 'slug')->required()->sortable()->hint('a-z, 0-9, -'),
Text::make('Name', 'name')->nullable()->sortable(),
Text::make('Name', 'name')->nullable()->sortable()->hint('Не используется на сайте, только для удобства администрирования!'),
Date::make('Updated at', 'updated_at')->sortable()->hideOnForm(),
];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class LaravelSiteSettingResource extends Resource
fn($item) => $item->name_human
)->nullable()->sortable(),
Text::make('Slug', 'slug')->required()->sortable()->hint('a-z, 0-9, -'),
Text::make('Name', 'name')->nullable()->sortable(),
Text::make('Name', 'name')->nullable()->sortable()->hint('Не используется на сайте, только для удобства администрирования!'),
Text::make('Value', 'value')->required()->sortable(),
Date::make('Updated at', 'updated_at')->sortable()->hideOnForm(),
];
Expand Down

0 comments on commit 11af114

Please sign in to comment.