How can i register my services and components in Shopware 5?

Viewed 391

I have a problem with registration my services and components in services.xml

This is what i receive when i tried to register my component.

PHP Fatal error: Uncaught Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException: The service "task.task_subscriber" has a dependency on a non-existent service "task.service.random_product". in /var/www/shopware/vendor/symfony/dependency-injection/Compiler/CheckExceptionOnInvalidReferenceBehaviorPass.php:31\nStack trace:\n#0 /var/www/shopware/vendor/symfony/dependency-injection/Compiler/AbstractRecursivePass.php(60): Symfony\Component\DependencyInjection\Compiler\CheckExceptionOnInvalidReferenceBehaviorPass->processValue(Object(Symfony\Component\DependencyInjection\Reference), false)\n#1 /var/www/shopware/vendor/symfony/dependency-injection/Compiler/CheckExceptionOnInvalidReferenceBehaviorPass.php(28): Symfony\Component\DependencyInjection\Compiler\AbstractRecursivePass->processValue(Array, false)\n#2 /var/www/shopware/vendor/symfony/dependency-injection/Compiler/AbstractRecursivePass.php(67): Symfony\Component\DependencyInjection\Compiler\CheckExceptionOnInvalidReferenceBehaviorPass->processValue(Array)\n#3 /var in /var/www/shopware/vendor/symfony/dependency-injection/Compiler/CheckExceptionOnInvalidReferenceBehaviorPass.php on line 31

This is my services.xml

<services>
    <service id="task.task_subscriber" class="Task\Subscriber\TaskSubscriber">
        <argument>%task.plugin_name%</argument>
        <argument>%task.plugin_dir%</argument>
        <argument type="service" id="task.components.random_product" />
        <argument type="service" id="shopware.plugin.cached_config_reader" />
        <tag name="shopware.event_subscriber" />
    </service>

    <service id="task.random_product" class="Task\Components\RandomProduct">
        <argument type="service" id="dbal_connection"/>
    </service>
</services>

Can someone explain to me please what is the problem?

1 Answers

you register your class RandomProduct with id="task.random_product"

so you need to use the same id in the other service

so this is supposed to work

<services>
    <service id="task.task_subscriber" class="Task\Subscriber\TaskSubscriber">
        <argument>%task.plugin_name%</argument>
        <argument>%task.plugin_dir%</argument>
        <argument type="service" id="task.random_product" />
        <argument type="service" id="shopware.plugin.cached_config_reader" />
        <tag name="shopware.event_subscriber" />
    </service>

    <service id="task.random_product" class="Task\Components\RandomProduct">
        <argument type="service" id="dbal_connection"/>
    </service>
</services>

and in your class TaskSubscriber you can pass the RandomProduct in

the __construct in the same order for the arguments and us dependency injection in your code

so it will look like this

<?php

namespace Task\Subscriber;

use Task\Components\RandomProduct;

class TaskSubscriber implements SubscriberInterface
{
    private $pluginName;
    private $pluginDirectory;
    private $randomProduct;
    private $config;

    public function __construct(
    $pluginName, 
    $pluginDirectory,
    RandomProduct $randomProduct,
    $config
    )
    {
        $this->pluginName = $pluginName;
        $this->pluginDirectory = $pluginDirectory;
        $this->randomProduct = $randomProduct;
        $this->config = $config;
    }
Related