4
4
5
5
use Illuminate \Console \Command ;
6
6
use Illuminate \Support \Facades \File ;
7
+ use Illuminate \Support \Str ;
7
8
use Symfony \Component \Console \Terminal ;
8
9
9
10
class InstallCommand extends Command
@@ -25,6 +26,7 @@ public function handle()
25
26
$ this ->ensureTailwindConfigExists ();
26
27
$ this ->ensureTailwindCliBinaryExists ();
27
28
$ this ->addImportStylesToLayouts ();
29
+ $ this ->installMiddlewareAfter ('SubstituteBindings::class ' , '\Tonysm\TailwindCss\Http\Middleware\AddLinkHeaderForPreloadedAssets::class ' );
28
30
$ this ->addIngoreLines ();
29
31
$ this ->runFirstBuild ();
30
32
@@ -121,6 +123,36 @@ private function addImportStylesToLayouts()
121
123
});
122
124
}
123
125
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
+
124
156
private function replaceMixStylesToLayouts ()
125
157
{
126
158
$ this ->existingLayoutFiles ()
0 commit comments