The symfony Cookbook

タスクの作り方

You are currently browsing
the website for symfony 1

Visit the Symfony2 website


About

You are currently reading "The symfony Cookbook" which is licensed under the Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 Unported License license.

Master symfony

Be trained by SensioLabs experts (2 to 6 day sessions -- French or English).
trainings.sensiolabs.com

Books on symfony

Learn more about symfony with the official guides.
books.sensiolabs.com

L'audit Qualité par SensioLabs

200 points de contrôle de votre applicatif web.
audit.sensiolabs.com

Chapter Content

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

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

他の便利な機能

symfony training
Be trained by symfony experts
Feb 13: Paris (Web Development with Symfony2 - Français)
Feb 13: Paris (Getting Started with Symfony2 - Français)
Feb 15: Paris (Mastering Symfony2 - Français)
Feb 21: Köln (Getting Started with Symfony2 - English)
Feb 27: Köln (Mastering Symfony2 - English)
and more...

Search


powered by google
You are currently browsing "The symfony Cookbook" in Japanese for the 1.2 version - Switch to version: - Switch to language:
Creative Commons License This work is licensed under a Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 Unported License.
Translation of this work into another language is explicitly allowed.
This version of symfony is not maintained anymore.
If some of your projects still use this version, consider upgrading as soon as possible.

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

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

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

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

$ 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

他の便利な機能

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

Questions & Feedback

If you find a typo or an error, please register and open a ticket.

If you need support or have a technical question, please post to the official user mailing-list.