123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675 |
- <?php
-
- namespace Illuminate\Http;
-
- use Closure;
- use ArrayAccess;
- use RuntimeException;
- use Illuminate\Support\Arr;
- use Illuminate\Support\Str;
- use Illuminate\Support\Traits\Macroable;
- use Illuminate\Contracts\Support\Arrayable;
- use Symfony\Component\HttpFoundation\ParameterBag;
- use Symfony\Component\HttpFoundation\Request as SymfonyRequest;
-
- class Request extends SymfonyRequest implements Arrayable, ArrayAccess
- {
- use Concerns\InteractsWithContentTypes,
- Concerns\InteractsWithFlashData,
- Concerns\InteractsWithInput,
- Macroable;
-
- /**
- * The decoded JSON content for the request.
- *
- * @var \Symfony\Component\HttpFoundation\ParameterBag|null
- */
- protected $json;
-
- /**
- * All of the converted files for the request.
- *
- * @var array
- */
- protected $convertedFiles;
-
- /**
- * The user resolver callback.
- *
- * @var \Closure
- */
- protected $userResolver;
-
- /**
- * The route resolver callback.
- *
- * @var \Closure
- */
- protected $routeResolver;
-
- /**
- * Create a new Illuminate HTTP request from server variables.
- *
- * @return static
- */
- public static function capture()
- {
- static::enableHttpMethodParameterOverride();
-
- return static::createFromBase(SymfonyRequest::createFromGlobals());
- }
-
- /**
- * Return the Request instance.
- *
- * @return $this
- */
- public function instance()
- {
- return $this;
- }
-
- /**
- * Get the request method.
- *
- * @return string
- */
- public function method()
- {
- return $this->getMethod();
- }
-
- /**
- * Get the root URL for the application.
- *
- * @return string
- */
- public function root()
- {
- return rtrim($this->getSchemeAndHttpHost().$this->getBaseUrl(), '/');
- }
-
- /**
- * Get the URL (no query string) for the request.
- *
- * @return string
- */
- public function url()
- {
- return rtrim(preg_replace('/\?.*/', '', $this->getUri()), '/');
- }
-
- /**
- * Get the full URL for the request.
- *
- * @return string
- */
- public function fullUrl()
- {
- $query = $this->getQueryString();
-
- $question = $this->getBaseUrl().$this->getPathInfo() == '/' ? '/?' : '?';
-
- return $query ? $this->url().$question.$query : $this->url();
- }
-
- /**
- * Get the full URL for the request with the added query string parameters.
- *
- * @param array $query
- * @return string
- */
- public function fullUrlWithQuery(array $query)
- {
- $question = $this->getBaseUrl().$this->getPathInfo() == '/' ? '/?' : '?';
-
- return count($this->query()) > 0
- ? $this->url().$question.http_build_query(array_merge($this->query(), $query))
- : $this->fullUrl().$question.http_build_query($query);
- }
-
- /**
- * Get the current path info for the request.
- *
- * @return string
- */
- public function path()
- {
- $pattern = trim($this->getPathInfo(), '/');
-
- return $pattern == '' ? '/' : $pattern;
- }
-
- /**
- * Get the current decoded path info for the request.
- *
- * @return string
- */
- public function decodedPath()
- {
- return rawurldecode($this->path());
- }
-
- /**
- * Get a segment from the URI (1 based index).
- *
- * @param int $index
- * @param string|null $default
- * @return string|null
- */
- public function segment($index, $default = null)
- {
- return Arr::get($this->segments(), $index - 1, $default);
- }
-
- /**
- * Get all of the segments for the request path.
- *
- * @return array
- */
- public function segments()
- {
- $segments = explode('/', $this->decodedPath());
-
- return array_values(array_filter($segments, function ($value) {
- return $value !== '';
- }));
- }
-
- /**
- * Determine if the current request URI matches a pattern.
- *
- * @param dynamic $patterns
- * @return bool
- */
- public function is(...$patterns)
- {
- foreach ($patterns as $pattern) {
- if (Str::is($pattern, $this->decodedPath())) {
- return true;
- }
- }
-
- return false;
- }
-
- /**
- * Determine if the route name matches a given pattern.
- *
- * @param dynamic $patterns
- * @return bool
- */
- public function routeIs(...$patterns)
- {
- return $this->route() && $this->route()->named(...$patterns);
- }
-
- /**
- * Determine if the current request URL and query string matches a pattern.
- *
- * @param dynamic $patterns
- * @return bool
- */
- public function fullUrlIs(...$patterns)
- {
- $url = $this->fullUrl();
-
- foreach ($patterns as $pattern) {
- if (Str::is($pattern, $url)) {
- return true;
- }
- }
-
- return false;
- }
-
- /**
- * Determine if the request is the result of an AJAX call.
- *
- * @return bool
- */
- public function ajax()
- {
- return $this->isXmlHttpRequest();
- }
-
- /**
- * Determine if the request is the result of an PJAX call.
- *
- * @return bool
- */
- public function pjax()
- {
- return $this->headers->get('X-PJAX') == true;
- }
-
- /**
- * Determine if the request is over HTTPS.
- *
- * @return bool
- */
- public function secure()
- {
- return $this->isSecure();
- }
-
- /**
- * Get the client IP address.
- *
- * @return string
- */
- public function ip()
- {
- return $this->getClientIp();
- }
-
- /**
- * Get the client IP addresses.
- *
- * @return array
- */
- public function ips()
- {
- return $this->getClientIps();
- }
-
- /**
- * Get the client user agent.
- *
- * @return string
- */
- public function userAgent()
- {
- return $this->headers->get('User-Agent');
- }
-
- /**
- * Merge new input into the current request's input array.
- *
- * @param array $input
- * @return \Illuminate\Http\Request
- */
- public function merge(array $input)
- {
- $this->getInputSource()->add($input);
-
- return $this;
- }
-
- /**
- * Replace the input for the current request.
- *
- * @param array $input
- * @return \Illuminate\Http\Request
- */
- public function replace(array $input)
- {
- $this->getInputSource()->replace($input);
-
- return $this;
- }
-
- /**
- * Get the JSON payload for the request.
- *
- * @param string $key
- * @param mixed $default
- * @return \Symfony\Component\HttpFoundation\ParameterBag|mixed
- */
- public function json($key = null, $default = null)
- {
- if (! isset($this->json)) {
- $this->json = new ParameterBag((array) json_decode($this->getContent(), true));
- }
-
- if (is_null($key)) {
- return $this->json;
- }
-
- return data_get($this->json->all(), $key, $default);
- }
-
- /**
- * Get the input source for the request.
- *
- * @return \Symfony\Component\HttpFoundation\ParameterBag
- */
- protected function getInputSource()
- {
- if ($this->isJson()) {
- return $this->json();
- }
-
- return in_array($this->getRealMethod(), ['GET', 'HEAD']) ? $this->query : $this->request;
- }
-
- /**
- * Create a new request instance from the given Laravel request.
- *
- * @param \Illuminate\Http\Request $from
- * @param \Illuminate\Http\Request|null $to
- * @return static
- */
- public static function createFrom(self $from, $to = null)
- {
- $request = $to ?: new static;
-
- $files = $from->files->all();
-
- $files = is_array($files) ? array_filter($files) : $files;
-
- $request->initialize(
- $from->query->all(),
- $from->request->all(),
- $from->attributes->all(),
- $from->cookies->all(),
- $files,
- $from->server->all(),
- $from->getContent()
- );
-
- $request->setJson($from->json());
-
- if ($session = $from->getSession()) {
- $request->setLaravelSession($session);
- }
-
- $request->setUserResolver($from->getUserResolver());
-
- $request->setRouteResolver($from->getRouteResolver());
-
- return $request;
- }
-
- /**
- * Create an Illuminate request from a Symfony instance.
- *
- * @param \Symfony\Component\HttpFoundation\Request $request
- * @return \Illuminate\Http\Request
- */
- public static function createFromBase(SymfonyRequest $request)
- {
- if ($request instanceof static) {
- return $request;
- }
-
- $content = $request->content;
-
- $request = (new static)->duplicate(
- $request->query->all(), $request->request->all(), $request->attributes->all(),
- $request->cookies->all(), $request->files->all(), $request->server->all()
- );
-
- $request->content = $content;
-
- $request->request = $request->getInputSource();
-
- return $request;
- }
-
- /**
- * {@inheritdoc}
- */
- public function duplicate(array $query = null, array $request = null, array $attributes = null, array $cookies = null, array $files = null, array $server = null)
- {
- return parent::duplicate($query, $request, $attributes, $cookies, $this->filterFiles($files), $server);
- }
-
- /**
- * Filter the given array of files, removing any empty values.
- *
- * @param mixed $files
- * @return mixed
- */
- protected function filterFiles($files)
- {
- if (! $files) {
- return;
- }
-
- foreach ($files as $key => $file) {
- if (is_array($file)) {
- $files[$key] = $this->filterFiles($files[$key]);
- }
-
- if (empty($files[$key])) {
- unset($files[$key]);
- }
- }
-
- return $files;
- }
-
- /**
- * Get the session associated with the request.
- *
- * @return \Illuminate\Session\Store
- *
- * @throws \RuntimeException
- */
- public function session()
- {
- if (! $this->hasSession()) {
- throw new RuntimeException('Session store not set on request.');
- }
-
- return $this->session;
- }
-
- /**
- * Get the session associated with the request.
- *
- * @return \Illuminate\Session\Store|null
- */
- public function getSession()
- {
- return $this->session;
- }
-
- /**
- * Set the session instance on the request.
- *
- * @param \Illuminate\Contracts\Session\Session $session
- * @return void
- */
- public function setLaravelSession($session)
- {
- $this->session = $session;
- }
-
- /**
- * Get the user making the request.
- *
- * @param string|null $guard
- * @return mixed
- */
- public function user($guard = null)
- {
- return call_user_func($this->getUserResolver(), $guard);
- }
-
- /**
- * Get the route handling the request.
- *
- * @param string|null $param
- *
- * @return \Illuminate\Routing\Route|object|string
- */
- public function route($param = null)
- {
- $route = call_user_func($this->getRouteResolver());
-
- if (is_null($route) || is_null($param)) {
- return $route;
- }
-
- return $route->parameter($param);
- }
-
- /**
- * Get a unique fingerprint for the request / route / IP address.
- *
- * @return string
- *
- * @throws \RuntimeException
- */
- public function fingerprint()
- {
- if (! $route = $this->route()) {
- throw new RuntimeException('Unable to generate fingerprint. Route unavailable.');
- }
-
- return sha1(implode('|', array_merge(
- $route->methods(),
- [$route->getDomain(), $route->uri(), $this->ip()]
- )));
- }
-
- /**
- * Set the JSON payload for the request.
- *
- * @param \Symfony\Component\HttpFoundation\ParameterBag $json
- * @return $this
- */
- public function setJson($json)
- {
- $this->json = $json;
-
- return $this;
- }
-
- /**
- * Get the user resolver callback.
- *
- * @return \Closure
- */
- public function getUserResolver()
- {
- return $this->userResolver ?: function () {
- //
- };
- }
-
- /**
- * Set the user resolver callback.
- *
- * @param \Closure $callback
- * @return $this
- */
- public function setUserResolver(Closure $callback)
- {
- $this->userResolver = $callback;
-
- return $this;
- }
-
- /**
- * Get the route resolver callback.
- *
- * @return \Closure
- */
- public function getRouteResolver()
- {
- return $this->routeResolver ?: function () {
- //
- };
- }
-
- /**
- * Set the route resolver callback.
- *
- * @param \Closure $callback
- * @return $this
- */
- public function setRouteResolver(Closure $callback)
- {
- $this->routeResolver = $callback;
-
- return $this;
- }
-
- /**
- * Get all of the input and files for the request.
- *
- * @return array
- */
- public function toArray()
- {
- return $this->all();
- }
-
- /**
- * Determine if the given offset exists.
- *
- * @param string $offset
- * @return bool
- */
- public function offsetExists($offset)
- {
- return array_key_exists(
- $offset,
- $this->all() + $this->route()->parameters()
- );
- }
-
- /**
- * Get the value at the given offset.
- *
- * @param string $offset
- * @return mixed
- */
- public function offsetGet($offset)
- {
- return $this->__get($offset);
- }
-
- /**
- * Set the value at the given offset.
- *
- * @param string $offset
- * @param mixed $value
- * @return void
- */
- public function offsetSet($offset, $value)
- {
- $this->getInputSource()->set($offset, $value);
- }
-
- /**
- * Remove the value at the given offset.
- *
- * @param string $offset
- * @return void
- */
- public function offsetUnset($offset)
- {
- $this->getInputSource()->remove($offset);
- }
-
- /**
- * Check if an input element is set on the request.
- *
- * @param string $key
- * @return bool
- */
- public function __isset($key)
- {
- return ! is_null($this->__get($key));
- }
-
- /**
- * Get an input element from the request.
- *
- * @param string $key
- * @return mixed
- */
- public function __get($key)
- {
- if (array_key_exists($key, $this->all())) {
- return data_get($this->all(), $key);
- }
-
- return $this->route($key);
- }
- }
|