TRAJOIN is an Application to Translate symfony documents Jointly.
home > 1.2/cookbook/en > tasks.txt
[1] Edit ↑TOPどのウェブアプリケーションであれ、プロジェクトには反復的なメンテナンスタスク、データベースオペレーション、もしくは定常業務で実行される他のコンソールスクリプトがあります。
Symfony 1.1は1.0のpakeタスクを拡張します。これによってプロジェクトのために強力で一貫したコマンドラインユーティリティを作り、symfonyコマンドラインインターフェイス(CLI - Command Line Interface)に完全に統合することができます。
symfony 1.1のプロジェクトディレクトリを開き次のコマンドを入力します:
$ php symfony generate:task doNothing
このコマンドを実行すればlib/task/doNothingTask.class.phpの中に空のタスクが作成されます。少し調整してみましょう。
class doNothingTask extends sfBaseTask
{
protected function configure()
{
$this->namespace = 'project';
$this->name = 'do-nothing';
$this->briefDescription = 'Does strictly nothing';
$this->detailedDescription = <<<EOF
This task is completely useless, and should be run as often as possible.
EOF;
}
protected function execute($arguments = array(), $options = array())
{
$this->logSection('do-nothing', 'I did nothing successfully!');
}
}
このタスクの内容は多くないですが、最初の基本概念を説明してくれます:
configure()メソッドはタスクの内容、起動名、スコープ、構文、ヘルプ、オプションと引数などを記述します。execute()メソッドはすべてのジョブを実際に実行し、タスクが実行されるときに呼び出されます。logSection()メソッドはメッセージをコンソールに出力するために使われます。これで少し遊んでみましょう:
$ php symfony help project:do-nothing
$ php symfony project:do-nothing
引数とオプションはタスクにパラメータを渡す手段です。
$ php symfony project:hello-world --name="Romain"
上記の例ではnameオプションにRomainをセットしてproject:hello-worldタスクを実行しています
$ php symfony project:hello-world Hi
今の例では、最初の引数をHiにセットして同じタスクを実行しました。
オプションと引数はオプションもしくは必須のデフォルト値を持ちタスク構文に表示される目的を埋め込むことができます。
project:hello-worldタスクを書いてみましょう:
class doHelloWorldTask extends sfBaseTask
{
protected function configure()
{
$this->addArgument('verb', sfCommandArgument::OPTIONAL, 'Customize the verb used to say hello', 'hello');
$this->addOption('name', null, sfCommandOption::PARAMETER_OPTIONAL, 'Customize the person to say hello to', 'world');
$this->namespace = 'project';
$this->name = 'hello-world';
$this->briefDescription = 'Spread the (hello) world';
$this->detailedDescription = <<<EOF
Runs an evolved hello world display, with customisable name and word.
EOF;
}
protected function execute($arguments = array(), $options = array())
{
$this->logSection('do', ucfirst($arguments['verb']).' '.ucfirst($options['name']));
}
}
新しいタスクの使い方がわからないユーザーのためにヘルプが表示されるか確認してみます:
$ php symfony project:hello-world invalid arguments given
$ php symfony help project:hello-world
そしてタスクで少し遊んでみます:
$ php symfony project:hello-world
$ php symfony project:hello-world --name="romain"
$ php symfony project:hello-world --name=romain hi
$ php symfony project:hello-world hi --name=romain
protected function execute($arguments = array(), $options = array())
{
$databaseManager = new sfDatabaseManager($this->configuration);
// ...
}
$myOtherTask = new myOtherTask($this->dispatcher, $this->formatter);
$myOtherTask->run($arguments = array('foo' => 'bar'), $options = array('far' => 'boo'));
::configure()メソッドにenvオプションを追加するだけでsymfonyはその値を環境として使用します。
$this->addOption('env', null, sfCommandOption::PARAMETER_OPTIONAL, 'Changes the environment this task is run in', 'prod');
どう思います?ケーキのチェリー、もしくは例えばsymfonyを越えたジャズ風のコーラスのように見えませんか?
Documentation
| What do you think? Isn ... | |
| brtRiver(2009-02-09 14:02:36) | |
| [php] $th ... | |
| brtRiver(2009-02-09 14:02:28) | |
| Just add the `env` opt ... | |
| brtRiver(2009-02-09 14:02:14) | |
| * **Need to let the use ... | |
| brtRiver(2009-02-09 14:02:00) | |
| [php] $my ... | |
| brtRiver(2009-02-09 14:01:43) | |
| * **Run another task wi ... | |
| brtRiver(2009-02-09 14:01:29) | |
| [php] pro ... | |
| brtRiver(2009-02-09 14:01:21) | |
| * **Do you need the dat ... | |
| brtRiver(2009-02-09 14:01:11) | |
| Some other handy features ... | |
| brtRiver(2009-02-09 14:01:00) | |
| $ php symfony project ... | |
| brtRiver(2009-02-09 14:00:48) | |
| And play a bit with the t ... | |
| brtRiver(2009-02-09 14:00:42) | |
| $ php symfony project ... | |
| brtRiver(2009-02-09 14:00:32) | |
| Now check out how symfony ... | |
| brtRiver(2009-02-09 14:00:22) | |
| [php] class doHel ... | |
| brtRiver(2009-02-09 14:00:00) | |
| Let's write our `pro ... | |
| brtRiver(2009-02-09 13:59:45) | |
| Options and arguments can ... | |
| brtRiver(2009-02-09 13:59:35) | |
| Now, we run the same task ... | |
| brtRiver(2009-02-09 13:59:20) | |
| $ php symfony project ... | |
| brtRiver(2009-02-09 13:59:09) | |
| Here we're running t ... | |
| brtRiver(2009-02-09 13:58:55) | |
| $ php symfony project ... | |
| brtRiver(2009-02-09 13:58:42) |