Development

Changeset 9641 for doc/branches/1.1/cookbook/en

You must first sign up to be able to contribute.

Show
Ignore:
Timestamp:
06/18/08 19:39:43 (5 years ago)
Author:
fabien
Message:

doc: fixed markdown

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • doc/branches/1.1/cookbook/en/tasks.txt

    r9640 r9641  
    2121-------------------------- 
    2222 
    23 First create a %SF_ROOT_DIR%/lib/task/doNothingTask.class.php file, and open it in your favorite editor. 
     23First create a `%SF_ROOT_DIR%/lib/task/doNothingTask.class.php` file, and open it in your favorite editor. 
    2424 
    25    [php] 
    26    class doNothingTask extends sfBaseTask 
    27    
    28      protected function configure() 
    29      
    30        $this->namespace        = 'project'; 
    31        $this->name             = 'do-nothing'; 
    32        $this->briefDescription = 'Does strictly nothing'; 
    33  
    34        $this->detailedDescription = <<<EOF 
    35    This task is completely useless, and should be run as often as possible. 
    36    EOF; 
    37      
    38  
    39      protected function execute($arguments = array(), $options = array()) 
    40      
    41        $this->logSection('do-nothing', 'I did nothing successfully!'); 
    42      
    43    
     25    [php] 
     26    class doNothingTask extends sfBaseTask 
     27   
     28      protected function configure() 
     29     
     30        $this->namespace        = 'project'; 
     31        $this->name             = 'do-nothing'; 
     32        $this->briefDescription = 'Does strictly nothing'; 
     33    
     34        $this->detailedDescription = <<<EOF 
     35    This task is completely useless, and should be run as often as possible. 
     36    EOF; 
     37     
     38    
     39      protected function execute($arguments = array(), $options = array()) 
     40     
     41        $this->logSection('do-nothing', 'I did nothing successfully!'); 
     42     
     43   
    4444 
    4545This task for sure does not much, but demonstrates the first basic concepts.: 
    4646 
    47  * The `configure()` method describes the task. Invocation name, scope, syntax, help, options and arguments. 
    48  * The `execute()` method is the one who does all the job actually, and will be called when the task is run. 
    49  * The `logSection()` method can be used to print messages on the console output. 
     47  * The `configure()` method describes the task. Invocation name, scope, syntax, help, options and arguments. 
     48  * The `execute()` method is the one who does all the job actually, and will be called when the task is run. 
     49  * The `logSection()` method can be used to print messages on the console output. 
    5050 
    5151You can play around a bit with it: 
    5252 
    53    $ php symfony help project:do-nothing 
    54    $ php symfony project:do-nothing 
     53    $ php symfony help project:do-nothing 
     54    $ php symfony project:do-nothing 
    5555 
    5656Some command line interaction 
     
    5959Arguments and options are the way to give parameters to a task. 
    6060 
    61    $ php symfony project:hello-world --name="Romain" 
     61    $ php symfony project:hello-world --name="Romain" 
    6262 
    6363Here we're running the `project:hello-world` task with the `name` option set to `Romain` 
    6464 
    65    $ php symfony project:hello-world Hi 
     65    $ php symfony project:hello-world Hi 
    6666 
    6767Now, we run the same task with the first argument set to `Hi`. 
     
    7171Let's write our `project:hello-world` task: 
    7272 
    73    [php] 
    74    class doHelloWorldTask extends sfBaseTask 
    75    
    76      protected function configure() 
    77      
    78        $this->addArgument('verb', sfCommandArgument::OPTIONAL, 'Customize the verb used to say hello', 'hello'); 
    79        $this->addOption('name', null, sfCommandOption::PARAMETER_OPTIONAL, 'Customize the person to say hello to', 'world'); 
    80  
    81        $this->namespace        = 'project'; 
    82        $this->name             = 'hello-world'; 
    83        $this->briefDescription = 'Spread the (hello) world'; 
    84  
    85        $this->detailedDescription = <<<EOF 
    86    Runs an evolved hello world display, with customisable name and word. 
    87    EOF; 
    88      
    89  
    90      protected function execute($arguments = array(), $options = array()) 
    91      
    92        $this->logSection('do', ucfirst($arguments['verb']).' '.ucfirst($options['name'])); 
    93      
    94    
     73    [php] 
     74    class doHelloWorldTask extends sfBaseTask 
     75   
     76      protected function configure() 
     77     
     78        $this->addArgument('verb', sfCommandArgument::OPTIONAL, 'Customize the verb used to say hello', 'hello'); 
     79        $this->addOption('name', null, sfCommandOption::PARAMETER_OPTIONAL, 'Customize the person to say hello to', 'world'); 
     80     
     81        $this->namespace        = 'project'; 
     82        $this->name             = 'hello-world'; 
     83        $this->briefDescription = 'Spread the (hello) world'; 
     84     
     85        $this->detailedDescription = <<<EOF 
     86    Runs an evolved hello world display, with customisable name and word. 
     87    EOF; 
     88     
     89     
     90      protected function execute($arguments = array(), $options = array()) 
     91     
     92        $this->logSection('do', ucfirst($arguments['verb']).' '.ucfirst($options['name'])); 
     93     
     94   
    9595 
    9696Now check out how symfony helps the lost user about how to use our new task: 
    9797 
    98    $ php symfony project:hello-world invalid arguments given 
    99    $ php symfony help project:hello-world 
     98    $ php symfony project:hello-world invalid arguments given 
     99    $ php symfony help project:hello-world 
    100100 
    101101And play a bit with the task: 
    102102 
    103    $ php symfony project:hello-world 
    104    $ php symfony project:hello-world --name="romain" 
    105    $ php symfony project:hello-world --name=romain hi 
    106    $ php symfony project:hello-world hi --name=romain 
     103    $ php symfony project:hello-world 
     104    $ php symfony project:hello-world --name="romain" 
     105    $ php symfony project:hello-world --name=romain hi 
     106    $ php symfony project:hello-world hi --name=romain 
    107107 
    108108Some other handy features 
    109109------------------------- 
    110110 
    111  * **Do you need the database layer?** 
     111  * **Do you need the database layer?** 
    112112 
    113113       [php] 
     
    119119       } 
    120120 
    121  * **Run another task within a task?** 
     121  * **Run another task within a task?** 
    122122 
    123123       [php] 
     
    125125       $myOtherTask->run($arguments = array('foo' => 'bar'), $options = array('far' => 'boo')); 
    126126 
    127  * **Need to let the user choose the environment, while providing a default one?** 
     127  * **Need to let the user choose the environment, while providing a default one?** 
    128128 
    129129   Just add the `env` option in the `::configure()` method and symfony will use its value as the environemnt. 
     
    132132       $this->addOption('env', null, sfCommandOption::PARAMETER_OPTIONAL, 'Changes the environment this task is run in', 'prod'); 
    133133 
    134  
    135134What do you think? Won't this new powerful batch subsystem greatly improve your project maintenance times?