Skip to content

Commit a5c32ba

Browse files
committed
feat: storing and loading data with json
1 parent 856b871 commit a5c32ba

2 files changed

Lines changed: 47 additions & 35 deletions

File tree

src/LogRequest.php

Lines changed: 44 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,29 @@ public function handle($request, Closure $next)
2424
{
2525

2626
if (! $this->isExceptRequest($request)) {
27-
(new RequestLogEntry)->forceFill([
28-
'ip' => $request->getClientIp(),
29-
'path' => urldecode($request->path()),
30-
'method' => $request->getMethod(),
31-
'agent' => $request->server('HTTP_USER_AGENT'),
32-
'get' => $this->get($request),
33-
'post' => $this->post($request),
34-
'cookies' => $this->cookies($request),
35-
'session' => $this->session($request)
36-
])->save();
27+
RequestLogEntry::create($this->getData());
3728
}
3829

3930
return $next($request);
4031
}
4132

33+
/**
34+
* @return array
35+
*/
36+
protected function getData() : array
37+
{
38+
return [
39+
'ip' => $request->getClientIp(),
40+
'path' => urldecode($request->path()),
41+
'method' => $request->getMethod(),
42+
'agent' => $request->server('HTTP_USER_AGENT'),
43+
'get' => $this->get($request),
44+
'post' => $this->post($request),
45+
'cookies' => $this->cookies($request),
46+
'session' => $this->session($request)
47+
];
48+
}
49+
4250
/**
4351
* @param \Illuminate\Http\Request $request
4452
* @return string|null
@@ -53,45 +61,49 @@ protected function session($request) : ?string
5361
}
5462

5563
/**
56-
* @param \Illuminate\Http\Request $request
57-
* @return string|null
64+
* @param array $data
65+
* @return array|null
5866
*/
59-
protected function get($request) : ?string
67+
protected function export(array $data = []) : ?array
6068
{
61-
$get = Arr::except($request->query->all(), $this->getExceptGet());
62-
if (count($get) > 0) {
63-
return var_export($get, true);
69+
if (count($data) === 0) {
70+
return null;
6471
}
6572

66-
return null;
73+
return $data;
6774
}
6875

6976
/**
7077
* @param \Illuminate\Http\Request $request
71-
* @return string|null
78+
* @return array|null
7279
*/
73-
protected function post($request) : ?string
80+
protected function get($request) : ?array
7481
{
75-
$post = Arr::except($request->request->all(), $this->getExceptPost());
76-
if (count($post) > 0) {
77-
return var_export($post, true);
78-
}
79-
80-
return null;
82+
return $this->export(
83+
Arr::except($request->query->all(), $this->getExceptGet())
84+
);
8185
}
8286

8387
/**
8488
* @param \Illuminate\Http\Request $request
85-
* @return string|null
89+
* @return array|null
8690
*/
87-
protected function cookies($request) : ?string
91+
protected function post($request) : ?array
8892
{
89-
$cookies = Arr::except($request->cookies->all(), $this->getExceptCookies());
90-
if (count($cookies) > 0) {
91-
return var_export($cookies, true);
92-
}
93+
return $this->export(
94+
Arr::except($request->request->all(), $this->getExceptPost())
95+
);
96+
}
9397

94-
return null;
98+
/**
99+
* @param \Illuminate\Http\Request $request
100+
* @return array|null
101+
*/
102+
protected function cookies($request) : ?array
103+
{
104+
return $this->export(
105+
Arr::except($request->cookies->all(), $this->getExceptCookies())
106+
);
95107
}
96108

97109
/**

src/RequestLogEntry.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ class RequestLogEntry extends Model
3232
'path' => 'string',
3333
'method' => 'string',
3434
'agent' => 'string',
35-
'get' => 'string',
36-
'post' => 'string',
37-
'cookies' => 'string',
35+
'get' => 'json',
36+
'post' => 'json',
37+
'cookies' => 'json',
3838
'session' => 'string'
3939
];
4040

0 commit comments

Comments
 (0)