Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Spatie\FlareClient\Context;
4
 
5
use RuntimeException;
6
use Symfony\Component\HttpFoundation\File\UploadedFile;
7
use Symfony\Component\HttpFoundation\Request;
8
use Symfony\Component\Mime\Exception\InvalidArgumentException;
9
use Throwable;
10
 
11
class RequestContextProvider implements ContextProvider
12
{
13
    protected ?Request $request;
14
 
15
    public function __construct(Request $request = null)
16
    {
17
        $this->request = $request ?? Request::createFromGlobals();
18
    }
19
 
20
    /**
21
     * @return array<string, mixed>
22
     */
23
    public function getRequest(): array
24
    {
25
        return [
26
            'url' => $this->request->getUri(),
27
            'ip' => $this->request->getClientIp(),
28
            'method' => $this->request->getMethod(),
29
            'useragent' => $this->request->headers->get('User-Agent'),
30
        ];
31
    }
32
 
33
    /**
34
     * @return array<int, mixed>
35
     */
36
    protected function getFiles(): array
37
    {
38
        if (is_null($this->request->files)) {
39
            return [];
40
        }
41
 
42
        return $this->mapFiles($this->request->files->all());
43
    }
44
 
45
    /**
46
     * @param array<int, mixed> $files
47
     *
48
     * @return array<string, string>
49
     */
50
    protected function mapFiles(array $files): array
51
    {
52
        return array_map(function ($file) {
53
            if (is_array($file)) {
54
                return $this->mapFiles($file);
55
            }
56
 
57
            if (! $file instanceof UploadedFile) {
58
                return;
59
            }
60
 
61
            try {
62
                $fileSize = $file->getSize();
63
            } catch (RuntimeException $e) {
64
                $fileSize = 0;
65
            }
66
 
67
            try {
68
                $mimeType = $file->getMimeType();
69
            } catch (InvalidArgumentException $e) {
70
                $mimeType = 'undefined';
71
            }
72
 
73
            return [
74
                'pathname' => $file->getPathname(),
75
                'size' => $fileSize,
76
                'mimeType' => $mimeType,
77
            ];
78
        }, $files);
79
    }
80
 
81
    /**
82
     * @return array<string, mixed>
83
     */
84
    public function getSession(): array
85
    {
86
        try {
87
            $session = $this->request->getSession();
88
        } catch (Throwable $exception) {
89
            $session = [];
90
        }
91
 
92
        return $session ? $this->getValidSessionData($session) : [];
93
    }
94
 
95
    protected function getValidSessionData($session): array
96
    {
97
        if (! method_exists($session, 'all')) {
98
            return [];
99
        }
100
 
101
        try {
102
            json_encode($session->all());
103
        } catch (Throwable $e) {
104
            return [];
105
        }
106
 
107
        return $session->all();
108
    }
109
 
110
    /**
111
     * @return array<int|string, mixed
112
     */
113
    public function getCookies(): array
114
    {
115
        return $this->request->cookies->all();
116
    }
117
 
118
    /**
119
     * @return array<string, mixed>
120
     */
121
    public function getHeaders(): array
122
    {
123
        /** @var array<string, list<string|null>> $headers */
124
        $headers = $this->request->headers->all();
125
 
126
        return array_filter(
127
            array_map(
128
                fn (array $header) => $header[0],
129
                $headers
130
            )
131
        );
132
    }
133
 
134
    /**
135
     * @return array<string, mixed>
136
     */
137
    public function getRequestData(): array
138
    {
139
        return [
140
            'queryString' => $this->request->query->all(),
141
            'body' => $this->request->request->all(),
142
            'files' => $this->getFiles(),
143
        ];
144
    }
145
 
146
    /** @return array<string, mixed> */
147
    public function toArray(): array
148
    {
149
        return [
150
            'request' => $this->getRequest(),
151
            'request_data' => $this->getRequestData(),
152
            'headers' => $this->getHeaders(),
153
            'cookies' => $this->getCookies(),
154
            'session' => $this->getSession(),
155
        ];
156
    }
157
}