Skip to content

Commit 42ed5ba

Browse files
authored
[11.x] Add make:job-middleware artisan command (#52965)
1 parent 045bc79 commit 42ed5ba

File tree

5 files changed

+147
-1
lines changed

5 files changed

+147
-1
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
3+
namespace Illuminate\Foundation\Console;
4+
5+
use Illuminate\Console\Concerns\CreatesMatchingTest;
6+
use Illuminate\Console\GeneratorCommand;
7+
use Symfony\Component\Console\Attribute\AsCommand;
8+
use Symfony\Component\Console\Input\InputOption;
9+
10+
#[AsCommand(name: 'make:job-middleware')]
11+
class JobMiddlewareMakeCommand extends GeneratorCommand
12+
{
13+
use CreatesMatchingTest;
14+
15+
/**
16+
* The console command name.
17+
*
18+
* @var string
19+
*/
20+
protected $name = 'make:job-middleware';
21+
22+
/**
23+
* The console command description.
24+
*
25+
* @var string
26+
*/
27+
protected $description = 'Create a new job middleware class';
28+
29+
/**
30+
* The type of class being generated.
31+
*
32+
* @var string
33+
*/
34+
protected $type = 'Middleware';
35+
36+
/**
37+
* Get the stub file for the generator.
38+
*
39+
* @return string
40+
*/
41+
protected function getStub()
42+
{
43+
return $this->resolveStubPath('/stubs/job.middleware.stub');
44+
}
45+
46+
/**
47+
* Resolve the fully-qualified path to the stub.
48+
*
49+
* @param string $stub
50+
* @return string
51+
*/
52+
protected function resolveStubPath($stub)
53+
{
54+
return file_exists($customPath = $this->laravel->basePath(trim($stub, '/')))
55+
? $customPath
56+
: __DIR__.$stub;
57+
}
58+
59+
/**
60+
* Get the default namespace for the class.
61+
*
62+
* @param string $rootNamespace
63+
* @return string
64+
*/
65+
protected function getDefaultNamespace($rootNamespace)
66+
{
67+
return $rootNamespace.'\Jobs\Middleware';
68+
}
69+
70+
/**
71+
* Get the console command options.
72+
*
73+
* @return array
74+
*/
75+
protected function getOptions()
76+
{
77+
return [
78+
['force', 'f', InputOption::VALUE_NONE, 'Create the class even if the job middleware already exists'],
79+
];
80+
}
81+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace {{ namespace }};
4+
5+
use Closure;
6+
7+
class {{ class }}
8+
{
9+
/**
10+
* Process the queued job.
11+
*
12+
* @param \Closure(object): void $next
13+
*/
14+
public function handle(object $job, Closure $next): void
15+
{
16+
$next($job);
17+
}
18+
}

src/Illuminate/Foundation/Providers/ArtisanServiceProvider.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
use Illuminate\Foundation\Console\ExceptionMakeCommand;
5757
use Illuminate\Foundation\Console\InterfaceMakeCommand;
5858
use Illuminate\Foundation\Console\JobMakeCommand;
59+
use Illuminate\Foundation\Console\JobMiddlewareMakeCommand;
5960
use Illuminate\Foundation\Console\KeyGenerateCommand;
6061
use Illuminate\Foundation\Console\LangPublishCommand;
6162
use Illuminate\Foundation\Console\ListenerMakeCommand;
@@ -199,6 +200,7 @@ class ArtisanServiceProvider extends ServiceProvider implements DeferrableProvid
199200
'FactoryMake' => FactoryMakeCommand::class,
200201
'InterfaceMake' => InterfaceMakeCommand::class,
201202
'JobMake' => JobMakeCommand::class,
203+
'JobMiddlewareMake' => JobMiddlewareMakeCommand::class,
202204
'LangPublish' => LangPublishCommand::class,
203205
'ListenerMake' => ListenerMakeCommand::class,
204206
'MailMake' => MailMakeCommand::class,
@@ -506,6 +508,18 @@ protected function registerJobMakeCommand()
506508
});
507509
}
508510

511+
/**
512+
* Register the command.
513+
*
514+
* @return void
515+
*/
516+
protected function registerJobMiddlewareMakeCommand()
517+
{
518+
$this->app->singleton(JobMiddlewareMakeCommand::class, function ($app) {
519+
return new JobMiddlewareMakeCommand($app['files']);
520+
});
521+
}
522+
509523
/**
510524
* Register the command.
511525
*

src/Illuminate/Routing/Console/MiddlewareMakeCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class MiddlewareMakeCommand extends GeneratorCommand
2323
*
2424
* @var string
2525
*/
26-
protected $description = 'Create a new middleware class';
26+
protected $description = 'Create a new HTTP middleware class';
2727

2828
/**
2929
* The type of class being generated.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace Illuminate\Tests\Integration\Generators;
4+
5+
class JobMiddlewareMakeCommandTest extends TestCase
6+
{
7+
protected $files = [
8+
'app/Jobs/Middleware/Foo.php',
9+
'tests/Feature/Jobs/Middleware/FooTest.php',
10+
];
11+
12+
public function testItCanGenerateJobFile()
13+
{
14+
$this->artisan('make:job-middleware', ['name' => 'Foo'])
15+
->assertExitCode(0);
16+
17+
$this->assertFileContains([
18+
'namespace App\Jobs\Middleware;',
19+
'class Foo',
20+
], 'app/Jobs/Middleware/Foo.php');
21+
22+
$this->assertFilenameNotExists('tests/Feature/Jobs/Middleware/FooTest.php');
23+
}
24+
25+
public function testItCanGenerateJobFileWithTest()
26+
{
27+
$this->artisan('make:job-middleware', ['name' => 'Foo', '--test' => true])
28+
->assertExitCode(0);
29+
30+
$this->assertFilenameExists('app/Jobs/Middleware/Foo.php');
31+
$this->assertFilenameExists('tests/Feature/Jobs/Middleware/FooTest.php');
32+
}
33+
}

0 commit comments

Comments
 (0)