Skip to content

Commit 56df912

Browse files
authored
setting can now be organize into groups (#5)
* setting can now be organized into groups
1 parent 42dacdc commit 56df912

File tree

6 files changed

+210
-6
lines changed

6 files changed

+210
-6
lines changed

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,28 @@ settings()->has($key);
7272
settings()->remove($key);
7373
```
7474

75+
### Groups
76+
77+
From `v 1.0.6` You can organize your settings into groups. If you skip the group name it will store settings with `default` group name.
78+
79+
> If you are updating from previous version dont forget to run the migration
80+
81+
You have all above methods available just set you working group by calling `->group('group_name')` method and chain on:
82+
83+
```php
84+
settings()->group('team.1')->set('app_name', 'My Team App');
85+
settings()->group('team.1')->get('app_name');
86+
> My Team App
87+
88+
settings()->group('team.2')->set('app_name', 'My Team 2 App');
89+
settings()->group('team.2')->get('app_name');
90+
> My Team 2 App
91+
92+
// You can use facade
93+
\Settings::group('team.1')->get('app_name')
94+
> My Team App
95+
```
96+
7597
### Changelog
7698

7799
Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

src/Setting/Setting.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,9 @@ class Setting extends Model
99
protected $guarded = ['updated_at', 'id'];
1010

1111
protected $table = 'settings';
12+
13+
public function scopeGroup($query, $groupName)
14+
{
15+
return $query->whereGroup($groupName);
16+
}
1217
}

src/Setting/SettingEloquentStorage.php

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,17 @@
33
namespace QCod\Settings\Setting;
44

55
use Illuminate\Support\Facades\Cache;
6+
use Illuminate\Database\Eloquent\Builder;
67

78
class SettingEloquentStorage implements SettingStorage
89
{
10+
/**
11+
* Group name.
12+
*
13+
* @var string
14+
*/
15+
protected $settingsGroupName = 'default';
16+
917
/**
1018
* Cache key.
1119
*
@@ -19,11 +27,11 @@ class SettingEloquentStorage implements SettingStorage
1927
public function all($fresh = false)
2028
{
2129
if ($fresh) {
22-
return $this->getSettingModel()->pluck('val', 'name');
30+
return $this->modelQuery()->pluck('val', 'name');
2331
}
2432

25-
return Cache::rememberForever($this->settingsCacheKey, function () {
26-
return $this->getSettingModel()->pluck('val', 'name');
33+
return Cache::rememberForever($this->getSettingsCacheKey(), function () {
34+
return $this->modelQuery()->pluck('val', 'name');
2735
});
2836
}
2937

@@ -49,8 +57,13 @@ public function set($key, $val = null)
4957
return true;
5058
}
5159

52-
$setting = $this->getSettingModel()->firstOrNew(['name' => $key]);
60+
$setting = $this->getSettingModel()->firstOrNew([
61+
'name' => $key,
62+
'group' => $this->settingsGroupName,
63+
]);
64+
5365
$setting->val = $val;
66+
$setting->group = $this->settingsGroupName;
5467
$setting->save();
5568

5669
$this->flushCache();
@@ -83,16 +96,49 @@ public function remove($key)
8396
*/
8497
public function flushCache()
8598
{
86-
return Cache::forget($this->settingsCacheKey);
99+
return Cache::forget($this->getSettingsCacheKey());
100+
}
101+
102+
/**
103+
* Get settings cache key.
104+
*
105+
* @return string
106+
*/
107+
protected function getSettingsCacheKey()
108+
{
109+
return $this->settingsCacheKey.'.'.$this->settingsGroupName;
87110
}
88111

89112
/**
90113
* Get settings eloquent model.
91114
*
92-
* @return \Illuminate\Database\Eloquent\Builder
115+
* @return Builder
93116
*/
94117
protected function getSettingModel()
95118
{
96119
return app('\QCod\Settings\Setting\Setting');
97120
}
121+
122+
/**
123+
* Get the model query builder.
124+
*
125+
* @return Builder
126+
*/
127+
protected function modelQuery()
128+
{
129+
return $this->getSettingModel()->group($this->settingsGroupName);
130+
}
131+
132+
/**
133+
* Set the group name for settings.
134+
*
135+
* @param string $groupName
136+
* @return $this
137+
*/
138+
public function group($groupName)
139+
{
140+
$this->settingsGroupName = $groupName;
141+
142+
return $this;
143+
}
98144
}

src/Setting/SettingStorage.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,12 @@ public function remove($key);
5555
* @return bool
5656
*/
5757
public function flushCache();
58+
59+
/**
60+
* Set the group name for settings.
61+
*
62+
* @param string $groupName
63+
* @return $this
64+
*/
65+
public function group($groupName);
5866
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
use Illuminate\Support\Facades\Schema;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Database\Migrations\Migration;
6+
7+
class AddGroupColumnOnSettingsTable extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::table('settings', function (Blueprint $table) {
17+
$table->dropUnique('settings_name_unique');
18+
$table->string('group')->default('default');
19+
});
20+
}
21+
22+
/**
23+
* Reverse the migrations.
24+
*
25+
* @return void
26+
*/
27+
public function down()
28+
{
29+
Schema::table('settings', function (Blueprint $table) {
30+
$table->dropColumn('group');
31+
});
32+
}
33+
}

tests/StorageTest.php

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace QCod\Settings\Tests\Feature;
44

55
use QCod\Settings\Tests\TestCase;
6+
use QCod\Settings\Setting\Setting;
67
use QCod\Settings\Setting\SettingEloquentStorage;
78
use Illuminate\Foundation\Testing\RefreshDatabase;
89

@@ -172,4 +173,93 @@ public function it_can_access_setting_via_facade()
172173

173174
$this->assertDatabaseHas('settings', ['name' => 'app_name']);
174175
}
176+
177+
/**
178+
* it has a default group name for settings
179+
*
180+
* @test
181+
*/
182+
public function it_has_a_default_group_name_for_settings()
183+
{
184+
settings()->set('app_name', 'Cool App');
185+
186+
$this->assertDatabaseHas('settings', [
187+
'name' => 'app_name',
188+
'val' => 'Cool App',
189+
'group' => 'default'
190+
]);
191+
}
192+
193+
/**
194+
* it can store setting with a group name
195+
*
196+
* @test
197+
*/
198+
public function it_can_store_setting_with_a_group_name()
199+
{
200+
settings()->group('set1')->set('app_name', 'Cool App');
201+
202+
$this->assertDatabaseHas('settings', [
203+
'name' => 'app_name',
204+
'val' => 'Cool App',
205+
'group' => 'set1'
206+
]);
207+
}
208+
209+
/**
210+
* it can get setting from a group
211+
*
212+
* @test
213+
*/
214+
public function it_can_get_setting_from_a_group()
215+
{
216+
settings()->group('set1')->set('app_name', 'Cool App');
217+
218+
$this->assertTrue(settings()->group('set1')->has('app_name'));
219+
$this->assertEquals('Cool App', settings()->group('set1')->get('app_name'));
220+
$this->assertFalse(settings()->group('set2')->has('app_name'));
221+
}
222+
223+
/**
224+
* it give you all settings from default group if you dont specify one
225+
*
226+
* @test
227+
*/
228+
public function it_give_you_all_settings_from_default_group_if_you_dont_specify_one()
229+
{
230+
settings()->set('app_name', 'Cool App 1');
231+
settings()->set('app_name', 'Cool App 2');
232+
233+
$this->assertCount(1, settings()->all(true));
234+
$this->assertCount(0, settings()->group('unknown')->all(true));
235+
}
236+
237+
/**
238+
* it allows same key to be used in different groups
239+
*
240+
* @test
241+
*/
242+
public function it_allows_same_key_to_be_used_in_different_groups()
243+
{
244+
settings()->group('team1')->set('app_name', 'Cool App 1');
245+
settings()->group('team2')->set('app_name', 'Cool App 2');
246+
247+
$this->assertCount(2, Setting::all());
248+
$this->assertEquals('Cool App 1', settings()->group('team1')->get('app_name'));
249+
$this->assertEquals('Cool App 2', settings()->group('team2')->get('app_name'));
250+
}
251+
252+
/**
253+
* it get group settings using facade
254+
*
255+
* @test
256+
*/
257+
public function it_get_group_settings_using_facade()
258+
{
259+
\Settings::group('team1')->set('app_name', 'Cool App');
260+
261+
$this->assertEquals('Cool App', \Settings::group('team1')->get('app_name'));
262+
263+
$this->assertDatabaseHas('settings', ['name' => 'app_name', 'group' => 'team1']);
264+
}
175265
}

0 commit comments

Comments
 (0)