add default value for getting options

This commit is contained in:
printempw 2016-08-11 18:10:14 +08:00
parent 67ef857544
commit 013edd87ac
3 changed files with 31 additions and 12 deletions

View File

@ -60,7 +60,7 @@ class Boot
throw new E("检测到 `textures` 文件夹已被删除,请重新运行 <a href='./setup'>安装程序</a>,或者手动放置一个。", -1, true);
}
if (Application::getVersion() != @Option::get('version')) {
if (Application::getVersion() != Option::get('version', '')) {
Http::redirect(Http::getBaseUrl().'/setup/update.php');
exit;
}

View File

@ -3,41 +3,60 @@
namespace App\Services;
use Illuminate\Database\Eloquent\Model;
use App\Exceptions\E;
use \Exception;
class Option
{
public static function get($key) {
public static function get($key, $default_value = null)
{
$option = OptionModel::where('option_name', $key)->first();
if (!$option) throw new E('Unexistent option.', 1);
if (!$option) {
if (!is_null($default_value)) {
return $default_value;
} else {
throw new Exception('Unexistent option.', 1);
}
}
return $option->option_value;
}
public static function set($key, $value) {
public static function set($key, $value)
{
$option = OptionModel::where('option_name', $key)->first();
if (!$option) throw new E('Unexistent option.', 1);
if (!$option)
throw new Exception('Unexistent option.', 1);
$option->option_value = $value;
return $option->save();
}
public static function add($key, $value) {
public static function add($key, $value)
{
if (self::has($key))
return true;
$option = new OptionModel;
$option->option_name = $key;
$option->option_value = $value;
$option->save();
}
public static function has($key) {
public static function has($key)
{
try {
OptionModel::where('option_name', $key)->firstOrFail();
} catch (Illuminate\Database\Eloquent\ModelNotFoundException $e) {
} catch (\Illuminate\Database\Eloquent\ModelNotFoundException $e) {
return false;
}
return true;
}
public static function delete($key) {
OptionModel::where('option_name', $key)->first()->delete();
public static function delete($key)
{
OptionModel::where('option_name', $key)->delete();
}
}

View File

@ -24,7 +24,7 @@ if (Config::checkDbConfig($db_config)) {
}
// If no update is available
if (App::getVersion() == @Option::get('version')) {
if (App::getVersion() == Option::get('version', '')) {
View::show('setup.locked');
exit;
}