Ken Guest’s online diary

July 31, 2017

And another phing…

Filed under: phing,php — kenguest @ 20:57

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 php.net), 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
https://www.phing.info/docs/guide/stable/app.factsheet.html – 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”);
or
$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">
     <interleave>
         <attribute name="file"/>
         <optional>
             <attribute name="adjust"/>
         </optional>
         <optional>
             <attribute name="length"/>
         </optional>
     <optional>
     <attribute name="create" a:defaultValue="true">
         <data type="boolean"/>
     </attribute>
     </optional>
     <optional>
         <attribute name="mkdirs" a:defaultValue="false">
             <data type="boolean"/>
         </attribute>
     </optional>
     </interleave>
     </element>
 </define>

 

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

Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.