I decided to only register my Action Commands automatically when the app is running in the console, like so:
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
if ($this->app->runningInConsole()) {
// automatically load and register any App\Action\* classes that have Artisan command functionality
\Lorisleiva\Actions\Facades\Actions::registerCommands();
}
}
}
Are there any pros/cons to this approach?
Cons:
Commands aren't (automatically) available in the app in the web context, and I'm not sure how to register commands at runtime.
I'm also not sure how to call Action::asCommand() directly even though it's public. I'm having difficulty injecting a CommandDecorator instance.
Pros:
It seems to me that performance would be improved somewhat by not loading commands when the app is running in the web context.
By not loading commands in the web context, developers will be nudged away from trying to run their Actions as Commands from controllers, models, etc. It's probably an anti-pattern, aside from maybe queueing Commands, but Actions are queueable anyway.
To me the cons to this approach aren't really considerations, since I don't need to (and probably shouldn't be) calling Actions as Commands anywhere but the console.
Anyway, what led me to this question was that if there's no significant benefit from executing commands outside the console context, this (my code block above) should be the documented way to auto-register commands here.
I decided to only register my Action Commands automatically when the app is running in the console, like so:
Are there any pros/cons to this approach?
Cons:
Action::asCommand()
directly even though it's public. I'm having difficulty injecting a CommandDecorator instance.Pros:
To me the cons to this approach aren't really considerations, since I don't need to (and probably shouldn't be) calling Actions as Commands anywhere but the console.
Anyway, what led me to this question was that if there's no significant benefit from executing commands outside the console context, this (my code block above) should be the documented way to auto-register commands here.
Any thoughts or objections?