Symfony, tasks and return values

In symfony, it's possible to return a value in your execute() method.

Let's create a test task :

┌─(yoda@box)(09:46:59)
└─(~/var/www/test)-> ./symfony generate:task test
>> task      Creating "/home/yoda/var/www/test.../testTask.class.php" task file

Adding a return value at the end of the method. :

<?php
 
class testTask extends sfBaseTask
{
  protected function configure()
  {
    // // add your own arguments here
    // $this->addArguments(array(
    //   new sfCommandArgument('my_arg', sfCommandArgument::REQUIRED, 'My argument'),
    // ));
 
    $this->addOptions(array(
      new sfCommandOption('application', null, sfCommandOption::PARAMETER_REQUIRED, 'The application name'),
      new sfCommandOption('env', null, sfCommandOption::PARAMETER_REQUIRED, 'The environment', 'dev'),
      new sfCommandOption('connection', null, sfCommandOption::PARAMETER_REQUIRED, 'The connection name', 'doctrine'),
      // add your own options here
    ));
 
    $this->namespace        = '';
    $this->name             = 'test';
    $this->briefDescription = '';
    $this->detailedDescription = <<<EOF
The [test|INFO] task does things.
Call it with:
 
  [php symfony test|INFO]|>
EOF;
  }
 
  protected function execute($arguments = array(), $options = array())
  {
    // initialize the database connection
    $databaseManager = new sfDatabaseManager($this->configuration);
    $connection = $databaseManager->getDatabase($options['connection'] ? $options['connection'] : null)->getConnection();
 
    // add your code here
    return 42;
  }
}

And now, execute the task :

┌─(yoda@box)(09:47:32)
└─(~/var/www/test)-> ./symfony test
┌─(yoda@box)(09:47:40)
└─(~/var/www/test)-> echo $?
42

The return value is correctly returned. Note that without return value, the task simply returns 0.

In the case that an exception occurs, let's see what is returned. I added a dummy exception in the execute method :

<?php
 
class testTask extends sfBaseTask
{
  protected function configure()
  {
    // // add your own arguments here
    // $this->addArguments(array(
    //   new sfCommandArgument('my_arg', sfCommandArgument::REQUIRED, 'My argument'),
    // ));
 
    $this->addOptions(array(
      new sfCommandOption('application', null, sfCommandOption::PARAMETER_REQUIRED, 'The application name'),
      new sfCommandOption('env', null, sfCommandOption::PARAMETER_REQUIRED, 'The environment', 'dev'),
      new sfCommandOption('connection', null, sfCommandOption::PARAMETER_REQUIRED, 'The connection name', 'doctrine'),
      // add your own options here
    ));
 
    $this->namespace        = '';
    $this->name             = 'test';
    $this->briefDescription = '';
    $this->detailedDescription = <<<EOF
The [test|INFO] task does things.
Call it with:
 
  [php symfony test|INFO]|>
EOF;
  }
 
  protected function execute($arguments = array(), $options = array())
  {
    // initialize the database connection
    $databaseManager = new sfDatabaseManager($this->configuration);
    $connection = $databaseManager->getDatabase($options['connection'] ? $options['connection'] : null)->getConnection();
 
    // add your code here
    throw new Exception('foo');
    return 42;
  }
}
┌─(yoda@box)(09:51:37)
└─(~/var/www/test)-> ./symfony test

               
  foo         
               

┌─(yoda@box)(09:51:43)
└─(~/var/www/test)-> echo $?
1

The task returns 1 in case of an exception.

Haut de page