Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[2.x] Inertia: Automatically publish & register Inertia Middleware #327

Merged
merged 17 commits into from
Oct 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 51 additions & 30 deletions src/Console/InstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function handle()
}

// Fortify Provider...
$this->installFortifyServiceProvider();
$this->installServiceProviderAfter('RouteServiceProvider', 'FortifyServiceProvider');

// Configure Session...
$this->configureSession();
Expand All @@ -69,22 +69,6 @@ public function handle()
}
}

/**
* Install the Fortify service providers in the application configuration file.
*
* @return void
*/
protected function installFortifyServiceProvider()
{
if (! Str::contains($appConfig = file_get_contents(config_path('app.php')), 'App\\Providers\\FortifyServiceProvider::class')) {
file_put_contents(config_path('app.php'), str_replace(
"App\\Providers\RouteServiceProvider::class,",
"App\\Providers\RouteServiceProvider::class,".PHP_EOL." App\Providers\FortifyServiceProvider::class,",
$appConfig
));
}
}

/**
* Configure the session driver for Jetstream.
*
Expand Down Expand Up @@ -141,8 +125,7 @@ protected function installLivewireStack()

// Tailwind Configuration...
copy(__DIR__.'/../../stubs/livewire/tailwind.config.js', base_path('tailwind.config.js'));
copy(__DIR__.'/../../stubs/webpack.config.js', base_path('webpack.config.js'));
copy(__DIR__.'/../../stubs/webpack.mix.js', base_path('webpack.mix.js'));
copy(__DIR__.'/../../stubs/livewire/webpack.mix.js', base_path('webpack.mix.js'));

// Directories...
(new Filesystem)->ensureDirectoryExists(app_path('Actions/Fortify'));
Expand All @@ -159,8 +142,7 @@ protected function installLivewireStack()

// Service Providers...
copy(__DIR__.'/../../stubs/app/Providers/JetstreamServiceProvider.php', app_path('Providers/JetstreamServiceProvider.php'));

$this->installJetstreamServiceProvider();
$this->installServiceProviderAfter('FortifyServiceProvider', 'JetstreamServiceProvider');

// Models...
copy(__DIR__.'/../../stubs/app/Models/User.php', app_path('Models/User.php'));
Expand Down Expand Up @@ -248,7 +230,7 @@ protected function livewireRouteDefinition()
protected function installInertiaStack()
{
// Install Inertia...
(new Process(['composer', 'require', 'inertiajs/inertia-laravel', 'laravel/sanctum:^2.6', 'tightenco/ziggy'], base_path()))
(new Process(['composer', 'require', 'inertiajs/inertia-laravel:^0.3', 'laravel/sanctum:^2.6', 'tightenco/ziggy'], base_path()))
->setTimeout(null)
->run(function ($type, $output) {
$this->output->write($output);
Expand Down Expand Up @@ -281,8 +263,8 @@ protected function installInertiaStack()

// Tailwind Configuration...
copy(__DIR__.'/../../stubs/inertia/tailwind.config.js', base_path('tailwind.config.js'));
copy(__DIR__.'/../../stubs/webpack.config.js', base_path('webpack.config.js'));
copy(__DIR__.'/../../stubs/webpack.mix.js', base_path('webpack.mix.js'));
copy(__DIR__.'/../../stubs/inertia/webpack.mix.js', base_path('webpack.mix.js'));
copy(__DIR__.'/../../stubs/inertia/webpack.config.js', base_path('webpack.config.js'));

// Directories...
(new Filesystem)->ensureDirectoryExists(app_path('Actions/Fortify'));
Expand All @@ -303,7 +285,15 @@ protected function installInertiaStack()
// Service Providers...
copy(__DIR__.'/../../stubs/app/Providers/JetstreamServiceProvider.php', app_path('Providers/JetstreamServiceProvider.php'));

$this->installJetstreamServiceProvider();
$this->installServiceProviderAfter('FortifyServiceProvider', 'JetstreamServiceProvider');

// Middleware
(new Process(['php', 'artisan', 'inertia:middleware', 'HandleInertiaRequests', '--force'], base_path()))
->setTimeout(null)
->run(function ($type, $output) {
$this->output->write($output);
});
$this->installMiddlewareAfter('SubstituteBindings::class', '\App\Http\Middleware\HandleInertiaRequests::class');

// Models...
copy(__DIR__.'/../../stubs/app/Models/User.php', app_path('Models/User.php'));
Expand Down Expand Up @@ -424,21 +414,52 @@ protected function ensureApplicationIsTeamCompatible()
}

/**
* Install the Jetstream service providers in the application configuration file.
* Install the service provider in the application configuration file.
*
* @param string $after
* @param string $name
* @return void
*/
protected function installJetstreamServiceProvider()
protected function installServiceProviderAfter($after, $name)
{
if (! Str::contains($appConfig = file_get_contents(config_path('app.php')), 'App\\Providers\\JetstreamServiceProvider::class')) {
if (! Str::contains($appConfig = file_get_contents(config_path('app.php')), 'App\\Providers\\'.$name.'::class')) {
file_put_contents(config_path('app.php'), str_replace(
"App\\Providers\FortifyServiceProvider::class,",
"App\\Providers\FortifyServiceProvider::class,".PHP_EOL." App\Providers\JetstreamServiceProvider::class,",
'App\\Providers\\'.$after.'::class,',
'App\\Providers\\'.$after.'::class,'.PHP_EOL.' App\\Providers\\'.$name.'::class,',
$appConfig
));
}
}

/**
* Install the middleware to a group in the application Http Kernel.
*
* @param string $after
* @param string $name
* @param string $group
* @return void
*/
protected function installMiddlewareAfter($after, $name, $group = 'web')
{
$httpKernel = file_get_contents(app_path('Http/Kernel.php'));
$middlewareGroups = Str::before(Str::after($httpKernel, '$middlewareGroups = ['), '];');
$middlewareGroup = Str::before(Str::after($middlewareGroups, "'$group' => ["), '],');

if (! Str::contains($middlewareGroup, $name)) {
$modifiedMiddlewareGroup = str_replace(
$after.',',
$after.','.PHP_EOL.' '.$name.',',
$middlewareGroup,
);

file_put_contents(app_path('Http/Kernel.php'), str_replace(
$middlewareGroups,
str_replace($middlewareGroup, $modifiedMiddlewareGroup, $middlewareGroups),
$httpKernel
));
}
}

/**
* Update the "package.json" file.
*
Expand Down
5 changes: 5 additions & 0 deletions src/JetstreamServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Laravel\Jetstream;

use App\Http\Middleware\HandleInertiaRequests;
use Illuminate\Contracts\Http\Kernel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Blade;
Expand Down Expand Up @@ -206,6 +207,10 @@ protected function bootInertia()
$kernel->appendMiddlewareToGroup('web', ShareInertiaData::class);
$kernel->appendToMiddlewarePriority(ShareInertiaData::class);

if (class_exists(HandleInertiaRequests::class)) {
$kernel->appendToMiddlewarePriority(HandleInertiaRequests::class);
}

Fortify::loginView(function () {
return Inertia::render('Auth/Login', [
'canResetPassword' => Route::has('password.request'),
Expand Down
File renamed without changes.
20 changes: 20 additions & 0 deletions stubs/inertia/webpack.mix.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const mix = require('laravel-mix');

/*
|--------------------------------------------------------------------------
| Mix Asset Management
|--------------------------------------------------------------------------
|
| Mix provides a clean, fluent API for defining some Webpack build steps
| for your Laravel applications. By default, we are compiling the CSS
| file for the application as well as bundling up all the JS files.
|
*/

mix.js('resources/js/app.js', 'public/js')
.postCss('resources/css/app.css', 'public/css', [
require('postcss-import'),
require('tailwindcss'),
])
.webpackConfig(require('./webpack.config'))
.version();
3 changes: 1 addition & 2 deletions stubs/webpack.mix.js → stubs/livewire/webpack.mix.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,4 @@ mix.js('resources/js/app.js', 'public/js')
.postCss('resources/css/app.css', 'public/css', [
require('postcss-import'),
require('tailwindcss'),
])
.webpackConfig(require('./webpack.config'));
]);