Skip to content

Commit bd3851c

Browse files
Publish a service provider to provide default versioning, shared props
1 parent f5b8c32 commit bd3851c

File tree

3 files changed

+89
-2
lines changed

3 files changed

+89
-2
lines changed

src/InertiaJsPreset.php

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@ public static function install($command)
1717
static::$command = $command;
1818

1919
static::updatePackages();
20-
static::updateComposer(false);
2120
static::updateBootstrapping();
21+
static::updateComposer(false);
22+
static::publishServiceProvider();
23+
static::registerInertiaServiceProvider();
2224
static::updateWelcomePage();
2325
static::updateGitignore();
2426
static::scaffoldComponents();
@@ -108,4 +110,43 @@ protected static function updateComposer($dev = true)
108110
json_encode($packages, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT).PHP_EOL
109111
);
110112
}
113+
114+
public static function publishServiceProvider()
115+
{
116+
copy(
117+
__DIR__.'/inertiajs-stubs/providers/InertiaJsServiceProvider.stub',
118+
app_path('Providers/InertiaJsServiceProvider.php')
119+
);
120+
}
121+
122+
public static function registerInertiaServiceProvider()
123+
{
124+
$namespace = Str::replaceLast('\\', '', Container::getInstance()->getNamespace());
125+
126+
$appConfig = file_get_contents(config_path('app.php'));
127+
128+
if (Str::contains($appConfig, $namespace.'\\Providers\\InertiaJsServiceProvider::class')) {
129+
return;
130+
}
131+
132+
$lineEndingCount = [
133+
"\r\n" => substr_count($appConfig, "\r\n"),
134+
"\r" => substr_count($appConfig, "\r"),
135+
"\n" => substr_count($appConfig, "\n"),
136+
];
137+
138+
$eol = array_keys($lineEndingCount, max($lineEndingCount))[0];
139+
140+
file_put_contents(config_path('app.php'), str_replace(
141+
"{$namespace}\\Providers\\RouteServiceProvider::class,".$eol,
142+
"{$namespace}\\Providers\\RouteServiceProvider::class,".$eol." {$namespace}\Providers\InertiaJsServiceProvider::class,".$eol,
143+
$appConfig
144+
));
145+
146+
file_put_contents(app_path('Providers/InertiaJsServiceProvider.php'), str_replace(
147+
"namespace App\Providers;",
148+
"namespace {$namespace}\Providers;",
149+
file_get_contents(app_path('Providers/InertiaJsServiceProvider.php'))
150+
));
151+
}
111152
}

src/InertiaJsPresetServiceProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
namespace LaravelFrontendPresets\InertiaJsPreset;
44

5-
use Illuminate\Support\ServiceProvider;
65
use Illuminate\Foundation\Console\PresetCommand;
6+
use Illuminate\Support\ServiceProvider;
77

88
class InertiaJsPresetServiceProvider extends ServiceProvider
99
{
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
namespace App\Providers;
4+
5+
use Illuminate\Support\Facades\Auth;
6+
use Illuminate\Support\Facades\Session;
7+
use Illuminate\Support\ServiceProvider;
8+
use Inertia\Inertia;
9+
10+
class InertiaJsServiceProvider extends ServiceProvider
11+
{
12+
public function register()
13+
{
14+
$this->registerInertia();
15+
}
16+
17+
protected function registerInertia()
18+
{
19+
Inertia::version(function () {
20+
return md5_file(public_path('mix-manifest.json'));
21+
});
22+
23+
Inertia::share([
24+
'auth' => function () {
25+
return [
26+
'user' => Auth::user() ? [
27+
'id' => Auth::user()->id,
28+
'name' => Auth::user()->name,
29+
'email' => Auth::user()->email,
30+
'avatar' => Auth::user()->avatar(),
31+
] : null,
32+
];
33+
},
34+
'flash' => function () {
35+
return [
36+
'success' => Session::get('success'),
37+
];
38+
},
39+
'errors' => function () {
40+
return Session::get('errors')
41+
? Session::get('errors')->getBag('default')->getMessages()
42+
: (object) [];
43+
},
44+
]);
45+
}
46+
}

0 commit comments

Comments
 (0)