Shells and Tasks
Last week I wrote about the new command line features of CakePHP 1.2. And this week it is already outdated what I wrote then ;-) The command line scripts are now called “Shells” instead of “CakeScripts”, so I adapted the previous article to those changes.
With the recent changes a new concept has been introduced: Tasks. Well, tasks are not entirely new as tasks were already used by bake2 (which no longer exists). But now tasks are for shells what components are for controllers.
Tasks are placed in app/vendors/shells/tasks if they are application-specific resp. in vendors/shells/tasks if they are general-purpose tasks. Like shell scripts, task classes have to extend the “Shell” class, but the class name has to end with “Task”. Here a simple example of a task:
// vendors/shells/tasks/test.php
class TestTask extends Shell {
function execute() {
// do something
}
}
In the core tasks (and in this test task) the main function is called “execute”, but you are free to use any name you want.
The test task can now be used in the shell script in the following way:
// vendors/shells/demo.php
class DemoShell extends Shell {
var $tasks = array('Test');
function initialize() {
// empty
}
function main() {
$this->Test->execute();
}
}
Happy baking :)




I noticed all these changes, but am a little unsure as to why there are still tasks when it seems that shells do the same thing. I am rewriting my migrations and fixtures tasks to work with the new console/shell system, and was simply going to add them as shell scripts in /vendors/shells.
Do you why there are still tasks when shell scripts can do the same thing?
>Joel Moss
and the notes task ! ;)
thx for the heads up dho
i cant keep up with the changes myself ;)
Shells are designed to manage full CLI-based applications, whereas the purpose of Tasks are to break out shell logic into discrete, reusable commands.
@kabturek: on its way, although since I switched to Mac and textmate, I no longer have a need for the notes task.
@Nate: hhmmm, seems like a bit of unneeded duplication to me.
Joel: I guess if you think components are unnecessary duplication of controllers, then yeah.
@nate: valid point ;)
Joel:
Bake is a good example of why tasks are useful.
example:
$ cake bake
the above command will run through bake in interactive mode, if it does not find the database.php it will run the DbConfigTask.
whereas:
$ cake bake db_config
will just run the DbConfigTask
For something like Acl there are no tasks because everything can be done through methods in the Shell.
$ cake acl create aro null John
will run the “create” method in the AclShell
Complete docs coming soon.
@all: Thanks for your comments :)
I suppose tasks are just a way of breaking up a console script into smaller tasks. Which is more manageable, but the same can still be achieved with one console script and a method for each task.
But each to their own ;)
@Joel: Well, the advantage of using tasks over using multiple methods is that tasks can be reused in other shell scripts.
Hey this is very cool, indeed, but i have been trying to find out how to use; if it is possible?, my models in app/models
Has anyone tried to??
@Diego: Hm, I don’t understand what you try to accomplish…
@cakebaker: Hi there! thanx for your help, there aren’t much examples out there, what i am trying to do is the following, from a shell script, i would like to access some data stored in the DB, Lets say the POSTS model for example, trying to get innactive posts or something like that. I’ve tried looking in the ACL shell without much success.
@Diego: You can use something like:
loadModel('Post'); $this->Post = new Post(); $this->Post->findAll();HTH
hi,
i want to write a console app which will do from cron stuff my web interface can do interactively. SO i don’t want my code in shells or tasks. i want my shells and my controllers to use the same stuff, the models and the components. unless my controllers can also run tasks, i would only write stuff in tasks which is only useful from the command line.
so do you think controllers can also run tasks or the code should go in a component or a vendor class?
@Debbie: I think it should be no problem to use a task inside a controller as long as you don’t use command line specific things in your task like reading user input from the command line. And you probably have to include the required files manually as it was not planned to use tasks in that way ;-)
Good luck!