-
Notifications
You must be signed in to change notification settings - Fork 255
/
Copy pathMiddleware.php
158 lines (134 loc) · 4.31 KB
/
Middleware.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<?php
namespace Inertia;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redirect;
use Inertia\Support\Header;
use Symfony\Component\HttpFoundation\Response;
class Middleware
{
/**
* The root template that's loaded on the first page visit.
*
* @see https://inertiajs.com/server-side-setup#root-template
*
* @var string
*/
protected $rootView = 'app';
/**
* Determines the current asset version.
*
* @see https://inertiajs.com/asset-versioning
*
* @return string|null
*/
public function version(Request $request)
{
if (config('app.asset_url')) {
return hash('xxh128', config('app.asset_url'));
}
if (file_exists($manifest = public_path('mix-manifest.json'))) {
return hash_file('xxh128', $manifest);
}
if (file_exists($manifest = public_path('build/manifest.json'))) {
return hash_file('xxh128', $manifest);
}
return null;
}
/**
* Defines the props that are shared by default.
*
* @see https://inertiajs.com/shared-data
*
* @return array
*/
public function share(Request $request)
{
return [
'errors' => Inertia::always($this->resolveValidationErrors($request)),
];
}
/**
* Sets the root template that's loaded on the first page visit.
*
* @see https://inertiajs.com/server-side-setup#root-template
*
* @return string
*/
public function rootView(Request $request)
{
return $this->rootView;
}
/**
* Handle the incoming request.
*
* @return Response
*/
public function handle(Request $request, Closure $next)
{
Inertia::version(function () use ($request) {
return $this->version($request);
});
Inertia::share($this->share($request));
Inertia::setRootView($this->rootView($request));
$response = $next($request);
$response->headers->set('Vary', Header::INERTIA);
if (! $request->header(Header::INERTIA)) {
return $response;
}
if ($request->method() === 'GET' && $request->header(Header::VERSION, '') !== Inertia::getVersion()) {
$response = $this->onVersionChange($request, $response);
}
if ($response->isOk() && empty($response->getContent())) {
$response = $this->onEmptyResponse($request, $response);
}
if ($response->getStatusCode() === 302 && in_array($request->method(), ['PUT', 'PATCH', 'DELETE'])) {
$response->setStatusCode(303);
}
return $response;
}
/**
* Determines what to do when an Inertia action returned with no response.
* By default, we'll redirect the user back to where they came from.
*/
public function onEmptyResponse(Request $request, Response $response): Response
{
return Redirect::back();
}
/**
* Determines what to do when the Inertia asset version has changed.
* By default, we'll initiate a client-side location visit to force an update.
*/
public function onVersionChange(Request $request, Response $response): Response
{
if ($request->hasSession()) {
$request->session()->reflash();
}
return Inertia::location($request->fullUrl());
}
/**
* Resolves and prepares validation errors in such
* a way that they are easier to use client-side.
*
* @return object
*/
public function resolveValidationErrors(Request $request)
{
if (! $request->hasSession() || ! $request->session()->has('errors')) {
return (object) [];
}
return (object) collect($request->session()->get('errors')->getBags())->map(function ($bag) {
return (object) collect($bag->messages())->map(function ($errors) {
return $errors[0];
})->toArray();
})->pipe(function ($bags) use ($request) {
if ($bags->has('default') && $request->header(Header::ERROR_BAG)) {
return [$request->header(Header::ERROR_BAG) => $bags->get('default')];
}
if ($bags->has('default')) {
return $bags->get('default');
}
return $bags->toArray();
});
}
}