From 4c848c4a7443fa164c0df1f80ed836e50cb91380 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Wed, 15 Jun 2016 16:43:31 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=96=E6=B6=88=20APP=5FDEBUG=20=E5=B8=B8?= =?UTF-8?q?=E9=87=8F=20=E6=94=B9=E4=B8=BA=20App::$debug=20=E5=B1=9E?= =?UTF-8?q?=E6=80=A7=E8=8E=B7=E5=8F=96=20=E8=AE=BE=E7=BD=AE=E8=B0=83?= =?UTF-8?q?=E8=AF=95=E6=A8=A1=E5=BC=8F=20=E6=94=B9=E4=B8=BA=20app=5Fdebug?= =?UTF-8?q?=20=E9=85=8D=E7=BD=AE=E5=8F=82=E6=95=B0=20=E5=9C=A8=E5=BA=94?= =?UTF-8?q?=E7=94=A8=E9=85=8D=E7=BD=AE=E6=96=87=E4=BB=B6=E4=B8=AD=E8=AE=BE?= =?UTF-8?q?=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- convention.php | 2 ++ library/think/App.php | 16 ++++++++++++---- library/think/Cache.php | 4 +++- library/think/Config.php | 4 +++- library/think/Db.php | 3 ++- library/think/Error.php | 3 ++- library/think/Hook.php | 5 +++-- library/think/Lang.php | 3 ++- library/think/Loader.php | 5 +++-- library/think/Log.php | 6 ++++-- library/think/Route.php | 3 ++- library/think/Session.php | 3 ++- library/think/Url.php | 3 ++- library/think/cache/driver/Redisd.php | 3 ++- library/think/controller/Hprose.php | 4 +++- library/think/controller/Rpc.php | 3 ++- library/think/db/Connection.php | 3 ++- library/think/exception/Handle.php | 11 ++++++----- library/think/view/driver/Php.php | 2 +- library/think/view/driver/Think.php | 2 +- start.php | 5 ----- tests/application/database.php | 2 +- tests/mock.php | 2 -- tests/thinkphp/baseTest.php | 1 - tpl/think_exception.tpl | 4 ++-- 25 files changed, 62 insertions(+), 40 deletions(-) diff --git a/convention.php b/convention.php index d1e1edd9..d69685a1 100644 --- a/convention.php +++ b/convention.php @@ -5,6 +5,8 @@ return [ // | 应用设置 // +---------------------------------------------------------------------- + // 应用调试模式 + 'app_debug' => true, // 应用模式状态 'app_status' => '', // 是否支持多模块 diff --git a/library/think/App.php b/library/think/App.php index ea833258..626b0845 100644 --- a/library/think/App.php +++ b/library/think/App.php @@ -39,6 +39,11 @@ class App */ public static $modulePath; + /** + * @var bool 应用调试模式 + */ + public static $debug = true; + /** * 执行应用程序 * @access public @@ -72,7 +77,7 @@ class App $dispatch = self::route($request, $config); } // 记录路由信息 - APP_DEBUG && Log::record('[ ROUTE ] ' . var_export($dispatch, true), 'info'); + self::$debug && Log::record('[ ROUTE ] ' . var_export($dispatch, true), 'info'); // 监听app_begin Hook::listen('app_begin', $dispatch); @@ -133,7 +138,7 @@ class App $reflect = new \ReflectionFunction($function); $args = self::bindParams($reflect, $vars); // 记录执行信息 - APP_DEBUG && Log::record('[ RUN ] ' . $reflect->getFileName() . '[ ' . var_export($vars, true) . ' ]', 'info'); + self::$debug && Log::record('[ RUN ] ' . $reflect->getFileName() . '[ ' . var_export($vars, true) . ' ]', 'info'); return $reflect->invokeArgs($args); } @@ -159,7 +164,7 @@ class App } $args = self::bindParams($reflect, $vars); // 记录执行信息 - APP_DEBUG && Log::record('[ RUN ] ' . $reflect->getFileName() . '[ ' . var_export($args, true) . ' ]', 'info'); + self::$debug && Log::record('[ RUN ] ' . $reflect->getFileName() . '[ ' . var_export($args, true) . ' ]', 'info'); return $reflect->invokeArgs(isset($class) ? $class : null, $args); } @@ -280,7 +285,7 @@ class App if (method_exists($instance, '_empty')) { $method = new \ReflectionMethod($instance, '_empty'); $data = $method->invokeArgs($instance, [$action, '']); - APP_DEBUG && Log::record('[ RUN ] ' . $method->getFileName(), 'info'); + self::$debug && Log::record('[ RUN ] ' . $method->getFileName(), 'info'); } else { throw new HttpException(404, 'method [ ' . (new \ReflectionClass($instance))->getName() . '->' . $action . ' ] not exists '); } @@ -297,6 +302,9 @@ class App // 初始化应用 self::$init = $config = self::init(); + // 是否调试模式 + self::$debug = Config::get('app_debug'); + // 注册根命名空间 if (!empty($config['root_namespace'])) { Loader::addNamespace($config['root_namespace']); diff --git a/library/think/Cache.php b/library/think/Cache.php index 3dccefb0..fd011d5d 100644 --- a/library/think/Cache.php +++ b/library/think/Cache.php @@ -11,6 +11,8 @@ namespace think; +use think\App; + class Cache { protected static $instance = []; @@ -42,7 +44,7 @@ class Cache $class = (!empty($options['namespace']) ? $options['namespace'] : '\\think\\cache\\driver\\') . ucwords($type); // 记录初始化信息 - APP_DEBUG && Log::record('[ CACHE ] INIT ' . $type . ':' . var_export($options, true), 'info'); + App::$debug && Log::record('[ CACHE ] INIT ' . $type . ':' . var_export($options, true), 'info'); if (true === $name) { return new $class($options); } else { diff --git a/library/think/Config.php b/library/think/Config.php index 774a58a4..63cfa10d 100644 --- a/library/think/Config.php +++ b/library/think/Config.php @@ -11,6 +11,8 @@ namespace think; +use think\App; + class Config { // 配置参数 @@ -59,7 +61,7 @@ class Config } if (is_file($file)) { // 记录加载信息 - APP_DEBUG && Log::record('[ CONFIG ] ' . $file, 'info'); + App::$debug && Log::record('[ CONFIG ] ' . $file, 'info'); $type = pathinfo($file, PATHINFO_EXTENSION); if ('php' != $type) { return self::parse($file, $type, $name, $range); diff --git a/library/think/Db.php b/library/think/Db.php index 06ba04c7..ed44c659 100644 --- a/library/think/Db.php +++ b/library/think/Db.php @@ -11,6 +11,7 @@ namespace think; +use think\App; use think\Collection; use think\db\Query; @@ -68,7 +69,7 @@ class Db } $class = (!empty($options['namespace']) ? $options['namespace'] : '\\think\\db\\connector\\') . ucwords($options['type']); // 记录初始化信息 - APP_DEBUG && Log::record('[ DB ] INIT ' . $options['type'] . ':' . var_export($options, true), 'info'); + App::$debug && Log::record('[ DB ] INIT ' . $options['type'] . ':' . var_export($options, true), 'info'); if (true === $name) { return new $class($options); } else { diff --git a/library/think/Error.php b/library/think/Error.php index 140fbb24..22334bb3 100644 --- a/library/think/Error.php +++ b/library/think/Error.php @@ -11,6 +11,7 @@ namespace think; +use think\App; use think\console\Output as ConsoleOutput; use think\exception\ErrorException; use think\exception\Handle; @@ -29,7 +30,7 @@ class Error set_exception_handler([__CLASS__, 'appException']); register_shutdown_function([__CLASS__, 'appShutdown']); - if (!APP_DEBUG) { + if (!App::$debug) { ini_set('display_errors', 'Off'); } } diff --git a/library/think/Hook.php b/library/think/Hook.php index 0a1fe9e4..7076918e 100644 --- a/library/think/Hook.php +++ b/library/think/Hook.php @@ -11,6 +11,7 @@ namespace think; +use think\App; use think\Debug; use think\Log; @@ -100,7 +101,7 @@ class Hook if (isset(self::$tags[$tag])) { foreach (self::$tags[$tag] as $name) { - if (APP_DEBUG) { + if (App::$debug) { Debug::remark('behavior_start', 'time'); } @@ -110,7 +111,7 @@ class Hook return $result; } - if (APP_DEBUG) { + if (App::$debug) { Debug::remark('behavior_end', 'time'); if ($name instanceof \Closure) { $name = 'Closure'; diff --git a/library/think/Lang.php b/library/think/Lang.php index fe94bf7e..fd606ba6 100644 --- a/library/think/Lang.php +++ b/library/think/Lang.php @@ -11,6 +11,7 @@ namespace think; +use think\App; use think\Cookie; use think\Log; @@ -78,7 +79,7 @@ class Lang foreach ($file as $_file) { if (is_file($_file)) { // 记录加载信息 - APP_DEBUG && Log::record('[ LANG ] ' . $_file, 'info'); + App::$debug && Log::record('[ LANG ] ' . $_file, 'info'); $_lang = include $_file; } else { $_lang = []; diff --git a/library/think/Loader.php b/library/think/Loader.php index 0cef94b7..3453d714 100644 --- a/library/think/Loader.php +++ b/library/think/Loader.php @@ -11,6 +11,7 @@ namespace think; +use think\App; use think\exception\HttpException; use think\exception\ClassNotFoundException; use think\Request; @@ -78,7 +79,7 @@ class Loader $filename = $path . str_replace('\\', DS, $class) . EXT; if (is_file($filename)) { // 开启调试模式Win环境严格区分大小写 - if (APP_DEBUG && IS_WIN && false === strpos(realpath($filename), $class . EXT)) { + if (App::$debug && IS_WIN && false === strpos(realpath($filename), $class . EXT)) { return false; } include $filename; @@ -253,7 +254,7 @@ class Loader $filename = $baseUrl . $class . $ext; if (is_file($filename)) { // 开启调试模式Win环境严格区分大小写 - if (APP_DEBUG && IS_WIN && false === strpos(realpath($filename), $class . $ext)) { + if (App::$debug && IS_WIN && false === strpos(realpath($filename), $class . $ext)) { return false; } include $filename; diff --git a/library/think/Log.php b/library/think/Log.php index 76474c21..709da31e 100644 --- a/library/think/Log.php +++ b/library/think/Log.php @@ -11,6 +11,8 @@ namespace think; +use think\App; + class Log { const LOG = 'log'; @@ -45,7 +47,7 @@ class Log unset($config['type']); self::$driver = new $class($config); // 记录初始化信息 - APP_DEBUG && Log::record('[ LOG ] INIT ' . $type . ': ' . var_export($config, true), 'info'); + App::$debug && Log::record('[ LOG ] INIT ' . $type . ': ' . var_export($config, true), 'info'); } /** @@ -59,7 +61,7 @@ class Log unset($config['type']); self::$alarm = new $class($config['alarm']); // 记录初始化信息 - APP_DEBUG && Log::record('[ CACHE ] ALARM ' . $type . ': ' . var_export($config, true), 'info'); + App::$debug && Log::record('[ CACHE ] ALARM ' . $type . ': ' . var_export($config, true), 'info'); } /** diff --git a/library/think/Route.php b/library/think/Route.php index 56f5cef4..5694b86d 100644 --- a/library/think/Route.php +++ b/library/think/Route.php @@ -11,6 +11,7 @@ namespace think; +use think\App; use think\Config; use think\Hook; use think\Log; @@ -766,7 +767,7 @@ class Route { if (!empty(self::$bind['type'])) { // 记录绑定信息 - APP_DEBUG && Log::record('[ BIND ] ' . var_export(self::$bind, true), 'info'); + App::$debug && Log::record('[ BIND ] ' . var_export(self::$bind, true), 'info'); // 如果有URL绑定 则进行绑定检测 switch (self::$bind['type']) { case 'class': diff --git a/library/think/Session.php b/library/think/Session.php index e9855a9e..5bc2a856 100644 --- a/library/think/Session.php +++ b/library/think/Session.php @@ -11,6 +11,7 @@ namespace think; +use think\App; use think\exception\ClassNotFoundException; class Session @@ -45,7 +46,7 @@ class Session $config = Config::get('session'); } // 记录初始化信息 - APP_DEBUG && Log::record('[ SESSION ] INIT ' . var_export($config, true), 'info'); + App::$debug && Log::record('[ SESSION ] INIT ' . var_export($config, true), 'info'); $isDoStart = false; if (isset($config['use_trans_sid'])) { ini_set('session.use_trans_sid', $config['use_trans_sid'] ? 1 : 0); diff --git a/library/think/Url.php b/library/think/Url.php index 0df14419..a0d774b6 100644 --- a/library/think/Url.php +++ b/library/think/Url.php @@ -11,6 +11,7 @@ namespace think; +use think\App; use think\Cache; use think\Config; use think\Request; @@ -326,7 +327,7 @@ class Url $route = is_array($route) ? $route[0] : $route; $item[$route][] = [$rule, [], []]; } - !APP_DEBUG && Cache::set('think_route_map', $item); + !App::$debug && Cache::set('think_route_map', $item); return $item; } diff --git a/library/think/cache/driver/Redisd.php b/library/think/cache/driver/Redisd.php index ad59cf90..0a424fb2 100644 --- a/library/think/cache/driver/Redisd.php +++ b/library/think/cache/driver/Redisd.php @@ -11,6 +11,7 @@ namespace think\cache\driver; +use think\App; use think\Cache; use think\Exception; use think\Log; @@ -148,7 +149,7 @@ class Redisd $this->handler->setOption(\Redis::OPT_PREFIX, $this->options['prefix']); } - APP_DEBUG && Log::record("[ CACHE ] INIT Redisd : {$host}:{$port} master->" . var_export($master, true), Log::ALERT); + App::$debug && Log::record("[ CACHE ] INIT Redisd : {$host}:{$port} master->" . var_export($master, true), Log::ALERT); } catch (\RedisException $e) { //phpredis throws a RedisException object if it can't reach the Redis server. //That can happen in case of connectivity issues, if the Redis service is down, or if the redis host is overloaded. diff --git a/library/think/controller/Hprose.php b/library/think/controller/Hprose.php index d6ab39d2..ae30ac04 100644 --- a/library/think/controller/Hprose.php +++ b/library/think/controller/Hprose.php @@ -11,6 +11,8 @@ namespace think\controller; +use think\App; + /** * ThinkPHP Hprose控制器类 */ @@ -45,7 +47,7 @@ abstract class Hprose $methods = array_diff($methods, array('__construct', '__call', '_initialize')); } $server->addMethods($methods, $this); - if (APP_DEBUG || $this->debug) { + if (App::$debug || $this->debug) { $server->setDebugEnabled(true); } // Hprose设置 diff --git a/library/think/controller/Rpc.php b/library/think/controller/Rpc.php index 2a014c16..e16fce2a 100644 --- a/library/think/controller/Rpc.php +++ b/library/think/controller/Rpc.php @@ -11,6 +11,7 @@ namespace think\controller; +use think\App; /** * ThinkPHP RPC控制器类 */ @@ -43,7 +44,7 @@ abstract class Rpc } $server->add($methods, $this); - if (APP_DEBUG || $this->debug) { + if (App::$debug || $this->debug) { $server->setDebugMode(true); } $server->setEnableGZIP(true); diff --git a/library/think/db/Connection.php b/library/think/db/Connection.php index 7884442a..6fc5b777 100644 --- a/library/think/db/Connection.php +++ b/library/think/db/Connection.php @@ -13,6 +13,7 @@ namespace think\db; use PDO; use PDOStatement; +use think\App; use think\Collection; use think\Db; use think\db\Query; @@ -260,7 +261,7 @@ abstract class Connection } $this->links[$linkNum] = new PDO($config['dsn'], $config['username'], $config['password'], $params); // 记录数据库连接信息 - APP_DEBUG && Log::record('[ DB ] CONNECT: ' . $config['dsn'], 'info'); + App::$debug && Log::record('[ DB ] CONNECT: ' . $config['dsn'], 'info'); } catch (\PDOException $e) { if ($autoConnection) { Log::record($e->getMessage(), 'error'); diff --git a/library/think/exception/Handle.php b/library/think/exception/Handle.php index fde2f248..cc93842c 100644 --- a/library/think/exception/Handle.php +++ b/library/think/exception/Handle.php @@ -12,6 +12,7 @@ namespace think\exception; use Exception; +use think\App; use think\Config; use think\Console; use think\console\Output; @@ -35,7 +36,7 @@ class Handle { if (!$this->isIgnoreReport($exception)) { // 收集异常数据 - if (APP_DEBUG) { + if (App::$debug) { $data = [ 'file' => $exception->getFile(), 'line' => $exception->getLine(), @@ -86,7 +87,7 @@ class Handle */ public function renderForConsole(Output $output, Exception $e) { - if (APP_DEBUG) { + if (App::$debug) { $output->setVerbosity(Output::VERBOSITY_DEBUG); } (new Console)->renderException($e, $output); @@ -100,7 +101,7 @@ class Handle { $status = $e->getStatusCode(); $template = Config::get('http_exception_template'); - if (!APP_DEBUG && !empty($template[$status])) { + if (!App::$debug && !empty($template[$status])) { return Response::create($template[$status], 'view')->vars(['e' => $e])->send(); } else { return $this->convertExceptionToResponse($e); @@ -114,7 +115,7 @@ class Handle protected function convertExceptionToResponse(Exception $exception) { // 收集异常数据 - if (APP_DEBUG) { + if (App::$debug) { // 调试模式,获取详细的错误信息 $data = [ 'name' => get_class($exception), @@ -144,7 +145,7 @@ class Handle ]; } - if (!APP_DEBUG && !Config::get('show_error_msg')) { + if (!App::$debug && !Config::get('show_error_msg')) { // 不显示详细错误信息 $data['message'] = Config::get('error_message'); } diff --git a/library/think/view/driver/Php.php b/library/think/view/driver/Php.php index 98660690..f5866e8a 100644 --- a/library/think/view/driver/Php.php +++ b/library/think/view/driver/Php.php @@ -66,7 +66,7 @@ class Php throw new TemplateNotFoundException('template file not exists:' . $template, $template); } // 记录视图信息 - APP_DEBUG && Log::record('[ VIEW ] ' . $template . ' [ ' . var_export(array_keys($data), true) . ' ]', 'info'); + App::$debug && Log::record('[ VIEW ] ' . $template . ' [ ' . var_export(array_keys($data), true) . ' ]', 'info'); extract($data, EXTR_OVERWRITE); include $template; } diff --git a/library/think/view/driver/Think.php b/library/think/view/driver/Think.php index be9a763c..2a443aad 100644 --- a/library/think/view/driver/Think.php +++ b/library/think/view/driver/Think.php @@ -76,7 +76,7 @@ class Think throw new TemplateNotFoundException('template file not exists:' . $template, $template); } // 记录视图信息 - APP_DEBUG && Log::record('[ VIEW ] ' . $template . ' [ ' . var_export(array_keys($data), true) . ' ]', 'info'); + App::$debug && Log::record('[ VIEW ] ' . $template . ' [ ' . var_export(array_keys($data), true) . ' ]', 'info'); $this->template->fetch($template, $data, $config); } diff --git a/start.php b/start.php index 20a7817c..b75d94f3 100644 --- a/start.php +++ b/start.php @@ -27,11 +27,6 @@ if (is_file(ROOT_PATH . 'env' . EXT)) { putenv("$name=$val"); } } -// 自动识别调试模式 -if (!defined('APP_DEBUG')) { - $debug = getenv(ENV_PREFIX . 'APP_DEBUG'); - define('APP_DEBUG', $debug); -} // 加载模式定义文件 $mode = require MODE_PATH . APP_MODE . EXT; diff --git a/tests/application/database.php b/tests/application/database.php index 885ebf84..24434efb 100644 --- a/tests/application/database.php +++ b/tests/application/database.php @@ -32,7 +32,7 @@ return [ // 数据库表前缀 'prefix' => '', // 数据库调试模式 - 'debug' => APP_DEBUG, + 'debug' => true, // 数据库部署方式:0 集中式(单一服务器),1 分布式(主从服务器) 'deploy' => 0, // 数据库读写是否分离 主从式有效 diff --git a/tests/mock.php b/tests/mock.php index 536d559d..4a620b0b 100644 --- a/tests/mock.php +++ b/tests/mock.php @@ -15,8 +15,6 @@ $_SERVER['REQUEST_METHOD'] = 'GET'; define('TEST_PATH', __DIR__ . '/'); // 定义项目路径 define('APP_PATH', __DIR__ . '/application/'); -// 开启调试模式 -define('APP_DEBUG', true); // 关闭应用自动执行 define('APP_AUTO_RUN', false); // 加载框架引导文件 diff --git a/tests/thinkphp/baseTest.php b/tests/thinkphp/baseTest.php index b61ad444..d8b2ce30 100644 --- a/tests/thinkphp/baseTest.php +++ b/tests/thinkphp/baseTest.php @@ -34,7 +34,6 @@ class baseTest extends \PHPUnit_Framework_TestCase $this->assertNotEmpty(TEMP_PATH); $this->assertNotEmpty(VENDOR_PATH); $this->assertNotEmpty(EXT); - $this->assertTrue(is_bool(APP_DEBUG)); $this->assertNotEmpty(ENV_PREFIX); $this->assertTrue(is_bool(IS_API)); $this->assertNotEmpty(APP_MODE); diff --git a/tpl/think_exception.tpl b/tpl/think_exception.tpl index 4e46f6ee..725fa9ba 100644 --- a/tpl/think_exception.tpl +++ b/tpl/think_exception.tpl @@ -181,7 +181,7 @@ - +
@@ -309,7 +309,7 @@ V { 十年磨一剑-为API开发设计的高性能框架 }
- +