Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[11.x] Add make:job-middleware artisan command #52965

Open
wants to merge 1 commit into
base: 11.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions src/Illuminate/Foundation/Console/JobMiddlewareMakeCommand.php
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 src/Illuminate/Foundation/Console/stubs/job.middleware.stub
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);
}
}
14 changes: 14 additions & 0 deletions src/Illuminate/Foundation/Providers/ArtisanServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
use Illuminate\Foundation\Console\ExceptionMakeCommand;
use Illuminate\Foundation\Console\InterfaceMakeCommand;
use Illuminate\Foundation\Console\JobMakeCommand;
use Illuminate\Foundation\Console\JobMiddlewareMakeCommand;
use Illuminate\Foundation\Console\KeyGenerateCommand;
use Illuminate\Foundation\Console\LangPublishCommand;
use Illuminate\Foundation\Console\ListenerMakeCommand;
Expand Down Expand Up @@ -199,6 +200,7 @@ class ArtisanServiceProvider extends ServiceProvider implements DeferrableProvid
'FactoryMake' => FactoryMakeCommand::class,
'InterfaceMake' => InterfaceMakeCommand::class,
'JobMake' => JobMakeCommand::class,
'JobMiddlewareMake' => JobMiddlewareMakeCommand::class,
'LangPublish' => LangPublishCommand::class,
'ListenerMake' => ListenerMakeCommand::class,
'MailMake' => MailMakeCommand::class,
Expand Down Expand Up @@ -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.
*
Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Routing/Console/MiddlewareMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
33 changes: 33 additions & 0 deletions tests/Integration/Generators/JobMiddlewareMakeCommandTest.php
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');
}
}