SystemConfig.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <?php
  2. namespace Webkul\Core;
  3. use Illuminate\Support\Arr;
  4. use Illuminate\Support\Collection;
  5. use Illuminate\Support\Facades\Config;
  6. use Webkul\Core\Models\CoreConfig;
  7. use Webkul\Core\Repositories\CoreConfigRepository;
  8. use Webkul\Core\SystemConfig\Item;
  9. class SystemConfig
  10. {
  11. /**
  12. * Items array.
  13. */
  14. public array $items = [];
  15. /**
  16. * Create a new class instance.
  17. *
  18. * @return void
  19. */
  20. public function __construct(protected CoreConfigRepository $coreConfigRepository) {}
  21. /**
  22. * Add Item.
  23. */
  24. public function addItem(Item $item): void
  25. {
  26. $this->items[] = $item;
  27. }
  28. /**
  29. * Get all configuration items.
  30. */
  31. public function getItems(): Collection
  32. {
  33. if (! $this->items) {
  34. $this->prepareConfigurationItems();
  35. }
  36. return collect($this->items)
  37. ->sortBy('sort');
  38. }
  39. /**
  40. * Retrieve Core Config
  41. */
  42. private function retrieveCoreConfig(): array
  43. {
  44. static $items;
  45. if ($items) {
  46. return $items;
  47. }
  48. return $items = config('core');
  49. }
  50. /**
  51. * Prepare configuration items.
  52. */
  53. public function prepareConfigurationItems()
  54. {
  55. $configWithDotNotation = [];
  56. foreach ($this->retrieveCoreConfig() as $item) {
  57. $configWithDotNotation[$item['key']] = $item;
  58. }
  59. $configs = Arr::undot(Arr::dot($configWithDotNotation));
  60. foreach ($configs as $configItem) {
  61. $subConfigItems = $this->processSubConfigItems($configItem);
  62. $this->addItem(new Item(
  63. children: $subConfigItems,
  64. fields: $configItem['fields'] ?? null,
  65. icon: $configItem['icon'] ?? null,
  66. icon_class: $configItem['icon_class'] ?? null,
  67. info: trans($configItem['info']) ?? null,
  68. key: $configItem['key'],
  69. name: trans($configItem['name']),
  70. route: $configItem['route'] ?? null,
  71. sort: $configItem['sort'],
  72. ));
  73. }
  74. }
  75. /**
  76. * Process sub config items.
  77. */
  78. private function processSubConfigItems($configItem): Collection
  79. {
  80. return collect($configItem)
  81. ->sortBy('sort')
  82. ->filter(fn ($value) => is_array($value) && isset($value['name']))
  83. ->map(function ($subConfigItem) {
  84. $configItemChildren = $this->processSubConfigItems($subConfigItem);
  85. return new Item(
  86. children: $configItemChildren,
  87. fields: $subConfigItem['fields'] ?? null,
  88. icon: $subConfigItem['icon'] ?? null,
  89. icon_class: $subConfigItem['icon_class'] ?? null,
  90. info: trans($subConfigItem['info']) ?? null,
  91. key: $subConfigItem['key'],
  92. name: trans($subConfigItem['name']),
  93. route: $subConfigItem['route'] ?? null,
  94. sort: $subConfigItem['sort'] ?? null,
  95. );
  96. });
  97. }
  98. /**
  99. * Get active configuration item.
  100. */
  101. public function getActiveConfigurationItem(): ?Item
  102. {
  103. if (! $slug = request()->route('slug')) {
  104. return null;
  105. }
  106. $activeItem = $this->getItems()->where('key', $slug)->first() ?? null;
  107. if (! $activeItem) {
  108. return null;
  109. }
  110. if ($slug2 = request()->route('slug2')) {
  111. $activeItem = $activeItem->getChildren()[$slug2];
  112. }
  113. return $activeItem;
  114. }
  115. /**
  116. * Get config field.
  117. */
  118. public function getConfigField(string $fieldName): ?array
  119. {
  120. foreach ($this->retrieveCoreConfig() as $coreData) {
  121. if (! isset($coreData['fields'])) {
  122. continue;
  123. }
  124. foreach ($coreData['fields'] as $field) {
  125. $name = $coreData['key'].'.'.$field['name'];
  126. if ($name == $fieldName) {
  127. return $field;
  128. }
  129. }
  130. }
  131. return null;
  132. }
  133. /**
  134. * Get core config values.
  135. */
  136. protected function getCoreConfig(string $field, ?string $channel, ?string $locale): ?CoreConfig
  137. {
  138. $fields = $this->getConfigField($field);
  139. if (! empty($fields['channel_based'])) {
  140. if (! empty($fields['locale_based'])) {
  141. $coreConfigValue = $this->coreConfigRepository->findOneWhere([
  142. 'code' => $field,
  143. 'channel_code' => $channel,
  144. 'locale_code' => $locale,
  145. ]);
  146. } else {
  147. $coreConfigValue = $this->coreConfigRepository->findOneWhere([
  148. 'code' => $field,
  149. 'channel_code' => $channel,
  150. ]);
  151. }
  152. } else {
  153. if (! empty($fields['locale_based'])) {
  154. $coreConfigValue = $this->coreConfigRepository->findOneWhere([
  155. 'code' => $field,
  156. 'locale_code' => $locale,
  157. ]);
  158. } else {
  159. $coreConfigValue = $this->coreConfigRepository->findOneWhere([
  160. 'code' => $field,
  161. ]);
  162. }
  163. }
  164. return $coreConfigValue;
  165. }
  166. /**
  167. * Get default config.
  168. */
  169. protected function getDefaultConfig(string $field): mixed
  170. {
  171. $configFieldInfo = $this->getConfigField($field);
  172. $fields = explode('.', $field);
  173. array_shift($fields);
  174. $field = implode('.', $fields);
  175. return Config::get($field, $configFieldInfo['default'] ?? null);
  176. }
  177. /**
  178. * Get the config data.
  179. */
  180. public function getConfigData(string $field, ?string $currentChannelCode = null, ?string $currentLocaleCode = null): mixed
  181. {
  182. if (empty($currentChannelCode)) {
  183. $currentChannelCode = core()->getRequestedChannelCode();
  184. }
  185. if (empty($currentLocaleCode)) {
  186. $currentLocaleCode = core()->getRequestedLocaleCode();
  187. }
  188. $coreConfig = $this->getCoreConfig($field, $currentChannelCode, $currentLocaleCode);
  189. if (! $coreConfig) {
  190. return $this->getDefaultConfig($field);
  191. }
  192. return $coreConfig->value;
  193. }
  194. }