The symfony Cookbook

How to create a task

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

Let's write our first task

Some command line interaction

Some other handy features

symfony training
Be trained by symfony experts
Feb 06: Paris (Mastering & Doctrine - Français)
Feb 13: Paris (Web Development with Symfony2 - Français)
Feb 13: Paris (Getting Started with Symfony2 - Français)
Feb 13: Berlin (Mastering Symfony2 - Deutsch)
Feb 13: Frankfurt (Mastering Symfony2 - Deutsch)
and more...

Search


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

As any web application, your project has repetitive maintenance tasks, database operations, or other console scripts running on a regular basis.

Symfony 1.1 extends symfony 1.0 pake tasks to create a powerful and uniform command line utility for your projects, fully integrated with the symfony Command Line Interface (CLI).

Let's write our first task

Open your symfony 1.1 project directory and type:

$ php symfony generate:task doNothing

It will bootstrap an empty task in lib/task/doNothingTask.class.php. Let's tune it a bit.

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

This task for sure does not much, but demonstrates the first basic concepts.:

You can play around a bit with it:

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

Some command line interaction

Arguments and options are the way to give parameters to a task.

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

Here we're running the project:hello-world task with the name option set to Romain

$ php symfony project:hello-world Hi

Now, we run the same task with the first argument set to Hi.

Options and arguments can have default values, be optional or required and embed their purpose to be displayed in task syntax.

Let's write our project:hello-world task:

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

Now check out how symfony helps the lost user about how to use our new task:

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

And play a bit with the task:

$ 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

Some other handy features

What do you think? Isn't this some cherry on the cake, or for instance, some jazzy chorus over the 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.