Qii.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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(
  32. array(Qii_DIR . DS . 'Config' . DS . 'Consts.php',
  33. Qii_DIR . DS . 'Functions' . DS . 'Funcs.php',
  34. Qii_DIR . DS . 'Autoloader' . DS . 'Factory.php',
  35. Qii_DIR . DS . 'Application.php',
  36. Qii_DIR . DS . 'Autoloader' . DS . 'Psr4.php',
  37. Qii_DIR . DS . 'Config' . DS . 'Arrays.php',
  38. )
  39. );
  40. use \Qii\Application;
  41. use \Qii\Autoloader\Factory;
  42. use \Qii\Autoloader\Psr4;
  43. use \Qii\Config\Register;
  44. class Qii extends Application
  45. {
  46. public function __construct()
  47. {
  48. parent::__construct();
  49. }
  50. /**
  51. * Instance func
  52. *
  53. **/
  54. public static function getInstance()
  55. {
  56. if (func_num_args() > 0) {
  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')->get($name);
  82. if (preg_match('/^\s*$/', $key)) {
  83. return $private;
  84. }
  85. if (isset($private[$key])) return $private[$key];
  86. }
  87. /**
  88. * 获取语言包内容指定的内容
  89. *
  90. * @return mixed
  91. */
  92. public static function i()
  93. {
  94. return call_user_func_array(array(
  95. Psr4::getInstance()->loadClass('\Qii\Language\Loader'), 'i'),
  96. func_get_args()
  97. );
  98. }
  99. /**
  100. * 错误设置,如果满足给定的条件就直接返回false,否则在设置了错误页面的情况下返回true
  101. * 如果出错后要终止的话,需要自行处理,此方法不错停止不执行
  102. *
  103. * @param Bool $condition
  104. * @param int $line 出错的行数,这样以便轻松定位错误
  105. * @param String $msg
  106. * @param Int|String $code
  107. * @return Bool
  108. */
  109. public static function setError($condition, $line = 0, $code, $args = null)
  110. {
  111. return call_user_func_array(array('\Qii\Exceptions\Error', 'setError'), func_get_args());
  112. }
  113. /**
  114. * 抛出异常
  115. *
  116. * @return mixed
  117. */
  118. public static function e()
  119. {
  120. return call_user_func_array(array('\Qii\Exceptions\Errors', 'e'), func_get_args());
  121. }
  122. /**
  123. * 返回当前app的配置
  124. * @param string $key 如果需要返回单独的某一个key就指定一下这个值
  125. * @return Mix
  126. */
  127. public static function appConfigure($key = null)
  128. {
  129. return Register::getAppConfigure(\Qii::getInstance()->getAppIniFile(), $key);
  130. }
  131. /**
  132. * 当调用Qii不存在的方法的时候,试图通过Autoload去加载对应的类
  133. * 示例:
  134. * \Qii::getInstance()->Qii_Autoloader_Psr4('instance', 'Model\User');
  135. * 此方法将调用:Qii_Autoloader_Psr4->instance('Model\User');
  136. */
  137. public function __call($className, $args)
  138. {
  139. return call_user_func_array(array(Psr4::getInstance(), 'loadClass'), $args);
  140. }
  141. /**
  142. * 当调用不存在的静态方法的时候会试图执行对应的类和静态方法
  143. * 示例:
  144. * \Qii::Qii_Autoloader_Psr4('getInstance')
  145. * 此方法将调用:\Qii\Autoloader\Psr4::getInstance静态方法
  146. */
  147. public static function __callStatic($className, $args)
  148. {
  149. $method = array_shift($args);
  150. $className = Psr4::getInstance()->getClassName($className);
  151. return call_user_func_array($className . '::' . $method, $args);
  152. }
  153. }
  154. if (!function_exists('catch_fatal_error')) {
  155. function catch_fatal_error()
  156. {
  157. // Getting Last Error
  158. $error = error_get_last();
  159. // Check if Last error is of type FATAL
  160. if (isset($error['type']) && $error['type'] == E_ERROR) {
  161. // Fatal Error Occurs
  162. $message = array();
  163. $message[] = 'Error file : ' . str_replace(Psr4::realpath($_SERVER['DOCUMENT_ROOT']), '', $error['file']);
  164. $message[] = 'Error line : ' . $error['line'] . ' on ' . \Qii\Exceptions\Errors::getLineMessage($error['file'], $error['line']);
  165. $message[] = 'Error description : ' . $error['message'];
  166. if (IS_CLI) {
  167. $cli = new \Qii\Response\Cli();
  168. return $cli->stdout(
  169. str_replace("&nbsp;"
  170. , " "
  171. , strip_tags(join(PHP_EOL, preg_replace("/[\n|\r\n]/", PHP_EOL, $message)))
  172. )
  173. );
  174. }
  175. \Qii\Exceptions\Error::showError($message);
  176. }
  177. }
  178. }
  179. //注册名称空间
  180. $namespace = _include(Qii_DIR . DS . 'Conf' . DS . 'namespace.php');
  181. \Qii\Autoloader\Psr4::getInstance()
  182. ->register()
  183. ->setUseNamespaces($namespace['setUseNamespace'] ?? [])
  184. ->addNamespaces($namespace['addNamespace'] ?? []);
  185. //加载默认语言包
  186. \Qii\Autoloader\Factory::getInstance('\Qii\Language\Loader')->load('error', Qii_DIR . DS . 'Language');
  187. \Qii\Autoloader\Factory::getInstance('\Qii\Language\Loader')->load('exception', Qii_DIR . DS . 'Language');
  188. \Qii\Autoloader\Factory::getInstance('\Qii\Language\Loader')->load('resource', Qii_DIR . DS . 'Language');
  189. //捕获FATAL错误,用户可以选择记录到日志,还是直接显示或者不显示错误
  190. register_shutdown_function('catch_fatal_error');
  191. set_exception_handler(array('\Qii\Exceptions\Errors', 'getError'));
  192. set_error_handler(array('\Qii\Exceptions\Errors', 'getError'), E_USER_ERROR);