Otaqui.Com

Pete Otaqui’s blog about web development and everything else

Archive for the ‘Professional’ Category

Create A Custom CakePHP Console Application Using Shells, Tasks, Models and Controllers

leave a comment

CakePHP doesn’t just come with its own console applications for baking code, managing ACL, inspecting classes, manipulating the schema, internationalisation and running the testsuite (whew!) it also lets you write your own console applications.

[NB: I always have an external cake core directory, for easy swapping. The code examples all assume you have done this, or at least created an alias in your terminal to point to the core cake directory with something like: alias cake='../cake/console/cake'

First off, to find out about the current console applications you are running jump into a terminal and cd to your app directory, then simple run the “cake” command to see what you have available:

app_dir $ cake

This will output a list of available shells in the core cake library and the two “vendors” directories.

So how do you go about creating your own? The CakePHP documentation on creating Shells & Tasks is a good place to start.

An application I’ve needed to automate with a cron-job, and also have available as an in-app, on-demand function is the sending of emails. I’m not talking about writing a mass mailer here, but rather just sending out course information to users who are booked to attend – and doing so both automatically and on-demand when an administrator decides it is necessary. This is an obvious instance where you don’t want to duplicate the code in the controller and the shell.

In my case, I have several kinds of email I want to send, so I set up a generalised Task, used by several different Shells. The advantage of doing things this way around is that any other Shells can also make use of the emailer Task in the fututre.

Here’s some simplified code from one of the Shells, a “booking reminder” for delegates:

// file app/vendors/shells/booking_reminder.php
<?php
class BookingReminderShell extends Shell {
  var $uses = array("Booking","User","Course");
  var $tasks = array("Emailer");
  public function main() {
    // use $this->Booking etc as if we were in a controller
    // use $this->Emailer for the task
  }
}

And then we have the Task:

// file app/vendors/shells/tasks/emailer.php
<?php
class Emailer extends Shell {
  public function execute() {
    // this method is called when the task is instantiated,
    // gives you a chance to setup
  }
  public function send($to,$from,$subject,$message,$attachments=null) {
    // called from shell with: $this->Emailer->send();
  }
}

Note how both Shells and tasks extend from the base “Shell” class. Also note how Shells use a method called “main()” when they are run, whereas Tasks use “execute()” when they are created.

Written by pete

February 10th, 2010 at 10:43 pm

Posted in Professional

Tagged with ,

Comment And Uncomment All Lines in a Linux File with Sed

leave a comment

Just a couple of sed one-liners for adding and removing comments in the form of # marks (in the case of my ~/.ssh/config file). I use this to toggle proxy settings (with connect.c) and it’s run by the awesome MarcoPolo location-aware app for OS X. Both of these are safe to run repeatedly (you won’t end up with multiple # marks or anything).

First adding comments, which means a “#” mark at the start of every line
sed -i '' 's/^\([^#]\)/#\1/g' ~/.ssh/config

Second removing the comments, just stripping out the “#” marks.
sed -i '' 's/^#//g' ~/.ssh/config

A few things to note here, on the assumption that you can see how Regular Expressions work and are vaguely familiar with sed’s /find/replace/ style syntax:

  1. the “-i” flag means “edit in place” and requires an extra argument for the backup file’s extension. I’ve given an empty string so that no backup is made
  2. sed requires escaping of capturing parentheses, hence the \( and \) in the first example
  3. the final ‘g’ at the end of the expressions means “global”, i.e. replace all occurences

Written by pete

February 9th, 2010 at 5:21 pm

Posted in Professional

Tagged with ,

Using a Proxy with Cucumber, Webrat and Mechanize

one comment

If you’re writing Cucumber tests using Webrat and Mechanize to test a site, and you are behind a proxy server, you can do something like this to tell mechanize about it in your webrat_steps.rb file:


When /^I am on (.+)$/ do |page_name|
webrat.adapter.mechanize.set_proxy('proxy.host.com',8080)
visit path_to(page_name)
end

I’m sure there’s a tidier way to do this, but it’s quick and it works with the following gem versions:

Cucumber 0.3.11
Webrat 0.5.3
Mechanize 0.9.3

Written by pete

December 17th, 2009 at 4:15 pm

Posted in Professional

Tagged with , ,

Cucumber with Webrat and Mechanize on CentOS 5

leave a comment

Thanks to Chris for this one.

The trick is to use JRuby and its gems, and also to install libxml2-devel

$ cd ~
$ wget http://jruby.kenai.com/downloads/1.4.0/jruby-bin-1.4.0.tar.gz
$ tar -C /usr/local/ -xzvf jruby-bin-1.4.0.tar.gz

You should now have Jruby. Add it’s bin directory to your path, presumably in your ~/.bash_profile file:
$ export PATH=$PATH:/usr/local/jruby-1.4.0/bin
$ jruby -v

Now install some mechanize requirements:
$ sudo yum install libxml2-devel libxslt-devel

If you don’t already have the “normal” ruby installed, you can get to the jruby “gem” command by just typing “gem”. If you do have ruby, or want to be absolutely sure, type the commands like this:
$ jruby -S gem install cucumber mechanize webrat

You’re good to go! You should now be able to run your features with:

$ cucumber features/

Written by pete

November 20th, 2009 at 9:55 pm

Posted in Professional

Tagged with , ,

CakePHP’s basics.php functions – a series of really useful shortcut functions available globally in your CakePHP app

leave a comment

If you are working with CakePHP and haven’t checked it out yet, it’s very well worth looking through basics.php in the API docs.

As well as adding some PHP 5 functionality on PHP 4 servers, the script also adds a fair number of utility functions to the global namespace, such as:


// similar to print_r(), but dumps to the view and
// is disabled if debug isn't > 0 in config.php
pr($anything); 

// returns the correctly internationalised plural or
// singular depending on locale.
__n($singular, $plural, $count, $return);

Written by pete

February 24th, 2009 at 10:07 pm

Posted in Professional

Tagged with