Qii.php 6.6 KB

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