From 42ed5ba86ee87d761b145d1a04562bf38c6eafb3 Mon Sep 17 00:00:00 2001 From: Davey Shafik Date: Tue, 1 Oct 2024 07:19:28 -0700 Subject: [PATCH] [11.x] Add `make:job-middleware` artisan command (#52965) --- .../Console/JobMiddlewareMakeCommand.php | 81 +++++++++++++++++++ .../Console/stubs/job.middleware.stub | 18 +++++ .../Providers/ArtisanServiceProvider.php | 14 ++++ .../Routing/Console/MiddlewareMakeCommand.php | 2 +- .../JobMiddlewareMakeCommandTest.php | 33 ++++++++ 5 files changed, 147 insertions(+), 1 deletion(-) create mode 100644 src/Illuminate/Foundation/Console/JobMiddlewareMakeCommand.php create mode 100644 src/Illuminate/Foundation/Console/stubs/job.middleware.stub create mode 100644 tests/Integration/Generators/JobMiddlewareMakeCommandTest.php diff --git a/src/Illuminate/Foundation/Console/JobMiddlewareMakeCommand.php b/src/Illuminate/Foundation/Console/JobMiddlewareMakeCommand.php new file mode 100644 index 000000000000..b3ccf8e45ce4 --- /dev/null +++ b/src/Illuminate/Foundation/Console/JobMiddlewareMakeCommand.php @@ -0,0 +1,81 @@ +resolveStubPath('/stubs/job.middleware.stub'); + } + + /** + * Resolve the fully-qualified path to the stub. + * + * @param string $stub + * @return string + */ + protected function resolveStubPath($stub) + { + return file_exists($customPath = $this->laravel->basePath(trim($stub, '/'))) + ? $customPath + : __DIR__.$stub; + } + + /** + * Get the default namespace for the class. + * + * @param string $rootNamespace + * @return string + */ + protected function getDefaultNamespace($rootNamespace) + { + return $rootNamespace.'\Jobs\Middleware'; + } + + /** + * Get the console command options. + * + * @return array + */ + protected function getOptions() + { + return [ + ['force', 'f', InputOption::VALUE_NONE, 'Create the class even if the job middleware already exists'], + ]; + } +} diff --git a/src/Illuminate/Foundation/Console/stubs/job.middleware.stub b/src/Illuminate/Foundation/Console/stubs/job.middleware.stub new file mode 100644 index 000000000000..391e8eb6aa42 --- /dev/null +++ b/src/Illuminate/Foundation/Console/stubs/job.middleware.stub @@ -0,0 +1,18 @@ + FactoryMakeCommand::class, 'InterfaceMake' => InterfaceMakeCommand::class, 'JobMake' => JobMakeCommand::class, + 'JobMiddlewareMake' => JobMiddlewareMakeCommand::class, 'LangPublish' => LangPublishCommand::class, 'ListenerMake' => ListenerMakeCommand::class, 'MailMake' => MailMakeCommand::class, @@ -506,6 +508,18 @@ protected function registerJobMakeCommand() }); } + /** + * Register the command. + * + * @return void + */ + protected function registerJobMiddlewareMakeCommand() + { + $this->app->singleton(JobMiddlewareMakeCommand::class, function ($app) { + return new JobMiddlewareMakeCommand($app['files']); + }); + } + /** * Register the command. * diff --git a/src/Illuminate/Routing/Console/MiddlewareMakeCommand.php b/src/Illuminate/Routing/Console/MiddlewareMakeCommand.php index 058109219643..3a9905f67978 100644 --- a/src/Illuminate/Routing/Console/MiddlewareMakeCommand.php +++ b/src/Illuminate/Routing/Console/MiddlewareMakeCommand.php @@ -23,7 +23,7 @@ class MiddlewareMakeCommand extends GeneratorCommand * * @var string */ - protected $description = 'Create a new middleware class'; + protected $description = 'Create a new HTTP middleware class'; /** * The type of class being generated. diff --git a/tests/Integration/Generators/JobMiddlewareMakeCommandTest.php b/tests/Integration/Generators/JobMiddlewareMakeCommandTest.php new file mode 100644 index 000000000000..9a9e9b43c854 --- /dev/null +++ b/tests/Integration/Generators/JobMiddlewareMakeCommandTest.php @@ -0,0 +1,33 @@ +artisan('make:job-middleware', ['name' => 'Foo']) + ->assertExitCode(0); + + $this->assertFileContains([ + 'namespace App\Jobs\Middleware;', + 'class Foo', + ], 'app/Jobs/Middleware/Foo.php'); + + $this->assertFilenameNotExists('tests/Feature/Jobs/Middleware/FooTest.php'); + } + + public function testItCanGenerateJobFileWithTest() + { + $this->artisan('make:job-middleware', ['name' => 'Foo', '--test' => true]) + ->assertExitCode(0); + + $this->assertFilenameExists('app/Jobs/Middleware/Foo.php'); + $this->assertFilenameExists('tests/Feature/Jobs/Middleware/FooTest.php'); + } +}