kenarkose / settlement
Framework Agnostic Persistent Typed and Group-able Settings with JSON Driver
Requires
- php: >=5.4.0
Requires (Dev)
- mikey179/vfsstream: ~1.5
- orchestra/testbench: ~3.0
- phpunit/phpunit: ~4.0
README
Framework Agnostic Persistent Typed and Group-able Settings with JSON Driver.
Settlement provides a simple solution for persistent settings with support for types and groups.
Features
- Flexible API for persistent settings
- Configurable JSON driver
- Grouped and typed settings
- Group and type validation
- Service provider for Laravel 5
- A phpunit test suite for easy development
Installation
Installing Settlement is simple. Just pull the package in through Composer.
{ "require": { "kenarkose/settlement": "~2.0" } }
Usage
Instantiate a new JSON repository with path and filename and you are good to go.
use Kenarkose\Settlement\Repository\JSONRepository; $settings = new JSONRepository('path/to', 'settings.json');
Here is a list of methods that are defined by the repository interface.
$settings->set('foo', 'bar'); $settings->set('foo', 42, null, 'number'); $settings->set('foo', 'bar', 'Label', 'text', 'baz_group'); $settings->get('foo'); // 'bar' $settings->getComplete('foo'); // ['value' => 'bar', 'label' =>'Label', 'type' => 'text', 'group' => 'baz_group'] $setting->has('foo'); // true $setting->delete('foo'); $setting->getGroup('baz_group'); // Name of the Baz Group $setting->setGroup('foo_group', 'Name of the Foo Group'); $setting->hasGroup('foo_group'); // true $setting->getGroupSettings('baz_group'); // ['foo' => ['value' => 'bar', 'label' => 'Label', 'type' => 'string', 'group' => 'baz_group']] $setting->deleteGroup('foo_group'); $setting->flush();
Additionally, you may use other methods that is provided by the JSONRepository.
$setting->path(); // Returns current path $setting->path('new/path'); // Sets a new path $setting->filename(); // Returns current filename $setting->filename('config.json'); // Sets a new filename $setting->settings(); // Returns all stored settings $setting->settings( ['foo' => ['value' => 'bar', 'type' => 'string', 'group' => null] ); // Replaces stored settings $setting->groups(); // Returns all stored groups $setting->groups( ['dummy' => 'Dummy Group'] ); // Replaces stored groups $setting->stored(); // Returns all stored information $setting->stored([ 'settings' => [...], 'groups' => [...] ]); // Replaces all stored information $setting->load(); // Loads from current path and filename $setting->load('new/path', 'config.json'); // Loads from new path and filename $setting->save(); // Saves to current path and filename $setting->save('new/path', 'config.json'); // Saves to new path and filename
For more options you may refer to source code and tests as Settlement is well tested and documented.
For Laravel Users
The Settlement Service Provider for Laravel 5 registers the default LaravelJSONRepository, the Facade, and the settings() helper.
Laravel Installation
-
In order to register Settlement Service Provider add
'Kenarkose\Settlement\Provider\Laravel\SettlementServiceProvider'
to the end ofproviders
array in yourconfig/app.php
file.'providers' => array( 'Illuminate\Foundation\Providers\ArtisanServiceProvider', 'Illuminate\Auth\AuthServiceProvider', ... 'Kenarkose\Settlement\Provider\Laravel\SettlementServiceProvider', ),
-
You may access the services provided by Settlement by using the supplied Facade.
Settings::get('foo'); Settings::set('bar', 'baz');
In order to register the Facade add
'Settings' => 'Kenarkose\Settlement\Provider\Laravel\Settings'
to the end ofaliases
array in yourconfig/app.php
file.'aliases' => array( 'App' => 'Illuminate\Support\Facades\App', 'Artisan' => 'Illuminate\Support\Facades\Artisan', ... 'Settings' => 'Kenarkose\Settlement\Provider\Laravel\Settings', ),
-
A helper function is also provided if the Facade is not preferred.
settings(); // Returns the repository // Getting a value settings('foo'); // is the same as settings()->get('foo'); // Setting a value settings('foo', 'bar'); // is the same as settings()->set('foo', 'bar'); // Other possible derivations of setting settings('foo', 'bar', 'string'); settings('foo', 'bar', 'string', 'group');
-
Finally, you may customize Settlement's behavior by publishing the configuration file. To do so, use the following command.
php artisan vendor:publish
Than, you will find the configuration file on the
config/settlement.php
path.
License
Settlement is released under MIT License.