Subscribe to PHP Freaks RSS

And another phing…

syndicated from on July 31, 2017

Additional things learnt from developing the Mercurial tasks for phing, the php build system based on Java’s Ant tool.

Versions and compatibility.

As much as we might want to ignore versions of PHP that have been EOL-ed (ones
that are no longer supported officially by the good people at, phing
requires that code defining [new] tasks parse ok when linted by php 5.2.
In other words – namespaces are out, “use” is not to be used, short array
syntax is forbidden, the elvis operator has to leave the building etc etc.

Best thing to do, that doesn’t involve installing an age-old version of PHP?
Use Wim Godden’s excellent PHPCompatibility sniffs for PHP_CodeSniffer, and set
it to lint for 5.2. This doesn’t mean that new tasks developed for Phing 2.x have to work in PHP5.2, it just means that loading the main task code has to lint ok.
This is going to change in phing 3.0 where the minimum version of PHP is going to be 5.6, for those slackers that haven’t upgraded to PHP 7 yet.

Don’t reinvent.

In phing xml syntax, you can specify ‘truthy’ values as “yes”, “on”, “true”, “t”, “no”, “off”, “false” and “f”. The best way to handle all this is with:

$bValue = StringHelper::booleanValue($xmlValue);

Much neater than rolling your own, which I was guilty of earlier on! It’s interesting to note though that there is also a Project::toBoolean($value) static function which does something quite similar, but doesn’t consider ‘t’ or ‘f’ as truthy values.

There’s also a StringHelper::isBoolean function that could also be of use,
depending on what you need.

If you refactor things out to a base or abstract class, make sure that you
pull in that class

e.g. require_once ‘phing/tasks/ext/directory/Filename.php’

Internal Properties

There are a number of “internal properties” available, listed at – to get the values
of these inside a Task subclass:

$project = $this->getProject();
$dir = $project->getProperty(‘application.startdir’);

Setting Properties

If you’re at the stage of coding a phing extension/task, you’ll most certainly
know how to define a property in phing XML:

But, defining a phing property in PHP?
You can do it with either:
$project->setNewProperty(“prop”, “value”);
$project->setProperty(“existingProp”, “newValue”);
depending on what you want to do.

Update your grammar

The etc/phing-grammar.rng file is used in PHPStorm and other such IDEs to
provide syntax highlighting and validation for phing build.xml files – so you
can tell at a glance whether you might have typed in xmlint instead of xmllint,
for example.

So it makes sense especially so if you’re adding a new attribute to an existing
phing task or type to update this file.

If you’re adding properties that take default values, this can be indicated in the phing-grammar.rng file as can be seen in the definition for the truncate task:

 <define name="truncate">
     <element name="truncate">
         <attribute name="file"/>
             <attribute name="adjust"/>
             <attribute name="length"/>
     <attribute name="create" a:defaultValue="true">
         <data type="boolean"/>
         <attribute name="mkdirs" a:defaultValue="false">
             <data type="boolean"/>


That’s all for now – just a quick blog entry this time!