├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── composer.json ├── nbproject ├── project.properties └── project.xml ├── phpunit.xml ├── src ├── Facades │ └── Setting.php ├── Setting.php ├── SettingServiceProvider.php ├── config │ └── setting.php └── interfaces │ ├── FallbackInterface.php │ └── LaravelFallbackInterface.php └── tests └── SettingTest.php /.gitignore: -------------------------------------------------------------------------------- 1 | /vendor 2 | composer.phar 3 | composer.lock 4 | .DS_Store 5 | /nbproject/private/ -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: php 2 | 3 | php: 4 | - 5.4 5 | - 5.5 6 | - 5.6 7 | 8 | before_script: 9 | - curl -s http://getcomposer.org/installer | php 10 | - php composer.phar install --dev 11 | 12 | script: phpunit -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | # The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Eric Leroy 4 | 5 | > Permission is hereby granted, free of charge, to any person obtaining a copy 6 | > of this software and associated documentation files (the "Software"), to deal 7 | > in the Software without restriction, including without limitation the rights 8 | > to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | > copies of the Software, and to permit persons to whom the Software is 10 | > furnished to do so, subject to the following conditions: 11 | > 12 | > The above copyright notice and this permission notice shall be included in 13 | > all copies or substantial portions of the Software. 14 | > 15 | > THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | > IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | > FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | > AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | > LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | > OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | > THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | [![Build Status](https://travis-ci.org/thetispro/laravel5-setting.svg)](https://travis-ci.org/thetispro/laravel5-setting) 3 | 4 | # Laravel 5 Setting 5 | 6 | ##Introduction 7 | This package is a fork of philf/setting, updated to work with Laravel 5.x. 8 | 9 | ##Description 10 | Persistent configuration settings for Laravel 5.x - Create, Read, Update and Delete settings stored in files using JSON. 11 | 12 | As Philf said : 13 | 14 | This package was the result of me not being able to save new settings to config files in a more persistent way. 15 | 16 | This package was designed not to replace the config solution currently offered by Laravel but rather complement it and be used in unison with it. 17 | 18 | By default the data is stored in base_path().'/storage/meta/setting.json' but this can be easily changed either in the config file or on the fly in realtime. 19 | 20 | This package also provides a fallback for the Laravel Config facade, you can set it in the config, if the key is not found in the json file it will look it up in the Config facade. 21 | 22 | ## Contributors 23 | For philf/setting: 24 | janhartigan (Treeface) 25 | Nils Plaschke (Chumper) 26 | 27 | ##Laravel 4.x users 28 | Please use philf/setting 29 | 30 | ## Installation 31 | Require this package in your composer.json: 32 | 33 | #### For Laravel 5 -> 5.3 34 | "thetispro/laravel5-setting": "1.0.x-dev" 35 | 36 | #### For Laravel 5.4 37 | "thetispro/laravel5-setting": "1.1.x-dev" 38 | 39 | Add the ServiceProvider to the 'providers' array in config/app.php 40 | 41 | 'providers' => [ 42 | // ... Illuminate Providers 43 | // ... App 44 | Thetispro\Setting\SettingServiceProvider::class, 45 | ], 46 | 47 | If you want to use the facade, add the alias to the 'aliases' array in config/app.php 48 | 49 | 'aliases' => [ 50 | // ... Illuminate Facades 51 | 'Setting' => Thetispro\Setting\Facades\Setting::class, 52 | ] 53 | 54 | Finaly, publish the config file 55 | 56 | $ php artisan vendor:publish 57 | 58 | ## Usage 59 | No other change from philf/setting; 60 | 61 | ##Config 62 | 63 | return array( 64 | 'path' => base_path('storage/meta'), 65 | 'filename' => 'setting.json', 66 | 'fallback' => true, 67 | ); 68 | 69 | ##Fallback capability built in. 70 | // Automatic fallback to Laravel config 71 | Setting::get('app.locale'); 72 | 73 | ##Single dimension 74 | 75 | set: Setting::set('name', 'Phil') 76 | get: Setting::get('name') 77 | forget: Setting::forget('name') 78 | has: Setting::has('name') 79 | 80 | #Multi dimensional 81 | 82 | set: Setting::set('names.firstName', 'Phil') 83 | set: Setting::set('names.surname', 'F') 84 | or 85 | set: Setting::set('names', array('firstName' => 'Phil', 'surname' => 'F')) 86 | setArray: Setting::setArray(array('firstName' => 'Phil', 'surname' => 'F')) 87 | get: Setting::get('names.firstName') 88 | forget: Setting::forget('names.surname')) 89 | has: Setting::has('names.firstName') 90 | 91 | #Array processing 92 | // Get all of the entries in the names array 93 | $names = Setting::get('names'); 94 | foreach ($names as $key => $val) 95 | { 96 | ... 97 | } 98 | 99 | // Get the whole array 100 | $everything = Setting::get(); 101 | 102 | You can also clear the JSON file with the clear command 103 | 104 | clear: Setting::clear() 105 | 106 | Using a different path (make sure the path exists and is writable) * 107 | 108 | Setting::path(app_path().'/storage/meta/sub')->set('names2', array('firstName' => 'Phil', 'surname' => 'F')); 109 | 110 | Using a different filename 111 | 112 | Setting::filename('setting2.json')->set('names2', array('firstName' => 'Phil', 'surname' => 'F')); 113 | 114 | Using both a different path and filename (make sure the path exists and is writable) 115 | 116 | Setting::path(app_path().'/storage/meta/sub')->filename('dummy.json')->set('names2', array('firstName' => 'Phil', 'surname' => 'F')); 117 | 118 | ## License 119 | 120 | Laravel 5 Setting is open-sourced software licensed under the [MIT license](http://opensource.org/licenses/MIT) 121 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "thetispro/laravel5-setting", 3 | "description": "Persistent configuration settings for Laravel 5 - Create, Read, Update and Delete settings stored in files using JSON", 4 | "homepage": "https://github.com/thetispro/laravel5-setting", 5 | "keywords": ["laravel", "config", "setting"], 6 | "license": "MIT", 7 | "authors": [ 8 | { 9 | "name": "Phil Foulston", 10 | "email": "info@weztec.com" 11 | }, 12 | { 13 | "name": "Eric Leroy", 14 | "email": "thetispro@me.com" 15 | } 16 | ], 17 | "require": { 18 | "php": ">=5.4.0", 19 | "illuminate/support": "~5" 20 | }, 21 | "require-dev": { 22 | "phpunit/phpunit": "3.7.*" 23 | }, 24 | "autoload": { 25 | "psr-4": { 26 | "Thetispro\\Setting\\": "src/" 27 | } 28 | }, 29 | "minimum-stability": "dev" 30 | } -------------------------------------------------------------------------------- /nbproject/project.properties: -------------------------------------------------------------------------------- 1 | include.path=${php.global.include.path} 2 | php.version=PHP_54 3 | source.encoding=UTF-8 4 | src.dir=. 5 | tags.asp=false 6 | tags.short=false 7 | web.root=. 8 | -------------------------------------------------------------------------------- /nbproject/project.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | org.netbeans.modules.php.project 4 | 5 | 6 | laravel5-setting 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /phpunit.xml: -------------------------------------------------------------------------------- 1 | 2 | 13 | 14 | 15 | ./tests/ 16 | 17 | 18 | -------------------------------------------------------------------------------- /src/Facades/Setting.php: -------------------------------------------------------------------------------- 1 | 'Phil', 'surname' => 'F')) 27 | * setArray: Setting::setArray(array('firstName' => 'Phil', 'surname' => 'F')) 28 | * get: Setting::get('names.firstName') 29 | * forget: Setting::forget('names.surname')) 30 | * has: Setting::has('names.firstName') 31 | * 32 | * Clear: 33 | * clear: Setting::clear() 34 | * 35 | * Using a different path (make sure the path exists and is writable) * 36 | * Setting::path('setting2.json')->set(array('names2' => array('firstName' => 'Phil', 'surname' => 'F'))); 37 | * 38 | * Using a different filename 39 | * Setting::filename('setting2.json')->set(array('names2' => array('firstName' => 'Phil', 'surname' => 'F'))); 40 | * 41 | * Using both a different path and filename (make sure the path exists and is writable) 42 | * Setting::path(app_path().'/storage/meta/sub')->filename('dummy.json')->set(array('names2' => array('firstName' => 'Phil', 'surname' => 'F'))); 43 | */ 44 | 45 | /** 46 | * Class Setting 47 | * @package Thetispro\Setting 48 | */ 49 | class Setting { 50 | 51 | /** 52 | * The path to the file 53 | * @var string 54 | */ 55 | protected $path; 56 | 57 | /** 58 | * The filename used to store the config 59 | * @var string 60 | */ 61 | protected $filename; 62 | 63 | /** 64 | * The class working array 65 | * @var array 66 | */ 67 | protected $settings; 68 | 69 | /** 70 | * Create the Setting instance 71 | * @param string $path The path to the file 72 | * @param string $filename The filename 73 | * @param interfaces\FallbackInterface $fallback 74 | */ 75 | public function __construct($path, $filename, $fallback = null) 76 | { 77 | $this->path = $path; 78 | $this->filename = $filename; 79 | $this->fallback = $fallback; 80 | 81 | // Load the file and store the contents in $this->settings 82 | $this->load($this->path, $this->filename); 83 | } 84 | 85 | /** 86 | * Set the path to the file to use 87 | * @param string $path The path to the file 88 | * @return \Thetispro\Setting\Setting 89 | */ 90 | public function path($path) 91 | { 92 | $this->path = $path; 93 | return $this; 94 | } 95 | 96 | /** 97 | * Set the filename to use 98 | * @param string $filename The filename 99 | * @return \Thetispro\Setting\Setting 100 | */ 101 | public function filename($filename) 102 | { 103 | $this->filename = $filename; 104 | return $this; 105 | } 106 | 107 | /** 108 | * Get a value and return it 109 | * @param string $key String using dot notation 110 | * @param Mixed $default 111 | * @return Mixed The value(s) found 112 | */ 113 | public function get($key = null, $default = null) 114 | { 115 | if (empty($key)) 116 | { 117 | return $this->settings; 118 | } 119 | 120 | $ts = microtime(true); 121 | 122 | if($ts !== array_get($this->settings, $key, $ts)) 123 | { 124 | return array_get($this->settings, $key); 125 | } 126 | 127 | if ( ! is_null($this->fallback) and $this->fallback->fallbackHas($key)) 128 | { 129 | return $this->fallback->fallbackGet($key, $default); 130 | } 131 | 132 | return $default; 133 | } 134 | 135 | /** 136 | * Store the passed value in to the json file 137 | * @param $key 138 | * @param mixed $value The value(s) to be stored 139 | * @return void 140 | */ 141 | public function set($key, $value) 142 | { 143 | array_set($this->settings,$key,$value); 144 | $this->save($this->path, $this->filename); 145 | $this->load($this->path, $this->filename); 146 | } 147 | 148 | /** 149 | * Forget the value(s) currently stored 150 | * @param mixed $deleteKey The value(s) to be removed (dot notation) 151 | * @return void 152 | */ 153 | public function forget($deleteKey) 154 | { 155 | array_forget($this->settings,$deleteKey); 156 | $this->save($this->path, $this->filename); 157 | $this->load($this->path, $this->filename); 158 | } 159 | 160 | /** 161 | * Check to see if the value exists 162 | * @param string $searchKey The key to search for 163 | * @return boolean True: found - False not found 164 | */ 165 | public function has($searchKey) 166 | { 167 | $default = microtime(true); 168 | 169 | if($default == array_get($this->settings, $searchKey, $default) and !is_null($this->fallback)) 170 | { 171 | return $this->fallback->fallbackHas($searchKey); 172 | } 173 | return $default !== array_get($this->settings, $searchKey, $default); 174 | } 175 | 176 | /** 177 | * Load the file in to $this->settings so values can be used immediately 178 | * @param string $path The path to be used 179 | * @param string $filename The filename to be used 180 | * @return \Thetispro\Setting\Setting 181 | */ 182 | public function load($path = null, $filename = null) 183 | { 184 | $this->path = isset($path) ? $path : $this->path; 185 | $this->filename = isset($filename) ? $filename : $this->filename; 186 | 187 | if (is_file($this->path.'/'.$this->filename)) 188 | { 189 | $this->settings = json_decode(file_get_contents($this->path.'/'.$this->filename), true); 190 | } 191 | else 192 | { 193 | $this->settings = []; 194 | } 195 | 196 | return $this; 197 | } 198 | 199 | /** 200 | * Save the file 201 | * @param string $path The path to be used 202 | * @param string $filename The filename to be used 203 | * @return void 204 | */ 205 | public function save($path = null, $filename = null) 206 | { 207 | $this->path = isset($path) ? $path : $this->path; 208 | $this->filename = isset($filename) ? $filename : $this->filename; 209 | if ( ! file_exists($this->path)) 210 | { 211 | mkdir($this->path, 0755, true); 212 | } 213 | 214 | $fh = fopen($this->path.'/'.$this->filename, 'w+'); 215 | fwrite($fh, json_encode($this->settings)); 216 | fclose($fh); 217 | } 218 | 219 | /** 220 | * Clears the JSON Config file 221 | */ 222 | public function clear() 223 | { 224 | $this->settings = array(); 225 | $this->save($this->path, $this->filename); 226 | $this->load($this->path, $this->filename); 227 | } 228 | 229 | /** 230 | * This will mass assign data to the Setting 231 | * @param array $data 232 | */ 233 | public function setArray(array $data) 234 | { 235 | foreach ($data as $key => $value) 236 | { 237 | array_set($this->settings,$key,$value); 238 | } 239 | 240 | $this->save($this->path, $this->filename); 241 | $this->load($this->path, $this->filename); 242 | } 243 | } -------------------------------------------------------------------------------- /src/SettingServiceProvider.php: -------------------------------------------------------------------------------- 1 | publishes([ 33 | __DIR__ . '/config/setting.php' => config_path('setting.php'), 34 | ]); 35 | 36 | $this->app->bind(['setting' => 'Thetispro\Setting\Setting'], 37 | function($app) { 38 | return new Setting( 39 | config('setting.path'), config('setting.filename'), 40 | config('setting.fallback') ? new LaravelFallbackInterface() : null); 41 | }); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/config/setting.php: -------------------------------------------------------------------------------- 1 | base_path('storage/meta'), 5 | 'filename' => 'setting.json', 6 | 'fallback' => true, 7 | ]; 8 | -------------------------------------------------------------------------------- /src/interfaces/FallbackInterface.php: -------------------------------------------------------------------------------- 1 | get($key, $default); 17 | } 18 | 19 | /** 20 | * @param $key 21 | * @return bool 22 | */ 23 | public function fallbackHas($key) 24 | { 25 | $settingExists = \App::make('config')->has($key); 26 | 27 | $setting = \App::make('config')->get($key); 28 | if (is_array($setting) and count($setting) == 0) { 29 | return false; 30 | } 31 | 32 | return $settingExists; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /tests/SettingTest.php: -------------------------------------------------------------------------------- 1 | setting = new Setting(sys_get_temp_dir(), $this->file); 28 | $this->setting->clear(); 29 | } 30 | 31 | /** 32 | * Delete test files 33 | */ 34 | public function tearDown() 35 | { 36 | if (file_exists('test/' . $this->file)) 37 | unlink('test/' . $this->file); 38 | } 39 | 40 | public function testSet() 41 | { 42 | $this->setting->set('testCase.foo', 'bar'); 43 | $this->assertTrue($this->setting->has('testCase.foo')); 44 | $this->assertEquals('bar', $this->setting->get('testCase.foo')); 45 | $this->assertEquals(['foo' => 'bar'], $this->setting->get('testCase')); 46 | 47 | $this->setting->set('a.b', 'c'); 48 | $this->assertTrue($this->setting->has('a')); 49 | $this->assertEquals(['b' => 'c'], $this->setting->get('a')); 50 | 51 | $this->setting->clear(); 52 | $this->setting->set('', 'FOOBAR'); 53 | $this->assertEquals(['' => 'FOOBAR'], $this->setting->get('')); 54 | 55 | $this->setting->set('1.2.3.4.5.6.7.8', 'f'); 56 | $this->assertTrue($this->setting->has('1.2.3.4')); 57 | 58 | $this->setting->set('1.2.3.4.5.6.7.8.', 'f'); 59 | $this->assertTrue($this->setting->has('1.2.3.4.5.6.7.8.')); 60 | $this->assertEquals('f', $this->setting->get('1.2.3.4.5.6.7.8.')); 61 | } 62 | 63 | public function testSetBooleans() 64 | { 65 | $this->setting->set('isFalse', false); 66 | $this->assertTrue($this->setting->has('isFalse')); 67 | $this->assertSame(false, $this->setting->get('isFalse')); 68 | 69 | $this->setting->set('isTrue', true); 70 | $this->assertTrue($this->setting->has('isTrue')); 71 | $this->assertSame(true, $this->setting->get('isTrue')); 72 | } 73 | 74 | public function testForget() 75 | { 76 | $this->setting->set('a.b.c.d.e', 'f'); 77 | $this->setting->forget('a.b.c'); 78 | $this->assertFalse($this->setting->has('a.b.c')); 79 | 80 | $this->setting->set('1.2.3.4.5.6', 'f'); 81 | $this->setting->forget('1.2.3.4.5'); 82 | $this->assertFalse($this->setting->has('1.2.3.4.5.6')); 83 | $this->assertTrue($this->setting->has('1.2.3.4')); 84 | 85 | $this->setting->set('1.2.3.4.5.6.', 'f'); 86 | $this->setting->forget('1.2.3.4.5.6.'); 87 | $this->assertFalse($this->setting->has('1.2.3.4.5.6.')); 88 | $this->assertTrue($this->setting->has('1.2.3.4.5')); 89 | } 90 | 91 | public function testUnicode() 92 | { 93 | $this->setting->set('a', 'Hälfte'); 94 | $this->setting->set('b', 'Höfe'); 95 | $this->setting->set('c', 'Hüfte'); 96 | $this->setting->set('d', 'saß'); 97 | $this->assertEquals('Hälfte', $this->setting->get('a')); 98 | $this->assertEquals('Höfe', $this->setting->get('b')); 99 | $this->assertEquals('Hüfte', $this->setting->get('c')); 100 | $this->assertEquals('saß', $this->setting->get('d')); 101 | } 102 | 103 | public function testSetArray() 104 | { 105 | $array = [ 106 | 'id' => "foo", 107 | 'user_info' => [ 108 | 'username' => "bar", 109 | 'recently_viewed' => false, 110 | ] 111 | ]; 112 | $this->setting->setArray($array); 113 | $this->assertEquals($array, $this->setting->get()); 114 | } 115 | 116 | public function testGet() 117 | { 118 | $value = $this->setting->get("key that doesn't exist", 0); 119 | $this->assertSame(0, $value); 120 | } 121 | 122 | } 123 | --------------------------------------------------------------------------------