deprecated-packages/symplify

[composer-json-factory] How to auto-wire into service

tacman opened this issue · 2 comments

I'm trying to inject ComposerJsonFactory into a service from my Symfony maker (to make the skeleton of a bundle, in fact), and it's not behaving as expected. Likely I'm not autowiring it correctly, or need an alias, or something like that.

class MyBundle extends AbstractBundle
{
    public function loadExtension(array $config, ContainerConfigurator $container, ContainerBuilder $builder): void
   {
        $builder->autowire(MakeBundle::class)
            ->addTag(MakeCommandRegistrationPass::MAKER_TAG) // 'maker.command'
            ->addArgument($config['template_path'])
            ->addArgument($config['relative_bundle_path']) // /packages
            ->addArgument($config['bundle_name'])
            ->setArgument('$jsonFileManager', new Reference(JsonFileManager::class))
            ->setArgument('$composerJsonFactory', new Reference(ComposerJsonFactory::class))
        ;

My MakeBundle class, that can make files from a command by implementing Symfony's MakerInterface

// MakeBundle.php
class MakeBundle extends AbstractMaker implements MakerInterface
{
    public function __construct(
        private string              $templatePath,
        private string              $bundlePath,
        private string              $bundleName,
        private JsonFileManager     $jsonFileManager,
        private ComposerJsonFactory $composerJsonFactory
    ) 
    {
    }
}

The error:

The service "Survos\Bundle\MakerBundle\Maker\MakeBundle" has a dependency on a non-existent service "Symplify\ComposerJsonManipulator\ComposerJsonFactory".

Any suggestions?

Possibly related: is this really a bundle?

{
    "name": "symplify/composer-json-manipulator",
    "description": "Package to load, merge and save composer.json file(s)",
    "license": "MIT",
    "type": "symfony-bundle",

Usually bundles extend Bundle or AbstractBundle, but I'm not seeing a class that does that, and the services are all private.

In the end, I got it to work by making the necessary classes public:

        $builder->register(ParameterProvider::class)
            ->setArgument('$container', new Reference('service_container'))
            ->setPublic(true)
            ->setAutowired(true)
            ->setAutoconfigured(true);


        foreach ([SmartFileSystem::class, JsonCleaner::class, JsonInliner::class, JsonFileManager::class, ComposerJsonFactory::class] as $symplifyClass) {
            $builder->register($symplifyClass)
                ->setPublic(true)
                ->setAutowired(true)
                ->setAutoconfigured(true);
        }

It doesn't feel right, though.

Hi, this is not a bundle. Yet you can include the service config and use services with ease :)
https://github.com/symplify/composer-json-manipulator/blob/main/config/config.php