Php 如何实现类似在参数中包含表达式以注入服务这样的功能?
在服务定义中,我通过表达式语言注入值:Php 如何实现类似在参数中包含表达式以注入服务这样的功能?,php,symfony,parameters,expression,parameter-passing,Php,Symfony,Parameters,Expression,Parameter Passing,在服务定义中,我通过表达式语言注入值: someService: arguments: - "@=service('otherService').getValue()" 现在,另一项服务需要相同的价值,所以我想我可以: parameter: leValue: "@=service('otherService').getValue()" someService: arguments: - "%leValue%" yetAnotherSe
someService:
arguments:
- "@=service('otherService').getValue()"
现在,另一项服务需要相同的价值,所以我想我可以:
parameter:
leValue: "@=service('otherService').getValue()"
someService:
arguments:
- "%leValue%"
yetAnotherService:
arguments:
- "%leValue"
但这给我带来了一个例外:
不能转储包含对的引用的参数的容器
其他服务
糟糕透了。是否有其他方法存储表达式?我不希望每次需要时都重复完整的表达式,而且我也不固定使用参数。我的目标是获得一点语法上的糖分。这个问题有一个类似的答案 你必须使用别名 例如:
public function load(array $configs, ContainerBuilder $container)
{
$configuration = new Configuration;
config = $this->processConfiguration($configuration, $configs);
$loader = new Loader\YamlFileLoader(
$container,
new FileLocator(__DIR__.'/../Resources/config')
);
$loader->load('services.yml');
$alias = $config['mailer']['driver'];
$container->setAlias('my_scope.mailer_driver', $alias);
}
为您服务。yml:
services:
my_scope.mailer_driver:
alias: my_scope.mailer_driver_smtp # Fallback
my_scope.mailer_driver_smtp:
class: My\Scope\Driver\Smtp
my_scope.mailer_driver_mock:
class: My\Scope\Driver\Mock
my_scope.mailer:
class: My\Scope\Mailer
arguments:
- @my_scope.mailer_driver