skip to Main Content

I have two services named "processors":

<?php
namespace AppProcessor;

use AppRepositoryChannelRepository;
use PsrLogLoggerInterface;

class FirstProcessor
{
    public function __construct(
        LoggerInterface $logger,
        ChannelRepository $channelRepository,
    ) {
    }

}

<?php
namespace AppProcessor;

use AppRepositoryChannelRepository;
use PsrLogLoggerInterface;

class SecondProcessor
{
    public function __construct(
        //LoggerInterface $logger,
        ChannelRepository $channelRepository,
    ) {
    }
}

Also I have an action, which is a public non-shared service, and it uses these processors:

<?php
namespace AppAction;

use AppProcessor;

class CheckEmail
{
    public function __construct(
        AppProcessorFirstProcessor $firstProcessor,
        AppProcessorSecondProcessor $secondProcessor,
    ) {
    }
}

And Console command that depends on CheckEmail action:

<?php
#[AsCommand('app:check-inbox', 'Check inbox and process emails')]
class CheckInboxCommand extends Command
{
    protected CheckEmail $action;

    public function __construct(
        CheckEmail $command
    )
    {
        parent::__construct();
    }
}

Somehow I started to get error "You have requested a non-existent service "AppRepositoryChannelRepository"". Of course ChannelRepository (Doctrine repository) exists and Symfony DI has been seeing it for two years, but something happened and now DI can’t find it. In the beginning the code was a big project with plenty of controllers and services. Trying to locate the problem, I got rid of everything except a few classes, which cause the issue. Full simplified code is available on github (composer install, symfony serve and opening https://127.0.0.1:8000 should be enough to reproduce the issue; php81 is required).

I’ve found a number of patterns:

  • If AppActionCheckEmail is made shared (commented "shared: false" in services.yaml), the error dissappears.
  • If I comment $logger in FirstProcessor constructor, the error disappears.
  • If I use CheckEmail in constructor of another service (and keep it in constructor of CheckInboxCommand), the error disappears. For instance, uncommenting $action in DevController resolves problem.
  • If I remove CheckEmail from CheckInboxCommand constructor and add it in DevController constructor, the error disappears.

There is definitely something I don’t know about non-shared services, but I can’t figure out what exactly.

2

Answers


  1. Chosen as BEST ANSWER

    This behavior seems to be a bug. Here is a possible workaround https://github.com/symfony/symfony/issues/54294#issuecomment-2000099327 (it works for me)


  2. The service is autowired. There is no need use this code in your project.

    I did check you github code and got it working, by simple removing this from services.

        AppActionCheckEmail:
          public: true
          shared: false
    

    There is no need to set this configuration in your example

    And also did change doctrine.yaml. but i don’t think this is needed for this problem.

            mappings:
            App:
                is_bundle: false
                dir: '%kernel.project_dir%/src'
                prefix: 'App'
                alias: App
    

    Also don’t forget to run bin/console cache:clear command before testing this configuration.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search