Qii.php 6.8 KB

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