我有一个由两个 Symfony 应用程序使用的库,这个库定义了一组我想要公开的服务(我希望能够直接通过容器检索它们。当我尝试访问一个服务时,我有这个:
编译容器时,“Library\Service\DerivedServices\OneSpecificImplementation”服务或别名已被删除或内联。您应该将其公开,或者直接停止使用容器并改用依赖注入。
问题是所述服务被声明为公开的。
基本上有:
Library\Service\BaseService
类,它有两个用于公共依赖项的 setter(此代码段中的 doctrine 和一个记录器);Library\Service\DerivedServices
命名空间中),每个都定义了一个新的服务(有自己的构造函数来直接处理DI)。所以这里是我的服务定义:
# Base: inject common dependencies
Library\Service\BaseService:
abstract: true
calls:
- [setDoctrine, ['@doctrine.orm.entity_manager']]
- [setLogger, ['@Library\Service\Logger']]
# These services are public to be retrieved directly by the container interface
Library\Service\DerivedServices\:
resource: '../vendor/company/library/src/Library/Service/DerivedServices'
public: true
autowire: true
autoconfigure: false
parent: Library\Service\BaseService
然后,Symfony 应用程序检索一个派生服务,例如:
$this->get('Library\Service\DerivedServices\OneSpecificImplementation');
这些没有任何区别:
我认为这是一些微不足道的配置,但我无法确定它(并且在尝试调试框架为什么将我的服务编译为私有的原因 2 小时后,我认为有人可能拥有这个并且可能可以帮助我)。
事实证明,服务的声明顺序很重要。正如我所想的那样,问题出在配置方面。
我有:
Library\Service\BaseService:
...
Library\Service\DerivedServices\:
...
Library\Service\:
resource: '../vendor/company/library/src/Library/Service'
最后一条指令将所有服务重新声明为私有(默认情况下)。
我把这个改成:
Library\Service\:
resource: '../vendor/company/library/src/Library/Service'
Library\Service\BaseService:
...
Library\Service\DerivedServices\:
...
这首先将所有服务声明为私有,然后使用新声明重新声明它们:使用 parent + public。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句