You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

169 lines
3.5 KiB

5 years ago
  1. <?php namespace Efriandika\LaravelSettings;
  2. use Illuminate\Database\DatabaseManager;
  3. use Illuminate\Support\Facades\Config;
  4. /**
  5. * Class Settings
  6. * @package Efriandika\LaravelSettings
  7. */
  8. class Settings
  9. {
  10. /**
  11. * Registry config
  12. *
  13. * @var array
  14. */
  15. protected $config;
  16. /**
  17. * Database manager instance
  18. *
  19. * @var \Illuminate\Database\DatabaseManager
  20. */
  21. protected $database;
  22. /**
  23. * Cache
  24. *
  25. * @var \Efriandika\LaravelSettings\Cache
  26. */
  27. protected $cache;
  28. /**
  29. * Constructor
  30. *
  31. * @param DatabaseManager $database
  32. */
  33. public function __construct(DatabaseManager $database, Cache $cache, $config = array ())
  34. {
  35. $this->database = $database;
  36. $this->config = $config;
  37. $this->cache = $cache;
  38. }
  39. /**
  40. * Gets a value
  41. *
  42. * @param string $key
  43. * @param string $default
  44. *
  45. * @return mixed
  46. */
  47. public function get($key, $default = null)
  48. {
  49. $value = $this->fetch($key);
  50. if(!is_null($value))
  51. return $value;
  52. else if($default != null)
  53. return $default;
  54. else if($this->config['fallback'])
  55. return Config::get($key, null);
  56. else
  57. return $default;
  58. }
  59. /**
  60. * @param $key
  61. *
  62. * @return mixed|null
  63. */
  64. private function fetch($key)
  65. {
  66. if ($this->cache->hasKey($key)) {
  67. return $this->cache->get($key);
  68. }
  69. $row = $this->database->table($this->config['db_table'])->where('setting_key', $key)->first(['setting_value']);
  70. return (!is_null($row)) ? $this->cache->set($key, unserialize($row->setting_value)) : null;
  71. }
  72. /**
  73. * Checks if setting exists
  74. *
  75. * @param $key
  76. *
  77. * @return bool
  78. */
  79. public function hasKey($key)
  80. {
  81. if ($this->cache->hasKey($key)) {
  82. return true;
  83. }
  84. $row = $this->database->table($this->config['db_table'])->where('setting_key', $key)->first(['setting_value']);
  85. return (count($row) > 0);
  86. }
  87. /**
  88. * Store value into registry
  89. *
  90. * @param string $key
  91. * @param mixed $value
  92. *
  93. * @return mixed
  94. */
  95. public function set($key, $value)
  96. {
  97. $value = serialize($value);
  98. $setting = $this->database->table($this->config['db_table'])->where('setting_key', $key)->first();
  99. if (is_null($setting)) {
  100. $this->database->table($this->config['db_table'])
  101. ->insert(['setting_key' => $key, 'setting_value' => $value]);
  102. } else {
  103. $this->database->table($this->config['db_table'])
  104. ->where('setting_key', $key)
  105. ->update(['setting_value' => $value]);
  106. }
  107. $this->cache->set($key, unserialize($value));
  108. return $value;
  109. }
  110. /**
  111. * Remove a setting
  112. *
  113. * @param string $key
  114. *
  115. * @return void
  116. */
  117. public function forget($key)
  118. {
  119. $this->database->table($this->config['db_table'])->where('setting_key', $key)->delete();
  120. $this->cache->forget($key);
  121. }
  122. /**
  123. * Remove all settings
  124. *
  125. * @return bool
  126. */
  127. public function flush()
  128. {
  129. $this->cache->flush();
  130. return $this->database->table($this->config['db_table'])->delete();
  131. }
  132. /**
  133. * Fetch all values
  134. *
  135. * @return mixed
  136. */
  137. public function getAll()
  138. {
  139. return $this->cache->getAll();
  140. }
  141. }