Command line fun in CakePHP 1.2

Posted by Felix Geisendörfer, on Oct 16, 2006 - in PHP & CakePHP » Core & Hacking

Deprecated post

The authors of this post have marked it as deprecated. This means the information displayed is most likely outdated, inaccurate, boring or a combination of all three.

Policy: We never delete deprecated posts, but they are not listed in our categories or show up in the search anymore.

Comments: You can continue to leave comments on this post, but please consult Google or our search first if you want to get an answer ; ).

If you, like many others, already have dived into the 1.2 branch of CakePHP, you've probably already made some pleasent discoveries as far as upcoming features go. One of those cool new things that'll probably ship with the 1.2 release, is a new bake script, called bake2.php. Like the old one, it can be found in /cake/scripts/ and is called via your command line. But unlike the old bake.php, which was only used to auto-generate code for you, this one follows a much cooler concept. The main idea is to have an interface to a wide variety Tasks. Those Tasks can either be part of the CakePHP core and rest in /cake/scripts/tasks or of your individual application and go into /vendors/tasks. This will help us to automate things we used to do by hand. For example if you are developing an image uploader, you probably want to clear the upload folder and database table from time to time, because it get's filled up with too much crab in the process of development. Or if you are like me, you probably want to write a DeployTask that can be used to automate the upload of your website via SVN diff and FTP ; ).

So if you are curious to try out this new feature, here is how you would create a new Task:

First of all, create a new php file in /vendors/tasks. We'll call ours cake_task.php right now. So after you've created the file, fire up your IDE and enter this code:

php
  1. class CakeTask extends BakeTask
  2. {
  3.     function execute($params)
  4.     {
  5.         echo "CakePHP 1.2 will be very tasty!\n\n";
  6.         echo "This Task was called with".count($params)." parameters.\n";
  7.     }
  8. }

So, as you might have guessed, the $params parameter will contain an array with additional data that can be used by the Task to perform individual actions. In order to call our incredibly productive task, all we have to do is this (after switching to the /cake/scripts directory):

php -q bake2.php cake a b c

After hitting enter we'll be informed that our task was called with 3 parameters. Now even so this wasn't exactly a very useful example, I hope it was good enough for you to grasp the concept and possibilities this new feature offers, and I'm sure we'll see some good Tasks in the Bakery soon ; ).

--Felix Geisendörfer aka the_undefined

PS: I'll need some testers for the alpha version of the DeployTask I'm working on right now soon. So if you are willing to give it a try, let me know in the comments of this post.