Web DevCenter    
 Published on Web DevCenter (http://www.oreillynet.com/javascript/)
 See this if you're having trouble printing code examples


PHP with Apple's Developer Tools

by John David Duncan and Apple Developer Connection
11/27/2002

Apple provides a powerful set of developer tools with Mac OS X. Originally developed by NeXT for programmers working with Objective-C, the tools have since evolved for use with a wide range of source languages, including Java, C++, and AppleScript. The Mac OS X developer tools package also includes the system header files and a C compiler, so it is an essential addition for anyone who wants a complete BSD Unix environment. At the heart of Mac OS X's developer tools is a full-featured text editor and IDE called Project Builder.

Though normally used with applications built with the Carbon and Cocoa frameworks, Project Builder is an excellent tool for Web developers working in languages like PHP and Perl. Along with a standard Apple graphical interface, it also provides emacs-style key bindings that will be familiar to Unix users: keys combinations like Ctrl-A or Ctrl-E can be used to move the cursor to the beginning or end of a line. Its syntax colorizer recognizes comments and quoted strings in a variety of languages, making source code easier to read. It is tightly integrated with CVS, a software management system commonly used in the open source community. When dealing with a large number of files, Project Builder allows the developer to sort the files into groups for easier navigation. It offers multiple editor windows, split views, and a powerful multi-file search-and-replace facility that can search for either plain text strings or a regular expressions.

Other tools bundled in the developer package include GNU Autoconf, both the BSD and GNU versions of make, and a useful application called FileMerge, which provides a graphical interface to the Unix diff utility. Project Builder uses FileMerge to display the differences between two versions of a file and allows developers to merge changes from the CVS repository into a new version. Some of the best features in the Developer Tools turn out to be of little use for Web scripting, though, such as the graphical Interface Builder and the excellent interaction between Project Builder and the gdb debugger.

Related Reading

PHP Cookbook
By David Sklar, Adam Trachtenberg

I regularly use Project Builder and CVS to create and maintain complex PHP applications with dozens of source files. In this article, I'll illustrate some of the advantages of Project Builder and show some of the tricks I've learned.

Obtaining the Developer Tools

In the early 1990s, the developer release of NeXTSTEP sold for about $500 more than the standard user version. Today you can get the Developer Tools for little or no cost. The Developer Tools CD is included with most new Macs. Apple also gives the CD to new Apple Developer Connection members, and sells it for $20 from the ADC member's Web site at http://connect.apple.com/. If you have no CD, but a good Internet connection, you can choose to download the developer package from the ADC site; it's a hefty download, weighing in at 218MB.

As of this writing, the December 2001 release of the Developer Tools was the latest available version. For people running Mac OS X version 10.1 or higher, the oldest usable version is the September 2001 release; the previous (May 2001) release runs only on the original OS X 10.0.

Once you install the Developer Tools, Project Builder should be available under /Developer/Applications/, and you should be able to double-click on any file with a .c extension and have Project Builder open it. You could also create a new C source code file and open it from a Terminal window (using the open command) by typing:

    touch file.c
    open file.c

When you open a single file this way, it appears in Project Builder's editor. Project Builder's online help is available using the Command-? combination. You will most often use Project Builder with groups of files, or projects, and in order to see most of its features in action, you have to create a project.

Creating a Project

In order to have an example project to work with, you can retrieve a large Open Source PHP project from a CVS repository and then import its files into Project Builder. For this example I chose PEAR, a library of PHP classes available from http://pear.php.net, currently consisting of nearly 2,000 files. It takes a few shell commands to retrieve a working copy of PEAR. First you need to set the CVSROOT environment variable. If using the bash shell, issue this command:

    CVSROOT=:pserver:cvsread@cvs.php.net:/repository

If using tcsh, use this:

    setenv CVSROOT ":pserver:cvsread@cvs.php.net:/repository"

Then you can check out PEAR with the following commands.

    cvs login
    [enter password: anonymous]
    cvs checkout pear

The cvs checkout command will fetch the PEAR code from the repository. Now, launch Project Builder and create a new project, like this:

At this point, Project Builder's main window should appear. On the left you'll see the File and Groups window; on the right, an editor panel displaying some release notes. Above the text you will see four tabs labeled Find, Build, Run, and Debug. Of these, only the Find tab is useful for web developers; the rest are used to compile and test applications written in C or Objective-C.

Next, you can import all of the PEAR files into this new project with a single command.

The group relative reference style tells Project Builder to create a project structure that mimics the directory hierarchy, where each directory corresponds to a group. This is not the only way that Project Builder can use groups; a small C project might have one group for header files, one for C source files, and one for executables, even though all files are in a single directory. (For more documentation on creating and using groups see the Files topic in Project Builder's help.) Once you select Add, Project Builder will display all of the folders from the pear/ directory in its left panel. It will automatically see the project's CVS directories and enable CVS. Whenever you reveal a group, a small CVS Status... notice will appear in the bottom left corner of Project Builder's window as it queries the CVS server to check for updates on each of the group's files.

Editing

The table below shows some of Project Builder's text navigation key bindings. These keys may not be listed under the Edit menu, but they are supported by Cocoa's NSTextView, so they are available in almost every Cocoa application. Even a text input box on a Web form displayed in OmniWeb supports emacs-style navigation keys like Ctrl-A and Ctrl-E.

Alt-Left Arrow Move back one word
Alt-Right Arrow Move forward one word
Ctrl-A Move to beginning of line
Ctrl-E Move to end of line
Command-Up Arrow Move to top of file
Command-Down Arrow Move to end of file
Ctrl-H Delete previous character
Ctrl-D Delete next character
Alt-Delete Delete previous word

Related Reading

CVS Pocket Reference
By Gregor N. Purdy

The editor has a number of other noteworthy features. When you are editing a file, you get many levels of undo and redo, using the command-z and command-shift-z key combinations; it is even possible to undo changes back to a point prior to the time a file was last saved. When you need to see two files at once, command-" (command-shift-quote) will split an editor window in two, and command-' (command-quote) will later collapse the two editors back into one. To search for a string in the current editor, you can obtain a standard search-and-replace dialog box using command-f. Much more powerful, however, is the Project-wide Batch Find feature, which is available by selecting the Find tab above the editor panel or with the key combination command-shift-F. In the batch find panel you can search the entire project or even a customized subset of files for a simple text string or a complex regular expression.

As previously mentioned, one of Project Builder's best features is its integration with CVS and FileMerge. To see an example of this, open one of the files in the PEAR project, make some changes, and save them to disk. Then pull down the CVS menu and select Compare/Merge with latest revision. Project Builder will initiate a cvs diff command to compare the file to the version in the repository, and then display the differences in FileMerge. If you were authorized to commit changes to the repository, but needed to merge your changes with those of another developer, FileMerge would enable you to easily navigate through the changes using the up and down arrow keys, and then use the left and right arrow key to select one version or the other for the merged file.

What's in a Project

At this point, it's helpful to understand what Project Builder does behind the scenes. Close the PEAR window, quit Project Builder, and look in the directory where you downloaded the PEAR files. Project Builder has created one folder, called PHP-pear.pbproj, containing two files, project.pbxproj and username.pbxuser. The PHP-pear.pbproj directory is what Mac OS X refers to as a wrapper, a directory whose contents encapsulate a single document. The two pbxproj files inside it are both property lists: Unicode text files containing structured data. Many OS X property lists are XML files, but these two happen to be encoded in an older non-XML format. You can actually look inside these files using a few different tools. In the Terminal window where you originally fetched the CVS source, try this:

    cd PHP-pear.pbproj
    ls
    open -a TextEdit project.pbxproj
    open -a PropertyListEditor *.pbxuser

The TextEdit window shows the raw text of the project file (you could easily corrupt it, so be careful not to save any changes). The PropertyListEditor application allows you to navigate the tree structure of a property list or dump it into XML format. The contents of these files can be cryptic, but one thing is clear about them: the pbxproj file encodes the structure of the project itself (its files and groups ) while the pbxuser file contains your personal preferences and window coordinates. This distinction is significant if you want to share the project with other developers; you can commit the pbproj directory and pbxproj file into the CVS tree, but you should not commit the pbxuser file. When other developers retrieve the pbproj bundle from CVS and open it, Project Builder will silently create new pbxuser files for them.

Related Reading

Learning Cocoa with Objective-C
By James Duncan Davidson, Apple Computer, Inc.

Some Oddities

Project Builder was built primarily for Cocoa developers but has many features that are useful for Web developers. Using it outside its intended realm like this, though, one tends to find small items of annoyance here and there. (Some of them might be called "bugs," but then for the developers to address them might also be deemed "feature creep.") For instance, many Web projects contain plain HTML files, but when Project Builder sees a file with an .html extension it sometimes attempts to render the HTML, as it does with its Release Notes file, rather than let you edit the HTML source. However, if you create the HTML file in Project Builder itself (using File -- > New File), it seems that Project Builder will consistently open it for editing rather than rendering.

Another glitch for PHP developers concerns syntax coloring. In PHP, a string that is delimited by single quotes can contain literal double quote characters. The following is a legal line of code:

    $anchor = ' <a target="right" href="'; 

The unmatched double quote at the end of the string confuses Project Builder's syntax colorizer. You can put the colorizer back on track using a second double quote in a shell-style comment, like this:

    $anchor = '<a target="right" href="';  #"

Conclusion

Mac OS X is my environment of choice for developing PHP and Perl applications, primarily because Project Builder does a better job than any editor I know at making it easy to navigate a large collection of files. It helps me grasp the interdependencies in a big collection of source files, and I can always find the code I'm looking for quickly and easily. When running Apache and PHP on my Mac OS X machine, debugging becomes very easy. And when I decide to work on code in Java or C, I get to keep working in the same environment and take advantage of even more of Project Builder's features.

The best source of information on Project Builder is its online help. Apple also provides a Web page at http://developer.apple.com/tools/projectbuilder/, and there is a projectbuilder-users discussion list at http://lists.apple.com/.

John David Duncan works as a MySQL database administrator and Unix sysadmin, and as a freelance Web developer building data-driven sites with Apache and PHP.


Return to the Web Development DevCenter.

Copyright © 2009 O'Reilly Media, Inc.