Application.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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. if (!class_exists($logerCls, false)) {
  256. throw new \Qii_Exceptions_ClassNotFound(Qii::i(1405, $logerCls), __LINE__);
  257. }
  258. $this->logerWriter = Instance::instance(
  259. '\Qii\Loger\Instance',
  260. Instance::instance($logerCls)
  261. );
  262. return $this;
  263. }
  264. /**
  265. * 设置数据库使用的文件
  266. *
  267. * @param $iniFile
  268. * @throws \Qii_Exceptions_Overwrite
  269. */
  270. public function setDBIniFile($iniFile)
  271. {
  272. Register::set(Consts::APP_DB, $iniFile);
  273. }
  274. /**
  275. * 获取当前数据库文件
  276. *
  277. * @return \Qii_Mix
  278. * @throws \Qii_Exceptions_Variable
  279. */
  280. public function getDBIniFile()
  281. {
  282. return Register::get(Consts::APP_DB);
  283. }
  284. /**
  285. * 设置数据库配置文件
  286. * @param string $ini 配置文件路径
  287. * @param string $env 环境
  288. */
  289. public function setDB($ini, $env = '')
  290. {
  291. if ($env == '') $env = $this->getEnv();
  292. $this->setDBIniFile($ini);
  293. if (!Register::setAppConfigure(
  294. Psr4::getInstance()->getFileByPrefix($ini),
  295. $env
  296. )
  297. ) {
  298. throw new \Qii\Exceptions\FileNotFound(\Qii::i(1405, $ini), __LINE__);
  299. }
  300. return $this;
  301. }
  302. /**
  303. * 设置路由规则
  304. *
  305. * @param string $router 路由配置文件位置
  306. * @return $this
  307. */
  308. public function setRouter($router)
  309. {
  310. Register::set(
  311. Consts::APP_SITE_ROUTER,
  312. Import::includes(
  313. Psr4::realpath(Psr4::getInstance()->getFileByPrefix($router)
  314. )
  315. )
  316. );
  317. //载入rewrite规则后重写request
  318. $rewrite = Psr4::loadStatic(
  319. '\Qii\Router\Parse',
  320. 'get',
  321. \Qii\Request\Url::getPathInfo(),
  322. $this->request->controller,
  323. $this->request->action,
  324. $this->request->url->get(2)
  325. );
  326. $rewrite['controller'] = $rewrite['controller'] ? $rewrite['controller'] : $this->request->defaultController();
  327. $rewrite['action'] = $rewrite['action'] ? $rewrite['action'] : $this->request->defaultAction();
  328. //是否已经rewrite,如果和url中的不一样就是已经rewrite
  329. if ($this->request->controller != $rewrite['controller'] || $this->request->action != $rewrite['action']) {
  330. $this->request->setRouted(true);
  331. }
  332. $this->request->setControllerName($rewrite['controller']);
  333. $this->request->setActionName($rewrite['action']);
  334. return $this;
  335. }
  336. /**
  337. * sprintf 格式化语言错误信息内容
  338. *
  339. * Qii::e($message, $argv1, $argv2, ..., $line);
  340. * $message = sprintf($message, $argv1, $argv2, ...);
  341. * throw new \Qii\Exceptions\Error($message, $line);
  342. */
  343. public function showError()
  344. {
  345. return call_user_func_array(array('\Qii\Exceptions\Errors', 'e'), func_get_args());
  346. }
  347. public function run()
  348. {
  349. $this->helper->load(self::$workspace);
  350. $this->dispatcher = Psr4::getInstance()->loadClass('\Qii\Base\Dispatcher');
  351. if (!$this->dispatcher instanceof \Qii\Base\Dispatcher) {
  352. throw new \Exception('Dispatcher must instance of Qii\Base\Dispatcher', __LINE__);
  353. }
  354. //如果设置了host的话,看host对应的controller路径
  355. $hosts = $this->appConfigure('hosts');
  356. if (count($hosts) > 0) {
  357. foreach ($hosts AS $host) {
  358. if ($host['domain'] == $this->request->host) {
  359. Register::set(
  360. Consts::APP_DEFAULT_CONTROLLER_PREFIX,
  361. ($host['path'] ? $host['path'] : $host['domain'])
  362. );
  363. break;
  364. }
  365. }
  366. }
  367. $this->request->setDispatcher($this->dispatcher);
  368. //rewrite规则
  369. $this->dispatcher->setRequest($this->request);
  370. $this->dispatcher->dispatch();
  371. $this->request->setDispatched(true);
  372. return $this;
  373. }
  374. }