2016-08-02 54 views
1

我在symfony3上有很大的項目。在這個項目中我有ProjectFrameworkBundle。在「xxx」命名空間中沒有定義命令

項目/ FrameworkBundle /控制檯/ Command.php

abstract class Command extends ContainerAwareCommand 
{ 
    //... 

    protected function execute(InputInterface $input, OutputInterface $output) 
    { 
     // do some regular staff 
     $exitCode = $this->executeCommand($input, $output); 
     // do some regular staff 
    } 

    abstract protected function executeCommand(InputInterface $input, OutputInterface $output); 

    //... 
} 

如果我會做任何命令,從命令類擴展,它工作正常(測試)。

不過,我還有一個包

項目/ FrameworkQueue /控制檯/ Command.php

use Project\FrameworkBundle\Console\Command as BaseCommand; 
abstract class Command extends BaseCommand 
{ 
    // ... 
    protected function executeCommand(InputInterface $input, OutputInterface $output) 
    { 
     // do some regular staff 
     $exitCode = $this->executeJob($input, $output); 
     // do some regular staff 
    } 

    abstract protected function executeJob(InputInterface $input, OutputInterface $output); 

    // ... 
} 

所以,當我改變艾米命令從extends Project\FrameworkBundle\Console\Commandextends Project\QueueBundle\Console\Command從命令列​​表中隱藏。我試圖從executeCommand中刪除QueueBundle中的所有員工,但它對我沒有幫助。但是,如果我在這個命令中對php代碼犯了錯誤,我會看到異常。

有什麼不對?我的錯誤在哪裏,或者這是一個錯誤。我在哪裏可以找到收集和檢查可用命令的symfony代碼?

謝謝!

P.S.問題不在於文件或類命名 - 我多次檢查它。當然,當我改變父類時,我改變了函數名。

回答

1

問題是重寫__construct方法在QueueBundle\Console\Command 。如果你會嘗試這樣做:

public function __construct($name) 
{ 
    parent::__construct($name); 
} 

......它不會工作。我不知道爲什麼,但是我將一些邏輯從這裏移到了「執行前」操作。

謝謝大家!

1

如果命令擴展ContainerAwareCommand,Symfony甚至會注入容器。但是,如果沒有 - 您必須將您的命令註冊爲服務。

#應用程序/配置/ config.yml 服務:

app.command.your_command: 
     class: Project\FrameworkBundle\Command\Console\YourCommand 
     tags: 
      - { name: console.command } 

在編譯內核的Symfony按標籤console.command找到你的命令,並注入到應用程序命令列表

查看詳細信息關於這個話題,你可以檢查官方文檔 - https://symfony.com/doc/current/console/commands_as_services.html

+0

謝謝你花時間回答我的問題,但我剛剛找到原因。我會寫在下面。 –

+1

啊,好的。歡迎! – Rinat

相關問題