2015-12-11 24 views
0

創建控制檯命令需要創建一個PHP文件,後綴爲Command.php。 例如,我想創建DocumentCommand.php。在同一文件中註冊多個控制檯命令

這裏是困難。我想在這裏插入3個命令,如:

document:unused:check 
document:unused:delete 
document:used:size 

什麼是如果可能的最好的方式做它在一個單一的文件(DocumentCommand.php)?

問候,R.

回答

0

試着這麼做:

class TestCommand extends ContainerAwareCommand 
{ 
    private $progress; 

    protected function configure() 
    { 
     $this 
      ->setName('acme:test') 
      ->setDescription('Write your description here.') 
      ->addArgument(
       'argument1', 
       InputArgument::REQUIRED, 
       'Which will be your first argument?' 
      )->addArgument(
       'argument2', 
       InputArgument::REQUIRED, 
       'Which will be your second argument?' 
      ) 
      ->addOption(
       'extra-option', 
       null, 
       InputOption::VALUE_NONE, 
       'Whether or not to do something optional' 
      ) 
     ; 
    } 
    protected function execute(InputInterface $input, OutputInterface $output) 
    { 

     $argument1 = $input->getArgument('argument1'); 
     $argument2 = $input->getArgument('argument2'); 

     $extraOption = $input->getArgument('extra-option'); 

     $output->writeln("Argument 1 is $argument1 , argument is $argument2"); 
     $output->writeln(PHP_EOL); 


     if ($extraOption != null) 
     { 
      $output->writeln("Extra option is selected"); 
      $output->writeln(PHP_EOL); 
     } 
     if($argument1 == 'unused' && $argument2 == 'check') 
     { 
      /* 
      * Do your stuff 
      */ 
     } 
     /** 
     * Do something here acording to the arguments passed 
     */ 

     return; 
    } 
} 

有了這個,你可以在終端運行(從項目目錄):

php app/console acme:test unused check --extra-option 

您可以添加更多的要求或根據您的需要選擇參數,並根據您的邏輯檢測未使用的檢查/刪除時執行某些操作。

編輯:(使用別名)

class TestCommand extends ContainerAwareCommand 
{ 
private $progress; 

protected function configure() 
{ 
    $this 
     ->setName('document:command') 
     ->setDescription('Write your description here.') 
     ->addArgument(
      'action', 
      InputArgument::REQUIRED, 
      'Which will be your first argument? (check/delete/size)' 
     ) 
     ->addOption(
      'extra-option', 
      null, 
      InputOption::VALUE_NONE, 
      'Whether or not to do something optional' 
     ) 
     ->setAliases(array('document:unused', 'document:used')) 
       ; 
} 
protected function execute(InputInterface $input, OutputInterface $output) 
{ 

    $action = $input->getArgument('action'); 

    $commandSelected = $input->getFirstArgument(); 

    $extraOption = $input->getArgument('extra-option'); 

    if ($commandSelected == 'document:unused') 
    { 
     /* 
     * Do something 
     */ 
    } 
    else if ($commandSelected == 'document:used') 
    { 
     /* 
     * Do something 
     */ 
    } 


    if ($extraOption != null) 
    { 
     $output->writeln("Extra option is selected"); 
     $output->writeln(PHP_EOL); 
    } 
    if($action== 'check') 
    { 
     /* 
     * Do your stuff 
     */ 
    } 
    else if($action == 'delete') 
    { 
     /* 
     * Do your stuff 
     */ 
    } 
    else if($action == 'size') 
    { 
     /* 
     * Do your stuff 
     */ 
    } 

    /** 
    * Do something here acording to the arguments passed 
    */ 

    return; 
} 
} 
+0

這是大約只需要一個命令「極致:測試」 :)我的問題是關於如何在一個文件中嵌入多個命令。 – Roland

+0

那麼,看看這個文件:「Symfony \ Component \ Console \ Command」如上圖所示,「acme:test」只是該命令的名稱屬性。你可以做的是寫一些別名到命令,如「acme:test:alias1」,並根據選定的別名做一些事情。這將是一種方法來實現你想要做的只有一個文件:)看到編輯我做了 –

+0

我真的appriciate你的嘗試,但它不是我想要的。除此之外,我做了一些研究,並且必須實現,沒有辦法在一個XYZCommand.php文件中放入多個命令。不得不接受,它需要爲每個eachen命令創建一個新的:( 無論如何,我真的非常感謝你的努力, – Roland