symfony sfTask cheatsheet
Arguments / Options
Optional Argument:
-
$this->addArgument('recursionDepth', sfCommandArgument::OPTIONAL, 'Levels of recursion', 3);
Optional option
-
$this->addOption('baseUrl', null, sfCommandOption::PARAMETER_OPTIONAL, 'Base URL', 'https://www.example.com');
Flag, option without value, only true or false
-
$this->addOption('no-confirmation', null, sfCommandOption::PARAMETER_NONE, 'Skip confirmation question');
-
if (!$options['no-confirmation']) { ... }
logBlock
$this->logBlock($text, 'INFO');
- 'ERROR' => array('bg' => 'red', 'fg' => 'white', 'bold' => true),
- 'INFO' => array('fg' => 'green', 'bold' => true),
- 'COMMENT' => array('fg' => 'yellow'),
- 'QUESTION' => array('bg' => 'cyan', 'fg' => 'black', 'bold' => false),
Error Handling
Normally a sfTask returns shell exit status / error code 0.
In case of PHP exception and errors it returns 1.
The shell exit status of the last command can be checked with
echo $?
This is great for using sfTasks in cronjobs. Normally they are quiet, but send an email in case of errors with the following configuration:
vi /etc/crontab
MAILTO="errors@example.com" # m h dom mon dow user command # ullMail 25 4 * * * php /foo/bar/symfony ull_mail:process-bounced-emails > /dev/null
Create context and use doctrine in sfTask
Note: it seems that the "prod" environment is necessary for createInstance()
protected function execute($arguments = array(), $options = array()) { $this->logSection($this->name, 'Initializing'); $databaseManager = new sfDatabaseManager($this->configuration); sfContext::createInstance($this->configuration); sfContext::getInstance()->getConfiguration()->loadHelpers(array('ull')); ... }
Include libs from /apps/
$autoloader = sfSimpleAutoload::getInstance(); $autoloader->addDirectory(sfConfig::get('sf_lib_dir')); $autoloader->addDirectory(sfConfig::get('sf_app_lib_dir')); $autoloader->register();
Use Mailing
$this->initializeDatabaseConnection($arguments, $options); sfContext::createInstance($this->configuration); sfContext::getInstance()->getConfiguration()->loadHelpers(array('ull')); ullCoreTools::fixRoutingForCliAbsoluteUrls(); // Force symfony autoloader to load Swift classes $this->getMailer();
Get all Doctrine Models
Doctrine::loadModels(sfConfig::get('sf_lib_dir') . '/model/'); $modelNames = Doctrine::getLoadedModels();