Skip to content

All artisan command constructors are fired when running any artisan command #26570

@samoldenburg

Description

@samoldenburg
  • Laravel Version: 5.7 (but should be reproducible going back much further)
  • PHP Version: 7.2
  • Database Driver & Version: not applicable

Description:

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

This is basically the same as #13789 but I would like to at least attempt to re-open this for discussion as that issue is several years old.

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, 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.

Steps To Reproduce:

See discussion in forums: https://laracasts.com/discuss/channels/laravel/all-artisan-command-constructors-are-fired-when-running-any-artisan-commandwhat

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