add installation check at BootServiceProvider
This commit is contained in:
parent
f7458bc638
commit
cc74d4020d
79
app/Providers/BootServiceProvider.php
Normal file
79
app/Providers/BootServiceProvider.php
Normal file
|
|
@ -0,0 +1,79 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Providers;
|
||||||
|
|
||||||
|
use Illuminate\Support\ServiceProvider;
|
||||||
|
use App\Exceptions\E;
|
||||||
|
|
||||||
|
class BootServiceProvider extends ServiceProvider
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Bootstrap any application services.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function boot()
|
||||||
|
{
|
||||||
|
\View::addExtension('tpl', 'blade');
|
||||||
|
$this->checkDbConfig();
|
||||||
|
$this->checkInstallation();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function checkDbConfig()
|
||||||
|
{
|
||||||
|
// use error control to hide shitty connect warnings
|
||||||
|
@$conn = new \mysqli(
|
||||||
|
config('database.connections.mysql.host'),
|
||||||
|
config('database.connections.mysql.username'),
|
||||||
|
config('database.connections.mysql.password'),
|
||||||
|
config('database.connections.mysql.database'),
|
||||||
|
config('database.connections.mysql.port')
|
||||||
|
);
|
||||||
|
|
||||||
|
if ($conn->connect_error)
|
||||||
|
throw new E("无法连接至 MySQL 服务器,请检查你的配置:".$conn->connect_error, $conn->connect_errno, true);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function checkInstallation()
|
||||||
|
{
|
||||||
|
if (!$this->checkTableExist()) {
|
||||||
|
\Http::redirect(url('/setup/index.php'));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!is_dir(BASE_DIR.'/textures/')) {
|
||||||
|
throw new E("检测到 `textures` 文件夹已被删除,请重新运行 <a href='".url('setup')."'>安装程序</a>,或者手动放置一个。", -1, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (config('app.version') != \Option::get('version', '')) {
|
||||||
|
\Http::redirect(url('/setup/update.php'));
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function checkTableExist()
|
||||||
|
{
|
||||||
|
$tables = ['users', 'closets', 'players', 'textures', 'options'];
|
||||||
|
|
||||||
|
foreach ($tables as $table_name) {
|
||||||
|
// prefix will be added automatically
|
||||||
|
if (!\Schema::hasTable($table_name)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Register any application services.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function register()
|
||||||
|
{
|
||||||
|
//
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -41,12 +41,19 @@ class Database
|
||||||
*/
|
*/
|
||||||
public function __construct($config = null)
|
public function __construct($config = null)
|
||||||
{
|
{
|
||||||
|
if (is_null($config)) {
|
||||||
|
$db_config = require BASE_DIR.'/config/database.php';
|
||||||
|
$config = $db_config['connections']['mysql'];
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->config = $config;
|
||||||
|
|
||||||
@$this->connection = new \mysqli(
|
@$this->connection = new \mysqli(
|
||||||
config('database.connections.mysql.host'),
|
$this->config['host'],
|
||||||
config('database.connections.mysql.username'),
|
$this->config['username'],
|
||||||
config('database.connections.mysql.password'),
|
$this->config['password'],
|
||||||
config('database.connections.mysql.database'),
|
$this->config['database'],
|
||||||
config('database.connections.mysql.port')
|
$this->config['port']
|
||||||
);
|
);
|
||||||
|
|
||||||
if ($this->connection->connect_error)
|
if ($this->connection->connect_error)
|
||||||
|
|
@ -138,7 +145,7 @@ class Database
|
||||||
|
|
||||||
public function hasTable($table_name)
|
public function hasTable($table_name)
|
||||||
{
|
{
|
||||||
$sql = "SELECT table_name FROM `INFORMATION_SCHEMA`.`TABLES` WHERE (table_name = '$table_name') AND TABLE_SCHEMA='".Config::getDbConfig()['database']."'";
|
$sql = "SELECT table_name FROM `INFORMATION_SCHEMA`.`TABLES` WHERE (table_name = '$table_name') AND TABLE_SCHEMA='".$this->config['database']."'";
|
||||||
return ($this->query($sql)->num_rows != 0) ? true : false;
|
return ($this->query($sql)->num_rows != 0) ? true : false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -191,8 +198,9 @@ class Database
|
||||||
|
|
||||||
public function __destruct()
|
public function __destruct()
|
||||||
{
|
{
|
||||||
if (!is_null($this->connection))
|
if (!is_null($this->connection)) {
|
||||||
$this->connection->close();
|
$this->connection->close();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
28
app/Services/Database/Migration.php
Normal file
28
app/Services/Database/Migration.php
Normal file
|
|
@ -0,0 +1,28 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Services\Database;
|
||||||
|
|
||||||
|
class Migration
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Create tables, prefix will be added automatically
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public static function creatTables()
|
||||||
|
{
|
||||||
|
require BASE_DIR."/setup/tables.php";
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function __callStatic($method, $args)
|
||||||
|
{
|
||||||
|
if (strpos($method, 'import') !== false) {
|
||||||
|
$filename = BASE_DIR."/setup/migrations/".snake_case($method).".php";
|
||||||
|
if (file_exists($filename)) {
|
||||||
|
return require $filename;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
throw new \InvalidArgumentException('Non-existent migration');
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -16,8 +16,12 @@ class Http
|
||||||
public static function redirect($url, $msg = "")
|
public static function redirect($url, $msg = "")
|
||||||
{
|
{
|
||||||
if ($msg !== "") {
|
if ($msg !== "") {
|
||||||
Session::flash('msg', $msg);
|
if (app()->bound('session')) {
|
||||||
Session::save();
|
Session::flash('msg', $msg);
|
||||||
|
Session::save();
|
||||||
|
} else {
|
||||||
|
$_SESSION['msg'] = $msg;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!headers_sent()) {
|
if (!headers_sent()) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user