Improved the config system

This commit is contained in:
Graham Campbell
2016-01-29 22:49:06 +00:00
parent 51e850ddc2
commit 1b24cdb1c5
35 changed files with 188 additions and 232 deletions
@@ -12,9 +12,10 @@
namespace CachetHQ\Cachet\Foundation\Providers;
use CachetHQ\Cachet\Config\Repository;
use CachetHQ\Cachet\Facades\Setting;
use CachetHQ\Cachet\Models\Setting as SettingModel;
use Exception;
use Illuminate\Contracts\Console\Kernel;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\ServiceProvider;
class ConfigServiceProvider extends ServiceProvider
@@ -26,40 +27,57 @@ class ConfigServiceProvider extends ServiceProvider
*/
public function boot()
{
$appDomain = $appLocale = $appTimezone = null;
try {
// Get app custom configuration.
$appDomain = Setting::get('app_domain');
$appLocale = Setting::get('app_locale');
$appTimezone = Setting::get('app_timezone');
// Setup Cors.
$allowedOrigins = $this->app->config->get('cors.defaults.allowedOrigins');
$allowedOrigins[] = Setting::get('app_domain');
// Add our allowed domains too.
if ($allowedDomains = Setting::get('allowed_domains')) {
$domains = explode(',', $allowedDomains);
foreach ($domains as $domain) {
$allowedOrigins[] = $domain;
}
if ($this->app->configurationIsCached()) {
if ($this->app->environment() === 'production') {
$this->app->terminating(function () {
if ($this->app->setting->stale()) {
$this->app->make(Kernel::class)->call('config:cache');
}
});
} else {
$allowedOrigins[] = $this->app->config->get('app.url');
$this->app->make(Kernel::class)->call('config:clear');
}
$this->app->config->set('cors.paths.api/v1/*.allowedOrigins', $allowedOrigins);
} catch (Exception $e) {
// Don't throw any errors, we may not be setup yet.
return;
}
// Override default app values.
$this->app->config->set('app.url', $appDomain ?: $this->app->config->get('app.url'));
$this->app->config->set('app.locale', $appLocale ?: $this->app->config->get('app.locale'));
$this->app->config->set('cachet.timezone', $appTimezone ?: $this->app->config->get('cachet.timezone'));
try {
$this->app->config->set('setting', $this->app->setting->all());
} catch (Exception $e) {
//
}
// Set custom lang.
$this->app->translator->setLocale($appLocale);
if ($appDomain = $this->app->config->get('setting.app_domain')) {
$this->app->config->set('app.url', $appDomain);
}
if ($appLocale = $this->app->config->get('setting.app.locale')) {
$this->app->config->set('app.locale', $appLocale);
$this->app->translator->setLocale($appLocale);
}
if ($appTimezone = $this->app->config->get('setting.app_timezone')) {
$this->app->config->set('cachet.timezone', $appTimezone);
}
$allowedOrigins = $this->app->config->get('cors.defaults.allowedOrigins');
if ($allowedDomains = $this->app->config->get('setting.allowed_domains')) {
$domains = explode(',', $allowedDomains);
foreach ($domains as $domain) {
$allowedOrigins[] = $domain;
}
} else {
$allowedOrigins[] = $this->app->config->get('app.url');
}
$this->app->config->set('cors.paths.api/v1/*.allowedOrigins', $allowedOrigins);
if ($this->app->environment() === 'production') {
$this->app->terminating(function () {
$this->app->make(Kernel::class)->call('config:cache');
});
}
}
/**