Config.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Model;
  4. use Illuminate\Support\Facades\Cache;
  5. class Config extends Model
  6. {
  7. protected $table = 'tb_config';
  8. public $keyType = 'string';
  9. public $incrementing = false;
  10. public $timestamps = false;
  11. public string $metaKey = 'id';
  12. public string $metaValue = 'value';
  13. public string $metaDescription = 'description';
  14. public string $cacheName = 'config-meta';
  15. public int $cacheTime = CACHE_EXPIRE_TIME;
  16. public function __set($name, $value)
  17. {
  18. $this->{$name} = $value;
  19. }
  20. public function __get($name)
  21. {
  22. return ($this->{$name} ?? null);
  23. }
  24. /**
  25. * 모든 관리자 설정 값 조회
  26. */
  27. public function getAllMeta(): object|array
  28. {
  29. $cacheName = $this->cacheName;
  30. if (!$result = Cache::get($cacheName)) {
  31. $res = $this->get();
  32. if ($res) {
  33. $result = [];
  34. foreach ($res as $val) {
  35. $result[$val[$this->metaKey]] = $val[$this->metaValue];
  36. }
  37. Cache::forever($cacheName, (object)$result);
  38. }
  39. }
  40. return $result;
  41. }
  42. /**
  43. * 저장
  44. */
  45. public function save($saveData = [], array $attributes = []): void
  46. {
  47. if ($saveData && is_array($saveData)) {
  48. foreach ($saveData as $column => $value) {
  49. if (array_key_exists($column, $attributes)) {
  50. $description = $attributes[$column];
  51. $this->_metaUpdate($column, $value, $description);
  52. }
  53. }
  54. }
  55. Cache::forget($this->cacheName);
  56. $this->getAllMeta();
  57. }
  58. /**
  59. * 조회
  60. */
  61. public function item($column = '', $default = false): mixed
  62. {
  63. if (empty($column)) {
  64. return false;
  65. }
  66. $meta = (array)$this->getAllMeta();
  67. $return = $default;
  68. if (array_key_exists($column, $meta)) {
  69. $return = $meta[$column];
  70. }
  71. return $return;
  72. }
  73. /**
  74. * 등록/수정
  75. */
  76. private function _metaUpdate($column = '', $value = false, $description = ''): mixed
  77. {
  78. $column = trim($column);
  79. if (empty($column)) {
  80. return false;
  81. }
  82. $oldValue = $this->item($column);
  83. if (empty($value) && $value != 0) {
  84. $value = '';
  85. }
  86. if ($value === $oldValue) {
  87. return false;
  88. }
  89. if (false === $oldValue) {
  90. return $this->_addMeta($column, $value, $description); // 새로 추가
  91. }
  92. return $this->_updateMeta($column, $value, $description); // 수정
  93. }
  94. /**
  95. * 등록
  96. */
  97. private function _addMeta($column = '', $value = '', $description = ''): mixed
  98. {
  99. $column = trim($column);
  100. if (empty($column)) {
  101. return false;
  102. }
  103. $insertData = [
  104. $this->metaKey => $column,
  105. $this->metaValue => $value,
  106. $this->metaDescription => $description
  107. ];
  108. return $this->insert($insertData);
  109. }
  110. /**
  111. * 수정
  112. */
  113. private function _updateMeta($column = '', $value = '', $description = ''): mixed
  114. {
  115. $column = trim($column);
  116. if (empty($column)) {
  117. return false;
  118. }
  119. $updateData = [
  120. $this->metaValue => $value,
  121. $this->metaDescription => $description
  122. ];
  123. return $this->where($this->metaKey, $column)->update($updateData);
  124. }
  125. }