|
2 | 2 |
|
3 | 3 | namespace VentureDrake\LaravelCrm\View\Composers;
|
4 | 4 |
|
| 5 | +use Illuminate\Support\Facades\Cache; |
5 | 6 | use Illuminate\Support\Facades\Schema;
|
6 | 7 | use Illuminate\View\View;
|
7 | 8 | use VentureDrake\LaravelCrm\Models\Setting;
|
8 | 9 |
|
9 | 10 | class SettingsComposer
|
10 | 11 | {
|
| 12 | + public static ?array $cachedParameters = null; |
| 13 | + |
11 | 14 | public function compose(View $view)
|
12 | 15 | {
|
13 |
| - if (Schema::hasTable(config('laravel-crm.db_table_prefix').'settings')) { |
14 |
| - $view->with('dateFormat', Setting::where('name', 'date_format')->first()->value ?? 'Y/m/d'); |
15 |
| - $view->with('timeFormat', Setting::where('name', 'time_format')->first()->value ?? 'H:i'); |
16 |
| - $view->with('timezone', Setting::where('name', 'timezone')->first()->value ?? 'UTC'); |
17 |
| - $view->with('taxName', Setting::where('name', 'tax_name')->first()->value ?? 'Tax'); |
18 |
| - |
19 |
| - if($setting = Setting::where('name', 'dynamic_products')->first()) { |
20 |
| - if($setting->value == 1) { |
21 |
| - $view->with('dynamicProducts', 'true'); |
| 16 | + static::$cachedParameters ??= Cache::remember( |
| 17 | + self::class, |
| 18 | + now()->addHour(), |
| 19 | + function () { |
| 20 | + $defaults = [ |
| 21 | + 'dateFormat' => 'Y/m/d', |
| 22 | + 'timeFormat' => 'H:i', |
| 23 | + 'timezone' => 'UTC', |
| 24 | + 'taxName' => 'Tax', |
| 25 | + 'dynamicProducts' => 'true', |
| 26 | + ]; |
| 27 | + |
| 28 | + if (! Schema::hasTable(config('laravel-crm.db_table_prefix').'settings')) { |
| 29 | + return $defaults; |
| 30 | + } |
| 31 | + |
| 32 | + if ($dynamicProductsSetting = Setting::where('name', 'dynamic_products')->first()) { |
| 33 | + if ($dynamicProductsSetting->value == 1) { |
| 34 | + $dynamicProducts = 'true'; |
| 35 | + } else { |
| 36 | + $dynamicProducts = 'false'; |
| 37 | + } |
22 | 38 | } else {
|
23 |
| - $view->with('dynamicProducts', 'false'); |
| 39 | + $dynamicProducts = $defaults['dynamicProducts']; |
24 | 40 | }
|
25 |
| - } else { |
26 |
| - $view->with('dynamicProducts', 'true'); |
| 41 | + |
| 42 | + return [ |
| 43 | + 'dateFormat' => Setting::where('name', 'date_format')->first()?->value ?? $defaults['dateFormat'], |
| 44 | + 'timeFormat' => Setting::where('name', 'time_format')->first()?->value ?? $defaults['timeFormat'], |
| 45 | + 'timezone' => Setting::where('name', 'timezone')->first()?->value ?? $defaults['timezone'], |
| 46 | + 'taxName' => Setting::where('name', 'tax_name')->first()?->value ?? $defaults['taxName'], |
| 47 | + 'dynamicProducts' => $dynamicProducts, |
| 48 | + ]; |
27 | 49 | }
|
28 |
| - } else { |
29 |
| - $view->with('dateFormat', 'Y/m/d'); |
30 |
| - $view->with('timeFormat', 'H:i'); |
31 |
| - $view->with('timezone', 'UTC'); |
32 |
| - $view->with('taxName', 'Tax'); |
33 |
| - } |
| 50 | + ); |
| 51 | + |
| 52 | + $view->with(static::$cachedParameters); |
34 | 53 | }
|
35 | 54 | }
|
0 commit comments