【发布时间】:2018-01-06 02:05:56
【问题描述】:
我使用的是 Symfony 4,有一个配置文件需要以数组的形式注入到命令App\Command\FooCommand 中。我在App\Kernel::configureContainer()注册了一个自定义DI扩展,用于验证自定义配置文件(为了开发方便,配置很大,在开发过程中会经常更改)。该命令的构造函数是public function __construct(Foo $foo, array $config),我希望配置作为第二个参数。
现在我该如何把这个配置放在那里?文档中提到了参数,但它不是参数。我正在考虑更改命令的定义并在 Extension::load 方法中添加此参数,如下所示:
class FooExtension extends Extension
{
public function load(array $configs, ContainerBuilder $container)
{
$configuration = $this->getConfiguration($configs, $container);
$config = $this->processConfiguration($configuration, $configs);
//inject the configuration into the command
$fooCmdDef = $container->getDefinition(FooCommand::class);
$fooCmdDef->addArgument($config);
}
}
但它以错误结束
您请求了一个不存在的服务“App\Command\FooCommand”。
但是该命令必须已自动注册为服务。
我做错了什么以及如何注入此配置?
【问题讨论】: