Qii.php 7.6 KB

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