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

There is not any default HelperSet?

Jax-p opened this issue · comments

I've just migrated from Kdyby/Console and I'm not able to use any native helper of Symfony/Console.
I'm trying to use question helper:

$this->helper = $this->getHelper('question');

which returns Cannot retrieve helper "question" because there is no HelperSet defined.
As the documentation says:

You could also define you custom helperSet just in case.`

I thought that there is some default/native helperSet (and I can add my own custom set too) but there is not helperSet at all. How do I implement basic helpers from Symfony? Is there any example? Is there any example of custom helperSet too?

I see that can do something like this:

$set = new HelperSet();
$set->set(new QuestionHelper());
$this->setHelperSet($set);

for every helper I need. But is there any shorter way (like - set every native helper from symfony)?

Thank you for help

Edit:
While testing, I've created BasicHelperSet which extends HelperSet and contains only one helper. I've registred it in config by using: helperSet: App\Console\BasicHelperSet in console section (as is written in documentation) but I'm still getting same error ..no HelperSet defined.. (I am purging temp aswell).

$this->helper = $this->getHelper('question');

Personally I don't like these as it's a hidden dependency which uses service locator, an anti-pattern. I don't feel like there should be any helpers registered by default, Symfony doesn't have them by default too (see here)

It looks like these helpers have no dependencies so you can simply create them in command in which you need them https://github.com/symfony/console/blob/master/Helper/QuestionHelper.php

But you can also register helpers for all commands.

console:
  helpers:
    - Foo\Bar\Helper
  helperSet:Foo\Bar\HelperSet()

no HelperSet defined

Are you using v0.7?

I really appreciate your help.

Problem of Symfony helpers

I'm currently using v0.7.1.

So your idea looks like this (in .php, not in config) right?

$this->questionHelper = new QuestionHelper();
//...
$this->questionHelper->ask($input, $output, $question);

I'm fine with that. Works well.

HelperSet problem

Btw. this one still throws ...no HelperSet defined even if dump in helperSet constructor is being called but I'm not going to use it anymore at this moment.

helperSet: App\Console\BasicHelperSet()

Migration from Kdyby/Console

There is one more helper which I'm missing here (which has been implemented as a default in Kdyby). It's container helper which injects DI Container. Is there any best-practice how to do it here?

I've tried to pass it through the constructor of my command which is extending Symfony/Command but it throws 0 arguments passed to my constructor. So I've tried to inject it with /** @var Container @inject */ on public variable and I've tried to add it to my decorator - still nothing. Any advice how to achieve this (I need container just because of container parameters. I could load .neon in manual way if there is not easy solution)

decorator:
    Symfony\Component\Console\Command\Command:
        tags: [Nette\DI\Extensions\InjectExtension::TAG_INJECT]

...no HelperSet defined even if dump in helperSet constructor is being called

That's weird. We register helper set same way as kdyby and if you check generated container then you should see $application->setHelperSet(...). Don't know what more can do about this. Could you prepare a simple repo with problem reproduction?

injects DI Container

That's weird, constructor should work as for all other services.
Inject is by-default enabled only for nette presenters, as they usually have a lot of dependencies, I don't feel like it should be enabled anywhere else.
Also, inject tag should work perfectly fine. Repo reproducing problem would be nice in that case too.

I need container just because of container parameters

Not really. You degrade DIC to service locator and any missing parameters mean a failure in run-time instead of compile-time. You can pass parameters to service constructor like that - Your\Super\Command(%parameter.name%)

Could you prepare a simple repo with problem reproduction?

I'll try to reproduce this problem on Nette Sandbox and I'll try to debug behavior. I'll provide example as soon as possible.

Also, inject tag should work perfectly fine.

I've used inject:true instead of tag and it works fine. I don't know if it has changed since Nette 2.4 but it's not trouble of this componette at all.

Your\Super\Command(%parameter.name%)

Yeah, you are right. That's better solution.

Thank you very much for your time!