Listing Composer dependencies in CSV format

We work with larger public sector organisations who often require lists of third party dependencies we use on projects. I took at look at the different ways you can list all Composer dependencies in a PHP project.

This command lists all PHP dependencies in a project:

composer show 

However, this displays as text. The compliance document I’m currently filling in is in Excel so ideally I need to copy this into Excel to send to the client.

You can also output your list of dependencies as JSON:

composer show --format=json

And then copy this into a tool such as Csvjson to convert to a CSV file.

I had to edit the JSON to convert to a flat CSV file, removing the root “installed” element. I edited the JSON from:

{
     "installed": [
         {
             "name": "brick/math",
             ...
         },
         ...
         {
             "name": "webmozart/assert",
             ...
         }
     ]
 }

To:

[
         {
             "name": "brick/math",
             ...
         },
         ...
         {
             "name": "webmozart/assert",
             ...
         }
]

The JSON to CSV tool then downloads a clean CSV file which I can import into Excel and supply to my client!

Another useful command is:

composer licenses

That outputs the open source license used for each package. This also supports JSON output so can be converted to CSV in the same way.

CSV output seems a useful feature to me, so I suggested this on the Composer discussion board. If others agree, I’ll see if I can contribute this. Otherwise I may look at building a small Composer plugin.

The front-end of Headless CMS

I wrote a blog on what Headless CMS is all about  on my agency site back in December. Partly to help explain to our clients what this technology is all about, partly to help crystalise some of my thoughts on the subject.

I’m currently embarking on an interesting project to build a set of tools to build a front-end website in PHP based on Headless CMS technology. My aim is to open source this front-end code, though we need to prove it first on a few projects.

I thought I’d blog about my experience and findings here. The following is a short technical focussed intro to all this. Continue reading “The front-end of Headless CMS”

PHP and me

PHP has just reached an incredible milestone, it’s 20 years old! On the 8th June, 1995, Rasmus Lerdorf released PHP 1.0. Now, 20 years later, PHP 7 is on the brink of being released and the language is stronger than its ever been before. With all the #20yearsofPHP stories around the internet I thought I’d add my own experiences and thoughts on how PHP has affected me.
Continue reading “PHP and me”

Checking your Zend Framework route order

The order that you create your routes in Zend Framework is important, with the last route defined in your code being matched first. This allows you to set up custom routes and if these aren’t matched Zend Framework helpfully falls back to the default route which is set up first. If you have a lot of routes though, set up in different places, it can get difficult to verify the order of your routes.

Just use this snippet of code in your controller to return a list of route names set up in your ZF application in the order they are matched via the routing system (i.e. the route at number 1 is matched first, then route 2, etc).

// Output list of routes, in the order they are matched
echo '<ol>';
$routes = array_reverse($this->getFrontController()->getRouter()->getRoutes());
foreach ($routes as $name => $route) {
    echo "	<li>$name</li>\n";
}
echo '</ol>';

Find out more about the ZF Router at the ZF manual.

Finding the Apache user in PHP

In PHP, knowing what the Apache user is on your webserver is very useful. Anything that writes a file to the server for example sessions, uploading files or other temporary file operations, needs to have the destination folder writeable by Apache. If you’re developing locally that’s not usually a problem. But as soon as you’re out in the real world file permission issues can sometimes be a pain.

If you’re having difficulties finding out what the Apache user is try this simple script to help you out.

It basically writes a small temporary file, checks the owner username, gets rid of the file and prints the Apache username to the screen. It should work on PHP4 and PHP5.

$tmpFilename = tempnam('/tmp', 'TEST');
$handle = fopen($tmpFilename, 'w');
fwrite($handle, 'testdata');
fclose($handle);
$info = posix_getpwuid(fileowner($tmpFilename));
$apacheUser = $info['name'];
unlink($tmpFilename);
echo "The Apache user is $apacheUser";

There’s also a small PHP source file you can easily download and use: getApacheUser.phps