Enhance settings performance queries
This commit is contained in:
@@ -147,6 +147,7 @@ return [
|
||||
'CachetHQ\Cachet\Providers\RoutingServiceProvider',
|
||||
'CachetHQ\Cachet\Providers\ViewComposerServiceProvider',
|
||||
'CachetHQ\Cachet\Providers\LoadConfigServiceProvider',
|
||||
'CachetHQ\Cachet\Providers\SettingsServiceProvider',
|
||||
|
||||
],
|
||||
|
||||
@@ -185,7 +186,7 @@ return [
|
||||
'Response' => 'Illuminate\Support\Facades\Response',
|
||||
'Route' => 'Illuminate\Support\Facades\Route',
|
||||
'Session' => 'Illuminate\Support\Facades\Session',
|
||||
'Setting' => 'CachetHQ\Cachet\Models\Setting',
|
||||
'Setting' => 'CachetHQ\Cachet\Facades\Setting',
|
||||
'Str' => 'Illuminate\Support\Str',
|
||||
|
||||
],
|
||||
|
||||
21
src/Facades/Setting.php
Normal file
21
src/Facades/Setting.php
Normal file
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
namespace CachetHQ\Cachet\Facades;
|
||||
|
||||
use Illuminate\Support\Facades\Facade;
|
||||
|
||||
/**
|
||||
* @see \CachetHQ\Cachet\Services\SettingsService
|
||||
*/
|
||||
class Setting extends Facade
|
||||
{
|
||||
/**
|
||||
* Get the registered name of the component.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected static function getFacadeAccessor()
|
||||
{
|
||||
return 'setting';
|
||||
}
|
||||
}
|
||||
@@ -2,8 +2,8 @@
|
||||
|
||||
namespace CachetHQ\Cachet\Http\Controllers;
|
||||
|
||||
use CachetHQ\Cachet\Facades\Setting;
|
||||
use CachetHQ\Cachet\Models\Incident;
|
||||
use CachetHQ\Cachet\Models\Setting;
|
||||
use Illuminate\Routing\Controller;
|
||||
use Illuminate\Support\Facades\Response;
|
||||
use Roumen\Feed\Facades\Feed;
|
||||
|
||||
@@ -2,9 +2,9 @@
|
||||
|
||||
namespace CachetHQ\Cachet\Http\Controllers;
|
||||
|
||||
use CachetHQ\Cachet\Facades\Setting;
|
||||
use CachetHQ\Cachet\Models\Component;
|
||||
use CachetHQ\Cachet\Models\Incident;
|
||||
use CachetHQ\Cachet\Models\Setting;
|
||||
use Exception;
|
||||
use GrahamCampbell\Binput\Facades\Binput;
|
||||
use GrahamCampbell\Markdown\Facades\Markdown;
|
||||
|
||||
@@ -2,8 +2,8 @@
|
||||
|
||||
namespace CachetHQ\Cachet\Http\Controllers;
|
||||
|
||||
use CachetHQ\Cachet\Facades\Setting;
|
||||
use CachetHQ\Cachet\Models\Incident;
|
||||
use CachetHQ\Cachet\Models\Setting;
|
||||
use Illuminate\Routing\Controller;
|
||||
use Illuminate\Support\Facades\Response;
|
||||
use Thujohn\Rss\RssFacade;
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
namespace CachetHQ\Cachet\Http\Controllers;
|
||||
|
||||
use CachetHQ\Cachet\Models\Setting;
|
||||
use CachetHQ\Cachet\Facades\Setting;
|
||||
use CachetHQ\Cachet\Models\User;
|
||||
use GrahamCampbell\Binput\Facades\Binput;
|
||||
use Illuminate\Routing\Controller;
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
|
||||
namespace CachetHQ\Cachet\Models;
|
||||
|
||||
use ErrorException;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
/**
|
||||
@@ -27,32 +26,4 @@ class Setting extends Model
|
||||
* @var string[]
|
||||
*/
|
||||
protected $attributes = ['value' => ''];
|
||||
|
||||
/**
|
||||
* Returns a setting from the database.
|
||||
*
|
||||
* @param string $settingName
|
||||
* @param bool $checkEnv
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
public static function get($settingName, $checkEnv = true)
|
||||
{
|
||||
$setting = null;
|
||||
|
||||
try {
|
||||
$setting = self::whereName($settingName)->first()->value;
|
||||
} catch (ErrorException $e) {
|
||||
if ($checkEnv) {
|
||||
$env = getenv(strtoupper($settingName));
|
||||
if (!$env) {
|
||||
return $env;
|
||||
}
|
||||
}
|
||||
|
||||
return $setting;
|
||||
}
|
||||
|
||||
return $setting;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
namespace CachetHQ\Cachet\Providers;
|
||||
|
||||
use CachetHQ\Cachet\Models\Setting;
|
||||
use CachetHQ\Cachet\Facades\Setting;
|
||||
use Illuminate\Database\QueryException;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
|
||||
|
||||
32
src/Providers/SettingsServiceProvider.php
Normal file
32
src/Providers/SettingsServiceProvider.php
Normal file
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
namespace CachetHQ\Cachet\Providers;
|
||||
|
||||
use CachetHQ\Cachet\Models\Setting as SettingModel;
|
||||
use CachetHQ\Cachet\Services\SettingsService;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
|
||||
class SettingsServiceProvider extends ServiceProvider
|
||||
{
|
||||
/**
|
||||
* Boot the service provider.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function boot()
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the service provider.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function register()
|
||||
{
|
||||
$this->app->bindShared('setting', function ($app) {
|
||||
return new SettingsService(new SettingModel());
|
||||
});
|
||||
}
|
||||
}
|
||||
68
src/Services/SettingsService.php
Normal file
68
src/Services/SettingsService.php
Normal file
@@ -0,0 +1,68 @@
|
||||
<?php
|
||||
|
||||
namespace CachetHQ\Cachet\Services;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class SettingsService
|
||||
{
|
||||
/**
|
||||
* The eloquent model instance.
|
||||
*
|
||||
* @var \CachetHQ\Cachet\Models\Setting
|
||||
*/
|
||||
protected $model;
|
||||
|
||||
/**
|
||||
* Cache of the settings.
|
||||
*
|
||||
* @var null|array
|
||||
*/
|
||||
protected $settings = null;
|
||||
|
||||
/**
|
||||
* Create a new settings service instance.
|
||||
*
|
||||
* @param \CachetHQ\Cachet\Models\Setting $model
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(Model $model)
|
||||
{
|
||||
$this->model = $model;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a setting from the database.
|
||||
*
|
||||
* @param string $settingName
|
||||
* @param bool $checkEnv
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
public function get($settingName, $checkEnv = true)
|
||||
{
|
||||
$setting = null;
|
||||
|
||||
try {
|
||||
if (! $this->settings) {
|
||||
$this->settings = $this->model->all()->lists('value', 'name');
|
||||
}
|
||||
|
||||
if (array_key_exists($settingName, $this->settings)) {
|
||||
return $this->settings[$settingName];
|
||||
}
|
||||
} catch (ErrorException $e) {
|
||||
if ($checkEnv) {
|
||||
$env = getenv(strtoupper($settingName));
|
||||
if (!$env) {
|
||||
return $env;
|
||||
}
|
||||
}
|
||||
|
||||
return $setting;
|
||||
}
|
||||
|
||||
return $setting;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user