0

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 :(

Tom Lorenz
  • 19
  • 2
  • Maybe the second time will be a charm. The suffix `Bundle` is usually reserved for actual Symfony bundle classes. Not the sort of thing you would define a service with. Are you sure you posted the correct configuration in your question? If you are actually trying to inject a Doctrine repository into an actual Bundle class then I fear you will be disappointed. Especially if you are also trying to make the bundle an event subscriber. – Cerad Jan 05 '23 at 23:22
  • @Cerad thanks already for responding a second time. Is it not possible too inject a Doctrine Repository? The Bundle is extend because it is said so in the documentation from Symfony. Can you give me a hint on where i can read documentation? The symfony Bundle documentation sadly doesnt help me – Tom Lorenz Jan 05 '23 at 23:30
  • 1
    I'm pretty sure you did not find anything in the Symfony docs concerning injecting repositories or making them event listeners. Bundles are part of the configuration process. Neither the container nor the services within the container (which includes repositories) has even been built yet. So no trying to access the database from within a bundle class. I have not used Akeneo myself but I'm guessing it has some `getting started` docs. – Cerad Jan 05 '23 at 23:35
  • thanks alot! your hint helped. I emptied the bundle class just for registration and moved all my code to a class which only implements EventSubscriberInterface and extends nothing. It works there like i expect. Thanks alot! – Tom Lorenz Jan 06 '23 at 15:17
  • @TomLorenz, using bundle class ( `implements BundleInterface` ) as one of your dependencies is **not the way** it works. Having dependencies and having "empty" constructor is also wrong. – V-Light Jan 08 '23 at 19:46

0 Answers0