The symfony Cookbook

Comment créer une tâche

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.

Symfony Live 2010 Paris Conference

Chapter Content

Créons notre première tâche

Un peu d'interaction

Quelques fonctionnalités pratiques

symfony training
Be trained by symfony experts
Jul 22: Paris (1.2 + Doctrine - Français)
Aug 19: San Francisco (1.2 + Doctrine - English)
Sep 23: Paris (1.2 + Doctrine - Français)
Oct 21: Nantes (1.2 + Doctrine - Français)
Nov 18: Paris (1.2 + Doctrine - Français)
and more...

Search


powered by google
You are currently browsing "The symfony Cookbook" in French for the 1.1 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.

Comme la plupart des applications, votre projet peut comporter des travaux de maintenance, des opérations de base de données ou d'autres scripts lancés régulièrement en ligne de commande.

Symfony 1.1 enrichie les tâches pake de la version 1.0 créant ainsi un utilitaire de commande uniforme et puissant, complètement intégré au CLI (interface en ligne de commande) de symfony.

Créons notre première tâche

Dans le répertoire de votre projet symfony 1.1, tapez:

$ php symfony generate:task doNothing

Cela créera une tâche vide dans lib/task/doNothingTask.class.php. Améliorons-la un peu.

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

Pour l'instant, cette tâche ne fait pas grand chose, mais elle décrit bien les concepts de base:

Vous pouvez déjà la tester en jouant un peu avec:

$ php symfony help project:do-nothing
$ php symfony project:do-nothing

Un peu d'interaction

Les arguments et les options permettent de fournir des paramètres à la tâche.

$ php symfony project:hello-world --name="Romain"

Nous lançons ici la tâche project:hello-world avec l'option name défini par Romain.

$ php symfony project:hello-world Hi

A présent, nous lançons la même tâche avec comme premier argument, Hi.

Les options et les arguments peuvent avoir des valeurs par défaut, être optionnelles ou requises, et peuvent décrire eux-mêmes leur rôle, qui pourra être indiqué dans la syntaxe de la tâche.

Ecrivons notre tâche 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']));
  }
}

Regardez maintenant comment symfony renseigne l'utilisateur perdu sur l'utilisation cette nouvelle tâche:

$ php symfony project:hello-world invalid arguments given
$ php symfony help project:hello-world

Jouez également un peu avec les tâches suivantes:

$ 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

Quelques fonctionnalités pratiques

C'est la cerise sur le gâteau vous ne trouvez pas? Où plutôt, quelques touches jazzy sur fond de symphonie!

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.

The Sensio Labs Network

Since 1998, Sensio Labs has been promoting the Open-Source software movement by providing quality web application development, training, consulting, and supporting several large Open-Source projects.