-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit cea8679
Showing
9 changed files
with
382 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 @@ | ||
/.idea |
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,117 @@ | ||
# Laravel Settings | ||
Package tested with Laravel 5.8. Other versions are not tested. | ||
|
||
| Laravel version | Tested | | ||
| ---------------- | ------- | | ||
| 5.8.* | ✅ | | ||
|
||
## Installation | ||
``` | ||
$ composer require kolirt/laravel-settings | ||
``` | ||
``` | ||
$ php artisan settings:install | ||
``` | ||
Configure translations config on config/settings.php path. | ||
|
||
## Methods | ||
|
||
#### Sync data (settings_sync) | ||
``` | ||
$data = [ | ||
'key1' => [ | ||
'subkey1' => [ | ||
'subsubnkey1' => 1, | ||
'subsubnkey2' => [ | ||
'test' => 'test' | ||
] | ||
], | ||
'subkey2' => 2 | ||
], | ||
'key2' => [] | ||
]; | ||
settings_sync('group', $data); | ||
$data = [ | ||
'key' => 1 | ||
]; | ||
settings_sync('group1', $data); | ||
``` | ||
|
||
#### Get all (settings) | ||
``` | ||
settings(); | ||
// result | ||
[ | ||
'group' => [ | ||
'key1' => [ | ||
'subkey1' => [ | ||
'subsubnkey1' => 1, | ||
'subsubnkey2' => [ | ||
'test' => 'test' | ||
] | ||
], | ||
'subkey2' => 2 | ||
], | ||
'key2' => [] | ||
], | ||
'group1' => [ | ||
'key' => 1 | ||
] | ||
]; | ||
``` | ||
|
||
#### Get by group name (setting) | ||
``` | ||
setting('group'); | ||
// result | ||
[ | ||
'key1' => [ | ||
'subkey1' => [ | ||
'subsubnkey1' => 1, | ||
'subsubnkey2' => [ | ||
'test' => 'test' | ||
] | ||
], | ||
'subkey2' => 2 | ||
], | ||
'key2' => [] | ||
]; | ||
``` | ||
|
||
#### Get by group name and key (setting) | ||
``` | ||
setting('group.key1'); | ||
// result | ||
[ | ||
'subkey1' => [ | ||
'subsubnkey1' => 1, | ||
'subsubnkey2' => [ | ||
'test' => 'test' | ||
] | ||
], | ||
'subkey2' => 2 | ||
]; | ||
``` | ||
|
||
#### Parse (setting) | ||
``` | ||
setting('group.key1.subkey1.subsubnkey2'); | ||
// result | ||
[ | ||
'test' => 'test' | ||
]; | ||
``` | ||
|
||
### Refresh cache | ||
``` | ||
setting('fresh'); | ||
``` |
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,34 @@ | ||
{ | ||
"name": "kolirt/laravel-settings", | ||
"description": "Package for settings", | ||
"keywords": [ | ||
"laravel", | ||
"settings" | ||
], | ||
"license": "MIT", | ||
"version": "1.2.0", | ||
"authors": [ | ||
{ | ||
"name": "kolirt" | ||
} | ||
], | ||
"require": {}, | ||
"autoload": { | ||
"files": [ | ||
"src/helpers.php" | ||
], | ||
"psr-4": { | ||
"Kolirt\\Settings\\": "src" | ||
} | ||
}, | ||
"extra": { | ||
"laravel": { | ||
"providers": [ | ||
"Kolirt\\Settings\\ServiceProvider" | ||
], | ||
"aliases": { | ||
"Settings": "Kolirt\\Settings\\Facade\\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,7 @@ | ||
<?php | ||
|
||
return [ | ||
'auto_locale' => true, | ||
'response' => 'object', // array, object, collect | ||
'connection' => config('database.default', 'mysql'), | ||
]; |
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,33 @@ | ||
<?php | ||
|
||
namespace Kolirt\Settings\Commands; | ||
|
||
use Illuminate\Console\Command; | ||
|
||
class InstallCommand extends Command | ||
{ | ||
/** | ||
* The name and signature of the console command. | ||
* | ||
* @var string | ||
*/ | ||
protected $signature = 'settings:install'; | ||
|
||
/** | ||
* The console command description. | ||
* | ||
* @var string | ||
*/ | ||
protected $description = 'Install settings package'; | ||
|
||
/** | ||
* Execute the console command. | ||
* | ||
* @return mixed | ||
*/ | ||
public function handle() | ||
{ | ||
$this->call('vendor:publish', ['--provider' => 'Kolirt\\Settings\\ServiceProvider']); | ||
$this->call('migrate'); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/Migrations/2019_04_20_085614_create_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,36 @@ | ||
<?php | ||
|
||
use Illuminate\Support\Facades\Schema; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Database\Migrations\Migration; | ||
|
||
class CreateSettingsTable extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function up() | ||
{ | ||
Schema::create('settings', function (Blueprint $table) { | ||
$table->bigIncrements('id'); | ||
|
||
$table->string('group'); | ||
$table->string('key'); | ||
$table->longText('value')->nullable(); | ||
|
||
$table->unique(['group', 'key']); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function down() | ||
{ | ||
Schema::dropIfExists('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,36 @@ | ||
<?php | ||
|
||
namespace Kolirt\Settings\Models; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
|
||
class Setting extends Model | ||
{ | ||
|
||
public $timestamps = false; | ||
|
||
protected $fillable = ['group', 'key', 'value']; | ||
|
||
public function __construct(array $attributes = []) | ||
{ | ||
$this->setConnection(config('settings.connection')); | ||
parent::__construct($attributes); | ||
} | ||
|
||
public static function sync(string $group, array $values) | ||
{ | ||
foreach ($values as $key => $value) { | ||
$setting = Setting::firstOrNew(['group' => $group, 'key' => $key]); | ||
$setting->value = json_encode($value); | ||
$setting->save(); | ||
} | ||
|
||
setting('fresh'); | ||
} | ||
|
||
public function getValueAttribute($value) | ||
{ | ||
return is_json($value) ? json_decode($value, 1) : $value; | ||
} | ||
|
||
} |
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,35 @@ | ||
<?php | ||
|
||
namespace Kolirt\Settings; | ||
|
||
use Illuminate\Support\ServiceProvider as BaseServiceProvider; | ||
|
||
class ServiceProvider extends BaseServiceProvider | ||
{ | ||
|
||
protected $commands = [ | ||
Commands\InstallCommand::class | ||
]; | ||
|
||
/** | ||
* Bootstrap any application services. | ||
*/ | ||
public function boot() | ||
{ | ||
$this->loadMigrationsFrom(__DIR__ . '/Migrations'); | ||
|
||
$this->mergeConfigFrom(__DIR__ . '/../config/settings.php', 'settings'); | ||
|
||
$this->publishes([ | ||
__DIR__ . '/../config/settings.php' => config_path('settings.php') | ||
]); | ||
} | ||
|
||
/** | ||
* Register any application services. | ||
*/ | ||
public function register() | ||
{ | ||
$this->commands($this->commands); | ||
} | ||
} |
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,83 @@ | ||
<?php | ||
|
||
if (!function_exists('settings_sync')) { | ||
function settings_sync(string $group, array $values) | ||
{ | ||
Kolirt\Settings\Models\Setting::sync($group, $values); | ||
} | ||
} | ||
|
||
if (!function_exists('setting')) { | ||
function setting(string $key, $default = null, $no_locale = null) | ||
{ | ||
return settings($key, $default, $no_locale); | ||
} | ||
} | ||
|
||
if (!function_exists('settings')) { | ||
function settings(string $key = null, $default = null, $no_locale = null) | ||
{ | ||
if (is_null($no_locale)) { | ||
$no_locale = !config('settings.auto_locale', false); | ||
} | ||
|
||
static $settings; | ||
|
||
if ($key === 'fresh') { | ||
Cache::forget('settings'); | ||
$settings = null; | ||
return true; | ||
} | ||
|
||
if (is_null($settings)) { | ||
$time = 24 * 60; | ||
|
||
$settings = Cache::remember('settings', $time, function () { | ||
$result = []; | ||
|
||
foreach (Kolirt\Settings\Models\Setting::all()->groupBy('group') as $group => $item) { | ||
foreach ($item as $setting) { | ||
$result[$group][$setting->key] = $setting->value; | ||
} | ||
} | ||
|
||
return $result; | ||
}); | ||
} | ||
|
||
$result = $settings; | ||
|
||
if (!is_null($key)) { | ||
foreach (explode('.', $key) as $key) { | ||
if (isset($result[$key])) { | ||
$result = $result[$key]; | ||
} else { | ||
$result = $default; | ||
break; | ||
} | ||
} | ||
} | ||
|
||
if (!$no_locale) { | ||
if (isset($result[app()->getLocale()])) { | ||
$result = $result[app()->getLocale()]; | ||
} | ||
} | ||
|
||
if (config('settings.response', 'object') === 'array') { | ||
return $result; | ||
} else if (config('settings.response', 'object') === 'object') { | ||
return json_decode(json_encode($result)); | ||
} else if (config('settings.response', 'object') === 'collect') { | ||
return collect($result); | ||
} | ||
} | ||
} | ||
|
||
if (!function_exists('is_json')) { | ||
function is_json($string) | ||
{ | ||
json_decode($string); | ||
return (json_last_error() == JSON_ERROR_NONE); | ||
} | ||
} |