Skip to content

Commit

Permalink
setting can now be organize into groups (#5)
Browse files Browse the repository at this point in the history
* setting can now be organized into groups
  • Loading branch information
saqueib authored Aug 1, 2019
1 parent 42dacdc commit 56df912
Show file tree
Hide file tree
Showing 6 changed files with 210 additions and 6 deletions.
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,28 @@ settings()->has($key);
settings()->remove($key);
```

### Groups

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.

> If you are updating from previous version dont forget to run the migration
You have all above methods available just set you working group by calling `->group('group_name')` method and chain on:

```php
settings()->group('team.1')->set('app_name', 'My Team App');
settings()->group('team.1')->get('app_name');
> My Team App

settings()->group('team.2')->set('app_name', 'My Team 2 App');
settings()->group('team.2')->get('app_name');
> My Team 2 App

// You can use facade
\Settings::group('team.1')->get('app_name')
> My Team App
```

### Changelog

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.
Expand Down
5 changes: 5 additions & 0 deletions src/Setting/Setting.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,9 @@ class Setting extends Model
protected $guarded = ['updated_at', 'id'];

protected $table = 'settings';

public function scopeGroup($query, $groupName)
{
return $query->whereGroup($groupName);
}
}
58 changes: 52 additions & 6 deletions src/Setting/SettingEloquentStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,17 @@
namespace QCod\Settings\Setting;

use Illuminate\Support\Facades\Cache;
use Illuminate\Database\Eloquent\Builder;

class SettingEloquentStorage implements SettingStorage
{
/**
* Group name.
*
* @var string
*/
protected $settingsGroupName = 'default';

/**
* Cache key.
*
Expand All @@ -19,11 +27,11 @@ class SettingEloquentStorage implements SettingStorage
public function all($fresh = false)
{
if ($fresh) {
return $this->getSettingModel()->pluck('val', 'name');
return $this->modelQuery()->pluck('val', 'name');
}

return Cache::rememberForever($this->settingsCacheKey, function () {
return $this->getSettingModel()->pluck('val', 'name');
return Cache::rememberForever($this->getSettingsCacheKey(), function () {
return $this->modelQuery()->pluck('val', 'name');
});
}

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

$setting = $this->getSettingModel()->firstOrNew(['name' => $key]);
$setting = $this->getSettingModel()->firstOrNew([
'name' => $key,
'group' => $this->settingsGroupName,
]);

$setting->val = $val;
$setting->group = $this->settingsGroupName;
$setting->save();

$this->flushCache();
Expand Down Expand Up @@ -83,16 +96,49 @@ public function remove($key)
*/
public function flushCache()
{
return Cache::forget($this->settingsCacheKey);
return Cache::forget($this->getSettingsCacheKey());
}

/**
* Get settings cache key.
*
* @return string
*/
protected function getSettingsCacheKey()
{
return $this->settingsCacheKey.'.'.$this->settingsGroupName;
}

/**
* Get settings eloquent model.
*
* @return \Illuminate\Database\Eloquent\Builder
* @return Builder
*/
protected function getSettingModel()
{
return app('\QCod\Settings\Setting\Setting');
}

/**
* Get the model query builder.
*
* @return Builder
*/
protected function modelQuery()
{
return $this->getSettingModel()->group($this->settingsGroupName);
}

/**
* Set the group name for settings.
*
* @param string $groupName
* @return $this
*/
public function group($groupName)
{
$this->settingsGroupName = $groupName;

return $this;
}
}
8 changes: 8 additions & 0 deletions src/Setting/SettingStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,12 @@ public function remove($key);
* @return bool
*/
public function flushCache();

/**
* Set the group name for settings.
*
* @param string $groupName
* @return $this
*/
public function group($groupName);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class AddGroupColumnOnSettingsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('settings', function (Blueprint $table) {
$table->dropUnique('settings_name_unique');
$table->string('group')->default('default');
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('settings', function (Blueprint $table) {
$table->dropColumn('group');
});
}
}
90 changes: 90 additions & 0 deletions tests/StorageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace QCod\Settings\Tests\Feature;

use QCod\Settings\Tests\TestCase;
use QCod\Settings\Setting\Setting;
use QCod\Settings\Setting\SettingEloquentStorage;
use Illuminate\Foundation\Testing\RefreshDatabase;

Expand Down Expand Up @@ -172,4 +173,93 @@ public function it_can_access_setting_via_facade()

$this->assertDatabaseHas('settings', ['name' => 'app_name']);
}

/**
* it has a default group name for settings
*
* @test
*/
public function it_has_a_default_group_name_for_settings()
{
settings()->set('app_name', 'Cool App');

$this->assertDatabaseHas('settings', [
'name' => 'app_name',
'val' => 'Cool App',
'group' => 'default'
]);
}

/**
* it can store setting with a group name
*
* @test
*/
public function it_can_store_setting_with_a_group_name()
{
settings()->group('set1')->set('app_name', 'Cool App');

$this->assertDatabaseHas('settings', [
'name' => 'app_name',
'val' => 'Cool App',
'group' => 'set1'
]);
}

/**
* it can get setting from a group
*
* @test
*/
public function it_can_get_setting_from_a_group()
{
settings()->group('set1')->set('app_name', 'Cool App');

$this->assertTrue(settings()->group('set1')->has('app_name'));
$this->assertEquals('Cool App', settings()->group('set1')->get('app_name'));
$this->assertFalse(settings()->group('set2')->has('app_name'));
}

/**
* it give you all settings from default group if you dont specify one
*
* @test
*/
public function it_give_you_all_settings_from_default_group_if_you_dont_specify_one()
{
settings()->set('app_name', 'Cool App 1');
settings()->set('app_name', 'Cool App 2');

$this->assertCount(1, settings()->all(true));
$this->assertCount(0, settings()->group('unknown')->all(true));
}

/**
* it allows same key to be used in different groups
*
* @test
*/
public function it_allows_same_key_to_be_used_in_different_groups()
{
settings()->group('team1')->set('app_name', 'Cool App 1');
settings()->group('team2')->set('app_name', 'Cool App 2');

$this->assertCount(2, Setting::all());
$this->assertEquals('Cool App 1', settings()->group('team1')->get('app_name'));
$this->assertEquals('Cool App 2', settings()->group('team2')->get('app_name'));
}

/**
* it get group settings using facade
*
* @test
*/
public function it_get_group_settings_using_facade()
{
\Settings::group('team1')->set('app_name', 'Cool App');

$this->assertEquals('Cool App', \Settings::group('team1')->get('app_name'));

$this->assertDatabaseHas('settings', ['name' => 'app_name', 'group' => 'team1']);
}
}

0 comments on commit 56df912

Please sign in to comment.