Qii.php 7.8 KB

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