Pragmatism in the real world

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.

Team culture and diversity

Last Friday, I attended a course on managing people led by Meri Williams and learnt a lot. I highly recommend booking her next course if you can. During the Q&A session, there was a question about hiring for diversity and Meri had some very interesting thoughts. I won't try to reproduce them all here as I'll be doing her a disservice. One comment that resonated was that ideally you want your team members to be… continue reading.

Use vim's :make to preview Markdown

As it becomes more painful to use a pointing device for long periods of time, I find myself using vim more and so I'm paying more attention to customisation so that the things I'm used to from Sublime Text are available to me. One thing I'm used to is that when I run the build command on a Markdown file, I expect Marked for Mac to open and render the file that I'm writing. Vim… continue reading.

PSR-7 file uploads in Slim 3

Handling file uploads in Slim 3 is reasonably easy as it uses the PSR-7 Request object, so let's take a look. The easiest way to get a Slim framework project up and running is to use the Slim-Skeleton to create a project: composer create-project slim/slim-skeleton slim3-file-uploads and then you can cd into the directory and run the PHP built-in web server using: php -S 0.0.0.0:8888 -t public public/index.php Displaying the form We can now create… continue reading.

Proxying SSL via Charles from Vagrant

The Swift application that I'm currently developing gets data from Twitter and I was struggling to get a valid auth token. To solve this, I wanted to see exactly what I was sending to Twitter and so opened up Charles on my Mac to have a look. As my application is running within a Vagrant box running Ubuntu Linux, I needed to tell it to proxy all requests through Charles. To do this, you set… 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.