Application.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. <?php
  2. namespace Qii;
  3. use \Qii\Base\Dispatcher;
  4. use \Qii\Autoloader\Factory;
  5. use \Qii\Autoloader\Import;
  6. use \Qii\Autoloader\Psr4;
  7. use \Qii\Autoloader\Instance;
  8. use \Qii\Config\Register;
  9. use \Qii\Config\Consts;
  10. use \Qii\Config\Setting;
  11. class Application
  12. {
  13. /**
  14. * 存储网站配置文件内容
  15. *
  16. * @var array $config 配置内容
  17. */
  18. protected static $config = [];
  19. /**
  20. * @var object $logerWriter 写日志工具
  21. */
  22. public $logerWriter = null;
  23. /**
  24. * @var string $workspace 工作目录
  25. */
  26. private static $workspace = './';/**
  27. * @var string $env 环境变量
  28. */
  29. public static $env = 'product';
  30. /**
  31. * @var array $paths 网站使用的路径
  32. */
  33. public static $paths = array('configure', 'controller', 'model', 'view', 'plugins', 'tmp');
  34. /**
  35. * Qii\Request\Url
  36. */
  37. public $request;
  38. /**
  39. * 分发器
  40. */
  41. public $dispatcher = null;
  42. public function __construct()
  43. {
  44. $this->helper = Psr4::getInstance()->loadClass('\Qii\Autoloader\Helper');
  45. }
  46. /**
  47. * 初始化本实例对象
  48. *
  49. * @return object
  50. */
  51. public static function getInstance()
  52. {
  53. $args = func_get_args();
  54. if (count($args) > 0) {
  55. $className = array_shift($args);
  56. return Psr4::getInstance($className);
  57. }
  58. return Factory::getInstance('\Qii\Application');
  59. }
  60. /**
  61. * 设置网站运行环境
  62. *
  63. * @param string $env 网站环境
  64. * @return $this
  65. */
  66. public function setEnv($env)
  67. {
  68. self::$env = $env;
  69. return $this;
  70. }
  71. /**
  72. * 设置缓存文件路径
  73. * @param string $path 缓存路径
  74. */
  75. public function setCachePath($path)
  76. {
  77. Register::set(Consts::APP_CACHE_PATH, $this->getCachePath($path));
  78. return $this;
  79. }
  80. /**
  81. * 保存网站的配置文件
  82. *
  83. * @param $iniFile
  84. */
  85. public function setAppIniFile($iniFile)
  86. {
  87. Register::set(Consts::APP_INI_FILE, $iniFile);
  88. return $this;
  89. }
  90. /**
  91. * 设置网站的工作目录,可以通过此方法将网站的重要文件指向到其他目录
  92. *
  93. * @param string $workspace 工作目录
  94. * @return $this
  95. */
  96. public function setWorkspace($workspace = './')
  97. {
  98. //此处转换成真实路径,防止workspace中引入的文件出错
  99. if (!is_dir($workspace)) {
  100. throw new \Qii\Exceptions\FolderDoesNotExist(\Qii::i(1045, $workspace), __LINE__);
  101. }
  102. $workspace = Psr4::getInstance()->realpath($workspace);
  103. Psr4::getInstance()->removeNamespace('workspace', self::$workspace);
  104. //如果配置了使用namespace就走namespace
  105. self::$workspace = $workspace;
  106. Psr4::getInstance()->addNamespace('workspace', $workspace, true);
  107. foreach (self::$paths AS $path) {
  108. Psr4::getInstance()->addNamespace($path, $workspace . '\\' . $path);
  109. }
  110. return $this;
  111. }
  112. /**
  113. * 获取指定路径的缓存绝对路径
  114. * @param string $path 路径
  115. * @return string 绝对路径
  116. */
  117. public function getCachePath($path)
  118. {
  119. if (self::$workspace != '') return self::$workspace . DS . $path;
  120. $dir = '';
  121. $workspace = sr4::getInstance()->getNamespace('workspace');
  122. foreach ($workspace AS $dir) {
  123. if (is_dir($dir)) $dir = $dir;
  124. }
  125. return $dir . DS . $path;
  126. }
  127. /**
  128. * 获取网站运行环境
  129. *
  130. * @return string
  131. */
  132. public function getEnv()
  133. {
  134. return self::$env;
  135. }
  136. /**
  137. * 获取当前工作目录
  138. */
  139. public function getWorkspace()
  140. {
  141. return self::$workspace;
  142. }
  143. /**
  144. * 获取网站的配置文件
  145. * @return Mix
  146. */
  147. public function getAppIniFile()
  148. {
  149. return Register::get(Consts::APP_INI_FILE);
  150. return $this;
  151. }
  152. /**
  153. * 设置网站配置文件
  154. * @param string $ini 配置文件路径
  155. * @param string $env 环境
  156. *
  157. */
  158. public function setAppConfigure($ini, $env = '')
  159. {
  160. if ($env == '') $env = $this->getEnv();
  161. $ini = Psr4::getInstance()->getFileByPrefix($ini);
  162. $this->setAppIniFile($ini);
  163. if (!Register::setAppConfigure(
  164. $ini,
  165. $env
  166. )
  167. ) throw new \Qii\Exceptions\FileNotFound(\Qii::i(1405, $ini), __LINE__);
  168. //载入request方法
  169. $this->request = Psr4::getInstance()->loadClass('\Qii\Request\Http');
  170. Setting::getInstance()->setDefaultTimeZone();
  171. Setting::getInstance()->setDefaultControllerAction();
  172. Setting::getInstance()->setDefaultNamespace();
  173. Setting::getInstance()->setDefaultLanguage();
  174. return $this;
  175. }
  176. /**
  177. * 合并ini文件生成的数组
  178. * @param String $iniFile ini文件名
  179. * @param Array $array
  180. */
  181. public function mergeAppConfigure($iniFile, $array)
  182. {
  183. if (!is_array($array)) return;
  184. Register::mergeAppConfigure($iniFile, $array);
  185. return $this;
  186. }
  187. /**
  188. * 覆盖/添加ini文件的key对应的值
  189. * @param String $iniFile ini文件名
  190. * @param String $key
  191. * @param String $val
  192. */
  193. public function rewriteAppConfigure($iniFile, $key, $val)
  194. {
  195. Register::rewriteConfig($iniFile, $key, $val);
  196. return $this;
  197. }
  198. /**
  199. * 设置指定的前缀是否使用命名空间
  200. * @param string $prefix 前缀
  201. * @param bool $useNamespace 是否使用
  202. * @return $this
  203. */
  204. public function setUseNamespace($prefix, $useNamespace = true)
  205. {
  206. Psr4::getInstance()->setUseNamespace($prefix, $useNamespace);
  207. return $this;
  208. }
  209. /**
  210. * 添加命名空间对应的网站目录
  211. * @param string $prefix 前缀
  212. * @param string $baseDir 对应的路径
  213. * @param bool $prepend 是否追加
  214. * @return $this
  215. */
  216. public function addNamespace($prefix, $baseDir, $prepend = false)
  217. {
  218. if (!is_dir($baseDir)) {
  219. throw new \Qii\Exceptions\FolderDoesNotExist(\Qii::i(1009, $baseDir), __LINE__);
  220. }
  221. $baseDir = Psr4::getInstance()->realpath($baseDir);
  222. Psr4::getInstance()->addNamespace($prefix, $baseDir, $prepend);
  223. return $this;
  224. }
  225. /**
  226. * 设置启动前执行的方法
  227. *
  228. * @return $this
  229. * @throws Exception
  230. */
  231. public function setBootstrap()
  232. {
  233. Psr4::getInstance()->loadFileByClass('Bootstrap');
  234. if (!class_exists('Bootstrap', false)) throw new \Qii\Exceptions\ClassNotFound(\Qii::i(1405, 'Bootstrap'), __LINE__);;
  235. $bootstrap = Psr4::getInstance()->instance('Bootstrap');
  236. if (!$bootstrap instanceof \Qii\Base\Bootstrap) {
  237. throw new \Qii\Exceptions\ClassInstanceof(Qii::i(1107, 'Bootstrap', 'Qii\Bootstrap'), __LINE__);;
  238. }
  239. $refectionClass = new \ReflectionClass('Bootstrap');
  240. $methods = $refectionClass->getMethods();
  241. //自动执行以init开头的公共方法
  242. foreach ($methods as $method) {
  243. $name = $method->getName();
  244. if (substr($name, 0, 4) == 'init' && $method->isPublic()) $bootstrap->$name();
  245. }
  246. return $this;
  247. }
  248. /**
  249. * 设置写loger的类
  250. *
  251. * @param LogerWriter $logerCls 日志记录类
  252. */
  253. public function setLoger($logerCls)
  254. {
  255. /*
  256. if (!class_exists($logerCls, false)) {
  257. throw new \Qii_Exceptions_ClassNotFound(Qii::i(1405, $logerCls), __LINE__);
  258. }*/
  259. $this->logerWriter = Instance::instance(
  260. '\Qii\Loger\Instance',
  261. Instance::instance($logerCls)
  262. );
  263. return $this;
  264. }
  265. /**
  266. * 设置数据库使用的文件
  267. *
  268. * @param $iniFile
  269. * @throws \Qii_Exceptions_Overwrite
  270. */
  271. public function setDBIniFile($iniFile)
  272. {
  273. Register::set(Consts::APP_DB, $iniFile);
  274. }
  275. /**
  276. * 获取当前数据库文件
  277. *
  278. * @return \Qii_Mix
  279. * @throws \Qii_Exceptions_Variable
  280. */
  281. public function getDBIniFile()
  282. {
  283. return Register::get(Consts::APP_DB);
  284. }
  285. /**
  286. * 设置数据库配置文件
  287. * @param string $ini 配置文件路径
  288. * @param string $env 环境
  289. */
  290. public function setDB($ini, $env = '')
  291. {
  292. if ($env == '') $env = $this->getEnv();
  293. $this->setDBIniFile($ini);
  294. if (!Register::setAppConfigure(
  295. Psr4::getInstance()->getFileByPrefix($ini),
  296. $env
  297. )
  298. ) {
  299. throw new \Qii\Exceptions\FileNotFound(\Qii::i(1405, $ini), __LINE__);
  300. }
  301. return $this;
  302. }
  303. /**
  304. * 设置路由规则
  305. *
  306. * @param string $router 路由配置文件位置
  307. * @return $this
  308. */
  309. public function setRouter($router)
  310. {
  311. Register::set(
  312. Consts::APP_SITE_ROUTER,
  313. Import::includes(
  314. Psr4::realpath(Psr4::getInstance()->getFileByPrefix($router)
  315. )
  316. )
  317. );
  318. //载入rewrite规则后重写request
  319. $rewrite = Psr4::loadStatic(
  320. '\Qii\Router\Parse',
  321. 'get',
  322. \Qii\Request\Url::getPathInfo(),
  323. $this->request->controller,
  324. $this->request->action,
  325. $this->request->url->get(2)
  326. );
  327. $rewrite['controller'] = $rewrite['controller'] ? $rewrite['controller'] : $this->request->defaultController();
  328. $rewrite['action'] = $rewrite['action'] ? $rewrite['action'] : $this->request->defaultAction();
  329. //是否已经rewrite,如果和url中的不一样就是已经rewrite
  330. if ($this->request->controller != $rewrite['controller'] || $this->request->action != $rewrite['action']) {
  331. $this->request->setRouted(true);
  332. }
  333. $this->request->setControllerName($rewrite['controller']);
  334. $this->request->setActionName($rewrite['action']);
  335. return $this;
  336. }
  337. /**
  338. * sprintf 格式化语言错误信息内容
  339. *
  340. * Qii::e($message, $argv1, $argv2, ..., $line);
  341. * $message = sprintf($message, $argv1, $argv2, ...);
  342. * throw new \Qii\Exceptions\Error($message, $line);
  343. */
  344. public function showError()
  345. {
  346. return call_user_func_array(array('\Qii\Exceptions\Errors', 'e'), func_get_args());
  347. }
  348. public function run()
  349. {
  350. $this->helper->load(self::$workspace);
  351. $this->dispatcher = Psr4::getInstance()->loadClass('\Qii\Base\Dispatcher');
  352. if (!$this->dispatcher instanceof \Qii\Base\Dispatcher) {
  353. throw new \Exception('Dispatcher must instance of Qii\Base\Dispatcher', __LINE__);
  354. }
  355. //如果设置了host的话,看host对应的controller路径
  356. $hosts = $this->appConfigure('hosts');
  357. if (count($hosts) > 0) {
  358. foreach ($hosts AS $host) {
  359. if ($host['domain'] == $this->request->host) {
  360. Register::set(
  361. Consts::APP_DEFAULT_CONTROLLER_PREFIX,
  362. ($host['path'] ? $host['path'] : $host['domain'])
  363. );
  364. break;
  365. }
  366. }
  367. }
  368. $this->request->setDispatcher($this->dispatcher);
  369. //rewrite规则
  370. $this->dispatcher->setRequest($this->request);
  371. $this->dispatcher->dispatch();
  372. $this->request->setDispatched(true);
  373. return $this;
  374. }
  375. }