Command class is missing a dispatchCommand method
This is a
-
[x] feature-discussion (RFC)
-
CakePHP Version: 3.6
-
Platform and Target: N\A
What you did
I was able to dispatch a shell within another shell as
$this->dispatchShell('schema', 'create', 'Blog', '--plugin', 'Blog');
Since the class is deprecated I tried to achieve the same thing with console commands.
What happened
The dispatchCommand method is missing from the Command class.
What you expected to happen
I usually separate commands in granular classes and code a few commands to use them in different aspects according to arguments.
As an example;
class DeployCommand extends Command
{
public function buildOptionParser(ConsoleOptionParser $parser)
{
$parser = parent::buildOptionParser($parser);
$parser
->addOption('type', [
'help' => 'type of deployment',
'required' => true,
'choices' => [
'cms', 'app'
]
]);
return $parser;
}
public function execute(Arguments $args, ConsoleIo $io)
{
if($args->getOption('type') === 'cms') {
// call deploy cms command here
} else {
// call deploy cms here
// call deploy app also here
}
}
}
So it would be useful to have a dispatchCommand method just like dispatchShell method.
Would you want to call other commands using an array of arguments or a string as if you were using the cli?
Instead of dispatching commands from within commands I'd suggest refactoring your code so the reusable logic is not tied to the console anymore.
Dispatching commands from another command doesn't sound like a good idea and we should avoid that.
@robertpustulka That is sound advice :+1:
I can't follow the logic behind this. I have commands. They are not sub-commands. They do work separately as they should. There are some commands firing other ones with some logic.
For instance; I have a command running every night. It checks the database and fires a command with different arguments according to the count of visitor countries. But I should be able to use that command with whatever argument I choose during the day since I need some daily reports time to time.
Within one of my old projects; I had a plugin including different shells for different cron jobs. A daily shell and an hourly shell. So I was able to manage cron jobs within my software layer easily.
I doubt cases like this and many more are just my concern. The sentence There are still many cases where you will want to invoke one shell from another though. is right from the 3.X documentation
So the suggestion is that I code a separate class (or classes) for my logic and call an instance within my commands.
I highly respect you and sincerely want to learn and understand the logic behind this suggestion.
Sounds to me like this boils down to similar issues as I noted a while back: That now you have to split into completely separate commands that might not even have the same namespace/order in the list, making it hard to group things together.
@suhaboncukcu The one shell from another one just means that you have too much business logic in your communication layer. Offloaded into a service layer you can call that same service lib/class then from all ivoking commands/shells.
Would providing wrappers around running other shells/commands as subprocesses work? It would cover the application cron daemon scenario well.
One challenge I have had with invoking other shells is that they run in the same process space and their failure/global state mutation will effect the calling shell.
Good idea. If you really need to fire a command from another command run it as another process. Use exec or any wrapper like symfony/process.
@robertpustulka I think using exec or shell_exec for dotenv users can cause an error since dotenv throws a LogicException for already defined constants.
This also doesnt sound good to me. Is there no way we can use internal dispatching passing around input/output and stack this way? More similar to the old shells?
Would taking an example from command bus architecture be absurd in this case? Creating handlers and calling handlers to fire up commands wherever necessary?
@suhaboncukcu you can leverage command bus pattern in order to extract your logic into reusable parts but be wary of its constraints such as a command handler should never return a value, only execute logic.
If you want to use Tactician, there's an adapter for CakePHP: https://github.com/robotusers/cakephp-tactician
Is there no way we can use internal dispatching passing around input/output and stack this way? More similar to the old shells?
I'm sure we could create a way. But why simulate subprocesses if we can use actual sub-processes. Instead of having folks use symfony/process we could provide a method on Command that opens a subprocess with the current process's ENV data.
My app is heavy using Invoking Other Shells from Your Shell and Shell Tasks form the Shell class but unfortunately, the Command class is missing these features.
@BassemN would your use case be covered by wrappers around sub-process that don't trigger environment errors for dotenv users?
Running other commands in the same process space is a bit complicated as commands don't have access to the runner.
I was thinking a bit more about this, and while sub-processes would cover a few of the scenarios described here, it would not make accepting interactive input easy to work with. The generated sub-processes could attach to the current process' stdin/stdout easily enough, but testing becomes challenging, as the sub-process would not be controlled by the test suite directly.
Running 'sub-shells' in the same PHP process presents some problems around isolation and error handling, but makes testing much simpler. I've been working on converting bake commands recently and have made good use of a pattern that looks like:
// In a command
$subcommand = new OtherCommand();
$newArgs = new Arguments(['widgets'], ['verbose' => true], ['name']);
$subcommand->execute($modelArgs, $io);
This pattern allows the calling command to pass in its ConsoleIo instance making tests easy, and enables commands to be constructed as needed (either from a DI container, or directly). This pattern could be wrapped into an abstraction that looks like:
// In a command. Call another command by classname
$this->executeCommand(OtherCommand::class, $io, ['widgets', '--verbose', true]);
// Or call another command by instance.
$other = new OtherCommand();
$this->executeCommand($other, $io, ['widgets', '--verbose', true]);
The executeCommand method would allow other commands, (but not shells) to be called. The executeCommand method would:
- Create the instance if a class name is passed.
- Build an option parser for the command, and use it to parse the argument array.
- Run the
executemethod on the command and return the command's return code.
What do folks think of that API?
@markstory Sounds like this could resolve the issues. :+1:
Pull request open now for the proposed method.