Skip to content
Binary file modified .DS_Store
Binary file not shown.
29 changes: 23 additions & 6 deletions src/SettingsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

namespace Backpack\Settings;

use Backpack\Settings\app\Models\Setting;
use Config;
use Illuminate\Routing\Router;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\ServiceProvider;

Expand Down Expand Up @@ -38,13 +42,26 @@ public function boot()
// define the routes for the application
$this->setupRoutes();

// only use the Settings package if the Settings table is present in the database
if (!App::runningInConsole() && Schema::hasTable(config('backpack.settings.table_name'))) {
/** @var \Illuminate\Database\Eloquent\Model $modelClass */
$modelClass = config('backpack.settings.model', \Backpack\Settings\app\Models\Setting::class);
// listen for settings to be saved and clear the cache when any Setting model is saved.
Setting::saved(function () {
Cache::forget('backpack_settings_cache');
});

// get all settings from the database
$settings = $modelClass::all();
// only use the Settings package if the Settings table is present in the database
$tableExists = Cache::remember('backpack_settings_table_exists', config('backpack.settings.cache_time', 60), function () {
return Schema::hasTable(config('backpack.settings.table_name'));
});

if (!App::runningInConsole() && $tableExists) {
// get all settings from the database if they're not in the database.
$settings = Cache::remember('backpack_settings_cache', config('backpack.settings.cache_time', 60), function () {
/** @var \Illuminate\Database\Eloquent\Model $modelClass */
$modelClass = config('backpack.settings.model', \Backpack\Settings\app\Models\Setting::class);

// get all settings from the database
$settings = $modelClass::all();
return $settings;
});

$config_prefix = config('backpack.settings.config_prefix');

Expand Down
44 changes: 44 additions & 0 deletions src/app/Console/Commands/CacheUpdate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace Backpack\Settings\app\Console\Commands;

use Artisan;
use Backpack\Settings\app\Models\Setting;
use Config;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Cache;

class CacheUpdate extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'backpack_settings:cache_update';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Update Settings Cache Manually';

/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
/** @var \Illuminate\Database\Eloquent\Model $modelClass */
$modelClass = config('backpack.settings.model', \Backpack\Settings\app\Models\Setting::class);

// get all settings from the database
$settings = $modelClass::all();

Cache::put('backpack_settings_cache', config('backpack.settings.cache_time', 60), $settings);

}

}
10 changes: 10 additions & 0 deletions src/config/backpack/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,16 @@
*/
'config_prefix' => 'settings',

/*
|--------------------------------------------------------------------------
| Cache Time
|--------------------------------------------------------------------------
|
| The cache time for storing settings in a cache rather than querying the database on each load.
|
*/
'cache_time' => 60,

/*
|--------------------------------------------------------------------------
| Migration file name
Expand Down