From 6031562851960197933ed937cbc04a8feaef57f5 Mon Sep 17 00:00:00 2001 From: Pig Fang Date: Sat, 21 Jul 2018 18:43:33 +0800 Subject: [PATCH] Simplify environment check when booting --- app/Providers/AppServiceProvider.php | 3 +- app/Providers/BootServiceProvider.php | 44 ------------------ app/helpers.php | 42 ----------------- bootstrap/chkenv.php | 67 +++++++++++++++++++++++++++ index.php | 15 +----- 5 files changed, 70 insertions(+), 101 deletions(-) create mode 100644 bootstrap/chkenv.php diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index 96e07f04..c4d7d954 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -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
'. '[错误] 不支持的密码加密方式 < %1$s >,请检查你的 .env 配置文件' , config('secure.cipher'))); diff --git a/app/Providers/BootServiceProvider.php b/app/Providers/BootServiceProvider.php index e3926968..828367f0 100644 --- a/app/Providers/BootServiceProvider.php +++ b/app/Providers/BootServiceProvider.php @@ -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 diff --git a/app/helpers.php b/app/helpers.php index 856dd889..fa50870e 100644 --- a/app/helpers.php +++ b/app/helpers.php @@ -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
". - "[错误] 你尚未安装 $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
". - "[错误] 目录 < $dir > 不存在,请在程序根目录下手动创建" - ); - } - - if (! is_writable($realPath)) { - die_with_utf8_encoding( - "[Error] The program lacks write permission to directory < $dir >
". - "[错误] 程序缺少对 < $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) diff --git a/bootstrap/chkenv.php b/bootstrap/chkenv.php new file mode 100644 index 00000000..9d3e9842 --- /dev/null +++ b/bootstrap/chkenv.php @@ -0,0 +1,67 @@ += 7.1.3, you are now using '.PHP_VERSION.'
'. + '[错误] 你的 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
". + "[错误] 你尚未安装 $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
". + "[错误] 目录 < $dir > 不存在,请在程序根目录下手动创建。" + ); + } + + if (! is_writable($realPath)) { + die_with_utf8_encoding( + "[Error] The program lacks write permission to directory < $dir >
". + "[错误] 程序缺少对 < $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)); +})(); diff --git a/index.php b/index.php index 585e66b2..812d3d5d 100755 --- a/index.php +++ b/index.php @@ -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.'
'. - '[错误] 你的 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';