Using app.yml options in your tasks

A few weeks ago I had a talk with @meandmymonkey about using app.yml in your symfony tasks. His special use case was that he wanted to make use of the options configured in his global app.yml. So we took a closer look at the task again and found something that turned out to be fix of our problem.

What happens when you generate a task

But first things first. When you generate a task using the following cli command:

 
./symfony generate:task sandbox:test-env
 

symfony will create the task at lib/task/sandboxEnvtestTask.class.php. This generated task will cover the following code to configure it's options:

 
<?php
 
class sandboxEnvtestTask extends sfBaseTask
{
  protected function configure()
  {
    $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
    ));
 
    // The rest the configure method...
  }
 
  // The execute method...
}
 
?>
 

Let's add an Application

If you use sfConfig::get() in this task you will have access to some basic options like sfconfigdir, etc. but you won't have access to the options configured in your global app.yml.

Adding access to your global app.yml is very easy. You might have guessed it but let's pretend you haven't. If you add a default application to the sfCommandOption instance that add's the 'application' option to your task you will have access to all app.yml options defined in this application. So if you change your code the following:

 
<?php
 
class sandboxEnvtestTask extends sfBaseTask
{
  protected function configure()
  {
    $this->addOptions(array(
      new sfCommandOption('application', null, sfCommandOption::PARAMETER_REQUIRED, 'The application name', 'frontend'),
      //all your other options...
    ));
 
    // The rest the configure method...
  }
 
  // The execute method...
}
 
?>
 

you will have access to all options defined in the app.yml of your 'frontend' application and your global app.yml. Of course you can also change the application if you run the task via cli:

 
./symfony generate:task sandbox:env-test --application=yourOtherApp
 



From: http://www.denderello.com/blog/using-app-yml-options-in-your-tasks
posted @ 2011-11-29 01:21  Lux.Y  阅读(298)  评论(0)    收藏  举报