請幫我配置控制檯應用程序,在第一 - 配置如何使用autowire命令設置控制檯應用程序?
#!/usr/bin/env php
<?php
use ....
...
$container = new ContainerBuilder();
$config = new YamlFileLoader($container, new FileLocator(__DIR__));
$config->load('config.yml');
$output = $container->get('symfony.console_output');$logger = $container->get('logger');
//I want automatic injection!!!!
$helloCommand = new HelloCommand($container, $logger);
$application = $container->get('symfony.application');
$application->add($helloCommand);
$application->run(null, $output);
而且我config.yml
services:
logger:
class: Symfony\Component\Console\Logger\ConsoleLogger
arguments:
- '@symfony.console_output'
symfony.application:
class: Symfony\Component\Console\Application
calls:
//by this variant autowire not working
- [add, [ '@app.command.hello_command' ]]
- [setDispatcher, ['@symfony.event_dispatcher']]
...
app.command.hello_command:
class: App\Command\HelloCommand
autowire: true
tags:
- { name: console.command }
所以我HelloCommand具有構造與ContainerInterface和LoggerInterface且僅當我設置它的工作直接參數,其他我有關於錯誤的構造函數的錯誤
或者可能存在另一種配置方式config.yml 只有記錄器 - 它將通過設置['@logger' ]作爲參數,但我怎樣才能設置當前容器作爲參數? 還是我心底必須安裝完整的symfony與httpkernel(但並不需要)
HelloCommand http://pastebin.com/VRr3FM7Q
你如何配置決定
app.command.hello_command:
class: App\Command\HelloCommand
arguments:
- '@service_container'
- '@logger'
tags:
- { name: console.command }
如果您已經嘗試將其註冊爲服務,爲什麼您需要手動創建'HelloCommand'對象?還請顯示你的'HelloCommand'類的代碼。 – xabbuh
它的替代變體;爲了說明,如果我創建如果手動與初始化 - 一切正常,但與配置注入不起作用 – Lenny
HelloCommand http://pastebin.com/VRr3FM7Q – Lenny