FEATURE: make settings accessible only using helper class/function
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
<?php
|
||||
|
||||
namespace App\Helpers;
|
||||
|
||||
use App\Models\Settings as SettingsModel;
|
||||
|
||||
/**
|
||||
* Class Settings
|
||||
*
|
||||
* Helper class for easier obtaining system settings
|
||||
*
|
||||
* @package App\Helpers
|
||||
*/
|
||||
class Settings
|
||||
{
|
||||
/**
|
||||
* @var array List of all used settings
|
||||
*/
|
||||
protected static $settings;
|
||||
/**
|
||||
* @var bool Are data already initialized?
|
||||
*/
|
||||
protected static $initialized = false;
|
||||
|
||||
/**
|
||||
* Getter for settings determined with key.
|
||||
*
|
||||
* @param string $key settings key
|
||||
* @param string|null $default default value, which will be returned when given key does not exist
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
public static function get($key, $default = null)
|
||||
{
|
||||
self::init();
|
||||
|
||||
return isset(self::$settings->$key) ? self::$settings->$key : $default;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all settings
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function all()
|
||||
{
|
||||
self::init();
|
||||
|
||||
return self::$settings;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize settings array
|
||||
*/
|
||||
private static function init()
|
||||
{
|
||||
if (!self::$initialized) {
|
||||
$model = new SettingsModel();
|
||||
self::$settings = $model->getAll();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -11,4 +11,16 @@
|
||||
function e($value)
|
||||
{
|
||||
return htmlspecialchars($value, ENT_QUOTES, 'UTF-8');
|
||||
}
|
||||
|
||||
/**
|
||||
* Shortcut for \App\Helpers\Settings::get()
|
||||
*
|
||||
* @param $key
|
||||
* @param null $default
|
||||
*
|
||||
* @return null|string
|
||||
*/
|
||||
function setting($key, $default = null) {
|
||||
return \App\Helpers\Settings::get($key, $default);
|
||||
}
|
||||
Reference in New Issue
Block a user