Skip to content

Commit a9c137d

Browse files
author
nottellyou
authored
Merge pull request #1 from vus520/master
fsdafds
2 parents 3661fd8 + 1f72c95 commit a9c137d

File tree

12 files changed

+446
-28
lines changed

12 files changed

+446
-28
lines changed

ThinkPHP/Library/Think/App.class.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ public static function run()
210210
}
211211
// 记录应用初始化时间
212212
G('initTime');
213-
App::exec();
213+
! defined('APP_NO_EXEC') && App::exec();
214214
// 应用结束标签
215215
Hook::listen('app_end');
216216
return;

ThinkPHP/Library/Think/Model.class.php

Lines changed: 41 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,17 @@ public function __construct($name = '', $tablePrefix = '', $connection = '')
9999
// 数据库初始化操作
100100
// 获取数据库操作对象
101101
// 当前模型有独立的数据库连接信息
102-
$this->db(0, empty($this->connection) ? $connection : $this->connection, true);
102+
//$this->db(0, empty($this->connection) ? $connection : $this->connection, true);
103+
$this->connection_args = $connection;
104+
}
105+
106+
//检查连接是否断开
107+
function db_instance()
108+
{
109+
if (empty($this->db) || empty($this->_db))
110+
$this->db(0, empty($this->connection) ? $this->connection_args : $this->connection, true);
111+
112+
return $this->db;
103113
}
104114

105115
/**
@@ -124,7 +134,8 @@ protected function _checkTableInfo()
124134
}
125135
}
126136
// 每次都会读取数据表信息
127-
$this->flush();
137+
if($this->db)
138+
$this->flush();
128139
}
129140
}
130141

@@ -136,9 +147,9 @@ protected function _checkTableInfo()
136147
public function flush()
137148
{
138149
// 缓存不存在则查询数据表信息
139-
$this->db->setModel($this->name);
150+
$this->db_instance()->setModel($this->name);
140151
$tableName = $this->getTableName();
141-
$fields = $this->db->getFields($tableName);
152+
$fields = $this->db_instance()->getFields($tableName);
142153
if (!$fields) {
143154
// 无法获取字段信息
144155
return false;
@@ -337,7 +348,7 @@ public function add($data = '', $options = array(), $replace = false)
337348
return false;
338349
}
339350
// 写入数据到数据库
340-
$result = $this->db->insert($data, $options, $replace);
351+
$result = $this->db_instance()->insert($data, $options, $replace);
341352
if (false !== $result && is_numeric($result)) {
342353
$pk = $this->getPk();
343354
// 增加复合主键支持
@@ -380,7 +391,7 @@ public function addAll($dataList, $options = array(), $replace = false)
380391
// 分析表达式
381392
$options = $this->_parseOptions($options);
382393
// 写入数据到数据库
383-
$result = $this->db->insertAll($dataList, $options, $replace);
394+
$result = $this->db_instance()->insertAll($dataList, $options, $replace);
384395
if (false !== $result) {
385396
$insertId = $this->getLastInsID();
386397
if ($insertId) {
@@ -403,7 +414,7 @@ public function selectAdd($fields = '', $table = '', $options = array())
403414
// 分析表达式
404415
$options = $this->_parseOptions($options);
405416
// 写入数据到数据库
406-
if (false === $result = $this->db->selectInsert($fields ?: $options['field'], $table ?: $this->getTableName(), $options)) {
417+
if (false === $result = $this->db_instance()->selectInsert($fields ?: $options['field'], $table ?: $this->getTableName(), $options)) {
407418
// 数据库插入操作失败
408419
$this->error = L('_OPERATION_WRONG_');
409420
return false;
@@ -476,7 +487,7 @@ public function save($data = '', $options = array())
476487
if (false === $this->_before_update($data, $options)) {
477488
return false;
478489
}
479-
$result = $this->db->update($data, $options);
490+
$result = $this->db_instance()->update($data, $options);
480491
if (false !== $result && is_numeric($result)) {
481492
if (isset($pkValue)) {
482493
$data[$pk] = $pkValue;
@@ -554,7 +565,7 @@ public function delete($options = array())
554565
if (false === $this->_before_delete($options)) {
555566
return false;
556567
}
557-
$result = $this->db->delete($options);
568+
$result = $this->db_instance()->delete($options);
558569
if (false !== $result && is_numeric($result)) {
559570
$data = array();
560571
if (isset($pkValue)) {
@@ -625,7 +636,7 @@ public function select($options = array())
625636
return $data;
626637
}
627638
}
628-
$resultSet = $this->db->select($options);
639+
$resultSet = $this->db_instance()->select($options);
629640
if (false === $resultSet) {
630641
return false;
631642
}
@@ -813,7 +824,7 @@ public function find($options = array())
813824
return $data;
814825
}
815826
}
816-
$resultSet = $this->db->select($options);
827+
$resultSet = $this->db_instance()->select($options);
817828
if (false === $resultSet) {
818829
return false;
819830
}
@@ -1010,7 +1021,7 @@ public function getField($field, $sepa = null)
10101021
if (!isset($options['limit'])) {
10111022
$options['limit'] = is_numeric($sepa) ? $sepa : '';
10121023
}
1013-
$resultSet = $this->db->select($options);
1024+
$resultSet = $this->db_instance()->select($options);
10141025
if (!empty($resultSet)) {
10151026
if (is_string($resultSet)) {
10161027
return $resultSet;
@@ -1041,7 +1052,7 @@ public function getField($field, $sepa = null)
10411052
// 当sepa指定为true的时候 返回所有数据
10421053
$options['limit'] = is_numeric($sepa) ? $sepa : 1;
10431054
}
1044-
$result = $this->db->select($options);
1055+
$result = $this->db_instance()->select($options);
10451056
if (!empty($result)) {
10461057
if (is_string($result)) {
10471058
return $result;
@@ -1524,7 +1535,7 @@ public function check($value, $rule, $type = 'regex')
15241535
*/
15251536
public function procedure($sql, $parse = false)
15261537
{
1527-
return $this->db->procedure($sql, $parse);
1538+
return $this->db_instance()->procedure($sql, $parse);
15281539
}
15291540

15301541
/**
@@ -1541,7 +1552,7 @@ public function query($sql, $parse = false)
15411552
array_shift($parse);
15421553
}
15431554
$sql = $this->parseSql($sql, $parse);
1544-
return $this->db->query($sql);
1555+
return $this->db_instance()->query($sql);
15451556
}
15461557

15471558
/**
@@ -1558,7 +1569,7 @@ public function execute($sql, $parse = false)
15581569
array_shift($parse);
15591570
}
15601571
$sql = $this->parseSql($sql, $parse);
1561-
return $this->db->execute($sql);
1572+
return $this->db_instance()->execute($sql);
15621573
}
15631574

15641575
/**
@@ -1573,8 +1584,9 @@ protected function parseSql($sql, $parse)
15731584
// 分析表达式
15741585
if (true === $parse) {
15751586
$options = $this->_parseOptions();
1576-
$sql = $this->db->parseSql($sql, $options);
1587+
$sql = $this->db_instance()->parseSql($sql, $options);
15771588
} elseif (is_array($parse)) {
1589+
$this->db_instance();
15781590
// SQL预处理
15791591
$parse = array_map(array($this->db, 'escapeString'), $parse);
15801592
$sql = vsprintf($sql, $parse);
@@ -1583,7 +1595,7 @@ protected function parseSql($sql, $parse)
15831595
$prefix = $this->tablePrefix;
15841596
$sql = preg_replace_callback("/__([A-Z0-9_-]+)__/sU", function ($match) use ($prefix) {return $prefix . strtolower($match[1]);}, $sql);
15851597
}
1586-
$this->db->setModel($this->name);
1598+
$this->db_instance()->setModel($this->name);
15871599
return $sql;
15881600
}
15891601

@@ -1674,7 +1686,7 @@ public function getTableName()
16741686
public function startTrans()
16751687
{
16761688
$this->commit();
1677-
$this->db->startTrans();
1689+
$this->db_instance()->startTrans();
16781690
return;
16791691
}
16801692

@@ -1685,7 +1697,7 @@ public function startTrans()
16851697
*/
16861698
public function commit()
16871699
{
1688-
return $this->db->commit();
1700+
return $this->db_instance()->commit();
16891701
}
16901702

16911703
/**
@@ -1695,7 +1707,7 @@ public function commit()
16951707
*/
16961708
public function rollback()
16971709
{
1698-
return $this->db->rollback();
1710+
return $this->db_instance()->rollback();
16991711
}
17001712

17011713
/**
@@ -1715,7 +1727,7 @@ public function getError()
17151727
*/
17161728
public function getDbError()
17171729
{
1718-
return $this->db->getError();
1730+
return $this->db_instance()->getError();
17191731
}
17201732

17211733
/**
@@ -1725,7 +1737,7 @@ public function getDbError()
17251737
*/
17261738
public function getLastInsID()
17271739
{
1728-
return $this->db->getLastInsID();
1740+
return $this->db_instance()->getLastInsID();
17291741
}
17301742

17311743
/**
@@ -1735,7 +1747,7 @@ public function getLastInsID()
17351747
*/
17361748
public function getLastSql()
17371749
{
1738-
return $this->db->getLastSql($this->name);
1750+
return $this->db_instance()->getLastSql($this->name);
17391751
}
17401752
// 鉴于getLastSql比较常用 增加_sql 别名
17411753
public function _sql()
@@ -1760,6 +1772,9 @@ public function getPk()
17601772
*/
17611773
public function getDbFields()
17621774
{
1775+
if (is_null($this->db))
1776+
$this->db_instance();
1777+
17631778
if (isset($this->options['table'])) {
17641779
// 动态指定表名
17651780
if (is_array($this->options['table'])) {
@@ -1771,7 +1786,7 @@ public function getDbFields()
17711786
return false;
17721787
}
17731788
}
1774-
$fields = $this->db->getFields($table);
1789+
$fields = $this->db_instance()->getFields($table);
17751790
return $fields ? array_keys($fields) : false;
17761791
}
17771792
if ($this->fields) {
@@ -2006,6 +2021,7 @@ public function where($where, $parse = null)
20062021
$parse = func_get_args();
20072022
array_shift($parse);
20082023
}
2024+
$this->db_instance();
20092025
$parse = array_map(array($this->db, 'escapeString'), $parse);
20102026
$where = vsprintf($where, $parse);
20112027
} elseif (is_object($where)) {

ThinkPHP/Mode/Api/App.class.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ public static function run()
143143
}
144144
// 记录应用初始化时间
145145
G('initTime');
146-
App::exec();
146+
! defined('APP_NO_EXEC') && App::exec();
147147
return;
148148
}
149149

ThinkPHP/Mode/Lite/App.class.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ public static function run()
156156
}
157157
// 记录应用初始化时间
158158
G('initTime');
159-
App::exec();
159+
! defined('APP_NO_EXEC') && App::exec();
160160
return;
161161
}
162162

ThinkPHP/Mode/Tiny/App.class.php

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
<?php
2+
namespace Think;
3+
4+
/**
5+
* ThinkPHP极简模式
6+
* @author shuhai
7+
*
8+
*/
9+
class App
10+
{
11+
/**
12+
* 应用程序初始化
13+
* @access public
14+
* @return void
15+
*/
16+
public static function init()
17+
{
18+
// 定义当前请求的系统常量
19+
define('NOW_TIME', $_SERVER['REQUEST_TIME']);
20+
define('REQUEST_METHOD', $_SERVER['REQUEST_METHOD']);
21+
define('IS_GET', REQUEST_METHOD == 'GET' ? true : false);
22+
define('IS_POST', REQUEST_METHOD == 'POST' ? true : false);
23+
define('IS_PUT', REQUEST_METHOD == 'PUT' ? true : false);
24+
define('IS_DELETE', REQUEST_METHOD == 'DELETE' ? true : false);
25+
define('IS_AJAX', ((isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') || !empty($_POST[C('VAR_AJAX_SUBMIT')]) || !empty($_GET[C('VAR_AJAX_SUBMIT')])) ? true : false);
26+
27+
if (C('REQUEST_VARS_FILTER')) {
28+
// 全局安全过滤
29+
array_walk_recursive($_GET, 'think_filter');
30+
array_walk_recursive($_POST, 'think_filter');
31+
array_walk_recursive($_REQUEST, 'think_filter');
32+
}
33+
34+
// 日志目录转换为绝对路径
35+
C('LOG_PATH', realpath(LOG_PATH) . '/');
36+
// TMPL_EXCEPTION_FILE 改为绝对地址
37+
C('TMPL_EXCEPTION_FILE', realpath(C('TMPL_EXCEPTION_FILE')));
38+
return;
39+
}
40+
41+
/**
42+
* 执行应用程序
43+
* @access public
44+
* @return void
45+
*/
46+
public static function exec()
47+
{
48+
// 获取控制器和操作名
49+
define('MODULE_NAME', defined('BIND_MODULE') ? BIND_MODULE : C('DEFAULT_MODULE'));
50+
define('CONTROLLER_NAME', defined('BIND_CONTROLLER') ? BIND_CONTROLLER : C('DEFAULT_CONTROLLER'));
51+
define('ACTION_NAME', defined('BIND_ACTION') ? BIND_ACTION : C('DEFAULT_ACTION'));
52+
53+
$module = sprintf("%s/%s", MODULE_NAME, CONTROLLER_NAME);
54+
$module = A($module);
55+
$action = ACTION_NAME;
56+
57+
//检查模块/控制器是否存在
58+
if (!$module) {
59+
// 是否定义Empty控制器
60+
$module = A('Empty');
61+
if (!$module) {
62+
E(L('_CONTROLLER_NOT_EXIST_') . ':' . CONTROLLER_NAME);
63+
}
64+
}
65+
66+
try {
67+
if (!preg_match('/^[A-Za-z](\w)*$/', $action)) {
68+
// 非法操作
69+
throw new \ReflectionException();
70+
}
71+
72+
//执行当前操作
73+
$method = new \ReflectionMethod($module, $action);
74+
if ($method->isPublic() && !$method->isStatic()) {
75+
$method->invoke($module);
76+
} else {
77+
// 操作方法不是Public 抛出异常
78+
throw new \ReflectionException();
79+
}
80+
} catch (\ReflectionException $e) {
81+
// 方法调用发生异常后 引导到__call方法处理
82+
$method = new \ReflectionMethod($module, '__call');
83+
$method->invokeArgs($module, array($action, ''));
84+
}
85+
return;
86+
}
87+
88+
/**
89+
* 运行应用实例 入口文件使用的快捷方法
90+
* @access public
91+
* @return void
92+
*/
93+
public static function run()
94+
{
95+
load_ext_file(COMMON_PATH);
96+
97+
App::init();
98+
! defined('APP_NO_EXEC') && App::exec();
99+
}
100+
101+
}

0 commit comments

Comments
 (0)