forked from atymic/laravel-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInertiaServiceProvider.php
93 lines (85 loc) · 2.44 KB
/
InertiaServiceProvider.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
<?php
namespace App\Providers;
use Illuminate\Foundation\Application;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\ServiceProvider;
use Inertia\Inertia;
class InertiaServiceProvider extends ServiceProvider
{
/**
* Register services.
*/
public function register()
{
$this->registerMixVersion();
$this->shareApplication();
$this->shareAuthentification();
$this->shareFlashes();
}
/**
* Sets the mix version from the manifest.
*/
public function registerMixVersion()
{
Inertia::version(function () {
return md5_file(public_path('mix-manifest.json'));
});
}
/**
* Share global application data.
*/
public function shareApplication()
{
Inertia::share([
'app' => function () {
$composer = json_decode(file_get_contents(base_path('/composer.json')), true);
return [
'name' => config('app.name'),
'laravel' => Application::VERSION,
'version' => $composer['version'] ?? '0.1',
'logo' => Storage::url('static/logo.png'),
'load_time' => (microtime(true) - LARAVEL_START),
'route' => Route::currentRouteName(),
];
},
]);
}
/**
* Share authentification data.
*/
public function shareAuthentification()
{
Inertia::share([
'auth' => function () {
return [
'user' => Auth::user() ? [
'id' => Auth::user()->id,
'name' => Auth::user()->name,
'email' => Auth::user()->email,
] : null,
];
},
]);
}
/**
* Share flashes and errors.
*/
public function shareFlashes()
{
Inertia::share([
'flash' => function () {
return [
'success' => Session::get('success'),
];
},
'errors' => function () {
return Session::get('errors')
? Session::get('errors')->getBag('default')->getMessages()
: (object) [];
},
]);
}
}