Without share.
This commit is contained in:
143
src/Cache.php
Executable file
143
src/Cache.php
Executable file
@ -0,0 +1,143 @@
|
||||
<?php namespace Efriandika\LaravelSettings;
|
||||
|
||||
/**
|
||||
* Class Cache
|
||||
* @package Efriandika\LaravelSettings
|
||||
*/
|
||||
class Cache
|
||||
{
|
||||
|
||||
/**
|
||||
* Path to cache file
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $cacheFile;
|
||||
|
||||
/**
|
||||
* Cached Settings
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $settings;
|
||||
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param string $cacheFile
|
||||
*/
|
||||
public function __construct($cacheFile)
|
||||
{
|
||||
$this->cacheFile = $cacheFile;
|
||||
$this->checkCacheFile();
|
||||
|
||||
$this->settings = $this->getAll();
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a value
|
||||
*
|
||||
* @param $key
|
||||
* @param $value
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function set($key, $value)
|
||||
{
|
||||
$this->settings[$key] = $value;
|
||||
$this->store();
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a value
|
||||
*
|
||||
* @param $key
|
||||
* @param null $default
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function get($key, $default = null)
|
||||
{
|
||||
return (array_key_exists($key, $this->settings) ? $this->settings[$key] : $default);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if $key is cached
|
||||
*
|
||||
* @param $key
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function hasKey($key)
|
||||
{
|
||||
return array_key_exists($key, $this->settings);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets all cached settings
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getAll()
|
||||
{
|
||||
$values = json_decode(file_get_contents($this->cacheFile), true);
|
||||
foreach ($values as $key => $value) {
|
||||
$values[$key] = unserialize($value);
|
||||
}
|
||||
return $values;
|
||||
}
|
||||
|
||||
/**
|
||||
* Stores all settings to the cache file
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function store()
|
||||
{
|
||||
$settings = [];
|
||||
foreach ($this->settings as $key => $value) {
|
||||
$settings[$key] = serialize($value);
|
||||
}
|
||||
file_put_contents($this->cacheFile, json_encode($settings));
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes a value
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function forget($key)
|
||||
{
|
||||
if (array_key_exists($key, $this->settings)) {
|
||||
unset($this->settings[$key]);
|
||||
}
|
||||
$this->store();
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes all values
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function flush()
|
||||
{
|
||||
file_put_contents($this->cacheFile, json_encode([]));
|
||||
// fixed the set after immediately the flush, should be returned empty
|
||||
$this->settings = [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the cache file exists and creates it if not
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function checkCacheFile()
|
||||
{
|
||||
if (!file_exists($this->cacheFile)) {
|
||||
$this->flush();
|
||||
}
|
||||
}
|
||||
}
|
||||
14
src/Facades/Settings.php
Executable file
14
src/Facades/Settings.php
Executable file
@ -0,0 +1,14 @@
|
||||
<?php namespace Efriandika\LaravelSettings\Facades;
|
||||
|
||||
use Illuminate\Support\Facades\Facade;
|
||||
|
||||
class Settings extends Facade {
|
||||
|
||||
/**
|
||||
* Get the registered name of the component.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected static function getFacadeAccessor() { return 'settings'; }
|
||||
|
||||
}
|
||||
169
src/Settings.php
Executable file
169
src/Settings.php
Executable file
@ -0,0 +1,169 @@
|
||||
<?php namespace Efriandika\LaravelSettings;
|
||||
|
||||
use Illuminate\Database\DatabaseManager;
|
||||
use Illuminate\Support\Facades\Config;
|
||||
|
||||
/**
|
||||
* Class Settings
|
||||
* @package Efriandika\LaravelSettings
|
||||
*/
|
||||
class Settings
|
||||
{
|
||||
|
||||
/**
|
||||
* Registry config
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $config;
|
||||
|
||||
|
||||
/**
|
||||
* Database manager instance
|
||||
*
|
||||
* @var \Illuminate\Database\DatabaseManager
|
||||
*/
|
||||
protected $database;
|
||||
|
||||
/**
|
||||
* Cache
|
||||
*
|
||||
* @var \Efriandika\LaravelSettings\Cache
|
||||
*/
|
||||
protected $cache;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param DatabaseManager $database
|
||||
*/
|
||||
public function __construct(DatabaseManager $database, Cache $cache, $config = array ())
|
||||
{
|
||||
$this->database = $database;
|
||||
$this->config = $config;
|
||||
$this->cache = $cache;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a value
|
||||
*
|
||||
* @param string $key
|
||||
* @param string $default
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function get($key, $default = null)
|
||||
{
|
||||
$value = $this->fetch($key);
|
||||
|
||||
if(!is_null($value))
|
||||
return $value;
|
||||
else if($default != null)
|
||||
return $default;
|
||||
else if($this->config['fallback'])
|
||||
return Config::get($key, null);
|
||||
else
|
||||
return $default;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $key
|
||||
*
|
||||
* @return mixed|null
|
||||
*/
|
||||
private function fetch($key)
|
||||
{
|
||||
|
||||
if ($this->cache->hasKey($key)) {
|
||||
return $this->cache->get($key);
|
||||
}
|
||||
|
||||
$row = $this->database->table($this->config['db_table'])->where('setting_key', $key)->first(['setting_value']);
|
||||
|
||||
return (!is_null($row)) ? $this->cache->set($key, unserialize($row->setting_value)) : null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Checks if setting exists
|
||||
*
|
||||
* @param $key
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function hasKey($key)
|
||||
{
|
||||
if ($this->cache->hasKey($key)) {
|
||||
return true;
|
||||
}
|
||||
$row = $this->database->table($this->config['db_table'])->where('setting_key', $key)->first(['setting_value']);
|
||||
|
||||
return (count($row) > 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Store value into registry
|
||||
*
|
||||
* @param string $key
|
||||
* @param mixed $value
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function set($key, $value)
|
||||
{
|
||||
$value = serialize($value);
|
||||
|
||||
$setting = $this->database->table($this->config['db_table'])->where('setting_key', $key)->first();
|
||||
|
||||
if (is_null($setting)) {
|
||||
$this->database->table($this->config['db_table'])
|
||||
->insert(['setting_key' => $key, 'setting_value' => $value]);
|
||||
} else {
|
||||
$this->database->table($this->config['db_table'])
|
||||
->where('setting_key', $key)
|
||||
->update(['setting_value' => $value]);
|
||||
}
|
||||
|
||||
$this->cache->set($key, unserialize($value));
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Remove a setting
|
||||
*
|
||||
* @param string $key
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function forget($key)
|
||||
{
|
||||
$this->database->table($this->config['db_table'])->where('setting_key', $key)->delete();
|
||||
$this->cache->forget($key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove all settings
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function flush()
|
||||
{
|
||||
$this->cache->flush();
|
||||
|
||||
return $this->database->table($this->config['db_table'])->delete();
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch all values
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function getAll()
|
||||
{
|
||||
return $this->cache->getAll();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
62
src/SettingsServiceProvider.php
Executable file
62
src/SettingsServiceProvider.php
Executable file
@ -0,0 +1,62 @@
|
||||
<?php namespace Efriandika\LaravelSettings;
|
||||
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
|
||||
class SettingsServiceProvider extends ServiceProvider
|
||||
{
|
||||
|
||||
/**
|
||||
* Indicates if loading of the provider is deferred.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
protected $defer = false;
|
||||
|
||||
/**
|
||||
* Bootstrap the application events.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function boot()
|
||||
{
|
||||
$this->publishes([
|
||||
__DIR__ . '/config/settings.php' => config_path('settings.php')
|
||||
]);
|
||||
$this->publishes([
|
||||
__DIR__ . '/database/migrations/' => base_path('/database/migrations')
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the service provider.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function register()
|
||||
{
|
||||
$this->mergeConfigFrom(
|
||||
__DIR__ . '/config/settings.php', 'settings'
|
||||
);
|
||||
$this->app['settings'] = function ($app) {
|
||||
|
||||
$config = $app->config->get('settings', [
|
||||
'cache_file' => storage_path('settings.json'),
|
||||
'db_table' => 'settings'
|
||||
]);
|
||||
|
||||
return new Settings($app['db'], new Cache($config['cache_file']), $config);
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get the services provided by the provider.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function provides()
|
||||
{
|
||||
return array ('settings');
|
||||
}
|
||||
|
||||
}
|
||||
33
src/config/settings.php
Normal file
33
src/config/settings.php
Normal file
@ -0,0 +1,33 @@
|
||||
<?php
|
||||
return [
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Cache Filename
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| Cache configuration path
|
||||
|
|
||||
*/
|
||||
'cache_file' => storage_path('settings.json'),
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Table name to store settings
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| Info: If you change this table name, dont forget to update your settings migrations file.
|
||||
|
|
||||
*/
|
||||
'db_table' => 'settings',
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Fallback setting
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| Return Laravel config if the value with particular key is not found in cache or DB.
|
||||
| It will work if default value in laravel setting is not set, and this value is set to true
|
||||
|
|
||||
*/
|
||||
'fallback' => true
|
||||
];
|
||||
@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
|
||||
class CreateSettingsTable extends Migration {
|
||||
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::create('settings', function(Blueprint $table)
|
||||
{
|
||||
$table->string('key', 100)->index()->unique('key');
|
||||
$table->text('value', 65535)->nullable();
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
Schema::drop('settings');
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
|
||||
class AlterSettingsTable extends Migration {
|
||||
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::table('settings', function ($table) {
|
||||
$table->renameColumn('key', 'setting_key');
|
||||
$table->renameColumn('value', 'setting_value');
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
Schema::table('settings', function ($table) {
|
||||
$table->renameColumn('setting_key', 'key');
|
||||
$table->renameColumn('setting_value', 'value');
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
20
src/helpers.php
Normal file
20
src/helpers.php
Normal file
@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
use Efriandika\LaravelSettings\Facades\Settings;
|
||||
|
||||
if (!function_exists('settings'))
|
||||
{
|
||||
/**
|
||||
* @param string|null $key
|
||||
* @param null $default
|
||||
* @return mixed|\Efriandika\LaravelSettings\Facades\Settings
|
||||
*/
|
||||
function settings($key = null, $default = null)
|
||||
{
|
||||
if (is_null($key)) {
|
||||
return app('settings');
|
||||
}
|
||||
|
||||
return Settings::get($key, $default);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user