From 8b2f2db6ebd6a8815de9f1cfddd22806171c8f86 Mon Sep 17 00:00:00 2001 From: Pig Fang Date: Wed, 27 May 2020 17:28:24 +0800 Subject: [PATCH] refactor controllers --- app/Http/Controllers/AdminController.php | 255 ----------------- app/Http/Controllers/OptionsController.php | 264 ++++++++++++++++++ routes/web.php | 8 +- .../OptionsControllerTest.php} | 2 +- 4 files changed, 269 insertions(+), 260 deletions(-) create mode 100644 app/Http/Controllers/OptionsController.php rename tests/{BrowserKitTests/AdminFormsTest.php => HttpTest/ControllersTest/OptionsControllerTest.php} (99%) diff --git a/app/Http/Controllers/AdminController.php b/app/Http/Controllers/AdminController.php index c7e01423..30220195 100644 --- a/app/Http/Controllers/AdminController.php +++ b/app/Http/Controllers/AdminController.php @@ -5,17 +5,14 @@ namespace App\Http\Controllers; use App\Models\Player; use App\Models\Texture; use App\Models\User; -use App\Services\OptionForm; use App\Services\PluginManager; use Blessing\Filter; -use Cache; use Carbon\Carbon; use Illuminate\Filesystem\Filesystem; use Illuminate\Http\Request; use Illuminate\Support\Arr; use Illuminate\Support\Collection; use Illuminate\Support\Str; -use Option; class AdminController extends Controller { @@ -95,258 +92,6 @@ class AdminController extends Controller ]; } - public function customize(Request $request) - { - $homepage = Option::form('homepage', OptionForm::AUTO_DETECT, function ($form) { - $form->text('home_pic_url')->hint(); - - $form->text('favicon_url')->hint()->description(); - - $form->checkbox('transparent_navbar')->label(); - - $form->checkbox('hide_intro')->label(); - - $form->checkbox('fixed_bg')->label(); - - $form->select('copyright_prefer') - ->option('0', 'Powered with ❤ by Blessing Skin Server.') - ->option('1', 'Powered by Blessing Skin Server.') - ->option('2', 'Proudly powered by Blessing Skin Server.') - ->option('3', '由 Blessing Skin Server 强力驱动。') - ->option('4', '自豪地采用 Blessing Skin Server。') - ->description(); - - $form->textarea('copyright_text')->rows(6)->description(); - })->handle(function () { - Option::set('copyright_prefer_'.config('app.locale'), request('copyright_prefer')); - Option::set('copyright_text_'.config('app.locale'), request('copyright_text')); - }); - - $customJsCss = Option::form('customJsCss', OptionForm::AUTO_DETECT, function ($form) { - $form->textarea('custom_css', 'CSS')->rows(6); - $form->textarea('custom_js', 'JavaScript')->rows(6); - })->addMessage()->handle(); - - if ($request->isMethod('post') && $request->input('action') === 'color') { - $navbar = $request->input('navbar'); - if ($navbar) { - option(['navbar_color' => $navbar]); - } - - $sidebar = $request->input('sidebar'); - if ($sidebar) { - option(['sidebar_color' => $sidebar]); - } - } - - return view('admin.customize', [ - 'colors' => [ - 'navbar' => [ - 'primary', 'secondary', 'success', 'danger', 'indigo', - 'purple', 'pink', 'teal', 'cyan', 'dark', 'gray', - 'fuchsia', 'maroon', 'olive', 'navy', - 'lime', 'light', 'warning', 'white', 'orange', - ], - 'sidebar' => [ - 'primary', 'warning', 'info', 'danger', 'success', 'indigo', - 'navy', 'purple', 'fuchsia', 'pink', 'maroon', 'orange', - 'lime', 'teal', 'olive', - ], - ], - 'forms' => [ - 'homepage' => $homepage, - 'custom_js_css' => $customJsCss, - ], - 'extra' => [ - 'navbar' => option('navbar_color'), - 'sidebar' => option('sidebar_color'), - ], - ]); - } - - public function score() - { - $rate = Option::form('rate', OptionForm::AUTO_DETECT, function ($form) { - $form->group('score_per_storage')->text('score_per_storage')->addon(); - - $form->group('private_score_per_storage') - ->text('private_score_per_storage')->addon()->hint(); - - $form->group('score_per_closet_item') - ->text('score_per_closet_item')->addon(); - - $form->checkbox('return_score')->label(); - - $form->group('score_per_player')->text('score_per_player')->addon(); - - $form->text('user_initial_score'); - })->handle(); - - $report = Option::form('report', OptionForm::AUTO_DETECT, function ($form) { - $form->text('reporter_score_modification')->description(); - - $form->text('reporter_reward_score'); - })->handle(); - - $sign = Option::form('sign', OptionForm::AUTO_DETECT, function ($form) { - $form->group('sign_score') - ->text('sign_score_from')->addon(trans('options.sign.sign_score.addon1')) - ->text('sign_score_to')->addon(trans('options.sign.sign_score.addon2')); - - $form->group('sign_gap_time')->text('sign_gap_time')->addon(); - - $form->checkbox('sign_after_zero')->label()->hint(); - })->after(function () { - $sign_score = request('sign_score_from').','.request('sign_score_to'); - Option::set('sign_score', $sign_score); - })->with([ - 'sign_score_from' => @explode(',', option('sign_score'))[0], - 'sign_score_to' => @explode(',', option('sign_score'))[1], - ])->handle(); - - $sharing = Option::form('sharing', OptionForm::AUTO_DETECT, function ($form) { - $form->group('score_award_per_texture') - ->text('score_award_per_texture') - ->addon(trans('general.user.score')); - $form->checkbox('take_back_scores_after_deletion')->label(); - $form->group('score_award_per_like') - ->text('score_award_per_like') - ->addon(trans('general.user.score')); - })->handle(); - - return view('admin.score', ['forms' => compact('rate', 'report', 'sign', 'sharing')]); - } - - public function options() - { - $general = Option::form('general', OptionForm::AUTO_DETECT, function ($form) { - $form->text('site_name'); - $form->text('site_description')->description(); - - $form->text('site_url') - ->hint() - ->format(function ($url) { - if (Str::endsWith($url, '/')) { - $url = substr($url, 0, -1); - } - - if (Str::endsWith($url, '/index.php')) { - $url = substr($url, 0, -10); - } - - return $url; - }); - - $form->checkbox('user_can_register')->label(); - $form->checkbox('register_with_player_name')->label(); - $form->checkbox('require_verification')->label(); - - $form->text('regs_per_ip'); - - $form->group('max_upload_file_size') - ->text('max_upload_file_size')->addon('KB') - ->hint(trans('options.general.max_upload_file_size.hint', ['size' => ini_get('upload_max_filesize')])); - - $form->select('player_name_rule') - ->option('official', trans('options.general.player_name_rule.official')) - ->option('cjk', trans('options.general.player_name_rule.cjk')) - ->option('custom', trans('options.general.player_name_rule.custom')); - - $form->text('custom_player_name_regexp')->hint()->placeholder(); - - $form->group('player_name_length') - ->text('player_name_length_min') - ->addon('~') - ->text('player_name_length_max') - ->addon(trans('options.general.player_name_length.suffix')); - - $form->checkbox('auto_del_invalid_texture')->label()->hint(); - - $form->checkbox('allow_downloading_texture')->label(); - - $form->select('status_code_for_private') - ->option('403', '403 Forbidden') - ->option('404', '404 Not Found'); - - $form->text('texture_name_regexp')->hint()->placeholder(); - - $form->textarea('content_policy')->rows(3)->description(); - })->handle(function () { - Option::set('site_name_'.config('app.locale'), request('site_name')); - Option::set('site_description_'.config('app.locale'), request('site_description')); - Option::set('content_policy_'.config('app.locale'), request('content_policy')); - }); - - $announ = Option::form('announ', OptionForm::AUTO_DETECT, function ($form) { - $form->textarea('announcement')->rows(10)->description(); - })->renderWithoutTable()->handle(function () { - Option::set('announcement_'.config('app.locale'), request('announcement')); - }); - - $meta = Option::form('meta', OptionForm::AUTO_DETECT, function ($form) { - $form->text('meta_keywords')->hint(); - $form->text('meta_description')->hint(); - $form->textarea('meta_extras')->rows(6); - })->handle(); - - $recaptcha = Option::form('recaptcha', 'reCAPTCHA', function ($form) { - $form->text('recaptcha_sitekey', 'sitekey'); - $form->text('recaptcha_secretkey', 'secretkey'); - $form->checkbox('recaptcha_invisible')->label(); - })->handle(); - - return view('admin.options') - ->with('forms', compact('general', 'announ', 'meta', 'recaptcha')); - } - - public function resource(Request $request) - { - $resources = Option::form('resources', OptionForm::AUTO_DETECT, function ($form) { - $form->checkbox('force_ssl')->label()->hint(); - $form->checkbox('auto_detect_asset_url')->label()->description(); - - $form->text('cache_expire_time')->hint(OptionForm::AUTO_DETECT); - $form->text('cdn_address') - ->hint(OptionForm::AUTO_DETECT) - ->description(OptionForm::AUTO_DETECT); - }) - ->type('primary') - ->hint(OptionForm::AUTO_DETECT) - ->after(function () { - $cdnAddress = request('cdn_address'); - if ($cdnAddress == null) { - $cdnAddress = ''; - } - if (Str::endsWith($cdnAddress, '/')) { - $cdnAddress = substr($cdnAddress, 0, -1); - } - Option::set('cdn_address', $cdnAddress); - }) - ->handle(); - - $cache = Option::form('cache', OptionForm::AUTO_DETECT, function ($form) { - $form->checkbox('enable_avatar_cache')->label(); - $form->checkbox('enable_preview_cache')->label(); - }) - ->type('warning') - ->addButton([ - 'text' => trans('options.cache.clear'), - 'type' => 'a', - 'class' => 'float-right', - 'style' => 'warning', - 'href' => '?clear-cache', - ]) - ->addMessage(trans('options.cache.driver', ['driver' => config('cache.default')]), 'info'); - - if ($request->has('clear-cache')) { - Cache::flush(); - $cache->addMessage(trans('options.cache.cleared'), 'success'); - } - $cache->handle(); - - return view('admin.resource')->with('forms', compact('resources', 'cache')); - } - public function status( Request $request, PluginManager $plugins, diff --git a/app/Http/Controllers/OptionsController.php b/app/Http/Controllers/OptionsController.php new file mode 100644 index 00000000..1eea9ba8 --- /dev/null +++ b/app/Http/Controllers/OptionsController.php @@ -0,0 +1,264 @@ +text('home_pic_url')->hint(); + + $form->text('favicon_url')->hint()->description(); + + $form->checkbox('transparent_navbar')->label(); + + $form->checkbox('hide_intro')->label(); + + $form->checkbox('fixed_bg')->label(); + + $form->select('copyright_prefer') + ->option('0', 'Powered with ❤ by Blessing Skin Server.') + ->option('1', 'Powered by Blessing Skin Server.') + ->option('2', 'Proudly powered by Blessing Skin Server.') + ->option('3', '由 Blessing Skin Server 强力驱动。') + ->option('4', '自豪地采用 Blessing Skin Server。') + ->description(); + + $form->textarea('copyright_text')->rows(6)->description(); + })->handle(function () { + Option::set('copyright_prefer_'.config('app.locale'), request('copyright_prefer')); + Option::set('copyright_text_'.config('app.locale'), request('copyright_text')); + }); + + $customJsCss = Option::form('customJsCss', OptionForm::AUTO_DETECT, function ($form) { + $form->textarea('custom_css', 'CSS')->rows(6); + $form->textarea('custom_js', 'JavaScript')->rows(6); + })->addMessage()->handle(); + + if ($request->isMethod('post') && $request->input('action') === 'color') { + $navbar = $request->input('navbar'); + if ($navbar) { + option(['navbar_color' => $navbar]); + } + + $sidebar = $request->input('sidebar'); + if ($sidebar) { + option(['sidebar_color' => $sidebar]); + } + } + + return view('admin.customize', [ + 'colors' => [ + 'navbar' => [ + 'primary', 'secondary', 'success', 'danger', 'indigo', + 'purple', 'pink', 'teal', 'cyan', 'dark', 'gray', + 'fuchsia', 'maroon', 'olive', 'navy', + 'lime', 'light', 'warning', 'white', 'orange', + ], + 'sidebar' => [ + 'primary', 'warning', 'info', 'danger', 'success', 'indigo', + 'navy', 'purple', 'fuchsia', 'pink', 'maroon', 'orange', + 'lime', 'teal', 'olive', + ], + ], + 'forms' => [ + 'homepage' => $homepage, + 'custom_js_css' => $customJsCss, + ], + 'extra' => [ + 'navbar' => option('navbar_color'), + 'sidebar' => option('sidebar_color'), + ], + ]); + } + + public function score() + { + $rate = Option::form('rate', OptionForm::AUTO_DETECT, function ($form) { + $form->group('score_per_storage')->text('score_per_storage')->addon(); + + $form->group('private_score_per_storage') + ->text('private_score_per_storage')->addon()->hint(); + + $form->group('score_per_closet_item') + ->text('score_per_closet_item')->addon(); + + $form->checkbox('return_score')->label(); + + $form->group('score_per_player')->text('score_per_player')->addon(); + + $form->text('user_initial_score'); + })->handle(); + + $report = Option::form('report', OptionForm::AUTO_DETECT, function ($form) { + $form->text('reporter_score_modification')->description(); + + $form->text('reporter_reward_score'); + })->handle(); + + $sign = Option::form('sign', OptionForm::AUTO_DETECT, function ($form) { + $form->group('sign_score') + ->text('sign_score_from')->addon(trans('options.sign.sign_score.addon1')) + ->text('sign_score_to')->addon(trans('options.sign.sign_score.addon2')); + + $form->group('sign_gap_time')->text('sign_gap_time')->addon(); + + $form->checkbox('sign_after_zero')->label()->hint(); + })->after(function () { + $sign_score = request('sign_score_from').','.request('sign_score_to'); + Option::set('sign_score', $sign_score); + })->with([ + 'sign_score_from' => @explode(',', option('sign_score'))[0], + 'sign_score_to' => @explode(',', option('sign_score'))[1], + ])->handle(); + + $sharing = Option::form('sharing', OptionForm::AUTO_DETECT, function ($form) { + $form->group('score_award_per_texture') + ->text('score_award_per_texture') + ->addon(trans('general.user.score')); + $form->checkbox('take_back_scores_after_deletion')->label(); + $form->group('score_award_per_like') + ->text('score_award_per_like') + ->addon(trans('general.user.score')); + })->handle(); + + return view('admin.score', ['forms' => compact('rate', 'report', 'sign', 'sharing')]); + } + + public function options() + { + $general = Option::form('general', OptionForm::AUTO_DETECT, function ($form) { + $form->text('site_name'); + $form->text('site_description')->description(); + + $form->text('site_url') + ->hint() + ->format(function ($url) { + if (Str::endsWith($url, '/')) { + $url = substr($url, 0, -1); + } + + if (Str::endsWith($url, '/index.php')) { + $url = substr($url, 0, -10); + } + + return $url; + }); + + $form->checkbox('user_can_register')->label(); + $form->checkbox('register_with_player_name')->label(); + $form->checkbox('require_verification')->label(); + + $form->text('regs_per_ip'); + + $form->group('max_upload_file_size') + ->text('max_upload_file_size')->addon('KB') + ->hint(trans('options.general.max_upload_file_size.hint', ['size' => ini_get('upload_max_filesize')])); + + $form->select('player_name_rule') + ->option('official', trans('options.general.player_name_rule.official')) + ->option('cjk', trans('options.general.player_name_rule.cjk')) + ->option('custom', trans('options.general.player_name_rule.custom')); + + $form->text('custom_player_name_regexp')->hint()->placeholder(); + + $form->group('player_name_length') + ->text('player_name_length_min') + ->addon('~') + ->text('player_name_length_max') + ->addon(trans('options.general.player_name_length.suffix')); + + $form->checkbox('auto_del_invalid_texture')->label()->hint(); + + $form->checkbox('allow_downloading_texture')->label(); + + $form->select('status_code_for_private') + ->option('403', '403 Forbidden') + ->option('404', '404 Not Found'); + + $form->text('texture_name_regexp')->hint()->placeholder(); + + $form->textarea('content_policy')->rows(3)->description(); + })->handle(function () { + Option::set('site_name_'.config('app.locale'), request('site_name')); + Option::set('site_description_'.config('app.locale'), request('site_description')); + Option::set('content_policy_'.config('app.locale'), request('content_policy')); + }); + + $announ = Option::form('announ', OptionForm::AUTO_DETECT, function ($form) { + $form->textarea('announcement')->rows(10)->description(); + })->renderWithoutTable()->handle(function () { + Option::set('announcement_'.config('app.locale'), request('announcement')); + }); + + $meta = Option::form('meta', OptionForm::AUTO_DETECT, function ($form) { + $form->text('meta_keywords')->hint(); + $form->text('meta_description')->hint(); + $form->textarea('meta_extras')->rows(6); + })->handle(); + + $recaptcha = Option::form('recaptcha', 'reCAPTCHA', function ($form) { + $form->text('recaptcha_sitekey', 'sitekey'); + $form->text('recaptcha_secretkey', 'secretkey'); + $form->checkbox('recaptcha_invisible')->label(); + })->handle(); + + return view('admin.options') + ->with('forms', compact('general', 'announ', 'meta', 'recaptcha')); + } + + public function resource(Request $request) + { + $resources = Option::form('resources', OptionForm::AUTO_DETECT, function ($form) { + $form->checkbox('force_ssl')->label()->hint(); + $form->checkbox('auto_detect_asset_url')->label()->description(); + + $form->text('cache_expire_time')->hint(OptionForm::AUTO_DETECT); + $form->text('cdn_address') + ->hint(OptionForm::AUTO_DETECT) + ->description(OptionForm::AUTO_DETECT); + }) + ->type('primary') + ->hint(OptionForm::AUTO_DETECT) + ->after(function () { + $cdnAddress = request('cdn_address'); + if ($cdnAddress == null) { + $cdnAddress = ''; + } + if (Str::endsWith($cdnAddress, '/')) { + $cdnAddress = substr($cdnAddress, 0, -1); + } + Option::set('cdn_address', $cdnAddress); + }) + ->handle(); + + $cache = Option::form('cache', OptionForm::AUTO_DETECT, function ($form) { + $form->checkbox('enable_avatar_cache')->label(); + $form->checkbox('enable_preview_cache')->label(); + }) + ->type('warning') + ->addButton([ + 'text' => trans('options.cache.clear'), + 'type' => 'a', + 'class' => 'float-right', + 'style' => 'warning', + 'href' => '?clear-cache', + ]) + ->addMessage(trans('options.cache.driver', ['driver' => config('cache.default')]), 'info'); + + if ($request->has('clear-cache')) { + Cache::flush(); + $cache->addMessage(trans('options.cache.cleared'), 'success'); + } + $cache->handle(); + + return view('admin.resource')->with('forms', compact('resources', 'cache')); + } +} diff --git a/routes/web.php b/routes/web.php index dba7e2e2..4d681f9e 100644 --- a/routes/web.php +++ b/routes/web.php @@ -120,10 +120,10 @@ Route::prefix('admin') Route::get('chart', 'AdminController@chartData'); Route::post('notifications/send', 'NotificationsController@send'); - Route::any('customize', 'AdminController@customize'); - Route::any('score', 'AdminController@score'); - Route::any('options', 'AdminController@options'); - Route::any('resource', 'AdminController@resource'); + Route::any('customize', 'OptionsController@customize'); + Route::any('score', 'OptionsController@score'); + Route::any('options', 'OptionsController@options'); + Route::any('resource', 'OptionsController@resource'); Route::get('status', 'AdminController@status'); diff --git a/tests/BrowserKitTests/AdminFormsTest.php b/tests/HttpTest/ControllersTest/OptionsControllerTest.php similarity index 99% rename from tests/BrowserKitTests/AdminFormsTest.php rename to tests/HttpTest/ControllersTest/OptionsControllerTest.php index 8e741409..492fdc23 100644 --- a/tests/BrowserKitTests/AdminFormsTest.php +++ b/tests/HttpTest/ControllersTest/OptionsControllerTest.php @@ -6,7 +6,7 @@ use Cache; use Illuminate\Foundation\Testing\DatabaseTransactions; use Mockery; -class AdminFormsTest extends BrowserKitTestCase +class OptionsControllerTest extends BrowserKitTestCase { use DatabaseTransactions;