Qii.php 7.0 KB

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