Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactored settings caching #1823

Merged
merged 11 commits into from
May 25, 2016
30 changes: 15 additions & 15 deletions app/Foundation/Providers/ConfigServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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, '<?php return '.var_export($this->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());
}
});

Expand 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);
Expand Down Expand Up @@ -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());
});
}
}
3 changes: 2 additions & 1 deletion app/Http/Controllers/Dashboard/SettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion app/Http/Controllers/SetupController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -175,7 +176,7 @@ public function postStep3()

Auth::login($user);

$setting = app('setting');
$setting = app(Repository::class);

$settings = array_pull($postData, 'settings');

Expand Down
96 changes: 96 additions & 0 deletions app/Settings/Cache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php

/*
* This file is part of Cachet.
*
* (c) Alt Three Services Limited
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace CachetHQ\Cachet\Settings;

use Illuminate\Filesystem\Filesystem;

class Cache
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing class header?

{
/**
* 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), '<?php return '.var_export($data, true).';'.PHP_EOL);
}

/**
* Load the settings from the cache.
*
* @param string $env
*
* @return array|false
*/
public function load($env)
{
try {
return $this->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";
}
}
2 changes: 1 addition & 1 deletion app/Settings/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
22 changes: 19 additions & 3 deletions app/Subscribers/CommandSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace CachetHQ\Cachet\Subscribers;

use CachetHQ\Cachet\Settings\Cache;
use Carbon\Carbon;
use Exception;
use Illuminate\Console\Command;
Expand All @@ -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
*/
Expand All @@ -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;
}

Expand All @@ -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 fire(Command $command)
{
$command->line('Clearing settings cache...');

$this->cache->clear();

$command->line('Settings cache cleared!');

$command->line('Backing up database...');

try {
Expand Down
2 changes: 2 additions & 0 deletions bootstrap/cachet/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*
!.gitignore