diff --git a/app/Foundation/Providers/ConfigServiceProvider.php b/app/Foundation/Providers/ConfigServiceProvider.php index 0e150f3a..626dea93 100644 --- a/app/Foundation/Providers/ConfigServiceProvider.php +++ b/app/Foundation/Providers/ConfigServiceProvider.php @@ -12,6 +12,7 @@ namespace CachetHQ\Cachet\Foundation\Providers; use CachetHQ\Cachet\Models\Setting as SettingModel; +use CachetHQ\Cachet\Settings\Cache; use CachetHQ\Cachet\Settings\Repository; use Exception; use Illuminate\Support\ServiceProvider; @@ -32,29 +33,24 @@ class ConfigServiceProvider extends ServiceProvider */ public function boot() { - $path = $this->app->bootstrapPath().'/cache/cachet.'.$this->app->environment().'.php'; + $env = $this->app->environment(); + $repo = $this->app->make(Repository::class); + $cache = $this->app->make(Cache::class); + $loaded = $cache->load($env); - try { - $cache = $this->app->files->getRequire($path); - } catch (Exception $e) { - $cache = false; - } - - $this->app->terminating(function () use ($cache, $path) { - if ($this->app->setting->stale() || $cache === false) { - $this->app->files->put($path, 'app->setting->all(), true).';'.PHP_EOL); + $this->app->terminating(function () use ($repo, $cache) { + if ($repo->stale()) { + $cache->clear(); } }); try { - // Get the default settings. - $defaultSettings = $this->app->config->get('setting'); + if ($loaded === false) { + $loaded = $repo->all(); + $cache->store($env, $loaded); + } - // Get the configured settings. - $appSettings = $cache === false ? $this->app->setting->all() : $cache; - - // Merge the settings - $settings = array_merge($defaultSettings, $appSettings); + $settings = array_merge($this->app->config->get('setting'), $loaded); $this->app->config->set('setting', $settings); } catch (Exception $e) { @@ -95,10 +91,12 @@ class ConfigServiceProvider extends ServiceProvider */ public function register() { - $this->app->singleton('setting', function () { - return new Repository(new SettingModel()); + $this->app->singleton(Cache::class, function ($app) { + return new Cache($app->files, $app->bootstrapPath().'/cachet'); }); - $this->app->alias('setting', Repository::class); + $this->app->singleton(Repository::class, function () { + return new Repository(new SettingModel()); + }); } } diff --git a/app/Http/Controllers/Dashboard/SettingsController.php b/app/Http/Controllers/Dashboard/SettingsController.php index fb8cf03a..d9d0f3c8 100644 --- a/app/Http/Controllers/Dashboard/SettingsController.php +++ b/app/Http/Controllers/Dashboard/SettingsController.php @@ -12,6 +12,7 @@ namespace CachetHQ\Cachet\Http\Controllers\Dashboard; use CachetHQ\Cachet\Models\User; +use CachetHQ\Cachet\Settings\Repository; use Exception; use GrahamCampbell\Binput\Facades\Binput; use Illuminate\Routing\Controller; @@ -220,7 +221,7 @@ class SettingsController extends Controller { $redirectUrl = Session::get('redirect_to', route('dashboard.settings.setup')); - $setting = app('setting'); + $setting = app(Repository::class); if (Binput::get('remove_banner') === '1') { $setting->set('app_banner', null); diff --git a/app/Http/Controllers/SetupController.php b/app/Http/Controllers/SetupController.php index 3de46056..d142d98b 100644 --- a/app/Http/Controllers/SetupController.php +++ b/app/Http/Controllers/SetupController.php @@ -12,6 +12,7 @@ namespace CachetHQ\Cachet\Http\Controllers; use CachetHQ\Cachet\Models\User; +use CachetHQ\Cachet\Settings\Repository; use Dotenv\Dotenv; use Dotenv\Exception\InvalidPathException; use GrahamCampbell\Binput\Facades\Binput; @@ -175,7 +176,7 @@ class SetupController extends Controller Auth::login($user); - $setting = app('setting'); + $setting = app(Repository::class); $settings = array_pull($postData, 'settings'); diff --git a/app/Settings/Cache.php b/app/Settings/Cache.php new file mode 100644 index 00000000..a8364872 --- /dev/null +++ b/app/Settings/Cache.php @@ -0,0 +1,103 @@ + + * @author James Brooks + */ +class Cache +{ + /** + * The filesystem instance. + * + * @var \Illuminate\Filesystem\Filesystem + */ + protected $files; + + /** + * Is path to the setting cache. + * + * @var string + */ + protected $path; + + /** + * Create a new settings cache instance. + * + * @param \Illuminate\Filesystem\Filesystem $files + * @param string $path + * + * @return void + */ + public function __construct(Filesystem $files, $path) + { + $this->files = $files; + $this->path = $path; + } + + /** + * Store the settings in the cache. + * + * @param string $env + * @param array $data + * + * @return void + */ + public function store($env, array $data) + { + $this->files->put($this->path($env), 'files->getRequire($this->path($env)); + } catch (Exception $e) { + return false; + } + } + + /** + * Clear the settings cache. + * + * Note that we're careful not to remove the .gitignore file. + * + * @return void + */ + public function clear() + { + $this->files->delete($this->files->allFiles($this->path)); + } + + /** + * Returns the settings cache path. + * + * @return string + */ + protected function path($env) + { + return "{$this->path}/{$env}.php"; + } +} diff --git a/app/Settings/Repository.php b/app/Settings/Repository.php index d7a1ba0a..fb9a9a33 100644 --- a/app/Settings/Repository.php +++ b/app/Settings/Repository.php @@ -13,6 +13,12 @@ namespace CachetHQ\Cachet\Settings; use CachetHQ\Cachet\Models\Setting; +/** + * This is the settings repository class. + * + * @author Graham Campbell + * @author James Brooks + */ class Repository { /** @@ -30,7 +36,7 @@ class Repository protected $stale = false; /** - * Create a new settings service instance. + * Create a new settings repository instance. * * @param \CachetHQ\Cachet\Models\Setting $model * diff --git a/app/Subscribers/CommandSubscriber.php b/app/Subscribers/CommandSubscriber.php index fdd38ea1..accddefe 100644 --- a/app/Subscribers/CommandSubscriber.php +++ b/app/Subscribers/CommandSubscriber.php @@ -11,6 +11,7 @@ namespace CachetHQ\Cachet\Subscribers; +use CachetHQ\Cachet\Settings\Cache; use Carbon\Carbon; use Exception; use Illuminate\Console\Command; @@ -26,7 +27,14 @@ use Illuminate\Contracts\Events\Dispatcher; class CommandSubscriber { /** - * The config repository. + * The settings cache instance. + * + * @var \CachetHQ\Cachet\Settings\Cache + */ + protected $cache; + + /** + * The config repository instance. * * @var \Illuminate\Contracts\Config\Repository */ @@ -35,12 +43,14 @@ class CommandSubscriber /** * Create a new command subscriber instance. * + * @param \CachetHQ\Cachet\Settings\Cache $cache * @param \Illuminate\Contracts\Config\Repository $config * * @return void */ - public function __construct(Repository $config) + public function __construct(Cache $cache, Repository $config) { + $this->cache = $cache; $this->config = $config; } @@ -59,7 +69,7 @@ class CommandSubscriber } /** - * Backup the databases. + * Clear the settings cache, and backup the databases. * * @param \Illuminate\Console\Command $command * @@ -67,6 +77,12 @@ class CommandSubscriber */ public function fire(Command $command) { + $command->line('Clearing settings cache...'); + + $this->cache->clear(); + + $command->line('Settings cache cleared!'); + $command->line('Backing up database...'); try { diff --git a/bootstrap/cachet/.gitignore b/bootstrap/cachet/.gitignore new file mode 100644 index 00000000..d6b7ef32 --- /dev/null +++ b/bootstrap/cachet/.gitignore @@ -0,0 +1,2 @@ +* +!.gitignore