Symfony Dependency Injection shows a weird behaviour

161 Views Asked by At

When i try to inject a service in my Symfony 4.4.25 Bundle (with Akeneo) i encounter a very strange behavior. Defining a mapped variable in the Bundle Config without having the variable in __construct() throws a fail. Adding the variable throws another strange failure

If i define my service like this:

services:
    acme_category_builder.event_subscriber.category_builder:
        class: Acme\Bundle\CategoryBuilderBundle\AcmeCategoryBuilderBundle
        arguments:
            $productModelRepository: '@pim_catalog.repository.product_model'
        tags: ['kernel.event_subscriber']

or like this:

services:
    acme_category_builder.event_subscriber.category_builder:
        class: Acme\Bundle\CategoryBuilderBundle\AcmeCategoryBuilderBundle
        arguments:
            - '@pim_catalog.repository.product_model'
        tags: ['kernel.event_subscriber']

with this constructor:

public function __construct(){}

i get with first the Error that my constructor is missing the $productModelRepository.

Invalid service "acme_category_builder.event_subscriber.category_builder": method "Acme\Bundle\CategoryBuilderBundle\AcmeCategoryBuilderBundle::__construct()" has no argument named "$productModelRepository". Check your service definition.

and with second the PHP Fatal Error from below

If i add it like this in the constructor:

public function __construct($productModelRepository){}

i get the failure, that nothing was passed as Argument

PHP Fatal error:  Uncaught ArgumentCountError: Too few arguments to function
Acme\Bundle\CategoryBuilderBundle\AcmeCategoryBuilderBundle::__construct(), 
0 passed in /home/Workspace/Akeneo/pim/src/Kernel.php on line 36 and exactly 
1 expected in /home/Workspace/Akeneo/pim/vendor/acme/category-builder-bundle/src/AcmeCategoryBuilderBundle.php:39

however if i add a type hint (which was my first guess that could be missing, i retrieve the same error.

use Akeneo\Pim\Enrichment\Component\Product\Repository\ProductModelRepositoryInterface;

public function __construct(ProductModelRepositoryInterface $productModelRepository){

}

How can this be explained? first it wants to pass a argument but cant, then it has to pass a argument and wont?

pls Help :(

0

There are 0 best solutions below