Qii.php 7.5 KB

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