The other day I needed to copy a Drupal project from my source folder to another folder, so rather than manually copy the files I decided to create a Phing script that would do it for me in one go. This Phing script will export your Drupal project into another directory, change the database credentials and create zip and tar files of the project. The first thing to do is create a properties, here is the contents of that file.
Automatic building with Phing makes deploying to a server nice and easy, and if you are using SVN as your source control system then you can easily deploy directly from your repository to your web server.
To allow Phing to utilise an SVN server you must first install the VersionControl_SVN pear library. Although this is in alpha release I have used it quite a bit with no issues. The only thing is that you will need to specify the version number to pear if you want to install it, like this:
Although most developers might not like it FTP is quite a common way of deploying the files for a site to the server. To simplify this process Phing comes with a handy FTP transfer action called ftpdeploy.
In order to use the ftpdeploy we first need to install the Net_FTP package. To install this package just enter the following command:
pear install Net_FTP
The first thing we need to create is a build.properties file to store our ftp details
When you run a phing script it will print things out to the console. These messages are either system messages (eg. BUILD STARTED) or echo messages that you have put into your build.xml file. All of this output is controlled and created by a logger file. The default logger is called (unsuprsingly) DefaultLogger and will be used as a default. There are a few different types of logger available, all of which can be found in the listener folder in your PEAR\phing directory.
PHPUnit is a unit testing framework, written in PHP, and which is used to test PHP code. You can integrate the testing that PHPUnit does into Phing. You might want to use Phing to create a nightly build that contains the latest version of your program. The last thing you want is Phing to create a nightly build that is riddled with errors.
The way around this is to use PHPUnit to test our code whilst we are running Phing. If any tests fail then Phing will not finish the build.
Building your projects into directories is nice, but distributing these projects is difficult if you have to build the compressed files yourself. Phing has the ability to create zip and tar files using simple commands.
The most convenient way of using the tar and zip commands is by using a fileset. But rather than use the fileset that was used to copy the files into the build directory it is best to create a separate fileset that is used to compress the contents of the build directory.
After following the last post on deleting files and directories in Phing you might now be wondering if you can create things as well. The answer is yes.
To create a directory with Phing you need to use the mkdir element. The dir attribute is used to give the name of the directory to be created. The following example will create a directory called myProject_build in the current working directory (ie. where you are running phing from).
Although using Phing is mainly about copying files, you might also need to delete directories and files using the delete element. Remember that the default behavior of copy command copies files only if the source files are different from the destination files. A prudent approach might be to delete the build directory and then recreate it, ready for Phing to copy files into.
The FilterChain element is where the power of Phing really comes into its own. This element will allow you to change the contents of the files of a fileset. This can range from a simple stripping of comments, to replacing values and numerous other filters.
One of the simplest thing that can be done with filterset is to strip all comments from the files in question. Take the following PHP file with two comments.
Aside from assigning and using your own properties Phing also comes with a set of built in properties that can be used to find out all sorts of information regarding the system that Phing is run on.
As an example, lets say you wanted to found out the operating system that phing is being run on. In this case you would use the variable host.os, which on a Windows XP system would print out WINNT.