Request.php 13 KB

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