|
| 1 | +<?php |
| 2 | +if (!function_exists('get_settings')) { |
| 3 | + /** |
| 4 | + * Get all autoload settings |
| 5 | + */ |
| 6 | + function get_settings($keys = null, $groups = null, $autoload = null) { |
| 7 | + $settings = \Mjpakzad\LaravelSettings\Models\Setting::query(); |
| 8 | + if($keys !== null) { |
| 9 | + if(is_array($keys)) { |
| 10 | + $settings->whereIn('key', $keys); |
| 11 | + } else { |
| 12 | + $settings->where('key', $keys); |
| 13 | + } |
| 14 | + } |
| 15 | + if($groups !== null) { |
| 16 | + if(is_array($groups)) { |
| 17 | + $settings->whereIn('group', $groups); |
| 18 | + } else { |
| 19 | + $settings->where('group', $groups); |
| 20 | + } |
| 21 | + } |
| 22 | + if($autoload !== null) { |
| 23 | + if($autoload) { |
| 24 | + $settings->autoload(); |
| 25 | + } else { |
| 26 | + $settings->manual(); |
| 27 | + } |
| 28 | + } |
| 29 | + return $settings->pluck('value', 'key'); |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +if (!function_exists('get_setting')) { |
| 34 | + /** |
| 35 | + * Get a specific setting |
| 36 | + */ |
| 37 | + function get_setting($key) { |
| 38 | + return \Mjpakzad\LaravelSettings\Models\Setting::where('key', $key)->value('value'); |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +if (!function_exists('set_setting')) { |
| 43 | + /** |
| 44 | + * Create a new setting. |
| 45 | + * |
| 46 | + * @return void |
| 47 | + */ |
| 48 | + function set_setting($key, $value, $group = null, $autoload = null) { |
| 49 | + $setting = [ |
| 50 | + 'key' => $key, |
| 51 | + 'value' => $value, |
| 52 | + 'autoload' => $autoload !== null ? $autoload : false, |
| 53 | + ]; |
| 54 | + if($group !== null) { |
| 55 | + $setting['group'] = $group; |
| 56 | + } |
| 57 | + \Mjpakzad\LaravelSettings\Models\Setting::create($setting); |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +if (!function_exists('setting_config')) { |
| 62 | + /** |
| 63 | + * Save settings in config. |
| 64 | + * |
| 65 | + * @return void |
| 66 | + */ |
| 67 | + function setting_config($keys = null, $groups = null, $autoload = null) { |
| 68 | + config()->set('settings', get_settings($keys, $groups, $autoload)->toArray()); |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +if(!function_exists('settings')) { |
| 73 | + /** |
| 74 | + * Creates a new setting or return settings. |
| 75 | + * |
| 76 | + * @param $key |
| 77 | + * @param $value |
| 78 | + * @param null $group |
| 79 | + * @param null $autoload |
| 80 | + * |
| 81 | + * @return \Illuminate\Support\Collection|void |
| 82 | + * @example setting('key'|['',], 'group'|['',], true) |
| 83 | + * |
| 84 | + * @example setting('site_name', 'laravel settings', 'site settings', true) creates a new settings. |
| 85 | + */ |
| 86 | + function setting($key, $value, $group = null, $autoload = null) { |
| 87 | + if($autoload !== null) { |
| 88 | + set_setting($key, $value, $group, $autoload); |
| 89 | + } else { |
| 90 | + return get_settings($key, $value, $autoload); |
| 91 | + } |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +if(!function_exists('setting_group')) { |
| 96 | + /** |
| 97 | + * Returns a group of settings. |
| 98 | + * |
| 99 | + * @param array|string $group The name of group or groups |
| 100 | + */ |
| 101 | + function setting_group(array|string $group): \Illuminate\Support\Collection |
| 102 | + { |
| 103 | + return get_settings(null, $group); |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +if(!function_exists('setting_autoload')) { |
| 108 | + /** |
| 109 | + * Return autoload or manual settings |
| 110 | + * |
| 111 | + * @param bool $autoload |
| 112 | + * |
| 113 | + * @return \Illuminate\Support\Collection |
| 114 | + */ |
| 115 | + function setting_autoload(bool $autoload = true): \Illuminate\Support\Collection |
| 116 | + { |
| 117 | + return get_settings(null, null, $autoload); |
| 118 | + } |
| 119 | +} |
0 commit comments