Developing software in the Real World

Replacing a built-in PHP function when testing a component

Recently I needed to test part of Slim that uses the built-in PHP functions header() and headers_sent(). To do this, I took advantage of PHP's namespace resolution rules where it will find a function within the same namespace first before finding one with the same name in the global namespace. The idea of how to do this came courtesy of Matthew Weier O'Phinney where this approach is used for similar testing in Zend-Diactoros. This is… continue reading.

Analysing the focal length of my photos

I'm currently thinking about upgrading my camera to an EOS R or Z6 and, as result, I'm thinking about which lenses I should get. While discussing options with Stuart, I wondered which were my favourite focal lengths for the photos that I've taken in the past. To work this out, I decided to use the wonderful exiftool and some scripting. This is the script:

There's a couple of key things going on here.… continue reading.

Using .vimrc for project specific settings

I'm more of a spaces person than a tabs person when it comes to source code and in Vim, I like to see the tab characters, so I have this setting:

This places a handy ⇥ character so that I can see the tabs: I'm currently working on a codebase where the coding style is to use tabs, so I need to change my settings. One option is to use EditorConfig, for the formatting… continue reading.

Notes for working on the OpenWhisk PHP Runtime

These are some notes for working on the OpenWhisk PHP Runtime, but are probably applicable to the other runtimes too. Setting up I have a clone of the runtimes I'm interested in and core side-by-side in a directory. You then need various tools for development, which are documented here for macOS & Ubuntu in the Prerequites section. Build the container The PHP runtime creates two containers, one for PHP 7.1 and one for PHP 7.2.… continue reading.

Using Fractal as your OpenWhisk API's view layer

When writing an API, it's common to produce an output that conforms to a known media type such as JSON API or HAL, etc. I'm a strong believer that even though I'm writing an API, my application has a view layer. It's not the same as building an HTML page, but you still need to separate out the code that creates the structured output from your model layer. For a couple of APIs that I've… continue reading.

Dependency Injection with OpenWhisk PHP

Any non-trivial PHP applications use various components to do its work, from PDO though to classes from Packagist. It's fairly common in a standard PHP application to use Dependency Injection to configure and load these classes when necessary. How do we do this in a serverless environment such as OpenWhisk? This question comes up because we do not have a single entry point into our application, instead we have one entry point per action. If… continue reading.

Using API Gateway with Serverless & OpenWhisk

As with all serverless offerings OpenWhisk offers an API Gateway to provide HTTP routing to your serverless actions. This provides a number of advantages over web actions, the most significant of which are routing based on HTTP method, authentication and custom domains (in IBM Cloud). Creating routes with the wsk CLI To route to an action using API Gateway, you first need to make your action a web action first:

(You can also use… continue reading.

Using Composer with Serverless & OpenWhisk

Every PHP project I write has dependencies on components from Packagist and my Serverless OpenWhisk PHP projects are no different. It turns out that adding Composer dependencies is trivial. Let's create a simple action that converts a number to it's string form. e.g. 123 becomes one hundred and twenty three. We'll start with our simple ow-php-hello project from my earlier article and add a new function to serverless.yml:

Our handler is n2w.main, so we… continue reading.

Using Serverless Framework with OpenWhisk PHP

Serverless Framework is a toolkit to help you mange and deploy a serverless application. (Personally, I'm not a fan of the name as the word "Serverless" already has a meaning in the same space!) It's a useful tool and supports all the major providers, though AWS Lambda seems to be first-among-equals. The OpenWhisk plugin for Serverless is maintained by the rather excellent James Thomas, so if you have any questions, ping him! As I build… continue reading.

Invoking many OpenWhisk actions from another one

I have a project where I need to store a number of items into a data store. I have an OpenWhisk action that stores the items so I wrote an action that takes advantage of the OpenWhisk JS client library to do invoke my store action once for each item in an array that this action receives. This is the JavaScript code that I used:

This is a NodeJS v8 action (–kind nodejs:8 when… continue reading.