PHP Objects, Patterns and Practice (3rd edition)

(Barry) #1
CHAPTER 19 ■ AUTOMATED BUILD WITH PHING

FileSet


Let’s say that you need to represent a directory in your build file, a common situation as you might
imagine. You could use a property to represent this directory, certainly, but you’d run into problems
straightaway if your developers use different platforms that support distinct directory separators. The
answer is the FileSet data type. FileSet is platform independent, so if you represent a directory with
forward slashes in the path, they will be automatically translated behind the scenes into backslashes
when the build is run on a Windows machine. You can define a minimal fileset element like this:



As you can see, I use the dir attribute to set the directory I wish to represent. You can optionally add
an id attribute, so that you can refer to the fileset later on:



The FileSet data type is particularly useful in specifying types of documents to include or exclude.
When installing a set of files, you may not wish those that match a certain pattern to be included. You
can handle conditions like this in an excludes attribute:


<fileset dir="src/lib" id="srclib"
excludes="*/_test.php */Test.php" />


Notice the syntax I have used in the excludes attribute. Double asterisks represent any directory or
subdirectory within src/lib. A single asterisk represents zero or more characters. So I am specifying that
I would like to exclude files that end in _test.php or Test.php in all directories below the starting point
defined in the dir attribute. The excludes attribute accepts multiple patterns separated by white space.
I can apply the same syntax to an includes attribute. Perhaps my src/lib directories contain many
non-PHP files that are useful to developers but should not find their way into an installation. I could
exclude those files, of course, but it might be simpler just to define the kinds of files I can include. In this
case, if a file doesn’t end in .php, it isn’t going to be installed:


<fileset dir="src/lib" id="srclib"
excludes="/*_test.php */Test.php"
includes="
/*.php" />


As you build up include and exclude rules, your fileset element is likely to become overly long.
Luckily, you can pull out individual exclude rules and place each one in its own exclude subelement. You
can do the same for include rules. I can now rewrite my FileSet like this:







You can see some of the attributes of the fileset element in Table 19–3.
Free download pdf