Skip to content

Commit

Permalink
Adds the middleware automatically
Browse files Browse the repository at this point in the history
tonysm committed Jan 12, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent aa6658c commit 60a50f3
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/Commands/InstallCommand.php
Original file line number Diff line number Diff line change
@@ -4,6 +4,7 @@

use Illuminate\Console\Command;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;
use Symfony\Component\Console\Terminal;

class InstallCommand extends Command
@@ -25,6 +26,7 @@ public function handle()
$this->ensureTailwindConfigExists();
$this->ensureTailwindCliBinaryExists();
$this->addImportStylesToLayouts();
$this->installMiddlewareAfter('SubstituteBindings::class', '\Tonysm\TailwindCss\Http\Middleware\AddLinkHeaderForPreloadedAssets::class');
$this->addIngoreLines();
$this->runFirstBuild();

@@ -121,6 +123,36 @@ private function addImportStylesToLayouts()
});
}

/**
* Install the middleware to a group in the application Http Kernel.
*
* @param string $after
* @param string $name
* @param string $group
* @return void
*/
private 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
));
}
}

private function replaceMixStylesToLayouts()
{
$this->existingLayoutFiles()

0 comments on commit 60a50f3

Please sign in to comment.