Pragmatism in the real world

Filtering the PSR-7 body in middleware

Sometimes, there's a requirement to alter the data in the Response's body after it has been created by your controller action. For example, we may want to ensure that our brand name is consistently capitalised. One way to do this is to create middleware that looks like this: $brandFilter = function ($request, $response, $next) { // call next middleware $response = $next($request, $response); $content = (string)$response->getBody(); $newContent = str_ireplace('nineteen feet', 'Nineteen Feet', $content); $response->getBody()->rewind(); $response->getBody()->write($newContent);… continue reading.

Slim 3.4.0 now provides PSR-7!

I've been neglecting Slim's PR queue recently, so this weekend I dedicated a lot of time to merging all the good work that our contributors have done. As a result, I'm delighted to release version 3.4.0! This release has a larger set of changes in it than I would have ideally liked which is a direct consequence of having gone two months between releases rather than one. One particularly interesting addition that we have a… continue reading.

Overriding Slim 3's error handling

Slim 3 registers two error handers: errorHandler to trap PHP Exceptions phpErrorHandler to trap PHP 7 Errors This means that if you want to override the default error handlers, you need to override both error handlers with your own code. Each error handler is a callable. The signatures are: errorHandler: function ($request, $response, $exception) phpErrorHandler: function ($request, $response, $error) To override a error handler, simply register a new callable with the Container: $container = $app->getContainer();… continue reading.

A few composer tips

I recently learned about a couple of features of composer that I thought I'd write down here so that I don't forget them! I also had to deal with a conflict in composer.lock recently, so I've noted down how I solved that too. List installed versions To list the current versions of all installed dependencies: composer show -i The output looks something like: container-interop/container-interop 1.1.0 Promoting the interoperability of container objects… monolog/monolog 1.17.2 Sends your… continue reading.

Determining the image type of a file

One thing I learnt recently which I probably should have known already is that getimagesize() returns more than just the width and height of the image. I've always used it like this: list($width, $height) = getimagesize($filename); However, getimagesize() also returns up to 5 more pieces of information. Interestingly, the data array is a mix of indexed elements and named elements For example, for a file I uploaded while testing a PR, the output of print_r(getimagesize($filename))… continue reading.

The internal pointer of an array

I discovered recently that if you walk through an array using array_walk or array_walk_recursive, then the array's internal pointer is left at the end of the array. Clearly this isn't something that I've needed to know before! This code example shows the fundamentals: $var = [ 'a' => 'a', 'b' => 'b', ]; array_walk($var, function ($value) { }); var_dump(key($var)); The output is NULL and you use reset() to put the internal pointed back to the… continue reading.

Using Composer with shared hosting

I can't use Composer because I'm using shared hosting and don't have SSH I've seen this sentiment a few times now, so this seems like a good time to point out that you do not need SSH access to your server in order to use Composer. In fact, I don't run Composer on a live server (regardless of whether it's using shared hosting) and it's not on my list of things to do in the… continue reading.

Running Phan against Slim 3

Having installed Phan, I decided to use it against the upcoming Slim 3 codebase. Phan needs a list of files to scan, and the place I started was with Lorna's article on Generating a file list for Phan. I started by removing all the developer dependencies: $ composer update –no-dev and then built the file list for the vendor and Slim directories. I started by using Lorna's grep statement, but that found too many non-PHP… continue reading.

Installing Phan on OS X

I use Homebrew for my local PHP installation on OS X and am currently running PHP 7.0.0 RC8. Phan is a static analyser for PHP 7 which was written by Rasmus and then rewritten by Andrew Morrison. As it benefits from PHP 7's abstract syntax tree it can find all kinds of subtle errors, so I wanted to install it locally to have a play with it. I started by trying to install Phan into… continue reading.

Writing PSR-7 middleware

Within Slim 3's Request object, there's a method called getIp() which is determines the client's IP address. However it's rather simplistic and potentially risky as it checks the X-Forwarded-For header with no ability to ignore this header or whitelist whether we trust the final proxy in the chain. Determining the client's IP address is an ideal use-case for middleware as we can inspect the headers in the request and then set an attribute so that… continue reading.