/myCore/lib/laravel/illuminate/http/Illuminate/Http/Request.php

https://gitlab.com/fabian.morales/marlon_becerra · PHP · 565 lines · 234 code · 77 blank · 254 comment · 20 complexity · 683d46764f4bbc9fffe9a2f86e745d6e MD5 · raw file

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