PageRenderTime 24ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/framework/Request/Request.php

https://gitlab.com/igorbabko/mindk
PHP | 306 lines | 136 code | 34 blank | 136 comment | 5 complexity | 8c1cc5d3af319716c2ab8eb1c0febdbc MD5 | raw file
  1. <?php
  2. /**
  3. * File /framework/request/Request.php contains Request class is used
  4. * to manipulated with http request easily.
  5. *
  6. * PHP version 5
  7. *
  8. * @package Framework\Request
  9. * @author Igor Babko <i.i.babko@gmail.com>
  10. */
  11. namespace Framework\Request;
  12. /**
  13. * Class Request - representation of http request.
  14. * Default implementation of {@link RequestInterface}.
  15. *
  16. * Class contains all needed information of http request such as cookie, session variables
  17. * http method, request headers, http uri, GET data, POST data, SERVER data etc.
  18. *
  19. * @package Framework\Request
  20. * @author Igor Babko <i.i.babko@gmail.com>
  21. */
  22. class Request implements RequestInterface
  23. {
  24. /**
  25. * @static
  26. * @var \Framework\Request\Request|null Request instance
  27. */
  28. private static $_instance = null;
  29. /**
  30. * @var \Framework\Cookie\Cookie $_cookie cookie object
  31. */
  32. private $_cookie;
  33. /**
  34. * @var \Framework\Session\Session $_session Session object
  35. */
  36. private $_session;
  37. /**
  38. * @var string $_uri request uri
  39. */
  40. private $_uri;
  41. /**
  42. * @var string $_method request method
  43. */
  44. private $_method;
  45. /**
  46. * @var array $_headers Array of request headers
  47. */
  48. private $_headers = array();
  49. /**
  50. * {@inheritdoc}
  51. */
  52. public function getRequestHeaders()
  53. {
  54. $out = array();
  55. foreach ($_SERVER as $key => $value) {
  56. if (substr($key, 0, 5) == "HTTP_") {
  57. $key = str_replace(" ", "-", ucwords(strtolower(str_replace("_", " ", substr($key, 5)))));
  58. $out[$key] = $value;
  59. } else {
  60. $out[$key] = $value;
  61. }
  62. }
  63. return $out;
  64. }
  65. /**
  66. * Request constructor.
  67. *
  68. * Request constructor:
  69. * - takes Session object and Cookie object as parameters;
  70. * - defines request method and request uri.
  71. *
  72. * @param \Framework\Session\Session|null $session Session object.
  73. * @param \Framework\Cookie\Cookie|null $cookie Cookie object.
  74. *
  75. * @return \Framework\Request\Request Request object.
  76. */
  77. private function __construct($session = null, $cookie = null)
  78. {
  79. $this->_headers = $this->getRequestHeaders();
  80. $this->_method = $_SERVER['REQUEST_METHOD'];
  81. $this->_uri = $_SERVER['REQUEST_URI'];
  82. $this->_session = $session;
  83. $this->_cookie = $cookie;
  84. }
  85. /**
  86. * Method to clone objects of its class.
  87. *
  88. * @return \Framework\Request\Request Request instance.
  89. */
  90. private function __clone()
  91. {
  92. }
  93. /**
  94. * Method returns Request instance creating it if it's not been instantiated before
  95. * otherwise existed Request object will be returned.
  96. *
  97. * @param \Framework\Session\Session|null $session Session object.
  98. * @param \Framework\Cookie\Cookie|null $cookie Cookie object.
  99. *
  100. * @return \Framework\Request\Request Request instance.
  101. */
  102. public static function getInstance($session = null, $cookie = null)
  103. {
  104. if (null === self::$_instance) {
  105. self::$_instance = new self($session, $cookie);
  106. }
  107. return self::$_instance;
  108. }
  109. /**
  110. * {@inheritdoc}
  111. */
  112. public function getURI()
  113. {
  114. return $this->_uri;
  115. }
  116. /**
  117. * {@inheritdoc}
  118. */
  119. public function getHeaders()
  120. {
  121. return $this->_headers;
  122. }
  123. /**
  124. * {@inheritdoc}
  125. */
  126. public function getHeader($name)
  127. {
  128. return $this->_headers[$name];
  129. }
  130. /**
  131. * {@inheritdoc}
  132. */
  133. public function getRequestMethod()
  134. {
  135. return $this->_method;
  136. }
  137. /**
  138. * {@inheritdoc}
  139. */
  140. public function getCookie()
  141. {
  142. return $this->_cookie;
  143. }
  144. /**
  145. * {@inheritdoc}
  146. */
  147. public function getSession()
  148. {
  149. return $this->_session;
  150. }
  151. /**
  152. * {@inheritdoc}
  153. */
  154. public function getEnv($name)
  155. {
  156. return isset($_ENV[$name])?$_ENV[$name]:null;
  157. }
  158. /**
  159. * {@inheritdoc}
  160. */
  161. public function get($name)
  162. {
  163. return isset($_REQUEST[$name])?$_REQUEST[$name]:null;
  164. }
  165. /**
  166. * {@inheritdoc}
  167. */
  168. public function getPost($name)
  169. {
  170. return isset($_POST[$name])?$_POST[$name]:null;
  171. }
  172. /**
  173. * {@inheritdoc}
  174. */
  175. public function getQuery($name)
  176. {
  177. return isset($_GET[$name])?$_GET[$name]:null;
  178. }
  179. /**
  180. * {@inheritdoc}
  181. */
  182. public function getServer($name)
  183. {
  184. return isset($_SERVER[$name])?$_SERVER[$name]:null;
  185. }
  186. /**
  187. * {@inheritdoc}
  188. */
  189. public function has($name)
  190. {
  191. return isset($_REQUEST[$name]);
  192. }
  193. /**
  194. * {@inheritdoc}
  195. */
  196. public function hasPost($name)
  197. {
  198. return isset($_POST[$name]);
  199. }
  200. /**
  201. * {@inheritdoc}
  202. */
  203. public function hasQuery($name)
  204. {
  205. return isset($_GET[$name]);
  206. }
  207. /**
  208. * {@inheritdoc}
  209. */
  210. public function hasServer($name)
  211. {
  212. return isset($_SERVER[$name]);
  213. }
  214. /**
  215. * {@inheritdoc}
  216. */
  217. public function getScheme()
  218. {
  219. $scheme = parse_url($_SERVER['REQUEST_URI'])['scheme'];
  220. return $scheme;
  221. }
  222. /**
  223. * {@inheritdoc}
  224. */
  225. public function isAjax()
  226. {
  227. return $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest';
  228. }
  229. /**
  230. * {@inheritdoc}
  231. */
  232. public function getServerAddress()
  233. {
  234. return $_SERVER['SERVER_ADDR'];
  235. }
  236. /**
  237. * {@inheritdoc}
  238. */
  239. public function getServerName()
  240. {
  241. return gethostname();
  242. }
  243. /**
  244. * {@inheritdoc}
  245. */
  246. public function getHttpHost()
  247. {
  248. $parsed_url = parse_url($_SERVER('REQUEST_URI'));
  249. return $parsed_url['scheme'].$parsed_url['host'].$parsed_url['port'];
  250. }
  251. /**
  252. * {@inheritdoc}
  253. */
  254. public function getClientAddress()
  255. {
  256. return $_SERVER['REMOTE_ADDR'];
  257. }
  258. /**
  259. * {@inheritdoc}
  260. */
  261. public function getUserAgent()
  262. {
  263. return $_SERVER['HTTP_USER_AGENT'];
  264. }
  265. /**
  266. * {@inheritdoc}
  267. */
  268. public function isMethod($name)
  269. {
  270. return $_SERVER['REQUEST_METHOD'] === strtoupper($name);
  271. }
  272. }