From ebaf4e939517b14499e403e0856a3012594c0103 Mon Sep 17 00:00:00 2001 From: Graham Campbell Date: Wed, 25 May 2016 09:49:48 +0100 Subject: [PATCH 01/10] WIP --- app/Subscribers/CommandSubscriber.php | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/app/Subscribers/CommandSubscriber.php b/app/Subscribers/CommandSubscriber.php index fdd38ea1e653..b73a7b6e8040 100644 --- a/app/Subscribers/CommandSubscriber.php +++ b/app/Subscribers/CommandSubscriber.php @@ -65,7 +65,7 @@ public function subscribe(Dispatcher $events) * * @return void */ - public function fire(Command $command) + public function backup(Command $command) { $command->line('Backing up database...'); @@ -84,4 +84,20 @@ public function fire(Command $command) $command->line('Backup completed!'); } + + /** + * Clear the cachet settings cache. + * + * @param \Illuminate\Console\Command $command + * + * @return void + */ + public function clear(Command $command) + { + $command->line('Clearing settings cache...'); + + $this->loader->clear(); + + $command->line('Settings cache cleared!'); + } } From 527e5872df3b5d1b8256accca522be6e68794334 Mon Sep 17 00:00:00 2001 From: Graham Campbell Date: Wed, 25 May 2016 10:55:22 +0100 Subject: [PATCH 02/10] Fixed typo --- app/Settings/Repository.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Settings/Repository.php b/app/Settings/Repository.php index d7a1ba0a18fe..d709ef9b08aa 100644 --- a/app/Settings/Repository.php +++ b/app/Settings/Repository.php @@ -30,7 +30,7 @@ class Repository protected $stale = false; /** - * Create a new settings service instance. + * Create a new settings repository instance. * * @param \CachetHQ\Cachet\Models\Setting $model * From 200cd62dc9d29dde6fa4617a824131ba67a58968 Mon Sep 17 00:00:00 2001 From: Graham Campbell Date: Wed, 25 May 2016 10:55:33 +0100 Subject: [PATCH 03/10] Resolve via class name --- app/Http/Controllers/Dashboard/SettingsController.php | 3 ++- app/Http/Controllers/SetupController.php | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/app/Http/Controllers/Dashboard/SettingsController.php b/app/Http/Controllers/Dashboard/SettingsController.php index fb8cf03a358d..d9d0f3c86f61 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 @@ public function postSettings() { $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 3de460567cfa..d142d98b38cf 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 @@ public function postStep3() Auth::login($user); - $setting = app('setting'); + $setting = app(Repository::class); $settings = array_pull($postData, 'settings'); From c3bc8fdd2d9ea6c0d38e547c026d7d2833c8fdf2 Mon Sep 17 00:00:00 2001 From: Graham Campbell Date: Wed, 25 May 2016 10:55:49 +0100 Subject: [PATCH 04/10] Refactor settings caching --- .../Providers/ConfigServiceProvider.php | 30 +++--- app/Settings/Cache.php | 97 +++++++++++++++++++ bootstrap/cachet/.gitignore | 2 + 3 files changed, 114 insertions(+), 15 deletions(-) create mode 100644 app/Settings/Cache.php create mode 100644 bootstrap/cachet/.gitignore diff --git a/app/Foundation/Providers/ConfigServiceProvider.php b/app/Foundation/Providers/ConfigServiceProvider.php index 0e150f3a452f..d9d49331a91c 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,17 +33,14 @@ class ConfigServiceProvider extends ServiceProvider */ public function boot() { - $path = $this->app->bootstrapPath().'/cache/cachet.'.$this->app->environment().'.php'; - - 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); + $env = $this->app->environment(); + $repo = $app->make(Repository::class); + $cache = $app->make(Cache::class); + $loaded = $cache->load(); + + $this->app->terminating(function () use ($env, $repo, $cache, $loaded) { + if ($repo->stale() || $loaded === false) { + $cache->store($env, $repo->all()); } }); @@ -51,7 +49,7 @@ public function boot() $defaultSettings = $this->app->config->get('setting'); // Get the configured settings. - $appSettings = $cache === false ? $this->app->setting->all() : $cache; + $appSettings = $loaded === false ? $repo->all() : $loaded; // Merge the settings $settings = array_merge($defaultSettings, $appSettings); @@ -95,10 +93,12 @@ public function boot() */ public function register() { - $this->app->singleton('setting', function () { - return new Repository(new SettingModel()); + $this->app->singleton(Cache::class, function ($app) { + return new Cache($app->filesystem, $app->bootstrapPath().'/cachet'); }); - $this->app->alias('setting', Repository::class); + $this->app->singleton(Repository::class, function () { + return new Repository(new SettingModel()); + }); } } diff --git a/app/Settings/Cache.php b/app/Settings/Cache.php new file mode 100644 index 000000000000..e5d476d19fc6 --- /dev/null +++ b/app/Settings/Cache.php @@ -0,0 +1,97 @@ +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/bootstrap/cachet/.gitignore b/bootstrap/cachet/.gitignore new file mode 100644 index 000000000000..d6b7ef32c847 --- /dev/null +++ b/bootstrap/cachet/.gitignore @@ -0,0 +1,2 @@ +* +!.gitignore From 8df65b2dc30ec542aad09ee46aa7d809c2ecdd63 Mon Sep 17 00:00:00 2001 From: Graham Campbell Date: Wed, 25 May 2016 10:58:14 +0100 Subject: [PATCH 05/10] Finished off the subscriber --- app/Subscribers/CommandSubscriber.php | 40 +++++++++++++-------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/app/Subscribers/CommandSubscriber.php b/app/Subscribers/CommandSubscriber.php index b73a7b6e8040..accddefe9e53 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 @@ 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,14 +69,20 @@ public function subscribe(Dispatcher $events) } /** - * Backup the databases. + * Clear the settings cache, and backup the databases. * * @param \Illuminate\Console\Command $command * * @return void */ - public function backup(Command $command) + public function fire(Command $command) { + $command->line('Clearing settings cache...'); + + $this->cache->clear(); + + $command->line('Settings cache cleared!'); + $command->line('Backing up database...'); try { @@ -84,20 +100,4 @@ public function backup(Command $command) $command->line('Backup completed!'); } - - /** - * Clear the cachet settings cache. - * - * @param \Illuminate\Console\Command $command - * - * @return void - */ - public function clear(Command $command) - { - $command->line('Clearing settings cache...'); - - $this->loader->clear(); - - $command->line('Settings cache cleared!'); - } } From 3a86862e4f2953490d57ffaf7cbc6a438881ca05 Mon Sep 17 00:00:00 2001 From: Graham Campbell Date: Wed, 25 May 2016 05:58:40 -0400 Subject: [PATCH 06/10] Applied fixes from StyleCI [ci skip] --- app/Settings/Cache.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/app/Settings/Cache.php b/app/Settings/Cache.php index e5d476d19fc6..47c5681eeb64 100644 --- a/app/Settings/Cache.php +++ b/app/Settings/Cache.php @@ -44,7 +44,7 @@ public function __construct(Filesystem $files, $path) } /** - * Store the settings in the cache, + * Store the settings in the cache,. * * @param string $env * @param array $data @@ -93,5 +93,4 @@ protected function path($env) { return "{$this->path}/{$env}.php"; } - } From 414efb0ce7a5201ae6c20cc74eea2cedead5f697 Mon Sep 17 00:00:00 2001 From: Graham Campbell Date: Wed, 25 May 2016 10:59:11 +0100 Subject: [PATCH 07/10] Fixed typo --- app/Settings/Cache.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Settings/Cache.php b/app/Settings/Cache.php index 47c5681eeb64..ba772659b80e 100644 --- a/app/Settings/Cache.php +++ b/app/Settings/Cache.php @@ -44,7 +44,7 @@ public function __construct(Filesystem $files, $path) } /** - * Store the settings in the cache,. + * Store the settings in the cache. * * @param string $env * @param array $data From 0bf2039e60d03caf451c3d1b62d03caca8e5cf87 Mon Sep 17 00:00:00 2001 From: James Brooks Date: Wed, 25 May 2016 11:14:16 +0100 Subject: [PATCH 08/10] Fix some bits --- app/Foundation/Providers/ConfigServiceProvider.php | 8 ++++---- app/Settings/Cache.php | 7 +++++++ 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/app/Foundation/Providers/ConfigServiceProvider.php b/app/Foundation/Providers/ConfigServiceProvider.php index d9d49331a91c..7db508655aac 100644 --- a/app/Foundation/Providers/ConfigServiceProvider.php +++ b/app/Foundation/Providers/ConfigServiceProvider.php @@ -34,9 +34,9 @@ class ConfigServiceProvider extends ServiceProvider public function boot() { $env = $this->app->environment(); - $repo = $app->make(Repository::class); - $cache = $app->make(Cache::class); - $loaded = $cache->load(); + $repo = $this->app->make(Repository::class); + $cache = $this->app->make(Cache::class); + $loaded = $cache->load($env); $this->app->terminating(function () use ($env, $repo, $cache, $loaded) { if ($repo->stale() || $loaded === false) { @@ -94,7 +94,7 @@ public function boot() public function register() { $this->app->singleton(Cache::class, function ($app) { - return new Cache($app->filesystem, $app->bootstrapPath().'/cachet'); + return new Cache($app->files, $app->bootstrapPath().'/cachet'); }); $this->app->singleton(Repository::class, function () { diff --git a/app/Settings/Cache.php b/app/Settings/Cache.php index ba772659b80e..51f69807a653 100644 --- a/app/Settings/Cache.php +++ b/app/Settings/Cache.php @@ -11,8 +11,15 @@ namespace CachetHQ\Cachet\Settings; +use Exception; use Illuminate\Filesystem\Filesystem; +/** + * This is the Cache class. + * + * @author Graham Campbell + * @author James Brooks + */ class Cache { /** From 72577a04b79462a1bcaa1f06672112fe520c011a Mon Sep 17 00:00:00 2001 From: Graham Campbell Date: Wed, 25 May 2016 11:23:48 +0100 Subject: [PATCH 09/10] Fixed headers --- app/Settings/Cache.php | 2 +- app/Settings/Repository.php | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/app/Settings/Cache.php b/app/Settings/Cache.php index 51f69807a653..a83648720656 100644 --- a/app/Settings/Cache.php +++ b/app/Settings/Cache.php @@ -15,7 +15,7 @@ use Illuminate\Filesystem\Filesystem; /** - * This is the Cache class. + * This is the settings cache class. * * @author Graham Campbell * @author James Brooks diff --git a/app/Settings/Repository.php b/app/Settings/Repository.php index d709ef9b08aa..fb9a9a33feb5 100644 --- a/app/Settings/Repository.php +++ b/app/Settings/Repository.php @@ -13,6 +13,12 @@ use CachetHQ\Cachet\Models\Setting; +/** + * This is the settings repository class. + * + * @author Graham Campbell + * @author James Brooks + */ class Repository { /** From 13d0ff320aa66d651c613491e5ee77fc0e991c03 Mon Sep 17 00:00:00 2001 From: Graham Campbell Date: Wed, 25 May 2016 11:28:03 +0100 Subject: [PATCH 10/10] Another attempt --- .../Providers/ConfigServiceProvider.php | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/app/Foundation/Providers/ConfigServiceProvider.php b/app/Foundation/Providers/ConfigServiceProvider.php index 7db508655aac..626dea9365a6 100644 --- a/app/Foundation/Providers/ConfigServiceProvider.php +++ b/app/Foundation/Providers/ConfigServiceProvider.php @@ -38,21 +38,19 @@ public function boot() $cache = $this->app->make(Cache::class); $loaded = $cache->load($env); - $this->app->terminating(function () use ($env, $repo, $cache, $loaded) { - if ($repo->stale() || $loaded === false) { - $cache->store($env, $repo->all()); + $this->app->terminating(function () use ($repo, $cache) { + if ($repo->stale()) { + $cache->clear(); } }); try { - // Get the default settings. - $defaultSettings = $this->app->config->get('setting'); - - // Get the configured settings. - $appSettings = $loaded === false ? $repo->all() : $loaded; + if ($loaded === false) { + $loaded = $repo->all(); + $cache->store($env, $loaded); + } - // 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) {