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 1 commit
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
Prev Previous commit
Next Next commit
Replace InertiaServiceProvider with Middleware
  • Loading branch information
claudiodekker committed Oct 9, 2020
commit 74e9a6564a527feec5f80a98da91576fd2d50e8f
36 changes: 34 additions & 2 deletions src/Console/InstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -283,10 +283,13 @@ protected function installInertiaStack()

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

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

// Middleware
copy(__DIR__.'/../../stubs/app/Http/Middleware/PrepareInertia.php', app_path('Http/Middleware/PrepareInertia.php'));

$this->installMiddlewareAfter('SubstituteBindings::class', '\App\Http\Middleware\PrepareInertia::class');

// Models...
copy(__DIR__.'/../../stubs/app/Models/User.php', app_path('Models/User.php'));
Expand Down Expand Up @@ -428,6 +431,35 @@ protected function installServiceProviderAfter($after, $name)
}
}

/**
* 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 = ['.PHP_EOL), '];'.PHP_EOL);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Particularly here. On Windows I'm not sure this Str::after call will return what you want.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I think you're right.. I totally forgot about Windows doing things like this differently.

Let me see if I can adjust this.

$middlewareGroup = Str::before(Str::after($middlewareGroups, "'$group' => [".PHP_EOL), '],'.PHP_EOL);

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\PrepareInertia;
use Illuminate\Contracts\Http\Kernel;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\Facades\Route;
Expand Down Expand Up @@ -204,5 +205,9 @@ protected function bootInertia()

$kernel->appendMiddlewareToGroup('web', ShareInertiaData::class);
$kernel->appendToMiddlewarePriority(ShareInertiaData::class);

if (class_exists(PrepareInertia::class)) {
$kernel->appendToMiddlewarePriority(PrepareInertia::class);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,28 +1,19 @@
<?php

namespace App\Providers;
namespace App\Http\Middleware;

use Illuminate\Support\ServiceProvider;
use Inertia\Inertia;

class InertiaServiceProvider extends ServiceProvider
class PrepareInertia
{
/**
* Register any authentication / authorization services.
* Handle the incoming request.
*
* @return void
* @param \Illuminate\Http\Request $request
* @param callable $next
* @return \Illuminate\Http\Response
*/
public function boot()
{
$this->configureVersioning();
}

/**
* Enables Inertia automatic asset cache busting.
*
* @return void
*/
protected function configureVersioning()
public function handle($request, $next)
{
Inertia::version(function () {
// When we are running on Laravel Vapor, asset URLs are automatically updated
Expand All @@ -37,8 +28,8 @@ protected function configureVersioning()
if (file_exists($manifest = public_path('mix-manifest.json'))) {
return md5_file($manifest);
}

return null;
});

return $next($request);
}
}