PHP Objects, Patterns and Practice (3rd edition)

(Barry) #1

■Chapter 19: Automated Build with Phing ..........................................................


for shell commands. This makes it hard to write programs that will install across platforms. Not all
environments will have the same version of make, or even have it at all. Even if you have make, you may
not have all the commands the makefile (the configuration file that drives make) requires.
Phing’s relationship with make is illustrated in its name: Phing stands for PHing Is Not Gnu make.
This playful recursion is a common coder’s joke (for example, GNU itself stands for Gnu is Not Unix).
Phing is a native PHP application that interprets a user-created XML file in order to perform
operations on a project. Such operations would typically involve the copying of files from a distribution
directory to various destination directories, but there is much more to Phing. Phing can be used to
generate documentation, run tests, invoke commands, run arbitrary PHP code, create PEAR packages,
replace keywords in files, strip comments, and generate tar/gzipped package releases. Even if Phing does
not yet do what you need, it is designed to be easily extensible.
Because Phing is itself a PHP application, all you need to run it is a recent PHP engine. Since
Phing is an application for installing PHP applications, the presence of a PHP executable is a
reasonably safe bet.
You have seen that PEAR packages are breathtakingly easy to install. PEAR supports its own
automated build mechanism. Since PEAR is bundled with PHP, should you not use the PEAR
mechanism to install your own projects? Ultimately the answer to this is yes. PEAR makes installation
easy, and supports dependencies well (so that you can ensure your packages are compatible with one
another). There’s a lot of tough work that must be automated during development, up to and including
package creation. This technique, to use Phing for project development but to have it generate a PEAR
package upon release, is used to produce the Phing application itself.


Getting and Installing Phing


If it is difficult to install an install tool, then something is surely wrong! However, assuming that you have
PHP 5 or better on your system (and if you haven’t, this isn’t the book for you!), installation of Phing
could not be easier.
You can acquire and install Phing with two simple commands.


$ pear channel-discover pear.phing.info
$ pear install phing/phing


This will install Phing as a PEAR package. You should have write permission for your PEAR
directories, which, on most Unix or Linux systems, will mean running the command as the root user.
If you run into any installation problems, you should visit the download page at
http://phing.info/trac/wiki/Users/Download. You will find plenty of installation instructions there.


Composing the Build Document


You should now be ready to get cracking with Phing! Let’s test things out:


$ phing -v
Phing version 2.4.0


The -v flag to the phing command causes the script to return version information. By the time you
read this, the version number may have changed, but you should see a similar message when you run
the command on your system.
Now I’ll run the phing command without arguments:


$ phing
Buildfile: build.xml does not exist!

Free download pdf