Setting up phpUnderControl (and CruiseControl) under debian etch

This might not be as useful since we finally have debian lenny available, but then again, maybe it will be useful for some people out there.

I had to setup phpUnderControl (and CruiseControl) at my job to manage our day to day work, and it took quite a bit of time to figure out how to setup everything properly from the ground up. This post is derived from my notes that we keep in our internal wiki.

  1. Edit /etc/apt/sources.list and make sure the unstable repo is listed in there
    deb http://ftp.debian.org/debian/ unstable non-free
    deb-src http://ftp.debian.org/debian/ unstable non-free
    
  2. apt-get update
  3. apt-get -t unstable install sun-java6-jre sun-java6-jdk
  4. Run “java -version” to verify the installation
    root@etch-64-dev-ui:~# java -version
    java version "1.6.0_10"
    Java(TM) SE Runtime Environment (build 1.6.0_10-b33)
    Java HotSpot(TM) 64-Bit Server VM (build 11.0-b15, mixed mode)
    
  5. Add JAVA_HOME to /etc/environment:
    JAVA_HOME="/usr/lib/jvm/java-6-sun"
    
  6. source /etc/environment
  7. Download binary distribution of CruiseControl (available here: http://cruisecontrol.sourceforge.net/download.html)
  8. Install CruiseControl:
    $ unzip cruisecontrol-bin-2.7.3.zip -d /opt
    $ ln -s /opt/cruisecontrol-bin-2.7.3 /opt/cruisecontrol
    
  9. Install PEAR (if it’s not already there):
    $ apt-get install php-pear
    
  10. Upgrade PEAR to latest release:
    $ pear upgrade-all
    
  11. Install PHPUnit and phpUnderControl:
    $ pear channel-discover pear.phpunit.de
    $ pear install phpunit/PHPUnit
    $ pear install --force --alldeps phpunit/phpUnderControl
    $ phpuc install /opt/cruisecontrol
    
  12. Install Xdebug (needed for code coverage analysis):
    $ pear install pecl/xdebug
    
  13. Add Xdebug zend extension to php.ini (on our case /etc/php5/cli/php.ini):
    zend_extension="/full/path/to/xdebug.so"
    
  14. Create a CruiseControl project for your existing PHP project’s unit tests:
    $ phpuc project --version-control svn --username "cruisecontrol" --password "password_here" --version-control-url svn://svn.domain.com/repository/project-name/trunk --test-case MyProjectTestSuite --test-dir "/opt/cruisecontrol/projects/my-project/source/unit_tests" --test-file "MyProjectTestSuite.php" --project-name MyProject /opt/cruisecontrol
    
  15. Edit projects/MyProject/build.xml so it contains the following content:
    <?xml version="1.0" encoding="UTF-8"?>
    <project name="MyProject" default="build" basedir=".">
      <target name="build" depends="php-documentor,php-codesniffer,phpunit"/>
      <target name="php-documentor">
        <exec executable="phpdoc" dir="${basedir}/source" logerror="on">
          <arg line="--title '${ant.project.name}' -i *pear/*,*Smarty/*,*fpdf/*,*jpgraph/* -ue on -t ${basedir}/build/api -d ${basedir}/source/lib -tb '/usr/share/php/data/phpUnderControl/data/phpdoc' -o HTML:Phpuc:phpuc"/>
        </exec>
      </target>
      <target name="php-codesniffer">
        <exec executable="phpcs" dir="${basedir}/source" output="${basedir}/build/logs/checkstyle.xml" error="/tmp/checkstyle.error.log">
          <arg line="--ignore=*/PEAR/*,*/Smarty/*,*/fpdf/*,*/jpgraph/*,*/misc/* --report=checkstyle --standard=PEAR ${basedir}/source"/>
        </exec>
      </target>
      <target name="phpunit">
        <exec executable="phpunit" dir="${basedir}/source/unit_tests" failonerror="on">
          <arg line=" --log-xml ${basedir}/build/logs/phpunit.xml --log-pmd ${basedir}/build/logs/phpunit.pmd.xml --coverage-xml ${basedir}/build/logs/phpunit.coverage.xml --coverage-html ${basedir}/build/coverage MyProjectTestSuite /opt/cruisecontrol/projects/MyProject/source/unit_tests/MyProjectTestSuite.php"/>
        </exec>
      </target>
    </project>
    

You will need to tweak a few things, like the “MyProject” name and probably some of the “ignore rules” for phpCodeSniffer, but it should be a great starting point for most projects.

Rasmus Lerdorf in Houston!

Rasmus Lerdorf, creator of the PHP language, will be in Houston in June and graciously volunteered to do a presentation to our group! Sorry about the last minute modification, but we will need to move our meeting from our usual date/time to Wednesday June 10th at 6:00pm. The presentation details below from Rasmus himself:

Architecture, Performance, Optimization and Security.

I take a look at the performance of some popular PHP applications and go
through and show the common mistakes people make and show how much of a
difference just a little bit of optimization makes.

Then, if there is still time and interest I’ll explain the current state
of XSS on the Web with some live examples of XSS issues on some local
sites. I usually like to have 2-3 hours, but I can squeeze it into less.

I hope to see you all here for this great opportunity! You can RSVP here.

–Joao

Customizing the background/border colors of a UITableView (grouped style)

Customizing the background color of a UITableView is somewhat easy, but only if you use the plain style. If you use the grouped style of a table view, it starts to get way more complex. There are no easy ways to quickly set a property for the border color and the background color of the whole table view cell. Well, there are, but if you try to change some of these values and try to use the grouped style, it will look completely wrong.

Below you have a proper grouped table view using the standard Apple theme:

Standard style

Looks pretty good, and it is also very simple to build that in objective-c. Assuming you use Interface Builder for your UI work, just set the table view as a “grouped”, and then set the title property of each cell, like this:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
    }
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
 
    NSArray *list = [NSArray arrayWithObjects:@"First", @"Second", @"Third", nil];
    cell.text = [list objectAtIndex:indexPath.row];
 
    return cell;
}

So now let’s try something tricky and simply change the backgroundColor property of cell.contentView, as that works fine when dealing with “plain” style table views. The code would be just a bit different:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
    }
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
 
    NSArray *list = [NSArray arrayWithObjects:@"First", @"Second", @"Third", nil];
    cell.text = [list objectAtIndex:indexPath.row];
 
    cell.contentView.backgroundColor = [UIColor redColor];
 
    return cell;
}

And this is what it looks like:

Broken results

At first I thought about changing cell.backgroundView, but that doesn’t work. So you can see that changing the contentView doesn’t work either, and it actually breaks a bunch of things:

  • No rounded corners on the left side of the table view anymore
  • The background color on the disclosure image is still set to white
  • How do we change the border color anyway?

So after researching this problem for a while, and asking around for a solution, it seemed like there was no way to customize this stuff without a lot of manual work. Indeed that was the case, but Mike Akers (another StackOverflow.com member) posted the source code to a solution that he came up with, and that works very well so far. You can see his source code here for a custom background view with a border.

Here it is a modified version of my code to use his class:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
    }
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
 
    UILabel *label = [[[UILabel alloc] init] autorelease];
    label.font = [UIFont boldSystemFontOfSize:16.0f];
    label.frame = CGRectMake(40.0f, 10.0f, 220.0f, 22.0f);
    label.textColor = [UIColor whiteColor];
    label.backgroundColor = [UIColor clearColor];
    label.opaque = NO;
 
    NSArray *list = [NSArray arrayWithObjects:@"First", @"Second", @"Third", nil];
    label.text = [list objectAtIndex:indexPath.row];
    [cell.contentView addSubview:label];
 
    CustomCellBackgroundView *bgView = [[CustomCellBackgroundView alloc] initWithFrame:CGRectZero];
    if (indexPath.row == 0) {
        bgView.position = CustomCellBackgroundViewPositionTop;
    } else if (indexPath.row == 1) {
        bgView.position = CustomCellBackgroundViewPositionMiddle;
    } else {
        bgView.position = CustomCellBackgroundViewPositionBottom;
    }
    cell.backgroundView = bgView;
    cell.accessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"more_arrow.png"]];
 
    return cell;
}

And the resulting interface:

Proper background and border colors

Ugly colors, but you get the idea. The code is also way more complex than before, but at least it’s possible to customize the background and border colors of a table view cell. This is more or less what we use on Knee Cap, our iPhone app that handles day to day money loans. This is what our custom table view looks like:

Knee Cap

iPhone SDK: Setting up a SQLite database before first use

It’s quite common to use SQLite databases in iPhone apps to serve as the backend for your product. While there is a way to create the database file dynamically from your objective-c code, it’s way simpler to create it in your Mac development machine, add it to your Xcode project, and then simply write the code to copy the database file from your app bundle to your app’s document directory.

I use the following code in my projects to do just that:

- (NSString *)copyDBToFinalPath {
    NSString *originalDBPath = [[NSBundle mainBundle] pathForResource:@"database_filename_here" ofType:@"db"];
    NSString *path = nil;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *appSupportDir = [paths objectAtIndex:0];
    NSString *appBundleName = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleName"];
    NSString *dbNameDir = [NSString stringWithFormat:@"%@/%@", appSupportDir, appBundleName];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    BOOL isDir = NO;
    BOOL dirExists = [fileManager fileExistsAtPath:dbNameDir isDirectory:&isDir];
    NSString *dbPath = [NSString stringWithFormat:@"%@/database_filename_here.db", dbNameDir];
    if (dirExists && isDir) {
        BOOL dbExists = [fileManager fileExistsAtPath:dbPath];
        if(!dbExists) {
            NSError *error = nil;
            BOOL success = [fileManager copyItemAtPath:originalDBPath toPath:dbPath error:&error];
            if (!success) {
                NSLog(@"error = %@", error);
            } else {
                path = dbPath;
            }
        } else {
            path = dbPath;
        }
    } else if (!dirExists) {
        NSError *error = nil;
        BOOL success =[fileManager createDirectoryAtPath:dbNameDir attributes:nil];
        if (!success) {
            NSLog(@"failed to create dir");
        }
        success = [fileManager copyItemAtPath:originalDBPath toPath:dbPath error:&error];
        if (!success) {
            NSLog(@"error = %@", error);
        } else {
            path = dbPath;
        }
    }
    return path;
}

I use that function like so:

- (void)applicationDidFinishLaunching:(UIApplication *)application {
    NSString *dbPath = [self copyDBToFinalPath];
    self.db = [FMDatabase databaseWithPath:dbPath];
    if (![self.db open]) {
        NSLog(@"Could not open database.");
    }
    //[self.db setTraceExecution:YES];
    //[self.db setLogsErrors:YES];
 
    // ...
    // rest of my code here
    // ...
}

To create that original SQLite database file, just use the standard “sqlite3″ command found in Mac OS X like so:

$ sqlite3 database_filename_here.db
SQLite version 3.4.0
Enter ".help" for instructions
sqlite> .read ./schema.sql

The “schema.sql” file is where I store my table definitions, and standard inserts.

My iPhone app is finally live!

primary Knee Cap, the iPhone app I have been working on for the past few weeks, has been approved by Apple and is finally for sale in the App Store.

I’m looking forward to see the reception it gets after all the hard work I put into it.

« Previous entries Next Page » Next Page »