laravel / framework

The Laravel Framework.
https://laravel.com
MIT License
32.54k stars 11.02k forks source link

ServiceProvider boot() method optimization #10492

Closed antonkomarev closed 9 years ago

antonkomarev commented 9 years ago

Hello, guys!

Because that all code inside ServiceProviders boot() methods is calling all the time in all requests, it even trying to publish migrations, configs and in some of packages it doing some kind of preparations of files before publishing or doing other stuff which isn't needed for production usage and should be used only in console. Am I missed something?

If it's true, what do you think about optimization of this behaviour by adding a helper methods to separate blocks on code for only console usage in boot method or at least write about it in documentation. Because I haven't seen any kind of checks for such thing in any of packages I've used recently.

brunogaspar commented 9 years ago

You can pretty much check if the app is running on console or not by doing something like

if ($this->app->runningInConsole()) { 
   // stuff that should run only on console here 
}
GrahamCampbell commented 9 years ago

https://github.com/laravel/spark/blob/master/app/Providers/SparkServiceProvider.php#L59

GrahamCampbell commented 9 years ago

If you want an example.

antonkomarev commented 9 years ago

Thanks for the publishing solution. I mean that maybe it would be good to have this recommendation in official documentation? Because most of the packages don't have it. It could be helpful to make custom packages better.