Pragmatism in the real world

Easily install Swift on Linux

This is the simplest set of steps that I've found so far in order to get Swift on (Ubuntu) Linux. Essentially, you can download a snapshot and you're good to go. If you're not already on Ubuntu 15:10, create a VM. Vagrant is an easy way to do this: $ vagrant init ubuntu/wily64 $ vagrant up $ vagrant ssh Install the dependencies: $ sudo apt-get install clang libicu-dev vim (Vim isn't an actual dependency, but… continue reading.

Unit testing with Swift PM

As I tend to work with Swift on Linux, I've been working out how to unit test using the Swift Package manager. This article is my way of remembering what I've learnt so far! Let's do this in the context of writing an a Todo entity from the Todo-Backend project. Create a package Creating a package is easy with Swift PM: mkdir Todo cd Todo swift build –init library (you can replace library with executable… 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.

Using Eloquent in Slim Framework

Eloquent is one of the easier ORMs to get started with, so let's look at how to use and test it within a Slim Framework application. Set up You can add Eloquent to your Slim project via composer: composer require illuminate/database You now need to bootstrap it. To do this we need some settings, so lets do that in our standard Slim configuration: $config = [ 'settings' => [ 'db' => [ // Eloquent configuration… continue reading.

Free the Geek appearance

I was delighted to spend an hour chatting with Matt Setter on episode 14 of Free the Geek. In this podcast, we talked about Slim 3 and development in general. Matthew gives me a great introduction (thanks!) and then we delve into the chat. We talk about the importance of semantic versioning as I think this is key to stability in a project. It's not very glamorous to work on code where you have to… 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.

Configuration in Slim Framework

Configuration in Slim Framework is nice and simple: the App's constructor takes a configuration array for the DI container; $config = []; $app = new Slim\App($config); Setting up The settings sub-array is used to hold the settings of your application: $config = [ 'settings' => [ 'displayErrorDetails' => true, 'logger' => [ 'name' => 'slim-app', 'level' => Monolog\Logger::DEBUG, 'path' => __DIR__ . '/../logs/app.log', ], ] ]; $app = new Slim\App($config); Slim comes with a number… continue reading.

Testing Slim Framework actions

To test a Slim Framework action, you need a request and a response object and mock whatever is in the action. This is one way to do this. Consider this simple echo action that returns the query parameters to you as a JSON encoded string: $ curl "http://localhost:8888/echo?foo=bar&this=that" {"foo":"bar","this":"that"} This is one of those useful API endpoints that your users can use to check that everything is working as expected. The action under test The… 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.

Improved error handling in Slim 3.2.0

We released Slim 3.2.0 yesterday which includes a number of minor bug fixes since 3.1.0 and also a few nice improvements around the way we handle errors. Writing to the error log Slim has a simple exception handler implemented that displays an error page that looks like this: It's not very informative, is it? That's intentional as we don't want to leak information on a live website. To display the error information you need to… continue reading.