Symfony2是否检测到服务之间的周期性依赖性?

Say you need to build a complex service relying on other services in Symfony2. One way to create these services is to create factories.

Yet, is it possible to create services with cyclical dependencies to other services in Symfony2 or not? I know this is not a good coding practice, but that is not my question.

Should one implement service setter methods as mentioned in the documentation to enable this? Else, how does Symfony2 deals with these chicken and egg issues? Does it throw an error?

The best way to find out is to try it:

1). Install a Symfony standard edition application

I installed Symfony 2.8.1 for this example.

2). In the default AppBundle add the following files in Services folder:

ServiceA.php:

namespace AppBundle\Services;

class ServiceA
{
    private $service;

    public function __construct(ServiceB $service)
    {
        $this->service = $service;
    }
}

ServiceB.php:

namespace AppBundle\Services;

class ServiceB
{
    private $service;

    public function __construct(ServiceA $service)
    {
        $this->service = $service;
    }
}

3). Add an extension subclass in DependencyInjection folder:

AppExtension.php:

namespace AppBundle\DependencyInjection;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\Extension\Extension;
use Symfony\Component\DependencyInjection\Loader;

class AppExtension extends Extension
{
    public function load(array $configs, ContainerBuilder $container)
    {
        $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources'));
        $loader->load('services.yml');
    }
}

4). And declare the circular-dependent services in Resources folder:

services.yml:

services:
    service_a:
        class: AppBundle\Services\ServiceA
        arguments:
            - @service_b

    service_b:
        class: AppBundle\Services\ServiceB
        arguments:
            - @service_a

After all this, if you run php app/console --version, you will get the following error:

[Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException]
Circular reference detected for service "service_a", path: "service_a -> service_b -> service_a".