Skip to content

Commit 60a50f3

Browse files
committed
Adds the middleware automatically
1 parent aa6658c commit 60a50f3

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

src/Commands/InstallCommand.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Illuminate\Console\Command;
66
use Illuminate\Support\Facades\File;
7+
use Illuminate\Support\Str;
78
use Symfony\Component\Console\Terminal;
89

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

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

126+
/**
127+
* Install the middleware to a group in the application Http Kernel.
128+
*
129+
* @param string $after
130+
* @param string $name
131+
* @param string $group
132+
* @return void
133+
*/
134+
private function installMiddlewareAfter($after, $name, $group = 'web')
135+
{
136+
$httpKernel = file_get_contents(app_path('Http/Kernel.php'));
137+
138+
$middlewareGroups = Str::before(Str::after($httpKernel, '$middlewareGroups = ['), '];');
139+
$middlewareGroup = Str::before(Str::after($middlewareGroups, "'$group' => ["), '],');
140+
141+
if (! Str::contains($middlewareGroup, $name)) {
142+
$modifiedMiddlewareGroup = str_replace(
143+
$after.',',
144+
$after.','.PHP_EOL.' '.$name.',',
145+
$middlewareGroup,
146+
);
147+
148+
file_put_contents(app_path('Http/Kernel.php'), str_replace(
149+
$middlewareGroups,
150+
str_replace($middlewareGroup, $modifiedMiddlewareGroup, $middlewareGroups),
151+
$httpKernel
152+
));
153+
}
154+
}
155+
124156
private function replaceMixStylesToLayouts()
125157
{
126158
$this->existingLayoutFiles()

0 commit comments

Comments
 (0)