Skip to content

Commit 6638de9

Browse files
committed
feat: initial commit
1 parent 65bdfb4 commit 6638de9

File tree

11 files changed

+400
-1
lines changed

11 files changed

+400
-1
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
---
2+
name: Bug report
3+
about: Create a report to help us improve
4+
title: ''
5+
labels: ''
6+
assignees: ''
7+
8+
---
9+
10+
**Describe the bug**
11+
A clear and concise description of what the bug is.
12+
13+
**To Reproduce**
14+
Steps to reproduce the behavior:
15+
1. Go to '...'
16+
2. Click on '....'
17+
3. Scroll down to '....'
18+
4. See error
19+
20+
**Expected behavior**
21+
A clear and concise description of what you expected to happen.
22+
23+
**Screenshots**
24+
If applicable, add screenshots to help explain your problem.
25+
26+
**Desktop (please complete the following information):**
27+
- OS: [e.g. iOS]
28+
- Browser [e.g. chrome, safari]
29+
- Version [e.g. 22]
30+
31+
**Smartphone (please complete the following information):**
32+
- Device: [e.g. iPhone6]
33+
- OS: [e.g. iOS8.1]
34+
- Browser [e.g. stock browser, safari]
35+
- Version [e.g. 22]
36+
37+
**Additional context**
38+
Add any other context about the problem here.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
name: Feature request
3+
about: Suggest an idea for this project
4+
title: ''
5+
labels: ''
6+
assignees: ''
7+
8+
---
9+
10+
**Is your feature request related to a problem? Please describe.**
11+
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
12+
13+
**Describe the solution you'd like**
14+
A clear and concise description of what you want to happen.

.github/pull_request_template.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
### Description
2+
3+
Explain what are you submitting.
4+
5+
### Fixes
6+
7+
Enter the issue you are fixing (e.g. This fixes #1.).
8+
9+
10+
11+

.gitignore

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

README.md

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,85 @@
1-
# laravel-settingable
1+
# Laravel Settingable
2+
23
Endow your Laravel models with settings!
4+
5+
[![Latest version](https://img.shields.io/github/release/CodeTechPt/laravel-settingable?style=flat-square)](https://github.com/CodeTechPt/laravel-settingable/releases)
6+
[![GitHub license](https://img.shields.io/github/license/CodeTechPt/laravel-settingable?style=flat-square)](https://github.com/CodeTechPt/laravel-settingable/blob/master/LICENSE)
7+
8+
9+
## Installation
10+
11+
Add the package to your Laravel app using composer
12+
13+
```
14+
composer require codetech/laravel-settingable
15+
```
16+
17+
18+
### Service Provider
19+
20+
Register the package's service provider in config/app.php. In Laravel versions 5.5 and beyond, this step can be skipped if package auto-discovery is enabled.
21+
22+
```
23+
'providers' => [
24+
25+
...
26+
CodeTech\Settingable\Providers\SettingableServiceProvider::class,
27+
...
28+
29+
];
30+
```
31+
32+
33+
### Migrations
34+
35+
Execute the next Artisan command to run the migrations.
36+
37+
```
38+
php artisan migrate
39+
40+
```
41+
42+
43+
## Usage
44+
45+
To start logging CRUD operations simply use the trait on your models.
46+
47+
```
48+
49+
use CodeTechCMS\Settingable\Traits\Settingable;
50+
51+
class Theme extends Model
52+
{
53+
use Settingable;
54+
55+
...
56+
```
57+
58+
### Retrieve model settings
59+
60+
#### Querying the database
61+
```
62+
// Get all settings
63+
$settings = $theme->settings;
64+
65+
// Get settings from a specific scope
66+
$scopedSettings = $theme->settings()->ofScope('colors')->get();
67+
```
68+
69+
#### Using the config helper
70+
```
71+
config('theme.colors.primary');
72+
```
73+
74+
75+
---
76+
77+
78+
## License
79+
80+
**codetech/laravel-settingable** is open-sourced software licensed under the [MIT license](https://github.com/CodeTechPt/laravel-settingable/blob/master/LICENSE).
81+
82+
83+
## About CodeTech
84+
85+
[CodeTech](https://www.codetech.pt) is a web development agency based on Matosinhos, Portugal. Oh, and we LOVE Laravel!

composer.json

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"name": "codetech/laravel-settingable",
3+
"description": "Endow your Laravel models with settings!",
4+
"keywords": [
5+
"codetech",
6+
"laravel-settingable",
7+
"laravel",
8+
"settings",
9+
"polymorphic"
10+
],
11+
"authors": [
12+
{
13+
"name": "José Osório",
14+
"email": "jfrosorio@gmail.com",
15+
"role": "Developer"
16+
}
17+
],
18+
"license": "MIT",
19+
"require": {
20+
"php": "^7.2",
21+
"laravel/framework": "~6.0"
22+
},
23+
"autoload": {
24+
"psr-4": {
25+
"CodeTech\\Settingable\\": "src/"
26+
}
27+
},
28+
"minimum-stability": "dev",
29+
"prefer-stable": true,
30+
"extra": {
31+
"laravel": {
32+
"providers": [
33+
"CodeTech\\Settingable\\Providers\\SettingableServiceProvider"
34+
]
35+
}
36+
}
37+
}

src/Models/ModelSetting.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
namespace CodeTechCMS\Settingable\Models;
4+
5+
use Illuminate\Database\Eloquent\Builder;
6+
use Illuminate\Database\Eloquent\Model;
7+
8+
class ModelSetting extends Model
9+
{
10+
/**
11+
* @inheritDoc
12+
*/
13+
protected $fillable = [
14+
'path',
15+
'name',
16+
'scope',
17+
'value'
18+
];
19+
20+
21+
/*
22+
|--------------------------------------------------------------------------
23+
| Scopes
24+
|--------------------------------------------------------------------------
25+
*/
26+
27+
/**
28+
* Scope a query to only include media items of a given scope.
29+
*
30+
* @param Builder $query
31+
* @param string $scope
32+
* @return Builder
33+
*/
34+
public function scopeOfScope(Builder $query, string $scope)
35+
{
36+
return $query->where('scope', $scope);
37+
}
38+
39+
40+
/*
41+
|--------------------------------------------------------------------------
42+
| Relationships
43+
|--------------------------------------------------------------------------
44+
*/
45+
46+
/**
47+
* Get the owning commentable model.
48+
*/
49+
public function settingable()
50+
{
51+
return $this->morphTo();
52+
}
53+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php
2+
3+
namespace CodeTechCMS\Settingable\Providers;
4+
5+
use CodeTechCMS\Settingable\Models\ModelSetting;
6+
use Illuminate\Support\Facades\Config;
7+
use Illuminate\Support\ServiceProvider;
8+
9+
class SettingableServiceProvider extends ServiceProvider
10+
{
11+
/**
12+
* Register services.
13+
*
14+
* @return void
15+
*/
16+
public function register()
17+
{
18+
$this->mergeConfigFrom(
19+
__DIR__ . '/../config/settingable.php', 'settingable'
20+
);
21+
}
22+
23+
/**
24+
* Bootstrap services.
25+
*
26+
* @return void
27+
*/
28+
public function boot()
29+
{
30+
// Load migrations from custom path
31+
$this->loadMigrationsFrom(__DIR__ . '/../database/migrations');
32+
33+
$this->setPublishableFiles();
34+
35+
if (config('settingable.load_into_memory')) {
36+
$this->loadSettings();
37+
}
38+
}
39+
40+
/**
41+
* Set which files can be published.
42+
*/
43+
private function setPublishableFiles()
44+
{
45+
$this->publishes([
46+
__DIR__ . '/../config/settingable.php' => config_path('settingable.php')
47+
], 'config');
48+
49+
$this->publishes([
50+
__DIR__ . '/../database/migrations/' => database_path('migrations')
51+
], 'migrations');
52+
}
53+
54+
/**
55+
* Load settings into memory.
56+
*/
57+
private function loadSettings()
58+
{
59+
$settings = ModelSetting::select('root', 'path', 'value')->get();
60+
61+
$results = [];
62+
63+
foreach ($settings as $setting) {
64+
$fullPath = $setting->root . '.' . $setting->path;
65+
66+
$keys = explode('.', $fullPath);
67+
68+
$temp = &$results;
69+
70+
foreach ($keys as $key) {
71+
$temp = &$temp[$key];
72+
}
73+
74+
$temp = $setting->value;
75+
76+
unset($temp);
77+
}
78+
79+
foreach ($results as $key => $result) {
80+
if (!is_array($result)) {
81+
Config::set($key, $result);
82+
83+
continue;
84+
}
85+
86+
$configs = config($key) ? array_merge(config($key), $result) : $result;
87+
88+
Config::set($key, $configs);
89+
}
90+
}
91+
}

src/Traits/Settingable.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace CodeTechCMS\Settingable\Traits;
4+
5+
use CodeTechCMS\Settingable\Models\ModelSetting;
6+
7+
trait Settingable
8+
{
9+
/**
10+
* Get all of this model's settings.
11+
*/
12+
public function settings()
13+
{
14+
return $this->morphMany(ModelSetting::class, 'settingable');
15+
}
16+
17+
}

src/config/settingable.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
return [
4+
5+
/*
6+
|--------------------------------------------------------------------------
7+
| Load from database
8+
|--------------------------------------------------------------------------
9+
|
10+
| Determines whether settings should be loaded into memory. When set
11+
| to true, settings will be accessible using the config() helper.
12+
|
13+
*/
14+
15+
'load_into_memory' => true,
16+
17+
];

0 commit comments

Comments
 (0)