silly icon indicating copy to clipboard operation
silly copied to clipboard

Get helper from classes autowired by PHP-DI

Open imam opened this issue 8 years ago • 1 comments

Currently, I can't use getHelperSet() method because I create a separate class that will be autowired by PHP-DI. So now, if I want to use getHelperSet() I have to use the old way to attach a command, by attaching a function to $app->command() second argument rather than attaching my separated class. Is there any workaround for this situation?

imam avatar Oct 24 '17 15:10 imam

Hi, funny I got hit by that just yesterday too :/

Here is what I did:

  • configure the Application class so that it can be autowired: https://github.com/mnapoli/gitstats/blob/a731d6c9d91c8e4f07db0bec6e22c912a55baef2/src/Application.php#L21-L29
  • inject it in a command: https://github.com/mnapoli/gitstats/blob/a731d6c9d91c8e4f07db0bec6e22c912a55baef2/src/TaskRunner.php#L45
  • call getHelperSet(): https://github.com/mnapoli/gitstats/blob/a731d6c9d91c8e4f07db0bec6e22c912a55baef2/src/TaskRunner.php#L81-L84

I'd like to find a better solution too. Maybe the silly-php-di package could preconfigure the Application class? (I cannot inject the HelperSet using PHP-DI because it needs to be constructed using the Input and Output)

Another solution could be to use the SymfonyStyle class and skip helpers altogether: https://symfony.com/doc/current/console/style.html

$io = new SymfonyStyle($input, $output);
$io->ask('What is your name?');

That's simpler, though a bit hidden in Symfony's documentation.

What kind of helpers are you using?

mnapoli avatar Oct 24 '17 17:10 mnapoli