Gaining application/module context from a symfony task
I have written a reporting suite, and I have a specific report that builds a CSV file. Serving this file via a browser on demand isn't an issue, but I need to be able to build this CSV file nightly, and email round a link to be able to download it.
Essentially, I need to be able to replace a specific action with a symfony task, run via cron. So how do I gain application/module context from a symfony task? And secondly, how would I invoke the SwiftMailer library from a symfony task?
I'm using symfony开发者_开发知识库 v1.4.4 and PHP v.5.2.13.
In the configure() function of your task, you need to define the application involved in your task :
$this->addOptions(array(
new sfCommandOption('application', null, sfCommandOption::PARAMETER_REQUIRED, 'The application name','frontend'),
new sfCommandOption('env', null, sfCommandOption::PARAMETER_REQUIRED, 'The environment', 'dev'),
new sfCommandOption('connection', null, sfCommandOption::PARAMETER_REQUIRED, 'The connection name', 'doctrine'),
));
And then you have to create the context in the execute() function :
sfContext::createInstance($this->configuration);
Finally you can call Swift very easily :
$this->getMailer()->composeAndSend($sender,$dest, $subject, $mailBody);
Let's assume we we have --application
and --env
CLI task options defined in taskName::configure()
method
Then receiving application context in taskName::execute()
method will look like:
$context = sfContext::createInstance(ProjectConfiguration::getApplicationConfiguration($options['application'], $options['env']))
精彩评论