Skip to content

Commit 6c0a759

Browse files
Merge pull request #9 from laravel-frontend-presets/chore/laravel-6-compat
Update for Laravel 6
2 parents 0facbe0 + 35ead49 commit 6c0a759

11 files changed

+182
-50
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ This preset scaffolding removes the manual steps required to get up and running
99

1010
## Usage
1111

12-
1. Fresh install Laravel >= 5.8 and cd to your app.
13-
2. Install this preset via `composer require laravel-frontend-presets/inertiajs`. Laravel will automatically discover this package. No need to register the service provider.
12+
1. Fresh install Laravel >= 6.0 and cd to your app.
13+
2. Install this preset via `composer require --dev laravel-frontend-presets/inertiajs`. Laravel will automatically discover this package. No need to register the service provider.
1414

1515
### Installation
1616

src/InertiaJsPreset.php

+77
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,23 @@
22

33
namespace LaravelFrontendPresets\InertiaJsPreset;
44

5+
use Illuminate\Container\Container;
56
use Illuminate\Filesystem\Filesystem;
67
use Illuminate\Foundation\Console\Presets\Preset;
78
use Illuminate\Support\Arr;
9+
use Illuminate\Support\Facades\Artisan;
10+
use Illuminate\Support\Str;
11+
use Symfony\Component\Process\Process;
812

913
class InertiaJsPreset extends Preset
1014
{
1115
public static function install()
1216
{
1317
static::updatePackages();
1418
static::updateBootstrapping();
19+
static::updateComposer(false);
20+
static::publishServiceProvider();
21+
static::registerInertiaServiceProvider();
1522
static::updateWelcomePage();
1623
static::updateGitignore();
1724
static::scaffoldComponents();
@@ -25,10 +32,18 @@ protected static function updatePackageArray(array $packages)
2532
'@babel/plugin-syntax-dynamic-import' => '^7.2.0',
2633
'@inertiajs/inertia' => '^0.1.0',
2734
'@inertiajs/inertia-vue' => '^0.1.0',
35+
'vue' => '^2.5.17',
2836
'vue-template-compiler' => '^2.6.10',
2937
], $packages);
3038
}
3139

40+
protected static function updateComposerArray(array $packages)
41+
{
42+
return array_merge([
43+
'inertiajs/inertia-laravel' => '^0.1',
44+
], $packages);
45+
}
46+
3247
protected static function updateBootstrapping()
3348
{
3449
copy(__DIR__.'/inertiajs-stubs/webpack.mix.js', base_path('webpack.mix.js'));
@@ -70,4 +85,66 @@ protected static function scaffoldRoutes()
7085
{
7186
copy(__DIR__.'/inertiajs-stubs/routes/web.php', base_path('routes/web.php'));
7287
}
88+
89+
protected static function updateComposer($dev = true)
90+
{
91+
if (! file_exists(base_path('composer.json'))) {
92+
return;
93+
}
94+
95+
$configurationKey = $dev ? 'require-dev' : 'require';
96+
97+
$packages = json_decode(file_get_contents(base_path('composer.json')), true);
98+
99+
$packages[$configurationKey] = static::updateComposerArray(
100+
array_key_exists($configurationKey, $packages) ? $packages[$configurationKey] : [],
101+
$configurationKey
102+
);
103+
104+
ksort($packages[$configurationKey]);
105+
106+
file_put_contents(
107+
base_path('composer.json'),
108+
json_encode($packages, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT).PHP_EOL
109+
);
110+
}
111+
112+
public static function publishInertiaServiceProvider()
113+
{
114+
copy(
115+
__DIR__.'/inertiajs-stubs/providers/InertiaJsServiceProvider.stub',
116+
app_path('Providers/InertiaJsServiceProvider.php')
117+
);
118+
}
119+
120+
public static function registerInertiaServiceProvider()
121+
{
122+
$namespace = Str::replaceLast('\\', '', Container::getInstance()->getNamespace());
123+
124+
$appConfig = file_get_contents(config_path('app.php'));
125+
126+
if (Str::contains($appConfig, $namespace.'\\Providers\\InertiaJsServiceProvider::class')) {
127+
return;
128+
}
129+
130+
$lineEndingCount = [
131+
"\r\n" => substr_count($appConfig, "\r\n"),
132+
"\r" => substr_count($appConfig, "\r"),
133+
"\n" => substr_count($appConfig, "\n"),
134+
];
135+
136+
$eol = array_keys($lineEndingCount, max($lineEndingCount))[0];
137+
138+
file_put_contents(config_path('app.php'), str_replace(
139+
"{$namespace}\\Providers\\RouteServiceProvider::class,".$eol,
140+
"{$namespace}\\Providers\\RouteServiceProvider::class,".$eol." {$namespace}\Providers\InertiaJsServiceProvider::class,".$eol,
141+
$appConfig
142+
));
143+
144+
file_put_contents(app_path('Providers/InertiaJsServiceProvider.php'), str_replace(
145+
"namespace App\Providers;",
146+
"namespace {$namespace}\Providers;",
147+
file_get_contents(app_path('Providers/InertiaJsServiceProvider.php'))
148+
));
149+
}
73150
}

src/InertiaJsPresetServiceProvider.php

+1-1
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
{

src/inertiajs-stubs/gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
/public/css
22
/public/js
3+
/public/mix-manifest.json
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
Inertia::version(function () {
15+
return md5_file(public_path('mix-manifest.json'));
16+
});
17+
18+
Inertia::share([
19+
'auth' => function () {
20+
return [
21+
'user' => Auth::user() ? [
22+
'id' => Auth::user()->id,
23+
'name' => Auth::user()->name,
24+
'email' => Auth::user()->email,
25+
'avatar' => Auth::user()->avatar(),
26+
] : null,
27+
];
28+
},
29+
'flash' => function () {
30+
return [
31+
'success' => Session::get('success'),
32+
];
33+
},
34+
'errors' => function () {
35+
return Session::get('errors')
36+
? Session::get('errors')->getBag('default')->getMessages()
37+
: (object) [];
38+
},
39+
]);
40+
}
41+
}

src/inertiajs-stubs/resources/js/Pages/About.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<template>
22
<layout>
3-
<h1>About</h1>
3+
<h1 class="text-2xl font-semibold mb-3">About</h1>
44
<p>About my first Inertia.js app!</p>
55
</layout>
66
</template>

src/inertiajs-stubs/resources/js/Pages/Contact.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<template>
22
<layout>
3-
<h1>Contact</h1>
3+
<h1 class="text-2xl font-semibold mb-3">Contact</h1>
44
<p>Contact me about my first Inertia.js app!</p>
55
</layout>
66
</template>

src/inertiajs-stubs/resources/js/Pages/Welcome.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<template>
22
<layout>
3-
<h1>Welcome</h1>
3+
<h1 class="text-2xl font-semibold mb-3">Welcome</h1>
44
<p>Welcome to my first Inertia.js app!</p>
55
</layout>
66
</template>
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,23 @@
11
<template>
2-
<div>
3-
<header>
4-
<nav class="navbar navbar-expand-md navbar-light navbar-laravel">
5-
<div class="container">
6-
<inertia-link class="navbar-brand" href="/">Inertia.js</inertia-link>
2+
<div>
3+
<navbar />
74

8-
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#mainNav" aria-controls="mainNav" aria-expanded="false" aria-label="Toggle navigation">
9-
<span class="navbar-toggler-icon"></span>
10-
</button>
11-
12-
<div class="collapse navbar-collapse" id="mainNav">
5+
<main>
6+
<div class="w-full max-w-6xl mx-auto p-4">
7+
<article>
8+
<slot />
9+
</article>
10+
</div>
11+
</main>
12+
</div>
13+
</template>
1314

14-
<ul class="navbar-nav mr-auto">
15-
<li class="nav-item">
16-
<inertia-link class="nav-link" href="/about">About</inertia-link>
17-
</li>
18-
<li class="nav-item">
19-
<inertia-link class="nav-link" href="/contact">Contact</inertia-link>
20-
</li>
21-
</ul>
15+
<script>
16+
import Navbar from '@/Shared/Navbar'
2217
23-
</div>
24-
</div>
25-
</nav>
26-
</header>
27-
<main class="py-4">
28-
<div class="container">
29-
<div class="row">
30-
<div class="col">
31-
<article>
32-
<slot />
33-
</article>
34-
</div>
35-
</div>
36-
</div>
37-
</main>
38-
</div>
39-
</template>
18+
export default {
19+
components: {
20+
Navbar,
21+
}
22+
}
23+
</script>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<template>
2+
<header class="bg-white shadow">
3+
<div class="w-full max-w-6xl mx-auto sm:flex sm:justify-between sm:items-center sm:px-4 sm:py-3">
4+
<div class="flex justify-between items-center px-4 py-3 sm:p-0">
5+
<div>
6+
<inertia-link class="navbar-brand" href="/">Inertia.js</inertia-link>
7+
</div>
8+
9+
<div class="sm:hidden">
10+
<button @click="isOpen = !isOpen" type="button" class="block text-gray-700 hover:text-gray-600 focus:text-gray-600 focus:outline-none">
11+
<svg class="h-6 w-6 fill-current" viewBox="0 0 24 24">
12+
<path v-if="isOpen" fill-rule="evenodd" d="M18.278 16.864a1 1 0 0 1-1.414 1.414l-4.829-4.828-4.828 4.828a1 1 0 0 1-1.414-1.414l4.828-4.829-4.828-4.828a1 1 0 0 1 1.414-1.414l4.829 4.828 4.828-4.828a1 1 0 1 1 1.414 1.414l-4.828 4.829 4.828 4.828z"/>
13+
<path v-if="!isOpen" fill-rule="evenodd" d="M4 5h16a1 1 0 0 1 0 2H4a1 1 0 1 1 0-2zm0 6h16a1 1 0 0 1 0 2H4a1 1 0 0 1 0-2zm0 6h16a1 1 0 0 1 0 2H4a1 1 0 0 1 0-2z"/>
14+
</svg>
15+
</button>
16+
</div>
17+
</div>
18+
19+
<nav :class="isOpen ? 'block' : 'hidden'" class="px-2 pb-4 sm:flex sm:p-0">
20+
<inertia-link class="block px-2 py-1 text-gray-700 font-semibold rounded hover:bg-gray-100 focus:bg-gray-100 focus:outline-none" href="/about">About</inertia-link>
21+
<inertia-link class="mt-1 block px-2 py-1 text-gray-700 font-semibold rounded hover:bg-gray-100 focus:bg-gray-100 focus:outline-none sm:mt-0 sm:ml-2" href="/contact">Contact</inertia-link>
22+
</nav>
23+
</div>
24+
</header>
25+
</template>
26+
27+
<script>
28+
export default {
29+
data() {
30+
return {
31+
isOpen: false,
32+
}
33+
}
34+
}
35+
</script>
+3-9
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
11

2+
// Tailwind
3+
@import url('https://unpkg.com/tailwindcss@^1.0/dist/tailwind.min.css');
4+
25
// Fonts
36
@import url('https://fonts.googleapis.com/css?family=Nunito');
47

58
// Variables
6-
@import 'variables';
79
@import 'nprogress';
8-
9-
// Bootstrap
10-
@import '~bootstrap/scss/bootstrap';
11-
12-
.navbar-laravel {
13-
background-color: #fff;
14-
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.04);
15-
}

0 commit comments

Comments
 (0)