2016-08-18 4 views
1

Bitte helfen Sie mir mit Config Console App, in erster - ConfigWie wird die Konsolenanwendung mit Autowire-Befehlen eingerichtet?

#!/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); 

Und mein 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 } 

So hat mein HelloCommand Konstruktor mit ContainerInterface und LoggerInterface und es nur funktionieren, wenn ich diesen Satz Argumente direkt, andere ich habe Fehler über falsche Konstruktor

Oder gibt es eine andere Möglichkeit zur Konfiguration mit config.yml für nur Logger - es wird einfach per Set ['@logger' ] als Argumente, aber wie kann ich den aktuellen Container als Argument setzen? Oder i `ll volle symfony mit httpkernel installieren müssen (aber es muss nicht)

HelloCommand http://pastebin.com/VRr3FM7Q

DIE ENTSCHEIDUNG

app.command.hello_command: 
class: App\Command\HelloCommand 
arguments: 
     - '@service_container' 
     - '@logger' 
tags: 
    - { name: console.command } 
+0

Warum erstellen Sie manuell ein Objekt des 'HelloCommand', wenn Sie bereits versuchen, es als Dienst zu registrieren? Bitte zeigen Sie auch den Code Ihrer 'HelloCommand' Klasse. – xabbuh

+0

es alternative Variante; zur Veranschaulichung, dass, wenn ich erstelle, wenn manuell mit Initialisierung - alles in Ordnung, aber mit Config-Injektion funktioniert nicht – Lenny

+0

HelloCommand http://pastebin.com/VRr3FM7Q – Lenny

Antwort

2

Das Problem liegt darin, wie Sie konfigurieren Ihr Befehl:

app.command.hello_command: 
    class: App\Command\HelloCommand 
    autowire: true 
    tags: 
     - { name: console.command } 

Dies fehlt die 2 Konstruktorargumente erforderlich: $container, $logger und ist wahrscheinlich, warum Sie die Ausnahme erhalten. Sie können Konstruktorargumente wie folgt hinzufügen:

app.command.hello_command: 
    class: App\Command\HelloCommand 
    arguments: 
     - '@service_container' 
     - '@logger' 
    [...] 

Ich bin nicht sicher, ob die ID für den service_container richtig ist. Ich übergebe nie den Container oder mache ContainerAware, aber du bekommst die allgemeine Idee. ;)

+0

Es versteht sich von selbst. Das Problem liegt in der Tatsache, dass ich nicht weiß, wie Container ID – Lenny

+0

Aber '@service_container' - es ist korrekt !!! Alles funktioniert ohne manuelle Definition in app.php viel Dank! – Lenny

+0

Ich hätte erwartet, dass autowire den Abschnitt arguments in der services-Datei nicht explizit definieren muss. Haben Sie die Argumente in Ihrem Befehlskonstruktor korrekt eingegeben? – Cerad

Verwandte Themen