PageRenderTime 27ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://gitlab.com/kimting254/wbms
PHP | 793 lines | 331 code | 106 blank | 356 comment | 25 complexity | dbef542e83d804f9b246270879705bca MD5 | raw file
  1. <?php namespace Illuminate\Http;
  2. use Closure;
  3. use ArrayAccess;
  4. use SplFileInfo;
  5. use RuntimeException;
  6. use Symfony\Component\HttpFoundation\ParameterBag;
  7. use Symfony\Component\HttpFoundation\Request as SymfonyRequest;
  8. class Request extends SymfonyRequest implements ArrayAccess {
  9. /**
  10. * The decoded JSON content for the request.
  11. *
  12. * @var string
  13. */
  14. protected $json;
  15. /**
  16. * The Illuminate session store implementation.
  17. *
  18. * @var \Illuminate\Session\Store
  19. */
  20. protected $sessionStore;
  21. /**
  22. * The user resolver callback.
  23. *
  24. * @var \Closure
  25. */
  26. protected $userResolver;
  27. /**
  28. * The route resolver callback.
  29. *
  30. * @var \Closure
  31. */
  32. protected $routeResolver;
  33. /**
  34. * Create a new Illuminate HTTP request from server variables.
  35. *
  36. * @return static
  37. */
  38. public static function capture()
  39. {
  40. static::enableHttpMethodParameterOverride();
  41. return static::createFromBase(SymfonyRequest::createFromGlobals());
  42. }
  43. /**
  44. * Return the Request instance.
  45. *
  46. * @return $this
  47. */
  48. public function instance()
  49. {
  50. return $this;
  51. }
  52. /**
  53. * Get the request method.
  54. *
  55. * @return string
  56. */
  57. public function method()
  58. {
  59. return $this->getMethod();
  60. }
  61. /**
  62. * Get the root URL for the application.
  63. *
  64. * @return string
  65. */
  66. public function root()
  67. {
  68. return rtrim($this->getSchemeAndHttpHost().$this->getBaseUrl(), '/');
  69. }
  70. /**
  71. * Get the URL (no query string) for the request.
  72. *
  73. * @return string
  74. */
  75. public function url()
  76. {
  77. return rtrim(preg_replace('/\?.*/', '', $this->getUri()), '/');
  78. }
  79. /**
  80. * Get the full URL for the request.
  81. *
  82. * @return string
  83. */
  84. public function fullUrl()
  85. {
  86. $query = $this->getQueryString();
  87. return $query ? $this->url().'?'.$query : $this->url();
  88. }
  89. /**
  90. * Get the current path info for the request.
  91. *
  92. * @return string
  93. */
  94. public function path()
  95. {
  96. $pattern = trim($this->getPathInfo(), '/');
  97. return $pattern == '' ? '/' : $pattern;
  98. }
  99. /**
  100. * Get the current encoded path info for the request.
  101. *
  102. * @return string
  103. */
  104. public function decodedPath()
  105. {
  106. return rawurldecode($this->path());
  107. }
  108. /**
  109. * Get a segment from the URI (1 based index).
  110. *
  111. * @param int $index
  112. * @param mixed $default
  113. * @return string
  114. */
  115. public function segment($index, $default = null)
  116. {
  117. return array_get($this->segments(), $index - 1, $default);
  118. }
  119. /**
  120. * Get all of the segments for the request path.
  121. *
  122. * @return array
  123. */
  124. public function segments()
  125. {
  126. $segments = explode('/', $this->path());
  127. return array_values(array_filter($segments, function($v) { return $v != ''; }));
  128. }
  129. /**
  130. * Determine if the current request URI matches a pattern.
  131. *
  132. * @param mixed string
  133. * @return bool
  134. */
  135. public function is()
  136. {
  137. foreach (func_get_args() as $pattern)
  138. {
  139. if (str_is($pattern, urldecode($this->path())))
  140. {
  141. return true;
  142. }
  143. }
  144. return false;
  145. }
  146. /**
  147. * Determine if the request is the result of an AJAX call.
  148. *
  149. * @return bool
  150. */
  151. public function ajax()
  152. {
  153. return $this->isXmlHttpRequest();
  154. }
  155. /**
  156. * Determine if the request is the result of an PJAX call.
  157. *
  158. * @return bool
  159. */
  160. public function pjax()
  161. {
  162. return $this->headers->get('X-PJAX') == true;
  163. }
  164. /**
  165. * Determine if the request is over HTTPS.
  166. *
  167. * @return bool
  168. */
  169. public function secure()
  170. {
  171. return $this->isSecure();
  172. }
  173. /**
  174. * Returns the client IP address.
  175. *
  176. * @return string
  177. */
  178. public function ip()
  179. {
  180. return $this->getClientIp();
  181. }
  182. /**
  183. * Returns the client IP addresses.
  184. *
  185. * @return array
  186. */
  187. public function ips()
  188. {
  189. return $this->getClientIps();
  190. }
  191. /**
  192. * Determine if the request contains a given input item key.
  193. *
  194. * @param string|array $key
  195. * @return bool
  196. */
  197. public function exists($key)
  198. {
  199. $keys = is_array($key) ? $key : func_get_args();
  200. $input = $this->all();
  201. foreach ($keys as $value)
  202. {
  203. if ( ! array_key_exists($value, $input)) return false;
  204. }
  205. return true;
  206. }
  207. /**
  208. * Determine if the request contains a non-empty value for an input item.
  209. *
  210. * @param string|array $key
  211. * @return bool
  212. */
  213. public function has($key)
  214. {
  215. $keys = is_array($key) ? $key : func_get_args();
  216. foreach ($keys as $value)
  217. {
  218. if ($this->isEmptyString($value)) return false;
  219. }
  220. return true;
  221. }
  222. /**
  223. * Determine if the given input key is an empty string for "has".
  224. *
  225. * @param string $key
  226. * @return bool
  227. */
  228. protected function isEmptyString($key)
  229. {
  230. $boolOrArray = is_bool($this->input($key)) || is_array($this->input($key));
  231. return ! $boolOrArray && trim((string) $this->input($key)) === '';
  232. }
  233. /**
  234. * Get all of the input and files for the request.
  235. *
  236. * @return array
  237. */
  238. public function all()
  239. {
  240. return array_replace_recursive($this->input(), $this->files->all());
  241. }
  242. /**
  243. * Retrieve an input item from the request.
  244. *
  245. * @param string $key
  246. * @param mixed $default
  247. * @return string|array
  248. */
  249. public function input($key = null, $default = null)
  250. {
  251. $input = $this->getInputSource()->all() + $this->query->all();
  252. return array_get($input, $key, $default);
  253. }
  254. /**
  255. * Get a subset of the items from the input data.
  256. *
  257. * @param array $keys
  258. * @return array
  259. */
  260. public function only($keys)
  261. {
  262. $keys = is_array($keys) ? $keys : func_get_args();
  263. $results = [];
  264. $input = $this->all();
  265. foreach ($keys as $key)
  266. {
  267. array_set($results, $key, array_get($input, $key));
  268. }
  269. return $results;
  270. }
  271. /**
  272. * Get all of the input except for a specified array of items.
  273. *
  274. * @param array $keys
  275. * @return array
  276. */
  277. public function except($keys)
  278. {
  279. $keys = is_array($keys) ? $keys : func_get_args();
  280. $results = $this->all();
  281. array_forget($results, $keys);
  282. return $results;
  283. }
  284. /**
  285. * Retrieve a query string item from the request.
  286. *
  287. * @param string $key
  288. * @param mixed $default
  289. * @return string|array
  290. */
  291. public function query($key = null, $default = null)
  292. {
  293. return $this->retrieveItem('query', $key, $default);
  294. }
  295. /**
  296. * Determine if a cookie is set on the request.
  297. *
  298. * @param string $key
  299. * @return bool
  300. */
  301. public function hasCookie($key)
  302. {
  303. return ! is_null($this->cookie($key));
  304. }
  305. /**
  306. * Retrieve a cookie from the request.
  307. *
  308. * @param string $key
  309. * @param mixed $default
  310. * @return string|array
  311. */
  312. public function cookie($key = null, $default = null)
  313. {
  314. return $this->retrieveItem('cookies', $key, $default);
  315. }
  316. /**
  317. * Retrieve a file from the request.
  318. *
  319. * @param string $key
  320. * @param mixed $default
  321. * @return \Symfony\Component\HttpFoundation\File\UploadedFile|array
  322. */
  323. public function file($key = null, $default = null)
  324. {
  325. return array_get($this->files->all(), $key, $default);
  326. }
  327. /**
  328. * Determine if the uploaded data contains a file.
  329. *
  330. * @param string $key
  331. * @return bool
  332. */
  333. public function hasFile($key)
  334. {
  335. if ( ! is_array($files = $this->file($key))) $files = array($files);
  336. foreach ($files as $file)
  337. {
  338. if ($this->isValidFile($file)) return true;
  339. }
  340. return false;
  341. }
  342. /**
  343. * Check that the given file is a valid file instance.
  344. *
  345. * @param mixed $file
  346. * @return bool
  347. */
  348. protected function isValidFile($file)
  349. {
  350. return $file instanceof SplFileInfo && $file->getPath() != '';
  351. }
  352. /**
  353. * Retrieve a header from the request.
  354. *
  355. * @param string $key
  356. * @param mixed $default
  357. * @return string|array
  358. */
  359. public function header($key = null, $default = null)
  360. {
  361. return $this->retrieveItem('headers', $key, $default);
  362. }
  363. /**
  364. * Retrieve a server variable from the request.
  365. *
  366. * @param string $key
  367. * @param mixed $default
  368. * @return string|array
  369. */
  370. public function server($key = null, $default = null)
  371. {
  372. return $this->retrieveItem('server', $key, $default);
  373. }
  374. /**
  375. * Retrieve an old input item.
  376. *
  377. * @param string $key
  378. * @param mixed $default
  379. * @return mixed
  380. */
  381. public function old($key = null, $default = null)
  382. {
  383. return $this->session()->getOldInput($key, $default);
  384. }
  385. /**
  386. * Flash the input for the current request to the session.
  387. *
  388. * @param string $filter
  389. * @param array $keys
  390. * @return void
  391. */
  392. public function flash($filter = null, $keys = array())
  393. {
  394. $flash = ( ! is_null($filter)) ? $this->$filter($keys) : $this->input();
  395. $this->session()->flashInput($flash);
  396. }
  397. /**
  398. * Flash only some of the input to the session.
  399. *
  400. * @param mixed string
  401. * @return void
  402. */
  403. public function flashOnly($keys)
  404. {
  405. $keys = is_array($keys) ? $keys : func_get_args();
  406. return $this->flash('only', $keys);
  407. }
  408. /**
  409. * Flash only some of the input to the session.
  410. *
  411. * @param mixed string
  412. * @return void
  413. */
  414. public function flashExcept($keys)
  415. {
  416. $keys = is_array($keys) ? $keys : func_get_args();
  417. return $this->flash('except', $keys);
  418. }
  419. /**
  420. * Flush all of the old input from the session.
  421. *
  422. * @return void
  423. */
  424. public function flush()
  425. {
  426. $this->session()->flashInput(array());
  427. }
  428. /**
  429. * Retrieve a parameter item from a given source.
  430. *
  431. * @param string $source
  432. * @param string $key
  433. * @param mixed $default
  434. * @return string|array
  435. */
  436. protected function retrieveItem($source, $key, $default)
  437. {
  438. if (is_null($key))
  439. {
  440. return $this->$source->all();
  441. }
  442. return $this->$source->get($key, $default, true);
  443. }
  444. /**
  445. * Merge new input into the current request's input array.
  446. *
  447. * @param array $input
  448. * @return void
  449. */
  450. public function merge(array $input)
  451. {
  452. $this->getInputSource()->add($input);
  453. }
  454. /**
  455. * Replace the input for the current request.
  456. *
  457. * @param array $input
  458. * @return void
  459. */
  460. public function replace(array $input)
  461. {
  462. $this->getInputSource()->replace($input);
  463. }
  464. /**
  465. * Get the JSON payload for the request.
  466. *
  467. * @param string $key
  468. * @param mixed $default
  469. * @return mixed
  470. */
  471. public function json($key = null, $default = null)
  472. {
  473. if ( ! isset($this->json))
  474. {
  475. $this->json = new ParameterBag((array) json_decode($this->getContent(), true));
  476. }
  477. if (is_null($key)) return $this->json;
  478. return array_get($this->json->all(), $key, $default);
  479. }
  480. /**
  481. * Get the input source for the request.
  482. *
  483. * @return \Symfony\Component\HttpFoundation\ParameterBag
  484. */
  485. protected function getInputSource()
  486. {
  487. if ($this->isJson()) return $this->json();
  488. return $this->getMethod() == 'GET' ? $this->query : $this->request;
  489. }
  490. /**
  491. * Determine if the request is sending JSON.
  492. *
  493. * @return bool
  494. */
  495. public function isJson()
  496. {
  497. return str_contains($this->header('CONTENT_TYPE'), '/json');
  498. }
  499. /**
  500. * Determine if the current request is asking for JSON in return.
  501. *
  502. * @return bool
  503. */
  504. public function wantsJson()
  505. {
  506. $acceptable = $this->getAcceptableContentTypes();
  507. return isset($acceptable[0]) && $acceptable[0] == 'application/json';
  508. }
  509. /**
  510. * Get the data format expected in the response.
  511. *
  512. * @param string $default
  513. * @return string
  514. */
  515. public function format($default = 'html')
  516. {
  517. foreach ($this->getAcceptableContentTypes() as $type)
  518. {
  519. if ($format = $this->getFormat($type)) return $format;
  520. }
  521. return $default;
  522. }
  523. /**
  524. * Create an Illuminate request from a Symfony instance.
  525. *
  526. * @param \Symfony\Component\HttpFoundation\Request $request
  527. * @return \Illuminate\Http\Request
  528. */
  529. public static function createFromBase(SymfonyRequest $request)
  530. {
  531. if ($request instanceof static) return $request;
  532. $content = $request->content;
  533. $request = (new static)->duplicate(
  534. $request->query->all(), $request->request->all(), $request->attributes->all(),
  535. $request->cookies->all(), $request->files->all(), $request->server->all()
  536. );
  537. $request->content = $content;
  538. $request->request = $request->getInputSource();
  539. return $request;
  540. }
  541. /**
  542. * {@inheritdoc}
  543. */
  544. public function duplicate(array $query = null, array $request = null, array $attributes = null, array $cookies = null, array $files = null, array $server = null)
  545. {
  546. return parent::duplicate($query, $request, $attributes, $cookies, array_filter((array) $files), $server);
  547. }
  548. /**
  549. * Get the session associated with the request.
  550. *
  551. * @return \Illuminate\Session\Store
  552. *
  553. * @throws \RuntimeException
  554. */
  555. public function session()
  556. {
  557. if ( ! $this->hasSession())
  558. {
  559. throw new RuntimeException("Session store not set on request.");
  560. }
  561. return $this->getSession();
  562. }
  563. /**
  564. * Get the user making the request.
  565. *
  566. * @return mixed
  567. */
  568. public function user()
  569. {
  570. return call_user_func($this->getUserResolver());
  571. }
  572. /**
  573. * Get the route handling the request.
  574. *
  575. * @return \Illuminate\Routing\Route|null
  576. */
  577. public function route()
  578. {
  579. if (func_num_args() == 1)
  580. {
  581. return $this->route()->parameter(func_get_arg(0));
  582. }
  583. else
  584. {
  585. return call_user_func($this->getRouteResolver());
  586. }
  587. }
  588. /**
  589. * Get the user resolver callback.
  590. *
  591. * @return \Closure
  592. */
  593. public function getUserResolver()
  594. {
  595. return $this->userResolver ?: function() {};
  596. }
  597. /**
  598. * Set the user resolver callback.
  599. *
  600. * @param \Closure $callback
  601. * @return $this
  602. */
  603. public function setUserResolver(Closure $callback)
  604. {
  605. $this->userResolver = $callback;
  606. return $this;
  607. }
  608. /**
  609. * Get the route resolver callback.
  610. *
  611. * @return \Closure
  612. */
  613. public function getRouteResolver()
  614. {
  615. return $this->routeResolver ?: function() {};
  616. }
  617. /**
  618. * Set the route resolver callback.
  619. *
  620. * @param \Closure $callback
  621. * @return $this
  622. */
  623. public function setRouteResolver(Closure $callback)
  624. {
  625. $this->routeResolver = $callback;
  626. return $this;
  627. }
  628. /**
  629. * Determine if the given offset exists.
  630. *
  631. * @param string $offset
  632. * @return bool
  633. */
  634. public function offsetExists($offset)
  635. {
  636. return array_key_exists($offset, $this->all());
  637. }
  638. /**
  639. * Get the value at the given offset.
  640. *
  641. * @param string $offset
  642. * @return mixed
  643. */
  644. public function offsetGet($offset)
  645. {
  646. return $this->input($offset);
  647. }
  648. /**
  649. * Set the value at the given offset.
  650. *
  651. * @param string $offset
  652. * @param mixed $value
  653. * @return void
  654. */
  655. public function offsetSet($offset, $value)
  656. {
  657. return $this->getInputSource()->set($offset, $value);
  658. }
  659. /**
  660. * Remove the value at the given offset.
  661. *
  662. * @param string $offset
  663. * @return void
  664. */
  665. public function offsetUnset($offset)
  666. {
  667. return $this->getInputSource()->remove($offset);
  668. }
  669. /**
  670. * Get an input element from the request.
  671. *
  672. * @param string $key
  673. * @return mixed
  674. */
  675. public function __get($key)
  676. {
  677. $input = $this->input();
  678. if (array_key_exists($key, $input))
  679. {
  680. return $this->input($key);
  681. }
  682. elseif ( ! is_null($this->route()))
  683. {
  684. return $this->route()->parameter($key);
  685. }
  686. }
  687. }