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:
- Artisan console
- Programatically executing commands via
Artisan::call()
- Running unit tests that use database seeding