contributte / console

:boom: Best minimal console (symfony/console) to Nette Framework (@nette)

Home Page:https://contributte.org/packages/contributte/console.html

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Lazy commands and callInjects

MartinMystikJonas opened this issue · 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?

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

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.

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.

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

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.

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

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?

Hi @MartinMystikJonas, is it still relevant?

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.

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

Pure laziness :-)

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