TRAJOIN is an Application to Translate symfony documents Jointly.

home > 1.2/cookbook/en > tasks.txt

[1] Edit ↑TOP

タスクの作り方


[2] Edit ↑TOP

どのウェブアプリケーションであれ、プロジェクトには反復的なメンテナンスタスク、データベースオペレーション、もしくは定常業務で実行される他のコンソールスクリプトがあります。


[3] Edit ↑TOP

Symfony 1.1は1.0のpakeタスクを拡張します。これによってプロジェクトのために強力で一貫したコマンドラインユーティリティを作り、symfonyコマンドラインインターフェイス(CLI - Command Line Interface)に完全に統合することができます。


[4] Edit ↑TOP
  • アクセシビリティ: 構文、説明、利用可能なオプションなどを調べるためのhelpパラメータがタスクに追加されます。誰でもあなたのタスクを実行できるようになります。
  • ユーザービリティ: symfony CLIを実行すればタスクの一覧が表示され、開発者でなくてもそれを実行する方法を簡単に学ぶことができます。
  • 統一性: すべてのオプションとパラメータを明示的に記述すればsymfony CLIがこれらを解析するので、$argvを解析するための面倒で反復的なタスクを気にしなくて済みます。間違った構文を使ったりパラメータが見つからなければ自動的に警告が表示されます。
  • 環境: 新しいProjectConfigurationクラスとApplicationConfigurationクラスのおかげで、コンテキストは完全にコントロールされます。決め打ちされた環境もしくはデバッグの設定に悩まなくて済みます。
  • 可読性: ソースコードを調べれば期待される入力とタスクの目的の説明を読むことができます。メンテナンスのために理解してとデバッグする時間は大いに減ります。

[5] Edit ↑TOP

最初のタスクを書いてみましょう


[6] Edit ↑TOP

symfony 1.1のプロジェクトディレクトリを開き次のコマンドを入力します:


[7] Edit ↑TOP
$ php symfony generate:task doNothing

[8] Edit ↑TOP

このコマンドを実行すればlib/task/doNothingTask.class.phpの中に空のタスクが作成されます。少し調整してみましょう。


[9] Edit ↑TOP

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!');
  }
}

[10] Edit ↑TOP

このタスクの内容は多くないですが、最初の基本概念を説明してくれます:


[11] Edit ↑TOP
  • configure()メソッドはタスクの内容、起動名、スコープ、構文、ヘルプ、オプションと引数などを記述します。
  • execute()メソッドはすべてのジョブを実際に実行し、タスクが実行されるときに呼び出されます。
  • logSection()メソッドはメッセージをコンソールに出力するために使われます。

[12] Edit ↑TOP

これで少し遊んでみましょう:


[13] Edit ↑TOP
$ php symfony help project:do-nothing
$ php symfony project:do-nothing

[14] Edit ↑TOP

いくつかのコマンドラインインタラクション


[15] Edit ↑TOP

引数とオプションはタスクにパラメータを渡す手段です。


[16] Edit ↑TOP
$ php symfony project:hello-world --name="Romain"

[17] Edit ↑TOP

上記の例ではnameオプションにRomainをセットしてproject:hello-worldタスクを実行しています


[18] Edit ↑TOP
$ php symfony project:hello-world Hi

[19] Edit ↑TOP

今の例では、最初の引数をHiにセットして同じタスクを実行しました。


[20] Edit ↑TOP

オプションと引数はオプションもしくは必須のデフォルト値を持ちタスク構文に表示される目的を埋め込むことができます。


[21] Edit ↑TOP

project:hello-worldタスクを書いてみましょう:


[22] Edit ↑TOP

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']));
  }
}

[23] Edit ↑TOP

新しいタスクの使い方がわからないユーザーのためにヘルプが表示されるか確認してみます:


[24] Edit ↑TOP
$ php symfony project:hello-world invalid arguments given
$ php symfony help project:hello-world

[25] Edit ↑TOP

そしてタスクで少し遊んでみます:


[26] Edit ↑TOP
$ 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

[27] Edit ↑TOP

他の便利な機能


[28] Edit ↑TOP
  • データベースレイヤーは必要ですか?

[29] Edit ↑TOP

    protected function execute($arguments = array(), $options = array())
    {
      $databaseManager = new sfDatabaseManager($this->configuration);

      // ...
    }

[30] Edit ↑TOP
  • タスクの範囲内で別のタスクを実行するには?

[31] Edit ↑TOP

    $myOtherTask = new myOtherTask($this->dispatcher, $this->formatter);
    $myOtherTask->run($arguments = array('foo' => 'bar'), $options = array('far' => 'boo'));

[32] Edit ↑TOP
  • デフォルトの環境を提供する一方で、ユーザーが環境を選べる必要がある場合は?

[33] Edit ↑TOP

::configure()メソッドにenvオプションを追加するだけでsymfonyはその値を環境として使用します。


[34] Edit ↑TOP

    $this->addOption('env', null, sfCommandOption::PARAMETER_OPTIONAL, 'Changes the environment this task is run in', 'prod');

[35] Edit ↑TOP

どう思います?ケーキのチェリー、もしくは例えばsymfonyを越えたジャズ風のコーラスのように見えませんか?


Comments

Menu

Documentation



Latest Histories

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)

Untranslated