From e21fb0fa31713aa192b2c53ad09dca7357e4ca0f Mon Sep 17 00:00:00 2001 From: Pig Fang Date: Sat, 21 Dec 2019 15:50:29 +0800 Subject: [PATCH] Inline some helper functions --- app/Console/Commands/BsInstallCommand.php | 5 +- app/Http/Controllers/AdminController.php | 10 +++- app/Http/Controllers/AuthController.php | 9 +-- app/Http/Controllers/SetupController.php | 12 +++- app/Http/Controllers/UserController.php | 2 +- app/Providers/AppServiceProvider.php | 22 +++++-- app/helpers.php | 57 +------------------ .../ControllersTest/AdminControllerTest.php | 1 - .../ControllersTest/UserControllerTest.php | 7 ++- 9 files changed, 47 insertions(+), 78 deletions(-) diff --git a/app/Console/Commands/BsInstallCommand.php b/app/Console/Commands/BsInstallCommand.php index 723a607c..bfd929e0 100644 --- a/app/Console/Commands/BsInstallCommand.php +++ b/app/Console/Commands/BsInstallCommand.php @@ -3,6 +3,7 @@ namespace App\Console\Commands; use App\Models\User; +use Carbon\Carbon; use Illuminate\Console\Command; use Illuminate\Filesystem\Filesystem; @@ -39,8 +40,8 @@ class BsInstallCommand extends Command $admin->password = app('cipher')->hash($this->argument('password'), config('secure.salt')); $admin->ip = '127.0.0.1'; $admin->permission = User::SUPER_ADMIN; - $admin->register_at = get_datetime_string(); - $admin->last_sign_at = get_datetime_string(time() - 86400); + $admin->register_at = Carbon::now(); + $admin->last_sign_at = Carbon::now()->subDay(); $admin->verified = true; $admin->save(); diff --git a/app/Http/Controllers/AdminController.php b/app/Http/Controllers/AdminController.php index 510ff347..e16da0fb 100644 --- a/app/Http/Controllers/AdminController.php +++ b/app/Http/Controllers/AdminController.php @@ -427,7 +427,13 @@ class AdminController extends Controller Filesystem $filesystem, Filter $filter ) { - $db = get_db_config(); + $db = config('database.connections.'.config('database.default')); + $dbType = Arr::get([ + 'mysql' => 'MySQL/MariaDB', + 'sqlite' => 'SQLite', + 'pgsql' => 'PostgreSQL', + ], config('database.default'), ''); + $enabledPlugins = $plugins->getEnabledPlugins()->map(function ($plugin) { return ['title' => trans($plugin->title), 'version' => $plugin->version]; }); @@ -473,7 +479,7 @@ class AdminController extends Controller 'os' => sprintf('%s %s %s', php_uname('s'), php_uname('r'), php_uname('m')), ], 'db' => [ - 'type' => humanize_db_type(), + 'type' => $dbType, 'host' => Arr::get($db, 'host', ''), 'port' => Arr::get($db, 'port', ''), 'username' => Arr::get($db, 'username'), diff --git a/app/Http/Controllers/AuthController.php b/app/Http/Controllers/AuthController.php index 03d6dff6..27435b0f 100644 --- a/app/Http/Controllers/AuthController.php +++ b/app/Http/Controllers/AuthController.php @@ -10,6 +10,7 @@ use App\Models\User; use App\Rules\Captcha; use Auth; use Cache; +use Carbon\Carbon; use Illuminate\Http\Request; use Laravel\Socialite\Facades\Socialite; use Mail; @@ -150,8 +151,8 @@ class AuthController extends Controller ?: app('cipher')->hash($data['password'], config('secure.salt')); $user->ip = get_client_ip(); $user->permission = User::NORMAL; - $user->register_at = get_datetime_string(); - $user->last_sign_at = get_datetime_string(time() - 86400); + $user->register_at = Carbon::now(); + $user->last_sign_at = Carbon::now()->subDay(); $user->save(); @@ -331,8 +332,8 @@ class AuthController extends Controller $user->password = ''; $user->ip = get_client_ip(); $user->permission = User::NORMAL; - $user->register_at = get_datetime_string(); - $user->last_sign_at = get_datetime_string(time() - 86400); + $user->register_at = Carbon::now(); + $user->last_sign_at = Carbon::now()->subDay(); $user->verified = true; $user->save(); diff --git a/app/Http/Controllers/SetupController.php b/app/Http/Controllers/SetupController.php index dd0c2405..be87ee71 100644 --- a/app/Http/Controllers/SetupController.php +++ b/app/Http/Controllers/SetupController.php @@ -4,11 +4,13 @@ namespace App\Http\Controllers; use App\Exceptions\PrettyPageException; use App\Models\User; +use Carbon\Carbon; use Illuminate\Contracts\Console\Kernel as Artisan; use Illuminate\Database\Connection; use Illuminate\Database\DatabaseManager; use Illuminate\Filesystem\Filesystem; use Illuminate\Http\Request; +use Illuminate\Support\Arr; use Illuminate\Support\Str; class SetupController extends Controller @@ -59,7 +61,11 @@ class SetupController extends Controller $manager->connection('temp')->getPdo(); } catch (\Exception $e) { $msg = iconv('gbk', 'utf-8', $e->getMessage()); - $type = humanize_db_type($request->input('type')); + $type = Arr::get([ + 'mysql' => 'MySQL/MariaDB', + 'sqlite' => 'SQLite', + 'pgsql' => 'PostgreSQL', + ], $request->input('type'), ''); throw new PrettyPageException(trans('setup.database.connection-error', compact('msg', 'type')), $e->getCode()); } @@ -148,8 +154,8 @@ class SetupController extends Controller $user->password = app('cipher')->hash($data['password'], config('secure.salt')); $user->ip = get_client_ip(); $user->permission = User::SUPER_ADMIN; - $user->register_at = get_datetime_string(); - $user->last_sign_at = get_datetime_string(time() - 86400); + $user->register_at = Carbon::now(); + $user->last_sign_at = Carbon::now()->subDay(); $user->verified = true; $user->save(); diff --git a/app/Http/Controllers/UserController.php b/app/Http/Controllers/UserController.php index fe1a6c1c..41a9ddcf 100644 --- a/app/Http/Controllers/UserController.php +++ b/app/Http/Controllers/UserController.php @@ -125,7 +125,7 @@ class UserController extends Controller if ($this->getSignRemainingTime($user) <= 0) { $acquiredScore = rand(...explode(',', option('sign_score'))); $user->score += $acquiredScore; - $user->last_sign_at = Carbon::now()->toDateTimeString(); + $user->last_sign_at = Carbon::now(); $user->save(); $gap = option('sign_gap_time'); diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index 362e0487..f7392bc0 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -5,6 +5,7 @@ namespace App\Providers; use App\Events; use App\Exceptions\PrettyPageException; use Event; +use Illuminate\Http\Request; use Illuminate\Support\Facades\Redis; use Illuminate\Support\ServiceProvider; @@ -33,10 +34,10 @@ class AppServiceProvider extends ServiceProvider * * @return void */ - public function boot() + public function boot(Request $request) { // Control the URL generated by url() function - $this->configureUrlGenerator(); + $this->configureUrlGenerator($request); Event::listen(Events\RenderingHeader::class, function ($event) { $blessing = [ @@ -74,11 +75,9 @@ class AppServiceProvider extends ServiceProvider /** * Configure the \Illuminate\Routing\UrlGenerator. * - * @return void - * * @codeCoverageIgnore */ - protected function configureUrlGenerator() + protected function configureUrlGenerator(Request $request): void { if (!option('auto_detect_asset_url')) { $rootUrl = option('site_url'); @@ -90,7 +89,18 @@ class AppServiceProvider extends ServiceProvider } } - if (option('force_ssl') || is_request_secure()) { + /** + * Check whether the request is secure or not. + * True is always returned when "X-Forwarded-Proto" header is set. + * + * We define this function because Symfony's "Request::isSecure()" method + * needs "setTrustedProxies()" which sucks when load balancer is enabled. + */ + $isRequestSecure = $request->server('HTTPS') === 'on' + || $request->server('HTTP_X_FORWARDED_PROTO') === 'https' + || $request->server('HTTP_X_FORWARDED_SSL') === 'on'; + + if (option('force_ssl') || $isRequestSecure) { $this->app['url']->forceScheme('https'); } } diff --git a/app/helpers.php b/app/helpers.php index 2b85339e..1af0d2d2 100644 --- a/app/helpers.php +++ b/app/helpers.php @@ -2,7 +2,6 @@ declare(strict_types=1); -use Carbon\Carbon; use Illuminate\Support\Arr; if (!function_exists('plugin')) { @@ -55,7 +54,7 @@ if (!function_exists('option')) { * * @param array|string $key * @param mixed $default - * @param raw $raw return raw value without convertion + * @param bool $raw return raw value without convertion * * @return mixed */ @@ -84,42 +83,6 @@ if (!function_exists('option_localized')) { } } -if (!function_exists('humanize_db_type')) { - function humanize_db_type($type = null): string - { - $map = [ - 'mysql' => 'MySQL/MariaDB', - 'sqlite' => 'SQLite', - 'pgsql' => 'PostgreSQL', - ]; - - $type = $type ?: config('database.default'); - - return Arr::get($map, $type, ''); - } -} - -if (!function_exists('get_db_config')) { - function get_db_config($type = null) - { - $type = $type ?: config('database.default'); - - return config("database.connections.$type"); - } -} - -if (!function_exists('get_datetime_string')) { - /** - * Get date time string in "Y-m-d H:i:s" format. - * - * @param int $timestamp - */ - function get_datetime_string($timestamp = 0): string - { - return $timestamp == 0 ? Carbon::now()->toDateTimeString() : Carbon::createFromTimestamp($timestamp)->toDateTimeString(); - } -} - if (!function_exists('get_client_ip')) { /** * Return the client IP address. @@ -156,24 +119,6 @@ if (!function_exists('get_string_replaced')) { } } -if (!function_exists('is_request_secure')) { - /** - * Check whether the request is secure or not. - * True is always returned when "X-Forwarded-Proto" header is set. - * - * We define this function because Symfony's "Request::isSecure()" method - * needs "setTrustedProxies()" which sucks when load balancer is enabled. - */ - function is_request_secure(): bool - { - $request = request(); - - return $request->server('HTTPS') === 'on' - || $request->server('HTTP_X_FORWARDED_PROTO') === 'https' - || $request->server('HTTP_X_FORWARDED_SSL') === 'on'; - } -} - if (!function_exists('png')) { function png($resource) { diff --git a/tests/HttpTest/ControllersTest/AdminControllerTest.php b/tests/HttpTest/ControllersTest/AdminControllerTest.php index 672ccb85..4c79bf9c 100644 --- a/tests/HttpTest/ControllersTest/AdminControllerTest.php +++ b/tests/HttpTest/ControllersTest/AdminControllerTest.php @@ -138,7 +138,6 @@ class AdminControllerTest extends TestCase $this->get('/admin/status') ->assertSee(PHP_VERSION) - ->assertSee(humanize_db_type()) ->assertSee('(1)') ->assertSee('MyPlugin') ->assertSee('0.0.0'); diff --git a/tests/HttpTest/ControllersTest/UserControllerTest.php b/tests/HttpTest/ControllersTest/UserControllerTest.php index 793e8ccb..26ec9202 100644 --- a/tests/HttpTest/ControllersTest/UserControllerTest.php +++ b/tests/HttpTest/ControllersTest/UserControllerTest.php @@ -8,6 +8,7 @@ use App\Models\User; use App\Notifications; use App\Services\Filter; use App\Services\Rejection; +use Carbon\Carbon; use Event; use Illuminate\Foundation\Testing\DatabaseTransactions; use Illuminate\Support\Facades\Mail; @@ -106,7 +107,7 @@ class UserControllerTest extends TestCase ]); // Remaining time is greater than 0 - $user = factory(User::class)->create(['last_sign_at' => get_datetime_string()]); + $user = factory(User::class)->create(['last_sign_at' => Carbon::now()]); option(['sign_gap_time' => 2]); $this->actingAs($user) ->postJson('/user/sign') @@ -123,7 +124,7 @@ class UserControllerTest extends TestCase // Can sign after 0 o'clock option(['sign_after_zero' => true]); - $user = factory(User::class)->create(['last_sign_at' => get_datetime_string()]); + $user = factory(User::class)->create(['last_sign_at' => Carbon::now()]); $diff = \Carbon\Carbon::now()->diffInSeconds(\Carbon\Carbon::tomorrow()); if ($diff / 3600 >= 1) { $diff = round($diff / 3600); @@ -146,7 +147,7 @@ class UserControllerTest extends TestCase ]); $user = factory(User::class)->create([ - 'last_sign_at' => \Carbon\Carbon::today()->toDateTimeString(), + 'last_sign_at' => \Carbon\Carbon::today(), ]); $this->actingAs($user)->postJson('/user/sign')->assertJson(['code' => 0]); }