zenstruck / console-extra

A modular set of features to reduce configuration boilerplate for your Symfony commands.
MIT License
78 stars 3 forks source link

creating an Invokable Command in a bundle #59

Closed tacman closed 10 months ago

tacman commented 10 months ago

I'm moving an InvokableServiceCommand to a bundle, and am stuck on how to inject the container, which happens automatically if it's in the application.

class SurvosApiGridBundle extends AbstractBundle
{
    public function loadExtension(array $config, ContainerConfigurator $container, ContainerBuilder $builder): void
    {

        $definition = $builder->autowire(ApiIndexCommand::class)
            ->setArgument('$entityManager', new Reference('doctrine.orm.entity_manager'))
            ->setArgument('$bag', new Reference('parameter_bag'))
            ->setArgument('$serializer', new Reference('serializer'))
            ->addTag('console.command')
        ;

This throws the error:

In DefinitionErrorExceptionPass.php line 51:

Cannot autowire service "Survos\ApiGrid\Command\ApiIndexCommand": argument "$container" of method "Zenstruck\Console\InvokableServiceCommand::setInvokeContainer()" references interface "Psr\Container\ContainerInterface" but no such service exists. Did you mean to target "parameter_bag" instead?

So I tried to add a method call

        $definition->addMethodCall('setInvokeContainer', [$container]);

But obviously this doesn't work, the $container is a ContainerConfigurator object.

So then I thought it belongs in the compiler pass, but got stuck.

How do I set up an InvokableServiceCommand from within a bundle?

Thanks!

kbond commented 10 months ago

Hey @tacman, does the following work?

$definition->setAutoconfigured(true);

tacman commented 10 months ago

Indeed it does! Thanks.