Qii.php 7.5 KB

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