Inline some helper functions

This commit is contained in:
Pig Fang 2019-12-21 15:50:29 +08:00
parent ab24dfe5bf
commit e21fb0fa31
9 changed files with 47 additions and 78 deletions

View File

@ -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();

View File

@ -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'),

View File

@ -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();

View File

@ -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();

View File

@ -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');

View File

@ -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');
}
}

View File

@ -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)
{

View File

@ -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');

View File

@ -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]);
}