Simplify environment check when booting

This commit is contained in:
Pig Fang 2018-07-21 18:43:33 +08:00
parent 0da5c0ce41
commit 6031562851
5 changed files with 70 additions and 101 deletions

View File

@ -55,7 +55,8 @@ class AppServiceProvider extends ServiceProvider
if (class_exists($className)) {
$this->app->singleton('cipher', $className);
} else {
die_with_utf8_encoding(sprintf(
header('Content-Type: text/html; charset=UTF-8');
exit(sprintf(
'[Error] Unsupported encryption method: < %1$s >, please check your .env configuration <br>'.
'[错误] 不支持的密码加密方式 < %1$s >,请检查你的 .env 配置文件'
, config('secure.cipher')));

View File

@ -24,56 +24,12 @@ class BootServiceProvider extends ServiceProvider
// Detect current locale
$this->app->call('App\Http\Middleware\DetectLanguagePrefer@detect');
$this->checkFilePermissions();
$this->checkDatabaseConnection();
// Skip the installation check when setup or under CLI
if (! $request->is('setup*') && PHP_SAPI != "cli") {
$this->checkInstallation();
}
}
protected function checkFilePermissions()
{
// Check dotenv file
if (! file_exists(app()->environmentFile())) {
throw new PrettyPageException(trans('setup.file.no-dot-env'), -1);
}
// Check permissions of storage path
if (! is_writable(storage_path())) {
throw new PrettyPageException(trans('setup.permissions.storage'), -1);
}
if (! SetupController::checkDirectories()) {
throw new PrettyPageException(trans('setup.file.permission-error'), -1);
}
}
protected function checkDatabaseConnection()
{
try {
DB::connection()->getPdo();
} catch (\Exception $e) {
if ($this->app->runningInConsole()) {
// Dump some useful information for debugging
dump([
'APP_ENV' => app()->environment(),
'DOTENV_FILE' => app()->environmentFile(),
'DB_CONNECTION' => get_db_config()
]);
}
$msg = iconv('gbk', 'utf-8', $e->getMessage());
$type = humanize_db_type();
throw new PrettyPageException(
trans('setup.database.connection-error', compact('msg', 'type')),
$e->getCode()
);
}
}
protected function checkInstallation()
{
// Redirect to setup wizard

View File

@ -406,48 +406,6 @@ if (! function_exists('validate')) {
}
}
if (! function_exists('runtime_check')) {
function runtime_check(array $requirements)
{
foreach ($requirements['extensions'] as $extension) {
if (! extension_loaded($extension)) {
die_with_utf8_encoding(
"[Error] You have not installed the $extension extension <br>".
"[错误] 你尚未安装 $extension 扩展!安装方法请自行搜索,蟹蟹。"
);
}
}
foreach (array_get($requirements, 'write_permission', []) as $dir) {
$realPath = realpath(__DIR__."/../$dir");
if (! file_exists($realPath)) {
die_with_utf8_encoding(
"[Error] The directory < $dir > does not exist <br>".
"[错误] 目录 < $dir > 不存在,请在程序根目录下手动创建"
);
}
if (! is_writable($realPath)) {
die_with_utf8_encoding(
"[Error] The program lacks write permission to directory < $dir > <br>".
"[错误] 程序缺少对 < $dir > 目录的写权限,请手动授权"
);
}
}
}
}
if (! function_exists('die_with_utf8_encoding')) {
function die_with_utf8_encoding($error)
{
header('Content-Type: text/html; charset=UTF-8');
exit($error);
}
}
if (! function_exists('humanize_db_type')) {
function humanize_db_type($type = null)

67
bootstrap/chkenv.php Normal file
View File

@ -0,0 +1,67 @@
<?php
(function () {
function die_with_utf8_encoding($error)
{
header('Content-Type: text/html; charset=UTF-8');
exit($error);
}
if (version_compare(PHP_VERSION, '7.1.3', '<')) {
die_with_utf8_encoding(
'[Error] Blessing Skin requires PHP version >= 7.1.3, you are now using '.PHP_VERSION.'<br>'.
'[错误] 你的 PHP 版本过低('.PHP_VERSION.'Blessing Skin 要求至少为 7.1.3'
);
}
$requirements = [
'extensions' => [
'pdo',
'openssl',
'gd',
'mbstring',
'tokenizer',
'ctype',
'xml',
'json',
'fileinfo'
],
'write_permission' => [
'bootstrap/cache',
'storage',
'plugins'
]
];
foreach ($requirements['extensions'] as $extension) {
if (! extension_loaded($extension)) {
die_with_utf8_encoding(
"[Error] You have not installed the $extension extension <br>".
"[错误] 你尚未安装 $extension 扩展!安装方法请自行搜索。"
);
}
}
foreach ($requirements['write_permission'] as $dir) {
$realPath = realpath(__DIR__."/../$dir");
if (! file_exists($realPath)) {
die_with_utf8_encoding(
"[Error] The directory < $dir > does not exist <br>".
"[错误] 目录 < $dir > 不存在,请在程序根目录下手动创建。"
);
}
if (! is_writable($realPath)) {
die_with_utf8_encoding(
"[Error] The program lacks write permission to directory < $dir > <br>".
"[错误] 程序缺少对 < $dir > 目录的写权限,请手动授权。"
);
}
}
$autoload = file_get_contents('vendor/autoload.php');
$lines = explode("\n", $autoload);
$lines[1] = '$GLOBALS["env_checked"] = true;';
file_put_contents('vendor/autoload.php', implode("\n", $lines));
})();

View File

@ -9,22 +9,9 @@
@ini_set('display_errors', 'on');
// Check PHP version
if (version_compare(PHP_VERSION, '7.1.3', '<')) {
header('Content-Type: text/html; charset=UTF-8');
exit(
'[Error] Blessing Skin requires PHP version >= 7.1.3, you are now using '.PHP_VERSION.'<br>'.
'[错误] 你的 PHP 版本过低('.PHP_VERSION.'Blessing Skin 要求至少为 7.1.3'
);
}
require __DIR__.'/bootstrap/autoload.php';
// Check the runtime environment
runtime_check(array(
'extensions' => array('pdo_mysql', 'openssl', 'gd', 'mbstring', 'tokenizer'),
'write_permission' => array('storage', 'plugins', 'bootstrap/cache')
));
if (!isset($GLOBALS['env_checked'])) require __DIR__.'/bootstrap/chkenv.php';
// Process the request
require __DIR__.'/bootstrap/kernel.php';