Add new tasks in your Symfony project
Par eNk` le vendredi, octobre 16 2009, 11:10 - Symfony - Lien permanent
So for my very first post in english I will talk about how to add a new task in symfony. Task are very useful to handle a lot of data and can be launched manually or placed in a cron job.
I started to create task on a new project, in the past I used to create a batch folder in my symfony project. In this folder I created a symfony.inc.php to load symfony and others php files which use the symfony.inc.php to handle data. So this way of creating task work, but symfony provide the sfBaseTask class so why don't use it ? This class allow you to add task to your symfony project in addition to those provided by the framework.
Let's see how does it work through a little exemple. The follwing exemple is quite stupid, but it's just to show the way you can create a task. So the aim of this task is to display a sentence given as an arguments for each user from your data base.
Basically you will have to create a class which extend from sfBaseTask and use the configure() method to set the name, the name space, the options and the arguments of your task. Then you will have to implement an execute() function to handle your data.
// myProject/lib/task/SayHelloTask.class.php
class SayHelloTask extends sfBaseTask
{
const ALL_USERS = -1;
public function configure()
{
// task's name, description and aliases
$this->name = "say-hello";
$this->namespace = "mytasks";
$this->briefDescription = "A little description of this task.";
$this->detailedDescription = "Detailed description, blablablabla blabla blabla bla.";
$this->aliases = array('mytasks-say-hello', 'sh');
// options
$this->addOptions(array(new sfCommandOption('env', null, sfCommandOption::PARAMETER_REQUIRED, 'The environement', 'prod')));
// arguments
$this->addArguments(array(new sfCommandArgument('message', sfCommandArgument::OPTIONAL, 'your message', 'Hello %s :)'),
new sfCommandArgument('number', sfCommandArgument::OPTIONAL, 'how many users ?', self::ALL_USERS)));
}
public function execute($arguments = array(), $options = array())
{
// data base manager to open a connection
$sfDatabaseManager = new sfDatabaseManager($this->configuration);
if(substr_count($arguments['message'], '%s') === 1)
{
// get users from data base
$users = UserTable::getAll();
$end = 0;
$nbUsers = count($users);
if($arguments['number'] === self::ALL_USERS)
{
$end = $nbUsers;
}
else
{
$nb = (int) $arguments['number'];
$end = ($nb <= $nbUsers) ? $nb : $nbUsers;
}
$i = 0;
while($i<$end)
{
$this->log(sprintf($arguments['message'], $users[$i]->getFullName()));
$i++;
}
}
else
{
$this->logBlock("Only one '%s' in the message.", 'ERROR');
}
}
}
To run this task you just have to execute this command line :
$ php symfony mytasks:say-hello OR $ php symfony mytasks:say-hello "Hi %s :p" OR $ php symfony sh "Hi %s :p"
Commentaires
Nice example, It clears me a lot of things about tasks (I don't use them so much). Thx for sharing!
Very good!
Hello,
Great post, thank you. Please write in englisch in future articles, too.
Your blog is in my rss reader now.
I'm from germany and don't speak french...