取消 APP_DEBUG 常量 改为 App::$debug 属性获取 设置调试模式 改为 app_debug 配置参数 在应用配置文件中设置

This commit is contained in:
thinkphp
2016-06-15 16:43:31 +08:00
parent 343252324a
commit 4c848c4a74
25 changed files with 62 additions and 40 deletions

View File

@@ -5,6 +5,8 @@ return [
// | 应用设置
// +----------------------------------------------------------------------
// 应用调试模式
'app_debug' => true,
// 应用模式状态
'app_status' => '',
// 是否支持多模块

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -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 = [];

View File

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

View File

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

View File

@@ -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':

View File

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

View File

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

View File

@@ -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.

View File

@@ -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设置

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -32,7 +32,7 @@ return [
// 数据库表前缀
'prefix' => '',
// 数据库调试模式
'debug' => APP_DEBUG,
'debug' => true,
// 数据库部署方式:0 集中式(单一服务器),1 分布式(主从服务器)
'deploy' => 0,
// 数据库读写是否分离 主从式有效

View File

@@ -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);
// 加载框架引导文件

View File

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

View File

@@ -181,7 +181,7 @@
</style>
</head>
<body>
<?php if(APP_DEBUG) { ?>
<?php if(\think\App::$debug) { ?>
<div class="exception">
<div class="message">
@@ -309,7 +309,7 @@
<span>V<?php echo THINK_VERSION; ?></span>
<span>{ -API开发设计的高性能框架 }</span>
</div>
<?php if(APP_DEBUG) { ?>
<?php if(\think\App::$debug) { ?>
<script>
var LINE = <?php echo $line; ?>;