我需要为Symfony 2.8应用程序配置自动装配 .

服务依赖于 Symfony\Component\Templating\EngineInterface

use Symfony\Component\Templating\EngineInterface;
class MyService
{
    /** @var EngineInterface */
    private $templating;

    public function __construct(EngineInterface $templating)
    {
        $this->templating = $templating;
    }
}

但是我明显得到一个错误,因为这个接口是在三个不同的类中实现的 .

无法为服务“myservice”自动装入“Symfony \ Component \ Templating \ EngineInterface”类型的参数 . 接口(templating.engine.delegating,templating.engine.php,templating.engine.twig)存在多种服务 .

每当需要 EngineInterface 时我想注入 templating.engine.delegating 所以我给 service.yml 添加了一个定义

services:
  template:
    autowiring_type: 'Symfony\Component\Templating\EngineInterface'
    alias: 'templating.engine.delegating'

但我仍然得到相同的错误 - 应用程序无法找到用于 EngineInterface 的服务

所以问题是可以将接口连接到已定义的内核服务吗?

当必须将抽象类连接到服务时,我设法处理类似的问题 .

服务:

use Doctrine\Common\Cache\CacheProvider;
class MyOtherService
{
    /** @var CacheProvider */
    private $cache;

    public function __construct(CacheProvider $cache)
    {
        $this->cache = $cache;
    }
}
services:
  memcache_cache:
    class: 'Doctrine\Common\Cache\MemcacheCache'
    autowiring_types: 'Doctrine\Common\Cache\CacheProvider'
    calls:
    - [ setMemcache, ["@memcache"] ]
    - [ setNamespace, ["%memcache.prefix%"] ]

所以,我已经创建了一个服务( memcache_cache )并为它定义了一个autowiring_types . 每次注入抽象CacheProvider时,都应该使用 memcache_cache .

但这很容易,因为这不是内核服务 . 我自己在 services.yml 中定义了它,而我没有访问权限扩展 templating.engine.delegating 服务以添加 autowiring_types: 'Symfony\Component\Templating\EngineInterface' 的方式