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

DI injection for commands? #46

Open
matak opened this issue Jan 22, 2016 · 7 comments
Open

DI injection for commands? #46

matak opened this issue Jan 22, 2016 · 7 comments
Labels

Comments

@matak
Copy link

matak commented Jan 22, 2016

Nebyl by tady nějaký injecting? Toto DI není úplně ok ne? od doby co global context je fuj?

class SendNewslettersCommand extends Command
{
    protected function configure()
    {
        $this->setName('app:newsletter')
            ->setDescription('Sends the newsletter');
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $newsletterSender = $this->getHelper('container')->getByType('Models\NewsletterSender');

@matej21
Copy link
Contributor

matej21 commented Jan 22, 2016

You can use inject methods or @inject properties

@matak
Copy link
Author

matak commented Jan 22, 2016

Perfect, thanks!

@fprochazka
Copy link
Member

So.. solved? :)

@matak
Copy link
Author

matak commented Jan 22, 2016

maybe this tip in doc would be fine,
somebody could appreciate this

@fprochazka
Copy link
Member

Agreed

@foxycode
Copy link
Contributor

foxycode commented May 6, 2016

@fprochazka One more thing, If I want to run command with injects annotations in other command using procedure from Symfony documentation

$container = $this->getHelper('container')->getContainer();
$command = $container->getByType('Kdyby\Console\Application')
    ->find('pair:productsToFilters');

I need to run

$container->callInjects($command);

by hand, injects aren't called in find.

I also need to run this:

$arguments = [
    'command' => 'pair:productsToFilters',
    '--email' => $input->getOption('email'),
];
$pairProductsToFiltersInput = new ArrayInput($arguments);
$command->run($pairProductsToFiltersInput, $output);

It would be nice to have shorter in Kdyby\Console for that. Something like:

$this->getHelper('command')
    ->find('pair:productsToFilters') // inject are already called in this stage
    ->run($arguments, $output);

@fprochazka
Copy link
Member

fprochazka commented May 6, 2016

@foxycode that is wrong, you should run other commands using the Console\Application, just like Console\Tester\ApplicationTester does it.

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

Successfully merging a pull request may close this issue.

4 participants