Skip to content

Commit ab49016

Browse files
committed
Initial commit
1 parent e3aca4a commit ab49016

File tree

7 files changed

+304
-0
lines changed

7 files changed

+304
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/vendor

README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# laravel-settings
2+
The laravel settings package let you save your settings in the database and access them easily.
3+
4+
## install
5+
Add this package to your project using composer<br>
6+
`composer require mjpakzad/laravel-settings`
7+
8+
Publish the migration file<br>
9+
`php artisan vendor:publish --provider="Mjpakzad\Settings\SettingsServiceProvider"`
10+
11+
Migrate the new migration to create settings table<br>
12+
`php artisan migrate`
13+
14+
## Usage
15+
Create a new setting<br>
16+
`set_setting($key, $value, $group, $autoload)`
17+
18+
You can group settings for example `footer-settings` or `seo` or anything else, default value of `$group` is `null`.<br>
19+
Also, you can autoload some settings to load in your AppServiceProvider, default value of `$autoload` is `false`.
20+
examples<br>
21+
`set_setting('site-name', 'Laravel settings')`<br>
22+
`set_setting('blog-title', 'My personal blog', 'blog', true)`
23+
24+
Get a value by key<br>
25+
`get_setting('site_name')`
26+
27+
Get values by group<br>
28+
`setting_group('blog')`
29+
30+
Get autoload settings<br>
31+
`setting_autoload($autoload)`<br>
32+
default value of `$autoload` is `true`
33+
34+
Also, you can bring all settings out to config.<br>
35+
`setting_config()`<br>
36+
This function save all settings as key-value pairs in config, you can filter settings by parameters like below:<br>
37+
`setting_config($keys, $groups, $autoload)`<br>
38+
All parameters have `null` value.
39+
40+
Now, you can access them by `config()` method:<br>
41+
`config('settings')`

composer.json

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"name": "mjpakzad/laravel-settings",
3+
"description": "The laravel settings package let you save your settings in the database and access them easily.",
4+
"homepage": "https://github.com/mjpakzad/laravel-settings",
5+
"license": "MIT",
6+
"authors": [
7+
{
8+
"name": "Mj Pakzad",
9+
"email": "[email protected]"
10+
}
11+
],
12+
"keywords": ["laravel-settings"],
13+
"autoload": {
14+
"files": [
15+
"src/helper.php"
16+
],
17+
"psr-4": {
18+
"Mjpakzad\\LaravelSettings\\": "src/"
19+
}
20+
},
21+
"minimum-stability": "dev",
22+
"require": {},
23+
"extra": {
24+
"laravel": {
25+
"providers": [
26+
"Mjpakzad\\LaravelSettings\\SettingServiceProvider"
27+
]
28+
}
29+
}
30+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
use Mjpakzad\LaravelSettings\Models\Setting;
7+
8+
return new class extends Migration
9+
{
10+
/**
11+
* Run the migrations.
12+
*
13+
* @return void
14+
*/
15+
public function up()
16+
{
17+
Schema::create('settings', function (Blueprint $table) {
18+
$table->id();
19+
$table->string('key');
20+
$table->text('value')->nullable();
21+
$table->string('group')->index()->nullable();
22+
$table->boolean('autoload')->default(Setting::MANUAL);
23+
$table->timestamps();
24+
});
25+
}
26+
27+
/**
28+
* Reverse the migrations.
29+
*
30+
* @return void
31+
*/
32+
public function down()
33+
{
34+
Schema::dropIfExists('settings');
35+
}
36+
};

src/Models/Setting.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace Mjpakzad\LaravelSettings\Models;
4+
5+
use Illuminate\Database\Eloquent\Factories\HasFactory;
6+
use Illuminate\Database\Eloquent\Model;
7+
8+
/**
9+
* @method string group()
10+
* @method string groups()
11+
* @method string manual()
12+
* @method string autoload()
13+
*/
14+
class Setting extends Model
15+
{
16+
use HasFactory;
17+
18+
public const MANUAL = false;
19+
public const AUTOLOAD = true;
20+
21+
protected $fillable = ['key', 'value', 'group', 'autoload'];
22+
23+
public function scopeGroup($query, $group)
24+
{
25+
return $query->whereGroup($group);
26+
}
27+
28+
public function scopeGroups($query, $groups)
29+
{
30+
return $query->whereIn('group', $groups);
31+
}
32+
33+
public function scopeManual($query)
34+
{
35+
return $query->whereAutoload(self::MANUAL);
36+
}
37+
38+
public function scopeAutoload($query)
39+
{
40+
return $query->whereAutoload(self::AUTOLOAD);
41+
}
42+
}

src/SettingServiceProvider.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Mjpakzad\LaravelSettings;
4+
5+
use Illuminate\Support\ServiceProvider;
6+
7+
class SettingServiceProvider extends ServiceProvider
8+
{
9+
/**
10+
* Register services.
11+
*
12+
* @return void
13+
*/
14+
public function register()
15+
{
16+
//
17+
}
18+
19+
/**
20+
* Bootstrap services.
21+
*
22+
* @return void
23+
*/
24+
public function boot()
25+
{
26+
if($this->app->runningInConsole()) {
27+
if(!class_exists('CreateSettingTable')) {
28+
$this->publishes([
29+
__DIR__ . '/../database/migrations/create_settings_table.php.stub' =>
30+
database_path('migrations/' . date('Y_m_d_His', time()) . '_create_settings_table.php'),
31+
], 'migrations');
32+
}
33+
}
34+
}
35+
}

src/helper.php

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
<?php
2+
if (!function_exists('get_settings')) {
3+
/**
4+
* Get all autoload settings
5+
*/
6+
function get_settings($keys = null, $groups = null, $autoload = null) {
7+
$settings = \Mjpakzad\LaravelSettings\Models\Setting::query();
8+
if($keys !== null) {
9+
if(is_array($keys)) {
10+
$settings->whereIn('key', $keys);
11+
} else {
12+
$settings->where('key', $keys);
13+
}
14+
}
15+
if($groups !== null) {
16+
if(is_array($groups)) {
17+
$settings->whereIn('group', $groups);
18+
} else {
19+
$settings->where('group', $groups);
20+
}
21+
}
22+
if($autoload !== null) {
23+
if($autoload) {
24+
$settings->autoload();
25+
} else {
26+
$settings->manual();
27+
}
28+
}
29+
return $settings->pluck('value', 'key');
30+
}
31+
}
32+
33+
if (!function_exists('get_setting')) {
34+
/**
35+
* Get a specific setting
36+
*/
37+
function get_setting($key) {
38+
return \Mjpakzad\LaravelSettings\Models\Setting::where('key', $key)->value('value');
39+
}
40+
}
41+
42+
if (!function_exists('set_setting')) {
43+
/**
44+
* Create a new setting.
45+
*
46+
* @return void
47+
*/
48+
function set_setting($key, $value, $group = null, $autoload = null) {
49+
$setting = [
50+
'key' => $key,
51+
'value' => $value,
52+
'autoload' => $autoload !== null ? $autoload : false,
53+
];
54+
if($group !== null) {
55+
$setting['group'] = $group;
56+
}
57+
\Mjpakzad\LaravelSettings\Models\Setting::create($setting);
58+
}
59+
}
60+
61+
if (!function_exists('setting_config')) {
62+
/**
63+
* Save settings in config.
64+
*
65+
* @return void
66+
*/
67+
function setting_config($keys = null, $groups = null, $autoload = null) {
68+
config()->set('settings', get_settings($keys, $groups, $autoload)->toArray());
69+
}
70+
}
71+
72+
if(!function_exists('settings')) {
73+
/**
74+
* Creates a new setting or return settings.
75+
*
76+
* @param $key
77+
* @param $value
78+
* @param null $group
79+
* @param null $autoload
80+
*
81+
* @return \Illuminate\Support\Collection|void
82+
* @example setting('key'|['',], 'group'|['',], true)
83+
*
84+
* @example setting('site_name', 'laravel settings', 'site settings', true) creates a new settings.
85+
*/
86+
function setting($key, $value, $group = null, $autoload = null) {
87+
if($autoload !== null) {
88+
set_setting($key, $value, $group, $autoload);
89+
} else {
90+
return get_settings($key, $value, $autoload);
91+
}
92+
}
93+
}
94+
95+
if(!function_exists('setting_group')) {
96+
/**
97+
* Returns a group of settings.
98+
*
99+
* @param array|string $group The name of group or groups
100+
*/
101+
function setting_group(array|string $group): \Illuminate\Support\Collection
102+
{
103+
return get_settings(null, $group);
104+
}
105+
}
106+
107+
if(!function_exists('setting_autoload')) {
108+
/**
109+
* Return autoload or manual settings
110+
*
111+
* @param bool $autoload
112+
*
113+
* @return \Illuminate\Support\Collection
114+
*/
115+
function setting_autoload(bool $autoload = true): \Illuminate\Support\Collection
116+
{
117+
return get_settings(null, null, $autoload);
118+
}
119+
}

0 commit comments

Comments
 (0)