console icon indicating copy to clipboard operation
console copied to clipboard

Lazy commands and callInjects

Open MartinMystikJonas opened this issue 4 years ago • 13 comments

I am trying this as replacement for Kdyby/Console and I had idea of small improvement. What about use container callInjects when lazy loading commands by CommandLoader. Commands then would behave similar to presenters. Creazed when needed with required dependencies autimatically injected. It would provide easy way to get dependencies without helpers. What do you think? Should I prepare PR?

MartinMystikJonas avatar Feb 20 '21 00:02 MartinMystikJonas

Hi @MartinMystikJonas. Why do you need that functionality? I mean, command can be autowired via constructor and it's lazily created.

f3l1x avatar Feb 21 '21 12:02 f3l1x

Parent class Command constructor already uses some parameters so I preffer not to mess up with that in my code to avoid incompatibilities. Imho injects are better solution here and I would prefer them over overiding constructor. So I think this could be an useful option.

MartinMystikJonas avatar Feb 21 '21 14:02 MartinMystikJonas

Or maybe just give us option to choose CommandLoader class via config. Currently it is not easy to use different loader by just redefining service because of command map parameter.

MartinMystikJonas avatar Feb 21 '21 14:02 MartinMystikJonas

Parent class Command constructor already uses some parameters so I preffer not to mess up with that in my code to avoid incompatibilities

That's nonsence. Just call the parent constructor with no arguments and specify command in $defaultName property. https://symfony.com/doc/current/console/commands_as_services.html#lazy-loading

JanTvrdik avatar Feb 22 '21 07:02 JanTvrdik

Ok. So I can be sure that redefining constructor signature will be ok. Fine.

Yet maybe support for custom command loader could be useful sometimes.

MartinMystikJonas avatar Feb 22 '21 08:02 MartinMystikJonas

I am ok to support other command loader, but I can't imagine any other, can you prepare PR with your idea?

f3l1x avatar Feb 22 '21 08:02 f3l1x

I will

MartinMystikJonas avatar Feb 22 '21 08:02 MartinMystikJonas

I started working on it and I have few questions about what approach select:

  1. Would you preffer separate config value
lazy: true
commandLoader: \Custom\MyCommandLoader

or allow lazy option to pass class (false = not lazy, true = default loader, class = use specific loader)

lazy: \Custom\MyCommandLoader
  1. I am thinking of making commandMap as a service so it can be easily autowired to any command loader instead of fixed loader constructor signature. Either service with getCommandMap() : array or service directly implementing \ArrayAccess. Do you agree? Which version do you preffer?

MartinMystikJonas avatar Feb 22 '21 11:02 MartinMystikJonas

Hi @MartinMystikJonas, is it still relevant?

f3l1x avatar Jan 03 '24 16:01 f3l1x

I use this workaround in our apps:


class Command extends \Symfony\Component\Console\Command\Command {

  public function setApplication(Application $application = null): void {
    parent::setApplication($application);
    if($application) {
      /** @var ContainerHelper $container */
      $container = $this->getHelper('container');
      $container->callInjects($this);
    }
  }

}

And it works just fine. So if nobody else is interested in this feature I can live with this custom solution.

MartinMystikJonas avatar Jan 04 '24 17:01 MartinMystikJonas

What is your motivation? Why don't you use constructor?

f3l1x avatar Jan 05 '24 08:01 f3l1x

Pure laziness :-)

MartinMystikJonas avatar Jan 08 '24 17:01 MartinMystikJonas

I see. Lets make it. Prepare PR my padawan.

f3l1x avatar Jan 08 '24 17:01 f3l1x