request = $request ?? Request::createFromGlobals(); } /** * @return array */ public function getRequest(): array { return [ 'url' => $this->request->getUri(), 'ip' => $this->request->getClientIp(), 'method' => $this->request->getMethod(), 'useragent' => $this->request->headers->get('User-Agent'), ]; } /** * @return array */ protected function getFiles(): array { if (is_null($this->request->files)) { return []; } return $this->mapFiles($this->request->files->all()); } /** * @param array $files * * @return array */ protected function mapFiles(array $files): array { return array_map(function ($file) { if (is_array($file)) { return $this->mapFiles($file); } if (! $file instanceof UploadedFile) { return; } try { $fileSize = $file->getSize(); } catch (RuntimeException $e) { $fileSize = 0; } try { $mimeType = $file->getMimeType(); } catch (InvalidArgumentException $e) { $mimeType = 'undefined'; } return [ 'pathname' => $file->getPathname(), 'size' => $fileSize, 'mimeType' => $mimeType, ]; }, $files); } /** * @return array */ public function getSession(): array { try { $session = $this->request->getSession(); } catch (Throwable $exception) { $session = []; } return $session ? $this->getValidSessionData($session) : []; } protected function getValidSessionData($session): array { if (! method_exists($session, 'all')) { return []; } try { json_encode($session->all()); } catch (Throwable $e) { return []; } return $session->all(); } /** * @return arrayrequest->cookies->all(); } /** * @return array */ public function getHeaders(): array { /** @var array> $headers */ $headers = $this->request->headers->all(); return array_filter( array_map( fn (array $header) => $header[0], $headers ) ); } /** * @return array */ public function getRequestData(): array { return [ 'queryString' => $this->request->query->all(), 'body' => $this->request->request->all(), 'files' => $this->getFiles(), ]; } /** @return array */ public function toArray(): array { return [ 'request' => $this->getRequest(), 'request_data' => $this->getRequestData(), 'headers' => $this->getHeaders(), 'cookies' => $this->getCookies(), 'session' => $this->getSession(), ]; } }