Config.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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(): ?Config
  28. {
  29. $cacheName = $this->cacheName;
  30. if (!$result = Cache::get($cacheName)) {
  31. $res = $this->get();
  32. if ($res) {
  33. foreach ($res as $val) {
  34. $this->{$val[$this->metaKey]} = $val[$this->metaValue];
  35. }
  36. $result = $this;
  37. Cache::put($cacheName, $this, $this->cacheTime);
  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. $data = $this->getAllMeta();
  67. if (property_exists($data, $column)) {
  68. $return = $data->{$column};
  69. } else {
  70. $return = $default;
  71. }
  72. return $return;
  73. }
  74. /**
  75. * 등록/수정
  76. */
  77. private function _metaUpdate($column = '', $value = false, $description = ''): bool|string
  78. {
  79. $column = trim($column);
  80. if (empty($column)) {
  81. return false;
  82. }
  83. $items = $this->getAllMeta();
  84. if (property_exists($items, $column)) {
  85. return $this->_updateMeta($column, $value, $description); // 수정
  86. } else {
  87. return $this->_addMeta($column, $value, $description); // 새로 추가
  88. }
  89. }
  90. /**
  91. * 등록
  92. */
  93. private function _addMeta($column = '', $value = '', $description = ''): bool
  94. {
  95. $column = trim($column);
  96. if (empty($column)) {
  97. return false;
  98. }
  99. $insertData = [
  100. $this->metaKey => $column,
  101. $this->metaValue => $value,
  102. $this->metaDescription => $description
  103. ];
  104. return $this->insert($insertData);
  105. }
  106. /**
  107. * 수정
  108. */
  109. private function _updateMeta($column = '', $value = '', $description = ''): bool
  110. {
  111. $column = trim($column);
  112. if (empty($column)) {
  113. return false;
  114. }
  115. $updateData = [
  116. $this->metaValue => $value,
  117. $this->metaDescription => $description
  118. ];
  119. return $this->where($this->metaKey, $column)->update($updateData);
  120. }
  121. }