Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lazy commands and callInjects #54

Open
MartinMystikJonas opened this issue Feb 20, 2021 · 13 comments
Open

Lazy commands and callInjects #54

MartinMystikJonas opened this issue Feb 20, 2021 · 13 comments

Comments

@MartinMystikJonas
Copy link
Member

MartinMystikJonas commented Feb 20, 2021

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?

@f3l1x
Copy link
Member

f3l1x commented Feb 21, 2021

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

@MartinMystikJonas
Copy link
Member Author

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
Copy link
Member Author

MartinMystikJonas commented Feb 21, 2021

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.

@JanTvrdik
Copy link
Contributor

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

@MartinMystikJonas
Copy link
Member Author

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.

@f3l1x
Copy link
Member

f3l1x commented Feb 22, 2021

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

@MartinMystikJonas
Copy link
Member Author

I will

@MartinMystikJonas
Copy link
Member Author

MartinMystikJonas commented Feb 22, 2021

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?

@f3l1x
Copy link
Member

f3l1x commented Jan 3, 2024

Hi @MartinMystikJonas, is it still relevant?

@MartinMystikJonas
Copy link
Member Author

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.

@f3l1x
Copy link
Member

f3l1x commented Jan 5, 2024

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

@MartinMystikJonas
Copy link
Member Author

Pure laziness :-)

@f3l1x
Copy link
Member

f3l1x commented Jan 8, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants