Refactor settings caching
This commit is contained in:
@@ -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';
|
||||
$env = $this->app->environment();
|
||||
$repo = $app->make(Repository::class);
|
||||
$cache = $app->make(Cache::class);
|
||||
$loaded = $cache->load();
|
||||
|
||||
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);
|
||||
$this->app->terminating(function () use ($env, $repo, $cache, $loaded) {
|
||||
if ($repo->stale() || $loaded === false) {
|
||||
$cache->store($env, $repo->all());
|
||||
}
|
||||
});
|
||||
|
||||
@@ -51,7 +49,7 @@ class ConfigServiceProvider extends ServiceProvider
|
||||
$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 @@ 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->filesystem, $app->bootstrapPath().'/cachet');
|
||||
});
|
||||
|
||||
$this->app->alias('setting', Repository::class);
|
||||
$this->app->singleton(Repository::class, function () {
|
||||
return new Repository(new SettingModel());
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
97
app/Settings/Cache.php
Normal file
97
app/Settings/Cache.php
Normal file
@@ -0,0 +1,97 @@
|
||||
<?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
|
||||
{
|
||||
/**
|
||||
* 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
bootstrap/cachet/.gitignore
vendored
Normal file
2
bootstrap/cachet/.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
*
|
||||
!.gitignore
|
||||
Reference in New Issue
Block a user