diff --git a/app/Http/Controllers/SetupController.php b/app/Http/Controllers/SetupController.php index 483e3d35..8ff93e47 100644 --- a/app/Http/Controllers/SetupController.php +++ b/app/Http/Controllers/SetupController.php @@ -2,6 +2,7 @@ namespace App\Http\Controllers; +use DB; use Log; use File; use Utils; @@ -17,20 +18,69 @@ use App\Exceptions\PrettyPageException; class SetupController extends Controller { - public function welcome() + public function database(Request $request) { - $type = get_db_type(); + config([ + 'database.connections.temp.driver' => $request->input('type'), + 'database.connections.temp.host' => $request->input('host'), + 'database.connections.temp.port' => $request->input('port'), + 'database.connections.temp.username' => $request->input('username'), + 'database.connections.temp.password' => $request->input('password'), + 'database.connections.temp.database' => $request->input('db') == '' ? null : $request->input('db'), + 'database.connections.temp.prefix' => $request->input('prefix') == '' ? null : $request->input('prefix'), + ]); - if ($type === 'SQLite') { - // @codeCoverageIgnoreStart - $server = get_db_config()['database']; - // @codeCoverageIgnoreEnd - } else { - $config = get_db_config(); - $server = "{$config['username']}@{$config['host']}"; + try { + DB::connection('temp')->getPdo(); + } catch (\Exception $e) { + $msg = iconv('gbk', 'utf-8', $e->getMessage()); + $type = humanize_db_type($request->input('type')); + + throw new PrettyPageException( + trans('setup.database.connection-error', compact('msg', 'type')), + $e->getCode() + ); } - return view('setup.wizard.welcome')->with(compact('type', 'server')); + $content = File::get('.env'); + $content = str_replace( + 'DB_CONNECTION = '.env('DB_CONNECTION'), + 'DB_CONNECTION = '.$request->input('type'), + $content + ); + $content = str_replace( + 'DB_HOST = '.env('DB_HOST'), + 'DB_HOST = '.$request->input('host'), + $content + ); + $content = str_replace( + 'DB_PORT = '.env('DB_PORT'), + 'DB_PORT = '.$request->input('port'), + $content + ); + $content = str_replace( + 'DB_DATABASE = '.env('DB_DATABASE'), + 'DB_DATABASE = '.$request->input('db'), + $content + ); + $content = str_replace( + 'DB_USERNAME = '.env('DB_USERNAME'), + 'DB_USERNAME = '.$request->input('username'), + $content + ); + $content = str_replace( + 'DB_PASSWORD = '.env('DB_PASSWORD'), + 'DB_PASSWORD = '.$request->input('password'), + $content + ); + $content = str_replace( + 'DB_PREFIX = '.env('DB_PREFIX'), + 'DB_PREFIX = '.$request->input('prefix'), + $content + ); + File::put('.env', $content); + + return redirect('setup/info'); } public function info() diff --git a/app/Providers/BootServiceProvider.php b/app/Providers/BootServiceProvider.php index 65e9b6dd..e3926968 100644 --- a/app/Providers/BootServiceProvider.php +++ b/app/Providers/BootServiceProvider.php @@ -65,7 +65,7 @@ class BootServiceProvider extends ServiceProvider } $msg = iconv('gbk', 'utf-8', $e->getMessage()); - $type = get_db_type(); + $type = humanize_db_type(); throw new PrettyPageException( trans('setup.database.connection-error', compact('msg', 'type')), diff --git a/app/helpers.php b/app/helpers.php index a35a3998..856dd889 100644 --- a/app/helpers.php +++ b/app/helpers.php @@ -448,9 +448,9 @@ if (! function_exists('die_with_utf8_encoding')) { } } -if (! function_exists('get_db_type')) { +if (! function_exists('humanize_db_type')) { - function get_db_type($type = null) + function humanize_db_type($type = null) { $map = [ 'mysql' => 'MySQL', diff --git a/resources/lang/en/setup.yml b/resources/lang/en/setup.yml index 9e95e955..2a964626 100644 --- a/resources/lang/en/setup.yml +++ b/resources/lang/en/setup.yml @@ -1,6 +1,5 @@ database: connection-error: "Unable to connect to the target :type database, please check your configuration. The server replied with: :msg" - connection-success: Connect to the target :type database [:server] successfully, just click NEXT to start installation. table-already-exists: There are some tables already exist in target database, which names conflict with ones we are going to create. To avoid data loss, please manually delete these tables :tables, or set a different table prefix. file: @@ -43,6 +42,20 @@ wizard: button: Next text: Welcome to Blessing Skin Server v:version! + database: + title: Database + text: The database is used for storing data of Blessing Skin. + + type: Database Type + host: Database Host + port: Database Port + username: Database Username + password: Database Password + db: Database Name + db-notice: You should provide the path to SQLite file and there is no need to fill other blanks if you use SQLite. + prefix: Prefix of Database Table (Optional) + prefix-notice: You don't need to use this option unless you want to install multiple Blessing Skin Server into one database. + info: title: Information needed button: Run install diff --git a/resources/lang/zh_CN/setup.yml b/resources/lang/zh_CN/setup.yml index 9f7415f0..92b3e74a 100644 --- a/resources/lang/zh_CN/setup.yml +++ b/resources/lang/zh_CN/setup.yml @@ -1,6 +1,5 @@ database: connection-error: 无法连接至 :type 服务器,请检查你的配置。服务器返回的信息:「:msg」 - connection-success: 成功连接至 :type 服务器 [:server] ,点击下一步以开始安装。 table-already-exists: 检测到目标数据库中已存在如下数据表 :tables,它们与本程序即将创建的数据表名称冲突,为了避免原有数据被覆盖,请手动删除它们,或者为本程序选择一个不同的数据表前缀。 file: @@ -43,6 +42,20 @@ wizard: button: 下一步 text: 欢迎使用 Blessing Skin Server v:version! + database: + title: 填写数据库信息 + text: 您提供的数据库将用于存储 Blessing Skin 的数据 + + type: 数据库类型 + host: 数据库服务器地址 + port: 端口 + username: 数据库用户名 + password: 数据库密码 + db: 数据库名称 + db-notice: 如果您使用 SQLite,那么您应该填写 SQLite 数据库文件的路径,并且无需填写其它信息 + prefix: 数据表前缀(可选) + prefix-notice: 通常您不需要填写此项,除非您有向同一数据库安装多个 Blessing Skin 的需要 + info: title: 填写信息 button: 开始安装 diff --git a/resources/views/setup/wizard/database.tpl b/resources/views/setup/wizard/database.tpl new file mode 100644 index 00000000..e0b2f883 --- /dev/null +++ b/resources/views/setup/wizard/database.tpl @@ -0,0 +1,89 @@ +@extends('setup.master') + +@section('content') +
@lang('setup.wizard.database.text')
+ + +@endsection + +@section('script') + +@endsection diff --git a/resources/views/setup/wizard/welcome.tpl b/resources/views/setup/wizard/welcome.tpl index ce085b01..bfe7e498 100644 --- a/resources/views/setup/wizard/welcome.tpl +++ b/resources/views/setup/wizard/welcome.tpl @@ -6,9 +6,8 @@@lang('setup.wizard.welcome.text', ['version' => config('app.version')])
-@lang('setup.database.connection-success', ['server' => $server, 'type' => $type])
- @lang('setup.wizard.welcome.button') + @lang('setup.wizard.welcome.button')
@endsection diff --git a/routes/setup.php b/routes/setup.php index 6dc365bf..7a254df8 100644 --- a/routes/setup.php +++ b/routes/setup.php @@ -17,7 +17,9 @@ Route::group(['prefix' => 'setup'], function () { Route::group(['middleware' => 'setup'], function () { - Route::get ('/', 'SetupController@welcome'); + Route::view('/', 'setup.wizard.welcome'); + Route::view('/database', 'setup.wizard.database'); + Route::post('/database', 'SetupController@database'); Route::get ('/info', 'SetupController@info'); Route::post('/finish', 'SetupController@finish'); }); diff --git a/storage/database.db b/storage/database.db new file mode 100644 index 00000000..e69de29b diff --git a/tests/SetupControllerTest.php b/tests/SetupControllerTest.php index d022d0db..6985ea70 100644 --- a/tests/SetupControllerTest.php +++ b/tests/SetupControllerTest.php @@ -37,16 +37,32 @@ class SetupControllerTest extends TestCase public function testWelcome() { - $type = get_db_type(); + $this->get('/setup')->assertViewIs('setup.wizard.welcome'); + } - if ($type === 'SQLite') { - $server = get_db_config()['database']; - } else { - $config = get_db_config(); - $server = "{$config['username']}@{$config['host']}"; - } + public function testDatabase() + { + $fake = [ + 'type' => 'mysql', + 'host' => '127.0.0.1', + 'port' => '3306', + 'db' => env('DB_DATABASE'), + 'username' => env('DB_USERNAME'), + 'password' => env('DB_PASSWORD'), + 'prefix' => '', + ]; + File::shouldReceive('get')->with('.env')->andReturn(''); + File::shouldReceive('put')->with('.env', ''); + $this->post('/setup/database', $fake)->assertRedirect('/setup/info'); + } - $this->get('/setup')->assertSee($type)->assertSee($server); + public function testReportDatabaseConnectionError() + { + $this->post('/setup/database', ['type' => 'sqlite', 'host' => '', 'db' => 'test']) + ->assertSee(trans('setup.database.connection-error', [ + 'type' => 'SQLite', + 'msg' => 'Database (test) does not exist.' + ])); } public function testInfo()