PHP Objects, Patterns and Practice (3rd edition)

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

property is not set, the target will exit silently. Here, I comment out the dbpass property and make the
main task require it using the if attribute:






database: ${dbname}
pass: ${dbpass}
host: ${dbhost}

Let’s run phing again:

$ phing
Buildfile: /home/bob/working/megaquiz/build.xml


megaquiz > main:


BUILD FINISHED


Total time: 0.2628 seconds


As you can see, I have raised no error, but the main task did not run. Why might I want to do this?
There is another way of setting properties in a project. They can be specified on the command line. You
tell Phing that you are passing it a property with the -D flag followed by a property assignment. So the
argument should look like this:


-Dname=value


In my example, I want the dbname property to be made available via the command line:

$ phing -Ddbpass=userset
Buildfile: /home/bob/working/megaquiz/build.xml


megaquiz > main:


[echo] database: megaquiz
[echo] pass: userset
[echo] host: localhost


BUILD FINISHED


Total time: 0.4611 seconds


The if attribute of the main target is satisfied that the dbpass property is present, and the target is
allowed to execute.
As you might expect, the unless attribute is the opposite of if. If a property is set and it is referenced in
a target’s unless attribute, then the target will not run. This is useful if you want to make it possible to
suppress a particular target from the command line. So I might add something like this to the main target:



main will be executed unless a suppressmain property is present:

$ phing -Dsuppressmain=yes

Free download pdf