Kdyby / Console

Symfony Console integration for Kdyby components
https://packagist.org/packages/kdyby/console
Other
52 stars 54 forks source link

DI injection for commands? #46

Open matak opened 8 years ago

matak commented 8 years ago

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 commented 8 years ago

You can use inject methods or @inject properties

matak commented 8 years ago

Perfect, thanks!

fprochazka commented 8 years ago

So.. solved? :)

matak commented 8 years ago

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

fprochazka commented 8 years ago

Agreed

foxycode commented 8 years ago

@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 commented 8 years ago

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