Skip to content
This repository was archived by the owner on Jul 16, 2021. It is now read-only.
This repository was archived by the owner on Jul 16, 2021. It is now read-only.

Change artisan console to only fire constructors for the command being called #1399

@samoldenburg

Description

@samoldenburg

Posted here as suggested from my issue in laravel/framework: laravel/framework#26570

Discussion detailed here: https://laracasts.com/discuss/channels/laravel/all-artisan-command-constructors-are-fired-when-running-any-artisan-commandwhat

In short, when running any artisan command, all artisan commands are instantiated to pull the command signature out, which is a protected property.

This is notably an issue if you use dependency injection in your constructors. In an application with many commands and dependencies, the artisan console will noticeably slow down.

This can be mitigated by resolving dependencies out of the container via $dependency = app(Dependency::class) in the handle() method, or by using dependency injection in the handle method rather than using dependency injection in the constructor, but the documentation specifically shows using dependency injection in the constructor with no warning about the repercussions of doing so. I have one or two particularly slow dependencies in my application which are used in only a couple artisan commands, yet all console methods and unit tests are slowed down as a result.

It would be great if the command signature could be delcared as public static or something so that instantiating the constructor for all artisan commands does not happen all the time. This would improve performance in (at least) 3 key areas for applications that use dependency injection in the constructor for their artisan console commands:

  1. Artisan console
  2. Programatically executing commands via Artisan::call()
  3. Running unit tests that use database seeding

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions