1
votes

For some reason I do not fully understand how to get an simple instance of an custom service. Here is the documentation which I have followed so far:

https://developer.shopware.com/docs/guides/plugins/plugins/framework/data-handling

 class WritingData
{
    private $productRepository;
    private $taxRepository;

    public function __construct(EntityRepositoryInterface $productRepository, EntityRepositoryInterface $taxRepository)
    {
        $this->productRepository = $productRepository;
        $this->taxRepository = $taxRepository;
    }
}

the services.xml is also set

<?xml version="1.0" ?>
    <services>
        <service id="Swag\BasicExample\Service\WritingData" >
            <argument type="service" id="product.repository"/>
            <argument type="service" id="tax.repository"/>
        </service>
    </services>
</container>

The question is: how do I may get an instance of the WrtingData inside my Command Service?

1

1 Answers

2
votes

You have to inject your custom service into your command service just like you injected the repositories into WritingData. You can find more information about the container and dependency injection in the Symfony documentation.

For example if this is your command service:

class ExampleCommand extends Command
{
    private $writingData;

    public function __construct(WritingData $writingData)
    {
        $this->writingData = $writingData;
    }
}

Then you inject WritingData into ExampleCommand in services.xml:

<service id="SwagBasicExample\Command\ExampleCommand">
    <argument type="service" id="Swag\BasicExample\Service\WritingData"/>
    <tag name="console.command"/>
</service>