Qii.php 7.7 KB

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