diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a09c56d --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/.idea diff --git a/README.md b/README.md new file mode 100644 index 0000000..e105e7b --- /dev/null +++ b/README.md @@ -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'); +``` \ No newline at end of file diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..600bd62 --- /dev/null +++ b/composer.json @@ -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" + } + } + } +} diff --git a/config/settings.php b/config/settings.php new file mode 100644 index 0000000..7f59f9a --- /dev/null +++ b/config/settings.php @@ -0,0 +1,7 @@ + true, + 'response' => 'object', // array, object, collect + 'connection' => config('database.default', 'mysql'), +]; diff --git a/src/Commands/InstallCommand.php b/src/Commands/InstallCommand.php new file mode 100644 index 0000000..413ad9a --- /dev/null +++ b/src/Commands/InstallCommand.php @@ -0,0 +1,33 @@ +call('vendor:publish', ['--provider' => 'Kolirt\\Settings\\ServiceProvider']); + $this->call('migrate'); + } +} diff --git a/src/Migrations/2019_04_20_085614_create_settings_table.php b/src/Migrations/2019_04_20_085614_create_settings_table.php new file mode 100644 index 0000000..4cdfe47 --- /dev/null +++ b/src/Migrations/2019_04_20_085614_create_settings_table.php @@ -0,0 +1,36 @@ +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'); + } +} diff --git a/src/Models/Setting.php b/src/Models/Setting.php new file mode 100644 index 0000000..788f915 --- /dev/null +++ b/src/Models/Setting.php @@ -0,0 +1,36 @@ +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; + } + +} diff --git a/src/ServiceProvider.php b/src/ServiceProvider.php new file mode 100644 index 0000000..f873d2a --- /dev/null +++ b/src/ServiceProvider.php @@ -0,0 +1,35 @@ +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); + } +} \ No newline at end of file diff --git a/src/helpers.php b/src/helpers.php new file mode 100644 index 0000000..549520c --- /dev/null +++ b/src/helpers.php @@ -0,0 +1,83 @@ +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); + } +}