Skip to content

Commit

Permalink
Merge pull request #9 from laravel-frontend-presets/chore/laravel-6-c…
Browse files Browse the repository at this point in the history
…ompat

Update for Laravel 6
  • Loading branch information
michaeldyrynda authored Sep 23, 2019
2 parents 0facbe0 + 35ead49 commit 6c0a759
Show file tree
Hide file tree
Showing 11 changed files with 182 additions and 50 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ This preset scaffolding removes the manual steps required to get up and running

## Usage

1. Fresh install Laravel >= 5.8 and cd to your app.
2. Install this preset via `composer require laravel-frontend-presets/inertiajs`. Laravel will automatically discover this package. No need to register the service provider.
1. Fresh install Laravel >= 6.0 and cd to your app.
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.

### Installation

Expand Down
77 changes: 77 additions & 0 deletions src/InertiaJsPreset.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,23 @@

namespace LaravelFrontendPresets\InertiaJsPreset;

use Illuminate\Container\Container;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Foundation\Console\Presets\Preset;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Str;
use Symfony\Component\Process\Process;

class InertiaJsPreset extends Preset
{
public static function install()
{
static::updatePackages();
static::updateBootstrapping();
static::updateComposer(false);
static::publishServiceProvider();
static::registerInertiaServiceProvider();
static::updateWelcomePage();
static::updateGitignore();
static::scaffoldComponents();
Expand All @@ -25,10 +32,18 @@ protected static function updatePackageArray(array $packages)
'@babel/plugin-syntax-dynamic-import' => '^7.2.0',
'@inertiajs/inertia' => '^0.1.0',
'@inertiajs/inertia-vue' => '^0.1.0',
'vue' => '^2.5.17',
'vue-template-compiler' => '^2.6.10',
], $packages);
}

protected static function updateComposerArray(array $packages)
{
return array_merge([
'inertiajs/inertia-laravel' => '^0.1',
], $packages);
}

protected static function updateBootstrapping()
{
copy(__DIR__.'/inertiajs-stubs/webpack.mix.js', base_path('webpack.mix.js'));
Expand Down Expand Up @@ -70,4 +85,66 @@ protected static function scaffoldRoutes()
{
copy(__DIR__.'/inertiajs-stubs/routes/web.php', base_path('routes/web.php'));
}

protected static function updateComposer($dev = true)
{
if (! file_exists(base_path('composer.json'))) {
return;
}

$configurationKey = $dev ? 'require-dev' : 'require';

$packages = json_decode(file_get_contents(base_path('composer.json')), true);

$packages[$configurationKey] = static::updateComposerArray(
array_key_exists($configurationKey, $packages) ? $packages[$configurationKey] : [],
$configurationKey
);

ksort($packages[$configurationKey]);

file_put_contents(
base_path('composer.json'),
json_encode($packages, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT).PHP_EOL
);
}

public static function publishInertiaServiceProvider()
{
copy(
__DIR__.'/inertiajs-stubs/providers/InertiaJsServiceProvider.stub',
app_path('Providers/InertiaJsServiceProvider.php')
);
}

public static function registerInertiaServiceProvider()
{
$namespace = Str::replaceLast('\\', '', Container::getInstance()->getNamespace());

$appConfig = file_get_contents(config_path('app.php'));

if (Str::contains($appConfig, $namespace.'\\Providers\\InertiaJsServiceProvider::class')) {
return;
}

$lineEndingCount = [
"\r\n" => substr_count($appConfig, "\r\n"),
"\r" => substr_count($appConfig, "\r"),
"\n" => substr_count($appConfig, "\n"),
];

$eol = array_keys($lineEndingCount, max($lineEndingCount))[0];

file_put_contents(config_path('app.php'), str_replace(
"{$namespace}\\Providers\\RouteServiceProvider::class,".$eol,
"{$namespace}\\Providers\\RouteServiceProvider::class,".$eol." {$namespace}\Providers\InertiaJsServiceProvider::class,".$eol,
$appConfig
));

file_put_contents(app_path('Providers/InertiaJsServiceProvider.php'), str_replace(
"namespace App\Providers;",
"namespace {$namespace}\Providers;",
file_get_contents(app_path('Providers/InertiaJsServiceProvider.php'))
));
}
}
2 changes: 1 addition & 1 deletion src/InertiaJsPresetServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace LaravelFrontendPresets\InertiaJsPreset;

use Illuminate\Support\ServiceProvider;
use Illuminate\Foundation\Console\PresetCommand;
use Illuminate\Support\ServiceProvider;

class InertiaJsPresetServiceProvider extends ServiceProvider
{
Expand Down
1 change: 1 addition & 0 deletions src/inertiajs-stubs/gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/public/css
/public/js
/public/mix-manifest.json
41 changes: 41 additions & 0 deletions src/inertiajs-stubs/providers/InertiaJsServiceProvider.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace App\Providers;

use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\ServiceProvider;
use Inertia\Inertia;

class InertiaJsServiceProvider extends ServiceProvider
{
public function register()
{
Inertia::version(function () {
return md5_file(public_path('mix-manifest.json'));
});

Inertia::share([
'auth' => function () {
return [
'user' => Auth::user() ? [
'id' => Auth::user()->id,
'name' => Auth::user()->name,
'email' => Auth::user()->email,
'avatar' => Auth::user()->avatar(),
] : null,
];
},
'flash' => function () {
return [
'success' => Session::get('success'),
];
},
'errors' => function () {
return Session::get('errors')
? Session::get('errors')->getBag('default')->getMessages()
: (object) [];
},
]);
}
}
2 changes: 1 addition & 1 deletion src/inertiajs-stubs/resources/js/Pages/About.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<template>
<layout>
<h1>About</h1>
<h1 class="text-2xl font-semibold mb-3">About</h1>
<p>About my first Inertia.js app!</p>
</layout>
</template>
Expand Down
2 changes: 1 addition & 1 deletion src/inertiajs-stubs/resources/js/Pages/Contact.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<template>
<layout>
<h1>Contact</h1>
<h1 class="text-2xl font-semibold mb-3">Contact</h1>
<p>Contact me about my first Inertia.js app!</p>
</layout>
</template>
Expand Down
2 changes: 1 addition & 1 deletion src/inertiajs-stubs/resources/js/Pages/Welcome.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<template>
<layout>
<h1>Welcome</h1>
<h1 class="text-2xl font-semibold mb-3">Welcome</h1>
<p>Welcome to my first Inertia.js app!</p>
</layout>
</template>
Expand Down
54 changes: 19 additions & 35 deletions src/inertiajs-stubs/resources/js/Shared/Layout.vue
Original file line number Diff line number Diff line change
@@ -1,39 +1,23 @@
<template>
<div>
<header>
<nav class="navbar navbar-expand-md navbar-light navbar-laravel">
<div class="container">
<inertia-link class="navbar-brand" href="/">Inertia.js</inertia-link>
<div>
<navbar />

<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#mainNav" aria-controls="mainNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>

<div class="collapse navbar-collapse" id="mainNav">
<main>
<div class="w-full max-w-6xl mx-auto p-4">
<article>
<slot />
</article>
</div>
</main>
</div>
</template>

<ul class="navbar-nav mr-auto">
<li class="nav-item">
<inertia-link class="nav-link" href="/about">About</inertia-link>
</li>
<li class="nav-item">
<inertia-link class="nav-link" href="/contact">Contact</inertia-link>
</li>
</ul>
<script>
import Navbar from '@/Shared/Navbar'
</div>
</div>
</nav>
</header>
<main class="py-4">
<div class="container">
<div class="row">
<div class="col">
<article>
<slot />
</article>
</div>
</div>
</div>
</main>
</div>
</template>
export default {
components: {
Navbar,
}
}
</script>
35 changes: 35 additions & 0 deletions src/inertiajs-stubs/resources/js/Shared/Navbar.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<template>
<header class="bg-white shadow">
<div class="w-full max-w-6xl mx-auto sm:flex sm:justify-between sm:items-center sm:px-4 sm:py-3">
<div class="flex justify-between items-center px-4 py-3 sm:p-0">
<div>
<inertia-link class="navbar-brand" href="/">Inertia.js</inertia-link>
</div>

<div class="sm:hidden">
<button @click="isOpen = !isOpen" type="button" class="block text-gray-700 hover:text-gray-600 focus:text-gray-600 focus:outline-none">
<svg class="h-6 w-6 fill-current" viewBox="0 0 24 24">
<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"/>
<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"/>
</svg>
</button>
</div>
</div>

<nav :class="isOpen ? 'block' : 'hidden'" class="px-2 pb-4 sm:flex sm:p-0">
<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>
<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>
</nav>
</div>
</header>
</template>

<script>
export default {
data() {
return {
isOpen: false,
}
}
}
</script>
12 changes: 3 additions & 9 deletions src/inertiajs-stubs/resources/sass/app.scss
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@

// Tailwind
@import url('https://unpkg.com/tailwindcss@^1.0/dist/tailwind.min.css');

// Fonts
@import url('https://fonts.googleapis.com/css?family=Nunito');

// Variables
@import 'variables';
@import 'nprogress';

// Bootstrap
@import '~bootstrap/scss/bootstrap';

.navbar-laravel {
background-color: #fff;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.04);
}

0 comments on commit 6c0a759

Please sign in to comment.