PageRenderTime 38ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/laravel/framework/src/Illuminate/Http/Request.php

https://bitbucket.org/helfreire/tccsite
PHP | 505 lines | 211 code | 66 blank | 228 comment | 16 complexity | 8d277612a665298496388e1330558bcb MD5 | raw file
  1. <?php namespace Illuminate\Http;
  2. use Illuminate\Session\Store as SessionStore;
  3. use Symfony\Component\HttpFoundation\ParameterBag;
  4. use Symfony\Component\HttpFoundation\Request as SymfonyRequest;
  5. class Request extends SymfonyRequest {
  6. /**
  7. * The decoded JSON content for the request.
  8. *
  9. * @var string
  10. */
  11. protected $json;
  12. /**
  13. * The Illuminate session store implementation.
  14. *
  15. * @var \Illuminate\Session\Store
  16. */
  17. protected $sessionStore;
  18. /**
  19. * Return the Request instance.
  20. *
  21. * @return \Illuminate\Http\Request
  22. */
  23. public function instance()
  24. {
  25. return $this;
  26. }
  27. /**
  28. * Get the root URL for the application.
  29. *
  30. * @return string
  31. */
  32. public function root()
  33. {
  34. return rtrim($this->getSchemeAndHttpHost().$this->getBaseUrl(), '/');
  35. }
  36. /**
  37. * Get the URL (no query string) for the request.
  38. *
  39. * @return string
  40. */
  41. public function url()
  42. {
  43. return rtrim(preg_replace('/\?.*/', '', $this->getUri()), '/');
  44. }
  45. /**
  46. * Get the full URL for the request.
  47. *
  48. * @return string
  49. */
  50. public function fullUrl()
  51. {
  52. $query = $this->getQueryString();
  53. return $query ? $this->url().'?'.$query : $this->url();
  54. }
  55. /**
  56. * Get the current path info for the request.
  57. *
  58. * @return string
  59. */
  60. public function path()
  61. {
  62. $pattern = trim($this->getPathInfo(), '/');
  63. return $pattern == '' ? '/' : $pattern;
  64. }
  65. /**
  66. * Get a segment from the URI (1 based index).
  67. *
  68. * @param string $index
  69. * @param mixed $default
  70. * @return string
  71. */
  72. public function segment($index, $default = null)
  73. {
  74. $segments = explode('/', trim($this->getPathInfo(), '/'));
  75. $segments = array_filter($segments, function($v) { return $v != ''; });
  76. return array_get($segments, $index - 1, $default);
  77. }
  78. /**
  79. * Get all of the segments for the request path.
  80. *
  81. * @return array
  82. */
  83. public function segments()
  84. {
  85. $path = $this->path();
  86. return $path == '/' ? array() : explode('/', $path);
  87. }
  88. /**
  89. * Determine if the current request URI matches a pattern.
  90. *
  91. * @param string $pattern
  92. * @return bool
  93. */
  94. public function is($pattern)
  95. {
  96. foreach (func_get_args() as $pattern)
  97. {
  98. if (str_is($pattern, $this->path()))
  99. {
  100. return true;
  101. }
  102. }
  103. return false;
  104. }
  105. /**
  106. * Determine if the request is the result of an AJAX call.
  107. *
  108. * @return bool
  109. */
  110. public function ajax()
  111. {
  112. return $this->isXmlHttpRequest();
  113. }
  114. /**
  115. * Determine if the request is over HTTPS.
  116. *
  117. * @return bool
  118. */
  119. public function secure()
  120. {
  121. return $this->isSecure();
  122. }
  123. /**
  124. * Determine if the request contains a given input item.
  125. *
  126. * @param string|array $key
  127. * @return bool
  128. */
  129. public function has($key)
  130. {
  131. if (count(func_get_args()) > 1)
  132. {
  133. foreach (func_get_args() as $value)
  134. {
  135. if ( ! $this->has($value)) return false;
  136. }
  137. return true;
  138. }
  139. if (is_bool($this->input($key)) or is_array($this->input($key)))
  140. {
  141. return true;
  142. }
  143. return trim((string) $this->input($key)) !== '';
  144. }
  145. /**
  146. * Get all of the input and files for the request.
  147. *
  148. * @return array
  149. */
  150. public function all()
  151. {
  152. return $this->input() + $this->files->all();
  153. }
  154. /**
  155. * Retrieve an input item from the request.
  156. *
  157. * @param string $key
  158. * @param mixed $default
  159. * @return string
  160. */
  161. public function input($key = null, $default = null)
  162. {
  163. $input = $this->getInputSource()->all() + $this->query->all();
  164. return array_get($input, $key, $default);
  165. }
  166. /**
  167. * Get a subset of the items from the input data.
  168. *
  169. * @param array $keys
  170. * @return array
  171. */
  172. public function only($keys)
  173. {
  174. $keys = is_array($keys) ? $keys : func_get_args();
  175. return array_only($this->input(), $keys) + array_fill_keys($keys, null);
  176. }
  177. /**
  178. * Get all of the input except for a specified array of items.
  179. *
  180. * @param array $keys
  181. * @return array
  182. */
  183. public function except($keys)
  184. {
  185. $keys = is_array($keys) ? $keys : func_get_args();
  186. $results = $this->input();
  187. foreach ($keys as $key) array_forget($results, $key);
  188. return $results;
  189. }
  190. /**
  191. * Retrieve a query string item from the request.
  192. *
  193. * @param string $key
  194. * @param mixed $default
  195. * @return string
  196. */
  197. public function query($key = null, $default = null)
  198. {
  199. return $this->retrieveItem('query', $key, $default);
  200. }
  201. /**
  202. * Retrieve a cookie from the request.
  203. *
  204. * @param string $key
  205. * @param mixed $default
  206. * @return string
  207. */
  208. public function cookie($key = null, $default = null)
  209. {
  210. return $this->retrieveItem('cookies', $key, $default);
  211. }
  212. /**
  213. * Retrieve a file from the request.
  214. *
  215. * @param string $key
  216. * @param mixed $default
  217. * @return \Symfony\Component\HttpFoundation\File\UploadedFile|array
  218. */
  219. public function file($key = null, $default = null)
  220. {
  221. return array_get($this->files->all(), $key, $default);
  222. }
  223. /**
  224. * Determine if the uploaded data contains a file.
  225. *
  226. * @param string $key
  227. * @return bool
  228. */
  229. public function hasFile($key)
  230. {
  231. if (is_array($file = $this->file($key))) $file = head($file);
  232. return $file instanceof \SplFileInfo;
  233. }
  234. /**
  235. * Retrieve a header from the request.
  236. *
  237. * @param string $key
  238. * @param mixed $default
  239. * @return string
  240. */
  241. public function header($key = null, $default = null)
  242. {
  243. return $this->retrieveItem('headers', $key, $default);
  244. }
  245. /**
  246. * Retrieve a server variable from the request.
  247. *
  248. * @param string $key
  249. * @param mixed $default
  250. * @return string
  251. */
  252. public function server($key = null, $default = null)
  253. {
  254. return $this->retrieveItem('server', $key, $default);
  255. }
  256. /**
  257. * Retrieve an old input item.
  258. *
  259. * @param string $key
  260. * @param mixed $default
  261. * @return mixed
  262. */
  263. public function old($key = null, $default = null)
  264. {
  265. return $this->getSessionStore()->getOldInput($key, $default);
  266. }
  267. /**
  268. * Flash the input for the current request to the session.
  269. *
  270. * @param string $filter
  271. * @param array $keys
  272. * @return void
  273. */
  274. public function flash($filter = null, $keys = array())
  275. {
  276. $flash = ( ! is_null($filter)) ? $this->$filter($keys) : $this->input();
  277. $this->getSessionStore()->flashInput($flash);
  278. }
  279. /**
  280. * Flash only some of the input to the session.
  281. *
  282. * @param dynamic string
  283. * @return void
  284. */
  285. public function flashOnly($keys)
  286. {
  287. $keys = is_array($keys) ? $keys : func_get_args();
  288. return $this->flash('only', $keys);
  289. }
  290. /**
  291. * Flash only some of the input to the session.
  292. *
  293. * @param dynamic string
  294. * @return void
  295. */
  296. public function flashExcept($keys)
  297. {
  298. $keys = is_array($keys) ? $keys : func_get_args();
  299. return $this->flash('except', $keys);
  300. }
  301. /**
  302. * Flush all of the old input from the session.
  303. *
  304. * @return void
  305. */
  306. public function flush()
  307. {
  308. $this->getSessionStore()->flashInput(array());
  309. }
  310. /**
  311. * Retrieve a parameter item from a given source.
  312. *
  313. * @param string $source
  314. * @param string $key
  315. * @param mixed $default
  316. * @return string
  317. */
  318. protected function retrieveItem($source, $key, $default)
  319. {
  320. if (is_null($key))
  321. {
  322. return $this->$source->all();
  323. }
  324. else
  325. {
  326. return $this->$source->get($key, $default, true);
  327. }
  328. }
  329. /**
  330. * Merge new input into the current request's input array.
  331. *
  332. * @param array $input
  333. * @return void
  334. */
  335. public function merge(array $input)
  336. {
  337. $this->getInputSource()->add($input);
  338. }
  339. /**
  340. * Replace the input for the current request.
  341. *
  342. * @param array $input
  343. * @return void
  344. */
  345. public function replace(array $input)
  346. {
  347. $this->getInputSource()->replace($input);
  348. }
  349. /**
  350. * Get the JSON payload for the request.
  351. *
  352. * @param string $key
  353. * @param mixed $default
  354. * @return mixed
  355. */
  356. public function json($key = null, $default = null)
  357. {
  358. if ( ! isset($this->json))
  359. {
  360. $this->json = new ParameterBag((array) json_decode($this->getContent(), true));
  361. }
  362. if (is_null($key)) return $this->json;
  363. return array_get($this->json->all(), $key, $default);
  364. }
  365. /**
  366. * Get the input source for the request.
  367. *
  368. * @return \Symfony\Component\HttpFoundation\ParameterBag
  369. */
  370. protected function getInputSource()
  371. {
  372. if ($this->isJson()) return $this->json();
  373. return $this->getMethod() == 'GET' ? $this->query : $this->request;
  374. }
  375. /**
  376. * Determine if the request is sending JSON.
  377. *
  378. * @return bool
  379. */
  380. public function isJson()
  381. {
  382. return str_contains($this->header('CONTENT_TYPE'), '/json');
  383. }
  384. /**
  385. * Determine if the current request is asking for JSON in return.
  386. *
  387. * @return bool
  388. */
  389. public function wantsJson()
  390. {
  391. $acceptable = $this->getAcceptableContentTypes();
  392. return isset($acceptable[0]) and $acceptable[0] == 'application/json';
  393. }
  394. /**
  395. * Get the data format expected in the response.
  396. *
  397. * @return string
  398. */
  399. public function format($default = 'html')
  400. {
  401. foreach ($this->getAcceptableContentTypes() as $type)
  402. {
  403. if ($format = $this->getFormat($type)) return $format;
  404. }
  405. return $default;
  406. }
  407. /**
  408. * Get the Illuminate session store implementation.
  409. *
  410. * @return \Illuminate\Session\Store
  411. */
  412. public function getSessionStore()
  413. {
  414. if ( ! isset($this->sessionStore))
  415. {
  416. throw new \RuntimeException("Session store not set on request.");
  417. }
  418. return $this->sessionStore;
  419. }
  420. /**
  421. * Set the Illuminate session store implementation.
  422. *
  423. * @param \Illuminate\Session\Store $session
  424. * @return void
  425. */
  426. public function setSessionStore(SessionStore $session)
  427. {
  428. $this->sessionStore = $session;
  429. }
  430. /**
  431. * Determine if the session store has been set.
  432. *
  433. * @return bool
  434. */
  435. public function hasSessionStore()
  436. {
  437. return isset($this->sessionStore);
  438. }
  439. }