Skip to content

Commit 84409b2

Browse files
committed
Simplify Middleware generator
1 parent 27783bc commit 84409b2

File tree

2 files changed

+27
-29
lines changed

2 files changed

+27
-29
lines changed

src/Console/CreateMiddleware.php

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@
22

33
namespace Inertia\Console;
44

5-
use Illuminate\Console\Command;
6-
use Throwable;
7-
use View;
5+
use Illuminate\Console\GeneratorCommand;
86

9-
class CreateMiddleware extends Command
7+
class CreateMiddleware extends GeneratorCommand
108
{
119
/**
1210
* The name and signature of the console command.
@@ -23,30 +21,30 @@ class CreateMiddleware extends Command
2321
protected $description = 'Creates a new Inertia middleware';
2422

2523
/**
26-
* Create a new command instance.
24+
* The type of class being generated.
2725
*
28-
* @return void
26+
* @var string
2927
*/
30-
public function __construct()
31-
{
32-
parent::__construct();
28+
protected $type = 'Middleware';
3329

34-
View::addNamespace('inertia', __DIR__.'/../../stubs');
30+
/**
31+
* Get the stub file for the generator.
32+
*
33+
* @return string
34+
*/
35+
protected function getStub()
36+
{
37+
return __DIR__.'/../../stubs/middleware.stub';
3538
}
3639

3740
/**
38-
* Execute the console command.
41+
* Get the default namespace for the class.
3942
*
40-
* @return void
41-
* @throws Throwable
43+
* @param string $rootNamespace
44+
* @return string
4245
*/
43-
public function handle()
46+
protected function getDefaultNamespace($rootNamespace)
4447
{
45-
file_put_contents(
46-
app_path('Http/Middleware/'.$this->argument('name').'.php'),
47-
view('inertia::Middleware', ['name' => $this->argument('name')])->render()
48-
);
49-
50-
$this->info('Inertia middleware ['.$this->argument('name').'] created successfully.');
48+
return $rootNamespace.'\Http\Middleware';
5149
}
5250
}

stubs/Middleware.blade.php renamed to stubs/middleware.stub

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
{!! '<?php' !!}
1+
<?php
22

3-
namespace App\Http\Middleware;
3+
namespace {{ namespace }};
44

55
use Illuminate\Http\Request;
66
use Inertia\Middleware;
77

8-
class {{ $name }} extends Middleware
8+
class {{ class }} extends Middleware
99
{
1010
/**
1111
* Determines the current asset version.
1212
*
13-
* {{ '@' }}see https://inertiajs.com/asset-versioning
14-
* {{ '@' }}param \Illuminate\Http\Request $request
15-
* {{ '@' }}return string|null
13+
* @see https://inertiajs.com/asset-versioning
14+
* @param \Illuminate\Http\Request $request
15+
* @return string|null
1616
*/
1717
public function version(Request $request)
1818
{
@@ -22,9 +22,9 @@ public function version(Request $request)
2222
/**
2323
* Defines the props that are shared by default.
2424
*
25-
* {{ '@' }}see https://inertiajs.com/shared-data
26-
* {{ '@' }}param \Illuminate\Http\Request $request
27-
* {{ '@' }}return array
25+
* @see https://inertiajs.com/shared-data
26+
* @param \Illuminate\Http\Request $request
27+
* @return array
2828
*/
2929
public function share(Request $request)
3030
{

0 commit comments

Comments
 (0)