-
Notifications
You must be signed in to change notification settings - Fork 11.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[11.x] Add
make:job-middleware
artisan command (#52965)
- Loading branch information
Showing
5 changed files
with
147 additions
and
1 deletion.
There are no files selected for viewing
81 changes: 81 additions & 0 deletions
81
src/Illuminate/Foundation/Console/JobMiddlewareMakeCommand.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?php | ||
|
||
namespace Illuminate\Foundation\Console; | ||
|
||
use Illuminate\Console\Concerns\CreatesMatchingTest; | ||
use Illuminate\Console\GeneratorCommand; | ||
use Symfony\Component\Console\Attribute\AsCommand; | ||
use Symfony\Component\Console\Input\InputOption; | ||
|
||
#[AsCommand(name: 'make:job-middleware')] | ||
class JobMiddlewareMakeCommand extends GeneratorCommand | ||
{ | ||
use CreatesMatchingTest; | ||
|
||
/** | ||
* The console command name. | ||
* | ||
* @var string | ||
*/ | ||
protected $name = 'make:job-middleware'; | ||
|
||
/** | ||
* The console command description. | ||
* | ||
* @var string | ||
*/ | ||
protected $description = 'Create a new job middleware class'; | ||
|
||
/** | ||
* The type of class being generated. | ||
* | ||
* @var string | ||
*/ | ||
protected $type = 'Middleware'; | ||
|
||
/** | ||
* Get the stub file for the generator. | ||
* | ||
* @return string | ||
*/ | ||
protected function getStub() | ||
{ | ||
return $this->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'], | ||
]; | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/Illuminate/Foundation/Console/stubs/job.middleware.stub
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace {{ namespace }}; | ||
|
||
use Closure; | ||
|
||
class {{ class }} | ||
{ | ||
/** | ||
* Process the queued job. | ||
* | ||
* @param \Closure(object): void $next | ||
*/ | ||
public function handle(object $job, Closure $next): void | ||
{ | ||
$next($job); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
tests/Integration/Generators/JobMiddlewareMakeCommandTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
namespace Illuminate\Tests\Integration\Generators; | ||
|
||
class JobMiddlewareMakeCommandTest extends TestCase | ||
{ | ||
protected $files = [ | ||
'app/Jobs/Middleware/Foo.php', | ||
'tests/Feature/Jobs/Middleware/FooTest.php', | ||
]; | ||
|
||
public function testItCanGenerateJobFile() | ||
{ | ||
$this->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'); | ||
} | ||
} |