functions.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006-2012 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. /**
  12. * Think 命令行模式公共函数库
  13. * @category Think
  14. * @package Common
  15. * @author liu21st <liu21st@gmail.com>
  16. */
  17. // 错误输出
  18. function halt($error) {
  19. exit($error);
  20. }
  21. // 自定义异常处理
  22. function throw_exception($msg, $type='ThinkException', $code=0) {
  23. halt($msg);
  24. }
  25. // 浏览器友好的变量输出
  26. function dump($var, $echo=true, $label=null, $strict=true) {
  27. $label = ($label === null) ? '' : rtrim($label) . ' ';
  28. if (!$strict) {
  29. if (ini_get('html_errors')) {
  30. $output = print_r($var, true);
  31. $output = "<pre>" . $label . htmlspecialchars($output, ENT_QUOTES) . "</pre>";
  32. } else {
  33. $output = $label . print_r($var, true);
  34. }
  35. } else {
  36. ob_start();
  37. var_dump($var);
  38. $output = ob_get_clean();
  39. if (!extension_loaded('xdebug')) {
  40. $output = preg_replace("/\]\=\>\n(\s+)/m", "] => ", $output);
  41. $output = '<pre>' . $label . htmlspecialchars($output, ENT_QUOTES) . '</pre>';
  42. }
  43. }
  44. if ($echo) {
  45. echo($output);
  46. return null;
  47. }else
  48. return $output;
  49. }
  50. // 区间调试开始
  51. function debug_start($label='') {
  52. $GLOBALS[$label]['_beginTime'] = microtime(TRUE);
  53. if (MEMORY_LIMIT_ON)
  54. $GLOBALS[$label]['_beginMem'] = memory_get_usage();
  55. }
  56. // 区间调试结束,显示指定标记到当前位置的调试
  57. function debug_end($label='') {
  58. $GLOBALS[$label]['_endTime'] = microtime(TRUE);
  59. echo '<div style="text-align:center;width:100%">Process ' . $label . ': Times ' . number_format($GLOBALS[$label]['_endTime'] - $GLOBALS[$label]['_beginTime'], 6) . 's ';
  60. if (MEMORY_LIMIT_ON) {
  61. $GLOBALS[$label]['_endMem'] = memory_get_usage();
  62. echo ' Memories ' . number_format(($GLOBALS[$label]['_endMem'] - $GLOBALS[$label]['_beginMem']) / 1024) . ' k';
  63. }
  64. echo '</div>';
  65. }
  66. // 全局缓存设置和读取
  67. function S($name, $value='', $expire='', $type='',$options=null) {
  68. static $_cache = array();
  69. alias_import('Cache');
  70. //取得缓存对象实例
  71. $cache = Cache::getInstance($type,$options);
  72. if ('' !== $value) {
  73. if (is_null($value)) {
  74. // 删除缓存
  75. $result = $cache->rm($name);
  76. if ($result)
  77. unset($_cache[$type . '_' . $name]);
  78. return $result;
  79. }else {
  80. // 缓存数据
  81. $cache->set($name, $value, $expire);
  82. $_cache[$type . '_' . $name] = $value;
  83. }
  84. return;
  85. }
  86. if (isset($_cache[$type . '_' . $name]))
  87. return $_cache[$type . '_' . $name];
  88. // 获取缓存数据
  89. $value = $cache->get($name);
  90. $_cache[$type . '_' . $name] = $value;
  91. return $value;
  92. }
  93. // 快速文件数据读取和保存 针对简单类型数据 字符串、数组
  94. function F($name, $value='', $path=DATA_PATH) {
  95. static $_cache = array();
  96. $filename = $path . $name . '.php';
  97. if ('' !== $value) {
  98. if (is_null($value)) {
  99. // 删除缓存
  100. return unlink($filename);
  101. } else {
  102. // 缓存数据
  103. $dir = dirname($filename);
  104. // 目录不存在则创建
  105. if (!is_dir($dir))
  106. mkdir($dir);
  107. return file_put_contents($filename, strip_whitespace("<?php\nreturn " . var_export($value, true) . ";\n?>"));
  108. }
  109. }
  110. if (isset($_cache[$name]))
  111. return $_cache[$name];
  112. // 获取缓存数据
  113. if (is_file($filename)) {
  114. $value = include $filename;
  115. $_cache[$name] = $value;
  116. } else {
  117. $value = false;
  118. }
  119. return $value;
  120. }
  121. // 取得对象实例 支持调用类的静态方法
  122. function get_instance_of($name, $method='', $args=array()) {
  123. static $_instance = array();
  124. $identify = empty($args) ? $name . $method : $name . $method . to_guid_string($args);
  125. if (!isset($_instance[$identify])) {
  126. if (class_exists($name)) {
  127. $o = new $name();
  128. if (method_exists($o, $method)) {
  129. if (!empty($args)) {
  130. $_instance[$identify] = call_user_func_array(array(&$o, $method), $args);
  131. } else {
  132. $_instance[$identify] = $o->$method();
  133. }
  134. }
  135. else
  136. $_instance[$identify] = $o;
  137. }
  138. else
  139. halt(L('_CLASS_NOT_EXIST_') . ':' . $name);
  140. }
  141. return $_instance[$identify];
  142. }
  143. // 根据PHP各种类型变量生成唯一标识号
  144. function to_guid_string($mix) {
  145. if (is_object($mix) && function_exists('spl_object_hash')) {
  146. return spl_object_hash($mix);
  147. } elseif (is_resource($mix)) {
  148. $mix = get_resource_type($mix) . strval($mix);
  149. } else {
  150. $mix = serialize($mix);
  151. }
  152. return md5($mix);
  153. }
  154. // 加载扩展配置文件
  155. function load_ext_file() {
  156. // 加载自定义外部文件
  157. if(C('LOAD_EXT_FILE')) {
  158. $files = explode(',',C('LOAD_EXT_FILE'));
  159. foreach ($files as $file){
  160. $file = COMMON_PATH.$file.'.php';
  161. if(is_file($file)) include $file;
  162. }
  163. }
  164. // 加载自定义的动态配置文件
  165. if(C('LOAD_EXT_CONFIG')) {
  166. $configs = C('LOAD_EXT_CONFIG');
  167. if(is_string($configs)) $configs = explode(',',$configs);
  168. foreach ($configs as $key=>$config){
  169. $file = CONF_PATH.$config.'.php';
  170. if(is_file($file)) {
  171. is_numeric($key)?C(include $file):C($key,include $file);
  172. }
  173. }
  174. }
  175. }