Pragmatism in the real world

Sending test emails from PHP

I've written before about redirecting email while developing using a trap mail script on my development system. Other people also like MailCatcher. I've recently switched to using a simple PHP script that creates a elm file on disk that can be opened by Apple's Mail client. This solution is much faster as there is no SMTP processing involved. Adam Royle came up with this solution in his article Setup a testing mail server using PHP… continue reading.

Customising Bootstrap 3

I'm sure everyone already knows this, but it turns out that you can customise Bootstrap 3 without having to understand Less. Part of the reason that I didn't realise this is that I run my web browser windows quite small and regularly don't see the main menu of getbootstrap.com as it's hidden being the "three dashes" button. However, there's an option called Customize on it. This page gives you a massive form where you can… continue reading.

Dependency injection in Slim framework 2

Slim framework comes with a Dependency Injection container called Set. The basics The DIC is accessed via the container property of $app. To set, you use the set() method: $app->container->set('foobar', function() { return new Foo\Bar(); } ); If you need a given resource to be shared, then use the singleton method: $app->container->singleton('foobar', function() { return new Foo\Bar(); } ); And then to retrieve from the container, there are multiple ways to do it: $fooBar =… continue reading.

Setup Doctrine Migrations to update MySQL timestamp on update

One project I'm working on uses MySQL exclusively and is also using Doctrine Migrations. I wanted to set up a column called updated that was automatically set to the timestamp of the last time the row was changed. This is done in SQL like this: CREATE TABLE foo ( id INT AUTO_INCREMENT NOT NULL, bar VARCHAR(100) NOT NULL, updated timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, PRIMARY KEY(id) ); It's not quite obvious how to do… continue reading.

Shorter directory text in Bash prompt

Rather helpfully, David Goodwin left a comment about how he shortens the space taken up by the directory section of his terminal's PS1 prompt by using a Bash script to remove the middle portion. This is a really good idea, so I ported it into my PS1 set up which resulted in some rearranging and thought I'd share here as I modified for OS X and I don't want to lose it! The relevant portion… continue reading.

Setting OS X's Terminal Tab to the current directory

I use many tabs in a Terminal window quite frequently, and while the window title will show the current directory name, the tab title doesn't. You can manually change it using shift+cmd+i, but who can be bothered? Automating it so that the tab title always matches the current directory turns out to be really easy and just requires a few lines in ~/.profile. Firstly, we need a function that sets the tab's title to the… continue reading.

Exclude elements from Zend\Form's getData()

If you need to exclude some elements from a Zend\Form's getData(), the easiest way is to use a validation group. For example: class SomeForm extends \Zend\Form\Form implements \Zend\InputFilter\InputFilterProviderInterface { public function init() { $this->add([ 'name' => 'name', 'options' => ['label' => 'Name'], ]); $this->add([ 'name' => 'email', 'options' => ['label' => 'Email'], ]); $this->add([ 'name' => 'submit', 'type' => 'button', 'options' => [ 'label' => 'Filter', ], ]); $this->setValidationGroup(['name', 'email']); } public function getInputFilterSpecification() {… continue reading.

2014 in pictures

2014 is coming to an end, so as usual, let's look back at the year as highlighted by the photos that I've taken. This year, I took at least one photo every day, so there's been plenty for me to choose from! I've done this recap in 2008, 2009, 2010, 2011, 2012 and 2013. January As usual, there was flooding in Worcester, but clearly the biggest personal event of the month was breaking my elbow… continue reading.

Recursively deleting elements from an array

I had a need recently to delete items from a nested associative array and also any empty sub-arrays. My initial thought was to use array_walk_recursive, but this doesn't work as you can't unset nested elements and you only have access to the leaves. Clearly I needed a recursive function. I'm sure that this has been done many times before, but this is my solution: /** * Remove any elements where the callback returns true *… continue reading.

SSL certificate verification on PHP 5.6

I recently updated my local OS X Zend Server installation to PHP 5.6 and when I ran composer self-update, I got this error message: [Composer\Downloader\TransportException] The "https://getcomposer.org/version" file could not be downloaded: SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed Failed to enable crypto failed to open stream: operation failed Googling around, I finally worked out that there have been various SSL improvements in PHP 5.6 and that the problem… continue reading.