FEATURE: make settings accessible only using helper class/function

This commit is contained in:
Michal Kváček
2016-09-22 21:09:48 +02:00
parent fb977ce1b9
commit b982831fbf
5 changed files with 81 additions and 7 deletions
+62
View File
@@ -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();
}
}
}