PHP

Posts about the server side scripting language PHP

Deleting Directories With Phing

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.

To delete a directory you need to use the dir attribute of the delete element, the delete element also accepts the file attribute to delete specific files. The following target will delete the directory myProject_build.

<target name="prepare">
<delete dir="myProject_build" />
</target>

The delete element can also accept a fileset element, which will delete multiple files. the following code will delete any php file from the myProject_build folder and from a sub folder called app.

FilterChain Element In Phing

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.

<?php
/**
 *
 * This is a comment
 *
 *
 **/
echo 'Hello World!';
 
// another comment.

These comments can be stripped out of the file by using the stripphpcomments element. This is added to the copy element in the following way.

Built In Properties In Phing

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, let's 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.

There are a lot of different properties available, so I have included the table from the phing website at the bottom of this post as a reference. However, there is one special variable called env that needs further explanation. The env variable references any environment variables that have been set. For example, if you set an environment variable using the following shell command (on UNIX based systems only).

export TESTVAR=mytestvar

You can now reference this in your build.xml file by using the following.

Using Custom Properties In Phing

Phing allows you to set up certain parameters that can be used throughout the rest of the script. These might be used to define a non standard build directory, or to store database connection details that can be written to the connection file during the build.

Properties are defined using the property element, which you should place at the top of your build.xml file in order to make it easy for other developers to see what is going on. The following example defines a property and the uses the main target to print the property out.

<?xml version="1.0"?>
<!-- build xml -->
<project name="myProject" default="main">
 <property name="property1" value="value1" />
 
 <target name="main">
  <echo>property1: ${property1}</echo>
 </target>
</project>

Running this build.xml file will product the following result.

Using Patternset With Fileset In Phing

When a project gets complicated then so to can the build.xml file associated with it. You might find it necessary to have multiple different filesets, each of which do something different, but all of which have the same core files that they use. Multiple filesets are useful if you want to create a version of your project with all of the testing files in place so that it can be tested by external developers.

Creating multiple filesets can create problems, one of which is maintenance. Confusion can also occur even if you have a single large fileset where lots of different rules for including and excluding files are defined.

This is where the patternset element comes in. You can create a set of reusable file lists that can be used by more than one fileset, but which also makes maintenance a little easier.

The following snippet shows two patternset elements that show include files and exclude files.

Force Browser To Refresh All Content Using PHP

One issue, especially when creating AJAX applications, is that the browser can cache the contents of the page so that when a similar request is made the same content is presented.

To force the browser to present the content you want it to without caching you can add the following headers to your page.

header("HTTP/1.1 202 Accepted");
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, proxy-revalidate, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Cache-Control: max-age=10000000, s-maxage=1000000");
header("Pragma: no-cache");

Remember to put this before you print out any content as you will get some nice looking errors otherwise.

Also, I wouldn't put this on every page on the site as it can create an overhead that just isn't needed.

Copying Files Using Phing

One of the main reasons to use Phing is to create a copy of your project in another directory that you can then use as your distribution copy. Your working directory might contain lots of testing code that is not needed in the final build.

To copy a file from one directory to another using Phing you need to use the copy element. Here is a simple example where a single file is copied from one directory to another.

Introduction To The Phing build.xml File

By default, Phing will look for a file in the current working directory called build.xml when you run it. This document tells Phing what it will be doing during the build. You can change this by using the -f or the -buildfile property of phing and giving the build file as the parameter. The following code makes phing look for a build file called wibble.xml.

phing -f wibble.xml

Assuming that the project is called myProject, then a minimal buildfile would be look like the following.

<!--?xml version="1.0"?-->
<!-- This is a comment -->
 
<project default="main" name="myProject">
  <target name="main">
</target>
</project>

Of course, this doesn't actually do anything at all, so it is utterly useless.

Getting And Installing Phing

To get Phing you will need to have PEAR installed along with PHP. On a Windows system you can install PEAR by running the go-pear.bat file and running through the prompts there.

To get Phing just run the following commands.

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

You should see the install output looking like this:

downloading phing-current.tgz ...
Starting to download phing-current.tgz (361,527 bytes)
.........................................................................done: 361,527 bytes
install ok: channel://pear.php.net/phing-2.1.1

You can now run Phing by typing the following command:

phing

However, this will automatically try to find a file in the current directory called build.xml, and if it doesn't find this file it will simply state the following before stopping:

Automated Build With Phing

Phing is a PHP tool that allows you to build projects. Phing stands for PHing Is Not Gnu make, which is kind of a common coder's joke. It is basically a tool that will take a set of files in multiple directories and create either a zip file or a numerous other things that you might want to do with a project.

Phing itself is a PHP program, so all that is needed to run it is a working copy of PHP. Also, because Phing is part of PEAR it is easy to install, and you don't need to install Apache to run it.

Over the next few posts I will be talking about the sort of things you can do with Phing, how to install it, how to create zip files and how to use variables to simplify things. I will add a list here for each of the posts.