Skip to content

Conversation

@amirhshokri
Copy link
Contributor

@amirhshokri amirhshokri commented Aug 3, 2025

This PR adds support for passing instantiated command objects (extending Symfony\Component\Console\Command\Command) directly to:

Schedule::command(...)
Artisan::call(...)

Why?

Currently, when working with Artisan commands, developers must pass either the command signature (a string) or the class name (e.g., TestCommand::class), which Laravel resolves from the container.

  • A similar pattern exists in Schedule::job() method, which accepts either a job class instance or a job class name. If a class name is provided, Laravel resolves it from the container:

https://github.com/laravel/framework/blob/12.x/src/Illuminate/Console/Scheduling/Schedule.php#L179

  • Laravel already supports working with actual command instances internally. For example, the call() method from the src/Illuminate/Console/Concerns/CallsCommands.php trait (used in the Illuminate\Console\Command class) allows this:
class TestCommand extends Command
{
  public function handle()
  {
      $this->call(new AnotherTestCommand);
  }
}

https://github.com/laravel/framework/blob/12.x/src/Illuminate/Console/Concerns/CallsCommands.php#L27

  • Additionally, this behavior is supported in the parseCommand() method in src/Illuminate/Console/Application.php, which checks if the argument is a subclass of SymfonyCommand. However, this logic was not accessible through higher-level APIs like Artisan::call():

https://github.com/laravel/framework/blob/12.x/src/Illuminate/Console/Application.php#L177

Changes

Before:

Schedule::command(TestCommand::class);
Artisan::call(AnotherTestCommand::class);

Now also supported:

Schedule::command(new TestCommand);
Artisan::call(new AnotherTestCommand);

@amirhshokri amirhshokri changed the title [12.x] Allow passing Symfony Command instances directly to Schedule::command() and Artisan::call() [12.x] Allow passing command class instances directly to Schedule::command() and Artisan::call() Aug 3, 2025
@taylorotwell taylorotwell merged commit d70e372 into laravel:12.x Aug 4, 2025
60 checks passed
@amirhshokri amirhshokri deleted the 12.x-run-command-using-command-class-object branch August 4, 2025 20:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants