Pragmatism in the real world

Passing on the baton

Lorna Mitchell has posted Joind.in Needs Help: For the last 6 years I've been a maintainer of this project, following a year or two of being a contributor. Over the last few months, myself and my comaintainer Rob Allen have been mostly inactive due to other commitments, and we have agreed it's time to step aside and let others take up the baton. I'm proud of my contributions to joind.in as a contributor and maintainer.… continue reading.

Checklist for releasing Slim

Release process for Slim so that I don't forget any steps; based on a check list created by Asgrim. I should probably automate some of this! Preparation: Ensure all merged PRs have been associated to the tag we're about to release.Find them via this search: [is:pr is:closed no:milestone is:merged]. Close the milestone on GitHub. Create a new milestone for the next patch release. Ensure that you have the latest master & that your index is… continue reading.

Standalone Doctrine Migrations redux

Since, I last wrote about using the Doctrine project's Migrations tool independently of Doctrine's ORM, it's now stable and much easier to get going with. Installation and configuration As with all good PHP tools, we simply use Composer: $ composer require –dev doctrine/migrations This will install the tool and a script is placed into vendor/bin/doctrine-migrations. To configure, we need to add two files to the root of our project: migrations.yml: name: Migrations migrations_namespace: Migrations table_name:… continue reading.

View header and body with curl

I recently discovered the -i switch to curl! I have no idea why I didn't know about this before… Curl is one of those tools that every developer should know. It's universal and tends to be available everywhere. When developing APIs, I prefer to use curl to view the output of a request like this: $ curl -v -H "Accept: application/json" https://api.joind.in/ * Trying 178.208.42.30… * Connected to api.joind.in (178.208.42.30) port 443 (#0) * TLS… continue reading.

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.