Phing

Posts about the PHP build package Phing

Phing Logo

Changing Memory Allocation In Phing

7th August 2013 - 3 minutes read time

Running complex tasks in Phing can mean running out of memory, especially when altering or changing lots of files. I was recently working on a image sorting Phing project that sorted images based on EXIF information. The many thousands of files involved, along with the custom target used to extract the EXIF data caused the default available memory to run out quite quickly.


<php expression="ini_set('memory_limit', '1G');"></php>

There is no direct way to alter the PHP memory limit setting through Phing, but it can easily be altered using a Phing php task. This evaluates the PHP function ini_set() and set the memory_limit value. The following Phing task sets this limit to be 1G, or 1 gigabyte.

Phing Logo

Listing Phing Targets In A Project

28th June 2013 - 5 minutes read time

Providing a Phing build file along with a project is a good way of allowing automation of certain aspects of the project. The only trouble is that users won't know what's in the build file unless they open it or just run it. You could provide documentation along with the build file so that users know what to use the file for, but a better approach is to list out the targets available in a project. This can be done easily by using the -l (lower case L) or list flag, which will just list the available targets in the supplied build file.

Running this on a build file will produce this sort of output from a build file with two targets, one of which is run as a default.

Phing Logo

Control Structures In Phing

2nd June 2013 - 22 minutes read time

Phing has a few different tasks and elements that allow you to select paths of code execution depending on what you need to happen in a build file. These are limited to loops and if statements, but a lot of functionality can be covered with just a couple of lines of XML.

Phing Logo

Source Controlled Git Hooks With Phing

11th May 2013 - 7 minutes read time

The other day I was experimenting with Git hooks. These are scripts that you can execute before certain actions are run in Git. For example, you might want to ensure that forced updates are not run, ensuring respository files have the correct permissions after merging, or that the files have ASCII standard names before being committed.

To use a hook in Git you just need to add them to the .git/hooks directory in your respository and to change the mode of the file so that it is executable. A new Git repository will create several sample hook files that can be used by removing the '.sample' from the end and making them executable. For more information on Git hooks and how to use them see the Git hooks manual page in the Git documentation.

Phing Logo

Detecting The Sudo User In Phing

10th April 2013 - 11 minutes read time

I use Phing for a lot of different tasks, it helps me to automate things that I would otherwise mess up if left to my own devices. Prime candidates for Phing scripts are things that I don't do that much and forget how to do them, or that have a number of complex steps. The only problem I have found is that because many of the Phing scripts I create rely on system changes (eg, configuring an Apache server) they therefore require system changing privileges. Normally I would just prefix the Phing command with sudo, but every now and then I forget all about that step and the build fails. This can be dangerous as I am then left with a build that failed, which might leave a system partly configured or even take a server offline.

Phing Logo

Checking Syntax Errors In PHP And JavaScript Using Phing

29th October 2012 - 8 minutes read time

Checking Syntax Errors In PHP And JavaScript Using Phing

Running a simple syntax check over your files is a good way to save time. This can be when testing code but best practice is to not to even commit code that contains syntax errors.

You can syntax check a single file using the -l (lowercase L) flag with the PHP executable like this.


$ php -l file.php

Unfortunately this can only check one file at a time so I set about trying to find a good way of checking a whole project at once. There are a couple of scripts available on the internet, but I set about creating my own solution using the phplint task in Phing. This means that I can just create a fileset and feed this into the phplint task without having to rewrite the whole thing if I wanted to include (or exclude) a particular directory or file.

Phing Logo

Using Phing To Create Apache Virtual Hosts

6th April 2012 - 12 minutes read time

Phing is an awesome tool for automating things and I use it more and more for automating all kinds of different tasks. One of the tasks that I don't tend to do all that much is setting up a new local virtual host for Apache on my development machines. I know how to do it, but there is always something I forget to do, or a convention that I don't follow which means that I have to repeat myself at a later date to fix something I have missed.

So to make my life a little easier I decided to create a Phing build file to automatically create a virtual host and the everything associated with it in one go. Essentially, I would need to do the following tasks:

Phing Logo

Adding A Context Menu Item For Phing On Windows

21st March 2012 - 2 minutes read time

To make things a bit quicker when using Phing on Windows use the following registry entry to create a right click option that integrates with Phing.


Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\*\shell]

[HKEY_CLASSES_ROOT\*\shell\Phing]
@="Phing It!"

[HKEY_CLASSES_ROOT\*\shell\Phing\command]
@="cmd.exe /K phing -f \"%1\""

Save the above text into a file with a .reg file extension and run it by double clicking on it (you can also right click and select Merge). You will get a message asking if you want to add information to the registry, click "Yes" to allow the entry.

Phing Logo

Using SSH and SCP in Phing

29th October 2011 - 9 minutes read time

Phing allows the running of SSH commands on servers and the copying of files to servers via SCP. Before you can use SSH and SCP commands in Phing you need to install the PECL extension SSH2. The SSH2 PECL extension requires the libssh2 package, so you need to install that before you can get started. The following install instructions are based on a Linux environment.

Download the libssh2 package from www.libssh2.org and install it by using the following commands. Your package version may vary.


sudo tar -zxvf libssh2-1.3.0.tar.gz
cd libssh2-1.3.0
./configure
make
sudo make install

You'll need the sudo on the last command to allow the make installer access to certain system directories.

Next, you need to install the SSH2 PECL extension, but because it's currently in a beta state you will need to give the full path to the library version along with the normal install command.

Phing Logo

Getting Time And Date Values In Phing

10th June 2011 - 6 minutes read time

The tstamp task in Phing can be used to generate a timestamp that can be used anywhere within the current project. The default behaviour of tstamp is to create the properties DSTAMP, TSTAMP and TODAY, which contain time and date values. All you need to do is include the tstamp XML tag in your project somewhere.

<tstamp>
</tstamp>

The properties DSTAMP, TSTAMP and TODAY use the PHP strftime() function as a basis for formatting the time. The following table shows the value of each property.