facebook twitter hatena line email

Php/Symfony/Symfony2/batch

提供: 初心者エンジニアの簡易メモ
移動: 案内検索

vi src/Acme/HelloBundle/Command/SampleCommand.php

namespace Acme\HelloBundle\Command;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
class SampleCommand extends ContainerAwareCommand
{
   protected function configure()
   {
       $this
           ->setName('hello:sample')
           ->setDescription('Sample someone')
           ->addArgument('name', InputArgument::OPTIONAL, 'sample name?')
           ->addOption('yell', null, InputOption::VALUE_NONE, 'If set, the task will yell in uppercase letters')
       ;
   }
   protected function execute(InputInterface $input, OutputInterface $output)
   {
       $name = $input->getArgument('name');
       if ($name) {
           $text = 'Hello '.$name;
       } else {
           $text = 'Hello';
       }
       if ($input->getOption('yell')) {
           $text = strtoupper($text);
       }
       $output->writeln($text);
   }
}

バッチ実行コマンド&結果

$ app/console hello:sample taro
Hello taro

参考

http://docs.symfony.gr.jp/symfony2/cookbook/console.html