PHP Objects, Patterns and Practice (3rd edition)

(Barry) #1

CHAPTER 19 ■ AUTOMATED BUILD WITH PHING


Let’s sneak a peek of the next section and place the filterchain element in a task:









The Copy task is probably the one you get most use out of. It copies files from place to place. As you
can see, I define the destination directory in the todir attribute. The source of the files is defined by the
fileset element I created in the previous section. Then comes the filterchain element. Any file copied
by the Copy task will have this transformation applied to it.
Phing supports filters for many operations including stripping new lines (StripLineBreaks) and
replacing tabs with spaces (TabToSpaces). There is even an XsltFilter for applying XSLT transformations
to source files! Perhaps the most commonly used filter, though, is ReplaceTokens. This allows you to
swap tokens in your source code for properties defined in your build file, pulled from environment
variables, or passed in on the command line. This is very useful for customizing an installation. It’s a
good idea to centralize your tokens into a central configuration file for easy overview of the variable
aspects of your project.
ReplaceTokens optionally accepts two attributes, begintoken and endtoken. You can use these to
define the characters that delineate token boundaries. If you omit these, Phing will assume the default
character of @. In order to recognize and replace tokens, you must add token elements to the
replacetokens element. Now to add a replacetokens element to my example:













As you can see, token elements require key and value attributes. Let’s see the effect of running this
task with its transformations on a file in my project. The original file lives in a source directory,
src/lib/Config.php:


/**



  • Quick and dirty Conf class
    **/
    class Config {
    public $dbname ="@dbname@";
    public $dbpass ="@dbpass@";
    public $dbhost ="@dbhost@";
    }


Running my main target containing the Copy task defined previously gives the following output:
Free download pdf