Request.php 15 KB

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