mdupor
1 month ago
166
3
Laravel

Running in console micro optimizations

Posted 1 month ago by mdupor

I was wondering if I can use app()->runningInConsole() for my Service Provider micro optimizations?

For example:

public function boot()
{
    $this->publishes([__DIR__ . '/Config/containers.php' => config_path('containers.php'),]);

    $this->registerCreator();
    $this->registerMigrateMakeCommand();

    $this->commands([
        'my.command.migrate.make',
    ]);
}

The whole purpose of the things within the boot() method is to publish the configuration, and provide additional methods when using the migrations.

I can't see a scenario where a request would trigger this part of the code.

Would it be wise then to do:

public function boot()
{
    if(app()->runningInConsole()) {

        $this->publishes([__DIR__ . '/Config/containers.php' => config_path('containers.php'),]);

        $this->registerCreator();
        $this->registerMigrateMakeCommand();

        $this->commands([
            'my.command.migrate.make',
        ]);
    }
}

So that if a standard request is made to prevent running these things unnecessarily?

Please sign in or create an account to participate in this conversation.