Add new tasks in your Symfony project
By eNk` on Friday, October 16 2009, 11:10 - Symfony - Permalink
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"
Comments
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...
Today I am very happy because i learned some useful principles of our life,let's share them as folloing:
Set a thief to catch a thief.
Send a wise man on an errand, and say nothing to him.
He that falls today may be up again tomorrow.
Great hopes make great ma.
Very good!
<a href="http://www.ndscardsale.com/DSTT">DSTT</a></p>
<a href="http://www.ndscardsale.com/DSTT">DSTT Card</a></p>
[url=http://www.edhardyclothing4u.com/]... hardy [/url]
[url=http://www.edhardyclothing4u.com/]... hardy uk [/url]
[url=http://www.edhardyclothing4u.com/]... hardy clothing [/url]
[url=http://www.edhardyclothing4u.com/Wo... hardy shirts[/url]
[url=http://www.edhardyclothing4u.com/]... ed hardy [/url]
[url=http://www.edhardyclothing4u.com/]... hardy shop [/url]
[url=http://www.edhardyclothing4u.com/]... hardy sale [/url]
[url=http://www.edhardyclothing4u.com/Wo... hardy t-shirts [/url]
[url=http://www.edhardyclothing4u.com/Ki... hardy kids[/url]
[url=http://www.edhardyclothing4u.com/]... hardy online [/url]
[url=http://www.edhardyclothing4u.com/]...
[url=http://www.edhardyclothing4u.com/]... clothing [/url]
[url=http://www.edhardyclothing4u.com/]... hardy case [/url]
[url=http://www.edhardyclothing4u.com/Ed... hardy iphone [/url]
[url=http://www.edhardyclothing4u.com/Me... hardy jeans [/url]
[url=http://www.edhardyclothing4u.com/Wo... hardy shoes [/url]
[url=http://www.edhardyclothing4u.com/Ba... hardy bags [/url]
[url=http://www.edhardyclothing4u.com/Wo... hardy hoodies [/url]
[url=http://www.edhardyclothing4u.com/Wo... hardy swimwear [/url]
[url=http://www.edhardyclothing4u.com/Su... hardy sunglasses [/url]
[url=http://www.edhardyclothing4u.com/Ca... hardy caps [/url]
[url=http://www.edhardyclothing4u.com/Me... hardy Swim Trunks[/url]
[url=http://www.edhardyclothing4u.com/]... uk[/url]
Efox-shop Grosshandel
[url=http://www.efox-shop.com/product_in... Apad[/url]
[url=http://www.efox-shop.com/product_in... Apad iRobo [/url]
[url=http://www.efox-shop.com/product_in... iRobot [/url]
[url=http://www.efox-shop.com/product_in... billig tablet pc [/url]
[url=http://www.efox-shop.com/product_in... billig mid [/url]
[url=http://www.efox-shop.com] Tablet Laptop[/url]
[url=http://www.efox-shop.com] Tablet PC [/url]
[url=http://www.efox-shop.com] Touchscreen Tablet Laptop [/url]
[url=http://www.efox-shop.com/product_in... Hipad [/url]
[url=http://www.efox-shop.com/mobileshop... Epad [/url]
[url=http://www.efox-shop.com/mobileshop... Opad [/url]
in http://www.efox-shop.com/.
Edward had a lot of money—I didn't even want to think about how much. Money meant next to nothing
to Edward or the rest of the Cullens. It was just something that accumulated when you had unlimited time
on your hands and a sister <a href="http://www.edhardy-discount.com/">ed hardy bags</a>
who had an uncanny ability to predict trends in the stock market. Edward
didn't seem to understand why I objected to him spending money on me—why it made me
uncomfortable if he took me to an expensive restaurant in Seattle, why he wasn't allowed to buy me a car
that could reach <a href="http://www.gobizfashion.com/">puma shoes cheap</a>
speeds over fifty-five miles an hour, or why I wouldn't let him pay my college tuition (he
was ridiculously enthusiastic about Plan B). Edward thought I was being unnecessarily difficult.
But how could I<a href="http://www.topjerseysworld.com/">discount GHD Hair Straighteners</a> let him give me things when I had nothing to reciprocate with? He, for some
unfathomable reason, wanted to be with me. Anything he gave me on top of that just threw us more out
of balance.
nice airticl, very great ! so cool ! I like it very much! Thank you for your sharing!
http://www.cheapairjordans.cc/
http://www.cheapairjordans.cc/air-j...
http://www.cheapairjordans.cc/jorda...
http://www.cheapairjordans.cc/jorda...