Qii.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <?php
  2. /**
  3. * Qii 框架基本库所在路径
  4. */
  5. define('Qii_DIR', dirname(__FILE__));
  6. /**
  7. * DIRECTORY_SEPARATOR 的简写
  8. */
  9. define('DS', DIRECTORY_SEPARATOR);
  10. /**
  11. * 定义包含的路径分隔符
  12. */
  13. define('PS', PATH_SEPARATOR);
  14. /**
  15. * 定义操作系统类型
  16. */
  17. define('OS', strtoupper(substr(PHP_OS, 0, 3)));
  18. define('IS_CLI', php_sapi_name() == 'cli' ? true : false);
  19. if(IS_CLI) {
  20. define('PATH_INFO', array_pop($argv));
  21. }else{
  22. define('PATH_INFO', isset($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : '');
  23. }
  24. /**
  25. * EOL 和 SPACE
  26. */
  27. define('QII_EOL', IS_CLI ? PHP_EOL : '<br />');
  28. define('QII_SPACE', IS_CLI ? ' ' : '&nbsp;');
  29. require Qii_DIR . DS . 'Autoloader' . DS . 'Import.php';
  30. \Qii\Autoloader\Import::setFileLoaded(Qii_DIR . DS . 'Autoloader' . DS . 'Import.php');
  31. \Qii\Autoloader\Import::requires(array(Qii_DIR . DS .'Consts'. DS . 'Config.php',
  32. Qii_DIR . DS . 'Functions'. DS . 'Funcs.php',
  33. Qii_DIR . DS .'Autoloader'. DS . 'Factory.php',
  34. Qii_DIR . DS . 'Application.php',
  35. Qii_DIR . DS .'Autoloader'. DS . 'Psr4.php',
  36. Qii_DIR . DS .'Config'. DS . 'Arrays.php',
  37. )
  38. );
  39. use \Qii\Application;
  40. use \Qii\Autoloader\Factory;
  41. use \Qii\Autoloader\Psr4;
  42. use \Qii\Config\Register;
  43. class Qii extends Application
  44. {
  45. public function __construct()
  46. {
  47. parent::__construct();
  48. }
  49. /**
  50. * Instance func
  51. *
  52. **/
  53. public static function getInstance()
  54. {
  55. if(func_num_args() > 0)
  56. {
  57. $args = func_get_args();
  58. return call_user_func_array(array('\Qii\Autoloader\Factory', 'getInstance'), $args);
  59. }
  60. return Factory::getInstance('\Qii');
  61. }
  62. /**
  63. * 设置private 属性
  64. *
  65. * @param String $name
  66. * @param Mix $value
  67. */
  68. public static function setPrivate($name, $value)
  69. {
  70. Psr4::getInstance()->loadClass('\Qii\Config\Arrays')->setPrivate($name, $value);
  71. }
  72. /**
  73. * 获取private属性
  74. *
  75. * @param String $name
  76. * @param String $key
  77. * @return Mix
  78. */
  79. public static function getPrivate($name, $key = '')
  80. {
  81. $private = Psr4::getInstance()->loadClass('\Qii\Config\Arrays')->getPrivate($name);
  82. if (preg_match('/^\s*$/', $key)) {
  83. return $private;
  84. }
  85. if (isset($private[$key])) return $private[$key];
  86. }
  87. public static function i()
  88. {
  89. return call_user_func_array(array(
  90. Psr4::getInstance()->loadClass('\Qii\Language\Loader'), 'i'),
  91. func_get_args()
  92. );
  93. }
  94. /**
  95. * 错误设置,如果满足给定的条件就直接返回false,否则在设置了错误页面的情况下返回true
  96. * 如果出错后要终止的话,需要自行处理,此方法不错停止不执行
  97. *
  98. * @param Bool $condition
  99. * @param int $line 出错的行数,这样以便轻松定位错误
  100. * @param String $msg
  101. * @param Int|String $code
  102. * @return Bool
  103. */
  104. public static function setError($condition, $line = 0, $code, $args = null)
  105. {
  106. return call_user_func_array(array('\Qii\Exceptions\Error', 'setError'), func_get_args());
  107. }
  108. /**
  109. * 抛出异常
  110. *
  111. * @return mixed
  112. */
  113. public static function e()
  114. {
  115. return call_user_func_array(array('\Qii\Exceptions\Errors', 'e'), func_get_args());
  116. }
  117. /**
  118. * 返回当前app的配置
  119. * @param string $key 如果需要返回单独的某一个key就指定一下这个值
  120. * @return Mix
  121. */
  122. public static function appConfigure($key = null)
  123. {
  124. return Register::getAppConfigure(\Qii::getInstance()->getAppIniFile(), $key);
  125. }
  126. /**
  127. * 当调用Qii不存在的方法的时候,试图通过Autoload去加载对应的类
  128. * 示例:
  129. * \Qii::getInstance()->Qii_Autoloader_Psr4('instance', 'Model\User');
  130. * 此方法将调用:Qii_Autoloader_Psr4->instance('Model\User');
  131. */
  132. public function __call($className, $args)
  133. {
  134. return call_user_func_array(array(Psr4::getInstance(), 'loadClass'), $args);
  135. }
  136. /**
  137. * 当调用不存在的静态方法的时候会试图执行对应的类和静态方法
  138. * 示例:
  139. * \Qii::Qii_Autoloader_Psr4('getInstance')
  140. * 此方法将调用:\Qii\Autoloader\Psr4::getInstance静态方法
  141. */
  142. public static function __callStatic($className, $args)
  143. {
  144. $method = array_shift($args);
  145. $className = Psr4::getInstance()->getClassName($className);
  146. return call_user_func_array($className . '::' . $method, $args);
  147. }
  148. }
  149. if (!function_exists('catch_fatal_error')) {
  150. function catch_fatal_error()
  151. {
  152. // Getting Last Error
  153. $error = error_get_last();
  154. // Check if Last error is of type FATAL
  155. if (isset($error['type']) && $error['type'] == E_ERROR) {
  156. // Fatal Error Occurs
  157. $message = array();
  158. $message[] = 'Error file : ' . ltrim($error['file'], Psr4::realpath($_SERVER['DOCUMENT_ROOT']));
  159. $message[] = 'Error line : ' . $error['line'] . ' on ' . \Qii\Exceptions\Errors::getLineMessage($error['file'], $error['line']);
  160. $message[] = 'Error description : ' . $error['message'];
  161. \Qii\Exceptions\Error::showError($message);
  162. }
  163. }
  164. }
  165. \Qii\Autoloader\Psr4::getInstance()
  166. ->register()
  167. ->setUseNamespace('Qii\\', true)
  168. ->setUseNamespace('Qii\Action', true)
  169. ->setUseNamespace('Qii\Autoloader', true)
  170. ->setUseNamespace('Qii\Bootstrap', true)
  171. ->setUseNamespace('Qii\Config', true)
  172. ->setUseNamespace('Qii\Consts', true)
  173. ->setUseNamespace('Qii\Controller', true)
  174. ->setUseNamespace('Qii\Exceptions', true)
  175. ->setUseNamespace('Qii\Language', true)
  176. ->setUseNamespace('Qii\Library', true)
  177. ->setUseNamespace('Qii\Loger', true)
  178. ->setUseNamespace('Qii\Plugin', true)
  179. ->setUseNamespace('Qii\Request', false)
  180. ->setUseNamespace('Qii\Router', true)
  181. ->setUseNamespace('Qii\View', true)
  182. ->setUseNamespace('WhichBrowser', true)
  183. ->setUseNamespace('Smarty\\', false)
  184. ->setUseNamespace('Smarty\\Internal', false);
  185. \Qii\Autoloader\Psr4::getInstance()
  186. ->addNamespace('Qii\\', Qii_DIR . DS)
  187. ->addNamespace('Qii\Action', Qii_DIR . DS . 'Action')
  188. ->addNamespace('Qii\Autoloader', Qii_DIR . DS . 'Autoloader')
  189. ->addNamespace('Qii\Controller', Qii_DIR . DS . 'Controller')
  190. ->addNamespace('Qii\Bootstrap', Qii_DIR . DS . 'Bootstrap')
  191. ->addNamespace('Qii\Config', Qii_DIR . DS . 'Config')
  192. ->addNamespace('Qii\Consts', Qii_DIR . DS . 'Consts')
  193. ->addNamespace('Qii\Exceptions', Qii_DIR . DS . 'Exceptions')
  194. ->addNamespace('Qii\Language', Qii_DIR . DS . 'Language')
  195. ->addNamespace('Qii\Library', Qii_DIR . DS . 'Library')
  196. ->addNamespace('Qii\Loger', Qii_DIR . DS . 'Loger')
  197. ->addNamespace('Qii\Plugin', Qii_DIR . DS . 'Plugin')
  198. ->addNamespace('Qii\Request', Qii_DIR . DS . 'Request')
  199. ->addNamespace('Qii\Response', Qii_DIR . DS . 'Response')
  200. ->addNamespace('Qii\Router', Qii_DIR . DS . 'Router')
  201. ->addNamespace('Qii\View', Qii_DIR . DS . 'View')
  202. ->addNamespace('Smarty', Qii_DIR . DS . 'View' . DS . 'smarty')
  203. ->addNamespace('Smarty', Qii_DIR . DS . 'View' . DS . 'smarty' . DS . 'sysplugins')
  204. ->addNamespace('WhichBrowser', Qii_DIR . DS . 'Library'. DS . 'Third'. DS . 'WhichBrowser')
  205. ;
  206. //加载默认语言包
  207. \Qii\Autoloader\Factory::getInstance('\Qii\Language\Loader')->load('error', Qii_DIR . DS . 'Language');
  208. \Qii\Autoloader\Factory::getInstance('\Qii\Language\Loader')->load('error', Qii_DIR . DS . 'Language');
  209. \Qii\Autoloader\Factory::getInstance('\Qii\Language\Loader')->load('exception', Qii_DIR . DS . 'Language');
  210. \Qii\Autoloader\Factory::getInstance('\Qii\Language\Loader')->load('resource', Qii_DIR . DS . 'Language');
  211. //捕获FATAL错误,用户可以选择记录到日志,还是直接显示或者不显示错误
  212. register_shutdown_function('catch_fatal_error');
  213. set_exception_handler(array('\Qii\Exceptions\Errors', 'getError'));
  214. set_error_handler(array('\Qii\Exceptions\Errors', 'getError'), E_USER_ERROR);