3.12.1.2 Tasks

Tasks are small extensions to shells. They allow logic to be shared between shells, and are added to shells by using the special $tasks class variable. For example in the core bake shell, there are a number of tasks defined:

<?php 
class BakeShell extends Shell {
   var $tasks = array('Project', 'DbConfig', 'Model', 'View', 'Controller');
}
?>
  1. <?php
  2. class BakeShell extends Shell {
  3. var $tasks = array('Project', 'DbConfig', 'Model', 'View', 'Controller');
  4. }
  5. ?>

Tasks are stored in /vendors/shells/tasks/ in files named after their classes. So if we were to create a new ‘cool’ task. Class CoolTask (which extends Shell) would be placed in /vendors/shells/tasks/cool.php.

Each task must at least implement an execute() method - shells will call this method to start the task logic.

<?php
class SoundTask extends Shell {
   var $uses = array('Model'); // same as controller var $uses
   function execute() {}
}
?>
  1. <?php
  2. class SoundTask extends Shell {
  3. var $uses = array('Model'); // same as controller var $uses
  4. function execute() {}
  5. }
  6. ?>

You can access tasks inside your shell classes and execute them there:

<?php 
class SeaShell extends Shell // found in /vendors/shells/sea.php {
   var $tasks = array('Sound'); //found in /vendors/shells/tasks/sound.php
   function main() {
       $this->Sound->execute();
   }
}
?>
  1. <?php
  2. class SeaShell extends Shell // found in /vendors/shells/sea.php {
  3. var $tasks = array('Sound'); //found in /vendors/shells/tasks/sound.php
  4. function main() {
  5. $this->Sound->execute();
  6. }
  7. }
  8. ?>

A method called “sound” in the SeaShell class would override the ability to access the functionality in the Sound task specified in the $tasks array.

You can also access tasks directly from the command line:

$ cake sea sound