Request.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  1. <?php
  2. namespace Qii\Base;
  3. use Qii\Config\Consts;
  4. use Qii\Config\Register;
  5. use Qii\Request\Url;
  6. abstract class Request
  7. {
  8. public $host = '';
  9. public $module;
  10. public $controller;
  11. public $action;
  12. public $method;
  13. public $dispatcher;
  14. protected $params;
  15. protected $language;
  16. protected $_exception;
  17. protected $_baseUri = '';
  18. protected $uri = '';
  19. protected $dispatched = false;
  20. protected $routed = false;
  21. protected $forward = false;
  22. /**
  23. * @var Qii\Request\Url url
  24. */
  25. public $url;
  26. /**
  27. * 初始化参数,获取链接中对应的参数并存放到$this->params中
  28. *
  29. */
  30. public function __construct()
  31. {
  32. foreach ($_GET AS $key => $val) {
  33. $this->setParam($key, $val);
  34. }
  35. $rewriteRule = \Qii::getInstance()->appConfigure(Consts::APP_SITE_METHOD);
  36. //如果获取配置文件失败,就是会用默认短链的形式
  37. if ($rewriteRule == null) $rewriteRule = "Short";
  38. $this->url = new Url($rewriteRule);
  39. $this->host = IS_CLI ? '' : $_SERVER['HTTP_HOST'];
  40. $params = (array)$this->url->getParams();
  41. if(count($params) > 0) $this->params = array_merge($this->params, $params);
  42. $routeInfo = (array)$this->url->getPathArgs();
  43. $controller = $this->defaultController();
  44. $action = $this->defaultAction();
  45. //cli 模式处理
  46. if(IS_CLI) {
  47. $routeInfo = $this->url->request->getCliPathArgs();
  48. }
  49. if(count($routeInfo) > 1)
  50. {
  51. $action = array_pop($routeInfo);
  52. $controller = join("\\", $routeInfo);
  53. }
  54. else if(count($routeInfo) == 1 && !empty($routeInfo[0])) {
  55. $controller = $routeInfo[0];
  56. } //处理url中的数据
  57. if (ucwords($rewriteRule) == 'Short') {
  58. $this->setControllerName($controller);
  59. $this->setActionName($action);
  60. }
  61. return $this;
  62. }
  63. /**
  64. * 获取POST数据
  65. */
  66. public function post($name = null, $default = null)
  67. {
  68. return call_user_func_array(array($this->url, 'post'), func_get_args());
  69. }
  70. /**
  71. * 默认controller
  72. */
  73. public function defaultController()
  74. {
  75. return Register::get(Consts::APP_DEFAULT_CONTROLLER, 'index');
  76. }
  77. /**
  78. * 默认action
  79. */
  80. public function defaultAction()
  81. {
  82. return Register::get(Consts::APP_DEFAULT_ACTION, 'index');
  83. }
  84. /**
  85. * 获取当前数据处理
  86. */
  87. public function uri()
  88. {
  89. return $this->uri;
  90. }
  91. public function redirect($url)
  92. {
  93. ob_clean();
  94. header('Location:' . $url);
  95. }
  96. /**
  97. * isGet
  98. *
  99. * @param void
  100. * @return boolean
  101. */
  102. public function isGet()
  103. {
  104. return (strtoupper($this->method) == 'GET');
  105. }
  106. /**
  107. * isPost
  108. *
  109. * @param void
  110. * @return boolean
  111. */
  112. public function isPost()
  113. {
  114. return (strtoupper($this->method) == 'POST');
  115. }
  116. /**
  117. * isPut
  118. *
  119. * @param void
  120. * @return boolean
  121. */
  122. public function isPut()
  123. {
  124. return (strtoupper($this->method) == 'PUT');
  125. }
  126. /**
  127. * isHead
  128. *
  129. * @param void
  130. * @return boolean
  131. */
  132. public function isHead()
  133. {
  134. return (strtoupper($this->method) == 'HEAD');
  135. }
  136. /**
  137. * isOptions
  138. *
  139. * @param void
  140. * @return boolean
  141. */
  142. public function isOptions()
  143. {
  144. return (strtoupper($this->method) == 'OPTIONS');
  145. }
  146. /**
  147. * isCli
  148. *
  149. * @param void
  150. * @return boolean
  151. */
  152. public function isCli()
  153. {
  154. return (strtoupper($this->method) == 'CLI');
  155. }
  156. /**
  157. * isXmlHttpRequest
  158. *
  159. * @param void
  160. * @return boolean
  161. */
  162. public function isXmlHttpRequest()
  163. {
  164. return false;
  165. }
  166. /**
  167. * getServer
  168. *
  169. * @param string $name
  170. * @param mixed $default
  171. * @return mixed
  172. */
  173. public function getServer($name = null, $default = null)
  174. {
  175. if (is_null($name)) {
  176. return $_SERVER;
  177. } elseif (isset($_SERVER[$name])) {
  178. return $_SERVER[$name];
  179. }
  180. return $default;
  181. }
  182. /**
  183. * getEnv
  184. *
  185. * @param string $name
  186. * @param mixed $default
  187. * @return mixed
  188. */
  189. public function getEnv($name = null, $default = null)
  190. {
  191. if (is_null($name)) {
  192. return $_ENV;
  193. } elseif (isset($_ENV[$name])) {
  194. return $_ENV[$name];
  195. }
  196. return $default;
  197. }
  198. /**
  199. * setParam
  200. *
  201. * @param mixed $name
  202. * @param mixed $value
  203. * @return boolean | Qii_Request_Abstract
  204. */
  205. public function setParam($name, $value = null)
  206. {
  207. if (is_null($value)) {
  208. if (is_array($name)) {
  209. $this->params = array_merge($this->params, $name);
  210. return $this;
  211. }
  212. } elseif (is_string($name)) {
  213. $this->params[$name] = $value;
  214. return $this;
  215. }
  216. return false;
  217. }
  218. /**
  219. * getParam
  220. *
  221. * @param string $name
  222. * @param mixed $default
  223. * @return mixed
  224. */
  225. public function getParam($name, $dafault = null)
  226. {
  227. if (isset($this->params[$name])) {
  228. return $this->params[$name];
  229. }
  230. return $dafault;
  231. }
  232. /**
  233. * setParams
  234. *
  235. * @param array
  236. * @return boolean | Qii_Request_Abstract
  237. */
  238. public function setParams($params)
  239. {
  240. if (is_array($params)) {
  241. $this->params = $params;
  242. return $this;
  243. }
  244. return false;
  245. }
  246. /**
  247. * getParams
  248. *
  249. * @param void
  250. * @return array
  251. */
  252. public function getParams()
  253. {
  254. return $this->params;
  255. }
  256. /**
  257. * setException
  258. *
  259. * @param Exception $exception
  260. * @return boolean | Qii_Request_Abstract
  261. */
  262. public function setException($exception)
  263. {
  264. if (is_object($exception)
  265. && ($exception instanceof Exception)
  266. ) {
  267. $this->_exception = $exception;
  268. return $this;
  269. }
  270. return false;
  271. }
  272. /**
  273. * getException
  274. *
  275. * @param void
  276. * @return Exception
  277. */
  278. public function getException()
  279. {
  280. if (is_object($this->_exception)
  281. && ($this->_exception instanceof Exception)
  282. ) {
  283. return $this->_exception;
  284. }
  285. return null;
  286. }
  287. /**
  288. * getModuleName
  289. *
  290. * @param void
  291. * @return string
  292. */
  293. public function getModuleName()
  294. {
  295. return $this->module;
  296. }
  297. /**
  298. * getControllerName
  299. *
  300. * @param void
  301. * @return string
  302. */
  303. public function getControllerName()
  304. {
  305. return $this->controller;
  306. }
  307. /**
  308. * getActionName
  309. *
  310. * @param void
  311. * @return string
  312. */
  313. public function getActionName()
  314. {
  315. return $this->action;
  316. }
  317. /**
  318. * setModuleName
  319. *
  320. * @param string $name
  321. * @return boolean | Qii_Request_Abstract
  322. */
  323. public function setModuleName($name)
  324. {
  325. if (!is_string($name)) {
  326. trigger_error('Expect a string module name', E_USER_WARNING);
  327. return false;
  328. }
  329. $this->module = $name;
  330. return $this;
  331. }
  332. /**
  333. * setControllerName
  334. *
  335. * @param string $name
  336. * @return boolean | Qii_Request_Abstract
  337. */
  338. public function setControllerName($name)
  339. {
  340. if (!is_string($name)) {
  341. trigger_error('Expect a string controller name', E_USER_WARNING);
  342. return $this;
  343. }
  344. $this->controller = $name;
  345. return $this;
  346. }
  347. /**
  348. * setActionName
  349. *
  350. * @param string $name
  351. * @return boolean | Qii_Request_Abstract
  352. */
  353. public function setActionName($name)
  354. {
  355. if (!is_string($name)) {
  356. trigger_error('Expect a string action name', E_USER_WARNING);
  357. return false;
  358. }
  359. $this->action = $name;
  360. return $this;
  361. }
  362. /**
  363. * getMethod
  364. *
  365. * @param void
  366. * @return string
  367. */
  368. public function getMethod()
  369. {
  370. return $this->method;
  371. }
  372. /**
  373. * getLanguage
  374. *
  375. * @param void
  376. * @return string
  377. */
  378. public function getLanguage()
  379. {
  380. return $this->language;
  381. }
  382. /**
  383. * setBaseUri
  384. *
  385. * @param string $baseUri
  386. * @return boolean | Qii_Request_Abstract
  387. */
  388. public function setBaseUri($baseUri)
  389. {
  390. if ($baseUri && is_string($baseUri)) {
  391. $this->_baseUri = $baseUri;
  392. return $this;
  393. }
  394. return false;
  395. }
  396. /**
  397. * getBaseUri
  398. *
  399. * @param void
  400. * @return string
  401. */
  402. public function getBaseUri()
  403. {
  404. return $this->_baseUri;
  405. }
  406. /**
  407. * setRequestUri
  408. *
  409. * @param string $uri
  410. * @return boolean | Qii_Request_Abstract
  411. */
  412. public function setRequestUri($uri)
  413. {
  414. if (is_string($uri)) {
  415. $this->uri = $uri;
  416. return $this;
  417. }
  418. return false;
  419. }
  420. /**
  421. * getRequestUri
  422. *
  423. * @param void
  424. * @return string
  425. */
  426. public function getRequestUri()
  427. {
  428. return $this->uri;
  429. }
  430. /**
  431. * isDispatched
  432. *
  433. * @param void
  434. * @return boolean
  435. */
  436. public function isDispatched()
  437. {
  438. return (boolean)$this->dispatched;
  439. }
  440. /**
  441. * setDispatched
  442. *
  443. * @param boolean $flag
  444. * @return boolean | Qii_Request_Abstract
  445. */
  446. public function setDispatched($flag = true)
  447. {
  448. if (is_bool($flag)) {
  449. $this->dispatched = $flag;
  450. return $this;
  451. }
  452. return false;
  453. }
  454. /**
  455. * 设置dispatcher
  456. *
  457. * @param \Qii\Controller\Dispatcher $dispatcher
  458. */
  459. public function setDispatcher(\Qii\Base\Dispatcher $dispatcher)
  460. {
  461. $this->dispatcher = $dispatcher;
  462. }
  463. /**
  464. * isRouted
  465. *
  466. * @param void
  467. * @return boolean
  468. */
  469. public function isRouted()
  470. {
  471. return $this->routed;
  472. }
  473. /**
  474. * setRouted
  475. *
  476. * @param boolean $flag
  477. * @return boolean | \Qii\Request\Abstract
  478. */
  479. public function setRouted($flag = true)
  480. {
  481. if (is_bool($flag)) {
  482. $this->routed = $flag;
  483. return $this;
  484. }
  485. return false;
  486. }
  487. /**
  488. * 是否需要转发
  489. */
  490. public function isForward()
  491. {
  492. return $this->forward;
  493. }
  494. /**
  495. * 设置是否需要转发
  496. * @param bool $flag
  497. * @return $this
  498. */
  499. public function setForward($flag = true)
  500. {
  501. if (is_bool($flag)) {
  502. $this->forward = $flag;
  503. return $this;
  504. }
  505. return $this;
  506. }
  507. /**
  508. * __setbaseUri
  509. *
  510. * @param string $baseUri
  511. * @param string $requestUri
  512. * @return boolean
  513. */
  514. protected function _setBaseUri($baseUri, $requestUri = null)
  515. {
  516. if ($baseUri && is_string($baseUri)) {
  517. $this->_baseUri = $baseUri;
  518. return true;
  519. } elseif ($requestUri && is_string($requestUri)) {
  520. $scriptFileName = $this->getServer('SCRIPT_FILENAME');
  521. do {
  522. if ($scriptFileName && is_string($scriptFileName)) {
  523. $fileName = basename($scriptFileName, \Qii::getInstance()->appConfigure('ext', '.php'));
  524. $fileNameLen = strlen($fileName);
  525. $scriptName = $this->getServer('SCRIPT_NAME');
  526. if ($scriptName && is_string($scriptName)) {
  527. $script = basename($scriptName);
  528. if (strncmp($fileName, $script, $fileNameLen) == 0) {
  529. $basename = $scriptName;
  530. break;
  531. }
  532. }
  533. $phpSelfName = $this->getServer('PHP_SELF');
  534. if ($phpSelfName && is_string($phpSelfName)) {
  535. $phpSelf = basename($phpSelfName);
  536. if (strncmp($fileName, $phpSelf, $fileNameLen) == 0) {
  537. $basename = $phpSelfName;
  538. break;
  539. }
  540. }
  541. $orig_name = $this->getServer('ORIG_SCRIPT_NAME');
  542. if ($orig_name && is_string($orig_name)) {
  543. $orig = basename($orig_name);
  544. if (strncmp($fileName, $orig, $fileNameLen) == 0) {
  545. $basename = $orig_name;
  546. break;
  547. }
  548. }
  549. }
  550. } while (0);
  551. if ($basename && strstr($requestUri, $basename) == $requestUri) {
  552. $this->_baseUri = rtrim($basename, '/');
  553. return true;
  554. } elseif ($basename) {
  555. $dirname = rtrim(dirname($basename), '/');
  556. if ($dirname) {
  557. if (strstr($requestUri, $dirname) == $requestUri) {
  558. $this->_baseUri = $dirname;
  559. return true;
  560. }
  561. }
  562. }
  563. $this->_baseUri = '';
  564. return true;
  565. }
  566. return false;
  567. }
  568. /**
  569. * 获取uri的extension
  570. *
  571. * @return array|string|string[]
  572. */
  573. public function getExtension() {
  574. $extension = pathinfo($this->uri, PATHINFO_EXTENSION);
  575. return $extension?? "html";
  576. }
  577. /**
  578. * 对于不存在的方法默认调用url中的方法
  579. */
  580. public function __call($method, $args)
  581. {
  582. return call_user_func_array(array($this->url, $method), $args);
  583. }
  584. }