Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
kolirt committed Jul 27, 2023
0 parents commit cea8679
Show file tree
Hide file tree
Showing 9 changed files with 382 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/.idea
117 changes: 117 additions & 0 deletions README.md
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');
```
34 changes: 34 additions & 0 deletions composer.json
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"
}
}
}
}
7 changes: 7 additions & 0 deletions config/settings.php
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'),
];
33 changes: 33 additions & 0 deletions src/Commands/InstallCommand.php
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 src/Migrations/2019_04_20_085614_create_settings_table.php
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');
}
}
36 changes: 36 additions & 0 deletions src/Models/Setting.php
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;
}

}
35 changes: 35 additions & 0 deletions src/ServiceProvider.php
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);
}
}
83 changes: 83 additions & 0 deletions src/helpers.php
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);
}
}

0 comments on commit cea8679

Please sign in to comment.