Setting.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. namespace Dcat\Admin\Support;
  3. use Dcat\Admin\Admin;
  4. use Dcat\Admin\Models\Setting as Model;
  5. use Illuminate\Database\QueryException;
  6. use Illuminate\Support\Arr;
  7. use Illuminate\Support\Fluent;
  8. class Setting extends Fluent
  9. {
  10. /**
  11. * 获取配置,并转化为数组.
  12. *
  13. * @param string $key
  14. * @param mixed $default
  15. *
  16. * @return array
  17. */
  18. public function getArray($key, $default = [])
  19. {
  20. $value = $this->get($key, $default);
  21. if (! $value) {
  22. return [];
  23. }
  24. return json_decode($value, true) ?: [];
  25. }
  26. /**
  27. * 获取配置.
  28. *
  29. * @param string $key
  30. * @param mixed $default
  31. *
  32. * @return mixed
  33. */
  34. public function get($key, $default = null)
  35. {
  36. return Arr::get($this->attributes, $key, $default);
  37. }
  38. /**
  39. * 设置配置信息.
  40. *
  41. * @param array $data
  42. *
  43. * @return $this
  44. */
  45. public function set($key, $value = null)
  46. {
  47. $data = is_array($key) ? $key : [$key => $value];
  48. foreach ($data as $key => $value) {
  49. Arr::set($this->attributes, $key, $value);
  50. }
  51. return $this;
  52. }
  53. /**
  54. * 追加数据.
  55. *
  56. * @param mixed $key
  57. * @param mixed $value
  58. * @param mixed $k
  59. *
  60. * @return $this
  61. */
  62. public function add($key, $value, $k = null)
  63. {
  64. $results = $this->getArray($key);
  65. if ($k !== null) {
  66. $results[] = $value;
  67. } else {
  68. $results[$k] = $value;
  69. }
  70. return $this->set($key, $results);
  71. }
  72. /**
  73. * 批量追加数据.
  74. *
  75. * @param string $key
  76. * @param array $value
  77. *
  78. * @return $this
  79. */
  80. public function addMany($key, array $value)
  81. {
  82. $results = $this->getArray($key);
  83. return $this->set($key, array_merge($results, $value));
  84. }
  85. /**
  86. * 保存配置到数据库.
  87. *
  88. * @param array $data
  89. *
  90. * @return $this
  91. */
  92. public function save(array $data = [])
  93. {
  94. if ($data) {
  95. $this->set($data);
  96. }
  97. foreach ($this->attributes as $key => $value) {
  98. if (is_array($value)) {
  99. $value = json_encode($value);
  100. }
  101. $model = Model::query()
  102. ->where('slug', $key)
  103. ->first() ?: new Model();
  104. $model->fill([
  105. 'slug' => $key,
  106. 'value' => (string) $value,
  107. ])->save();
  108. }
  109. return $this;
  110. }
  111. /**
  112. * @return static
  113. */
  114. public static function fromDatabase()
  115. {
  116. $values = [];
  117. try {
  118. $values = Model::pluck('value', 'slug')->toArray();
  119. } catch (QueryException $e) {
  120. Admin::reportException($e);
  121. }
  122. return new static($values);
  123. }
  124. }