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.

Viewing images on the command line and the “No identify available” error

I’ve been testing a website that generates images on the fly and in the past had used the less command to view the file contents, this helped see when PHP errors had unfortunately made their way into an image file.

However, sometimes when viewing a file I got the following error returned:

No identify available
Install ImageMagick or GraphicsMagick to browse images

I’m pretty sure I worked this one out a few years ago, but had obviously forgotten. Turns out you can’t view binary files via a command like less!

The right way to view an image file is a command such as xxd. To view the top of a file (which usually points in the direction of the file format) use a command such as:

xxd /path/to/file.jpg | head

This command works just as well for text files, so will still pick up if PHP errors are inside the image file instead of the correct binary data.

Saving this one for later so I don’t forget again!

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.

Understanding the stack index for Zend Framework Controller plugins

Zend Framework Controller plugins are a powerful way to inject logic into your controller system at various points, such as before and after an action dispatch. Plugins are run in the order they are added, though it is possible to change the order by defining a custom stack index. ZF internal plugins such as Zend_Controller_Plugin_ErrorHandler, which displays a nice Error 404 page, has a stack index of 100 to ensure it runs near the end of any plugin cycle. However, it’s not so obvious from the ZF manual how to set a custom stack index.
Continue reading “Understanding the stack index for Zend Framework Controller plugins”

Multiple accounts for my.rackspace.com

We host most of our client sites at Rackspace so we have a fair few servers and different accounts we need to login with whenever accessing the my.rackspace.com portal.

While the site has a cookie to remember the last entered account number and username, it doesn’t help if you have half a dozen different accounts you need to login with on a frequent basis. Switching browsers can be impractical and remembering the account IDs can just get tiring.

To solve this problem I created the My.Rackspace bookmarklet for logging in with multiple accounts. When clicked it takes you to my.rackspace.com. If you’re already there, it displays a select list of your different accounts and populates the login form once you’ve chosen.
Continue reading “Multiple accounts for my.rackspace.com”

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