function get(string $key, $default = null) { return parent::get($key, $default); } /** * Get the JSON payload for the request. * * @param string|null $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->headers->replace($from->headers->all()); $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 static */ public static function createFromBase(SymfonyRequest $request) { $newRequest = (new static)->duplicate( $request->query->all(), $request->request->all(), $request->attributes->all(), $request->cookies->all(), $request->files->all(), $request->server->all() ); $newRequest->headers->replace($request->headers->all()); $newRequest->content = $request->content; $newRequest->request = $newRequest->getInputSource(); return $newRequest; } /** * {@inheritdoc} * * @return static */ 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 * @param mixed $default * @return \Illuminate\Routing\Route|object|string|null */ public function route($param = null, $default = null) { $route = call_user_func($this->getRouteResolver()); if (is_null($route) || is_null($param)) { return $route; } return $route->parameter($param, $default); } /** * 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 */ #[\ReturnTypeWillChange] public function offsetExists($offset) { $route = $this->route(); return Arr::has( $this->all() + ($route ? $route->parameters() : []), $offset ); } /** * Get the value at the given offset. * * @param string $offset * @return mixed */ #[\ReturnTypeWillChange] public function offsetGet($offset) { return $this->__get($offset); } /** * Set the value at the given offset. * * @param string $offset * @param mixed $value * @return void */ #[\ReturnTypeWillChange] public function offsetSet($offset, $value) { $this->getInputSource()->set($offset, $value); } /** * Remove the value at the given offset. * * @param string $offset * @return void */ #[\ReturnTypeWillChange] 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) { return Arr::get($this->all(), $key, function () use ($key) { return $this->route($key); }); } }