Using sfTask-based Classes

Introduction

Tasks are classes designed to perform one process or action. For example, the Doctrine plugin for Symfony has individual tasks for generating model classes, building database tables, and populating the database with initial data. All Symfony tasks have the @sfTask@ abstract class as their base, but task classes can extend other task classes.

A task class accepts two types of parameters, arguments and options, which are provided using the @addArguments@ and @addOptions@ methods. A task class also requires a dispatcher and a formatter class to be provided to be instantiated.

Calling a Task From PHP Code

All @sfTask@-derived classes can be called in the following manner:

$task = new sfTask($dispatcher, $formatter);
$task->addArguments(array());
$task->addOptions(array());
$task->run();

All methods and properties of derived classes are generally private or protected, so you have to use the base class's public methods to work with the classes that extend @sfTask@.

To pass multiple arguments or options, use the @addArguments()@ and @addOptions()@ methods, both of which take an array as the argument. If you only need to add one argument or option, you can use the @addArgument()@ or @addOption()@ methods.

If you don't already have a dispatcher and formatter declared, you can create placeholder ones like so:

$dispatcher = new sfEventDispatcher();
$formatter = new sfFormatter();

Example

Here is an example of calling @sfDoctrineConfigureDatabaseTask@:

$arguments = array(
  'task' => 'configure:database',
  'dsn' => $dsnString,
  'username' => $dbUser,
  'password' => $dbPass
);

$options = array(
  'help' => null,
  'quiet' => null,
  'trace' => null,
  'version' => null,
  'color' => null,
  'env' => 'all',
  'name' => 'doctrine',
  'class' => 'sfDoctrineDatabase',
  'app' => null
);

$dispatcher = new sfEventDispatcher();
$formatter = new sfFormatter();

$dbConfig = new sfDoctrineConfigureDatabaseTask($dispatcher, $formatter);
$dbConfig->addArguments($arguments);
$dbConfig->addOptions($options);
$dbConfig->run();

QR Code
QR Code agasti:developer:sftask_based_classes (generated for current page)