Pragmatism in the real world

File uploads with Slim 4, tested with curl

Unsurprisingly, uploading files with Slim 4 is pretty much the same as for Slim 3 as they are both use PSR-7 for Requests. Recently, Matthew asked a question about why he was getting an error, so I looked into it. One thing that's really nice about Slim is that you can write a complete application in a single file (+ the vendor directory) which is helpful for testing things. Here's the entire application: public/index.php: <?php… continue reading.

UUID v7

It's common to use a UUID when you need a primary key for your database records. Unlike incrementing numeric keys, it has the advantage that it's not tied to a specific database instance and can be created before insertion into the database. Usually, people use version 4 UUIDs, which contains a lot of randomness to ensure that it's going to be unique and not clash with any other id. Recently, I became aware of version… continue reading.

Keyword substitutions make life easier

I'm a huge fan of making my life easier and one thing I have found really helpful is automatic text substitution. The Mac has a built-in solution, but it's slightly clunky as it uses a popup to confirm that you want to substitute, so I use Keyboard Maestro, however there's many alternatives out there. My personal preference is to prefix all my substitutions with a semicolon as there are no real words that start with… continue reading.

Damaged application error when developing for Mac

I've been doing a few updates to Daily Jotter, my little Mac app that's available in the Mac App Store. It's been a little while since I last updated it and a few things have changed. After updating the code to fix deprecation warnings, my immediate problem was that the debug version of the app wouldn't start up and I see this error. I know this error well. It means that my app has exited… continue reading.

Missing trailing new line when using cat in a bash script

Recently, I was writing a bash script that read a file into a string and then passed that string elsewhere that then calculated the hash of it. To my surprise, I was getting a different hash to doing the equivalent in PHP with file_get_contents(). Digging into it, I discovered that when you assign the output of cat to a variable, the trailing new line in the file is stripped off, which explained the difference. You… continue reading.

Use xxd to convert to hex

If you want to see the hex values of a file, the easiest way to do this is to use xxd. Given foo.txt that contains "This is some text!": $ xxd foo.txt 00000000: 5468 6973 2069 7320 736f 6d65 2074 6578 This is some tex 00000010: 7421 0a t!. There are three columns in the output: number of first character in hex Hex representation of up to 16 bytes, separated every 2 bytes ASCII representation,… continue reading.

Changing port maps in Docker Compose

When using Docker Compose, I often map the ports to local ones in the compose.yaml like this: services: web: build: context: ./docker/web ports: – "8080:80" Adding to the port map If port 8080 is in use already on a developer's machine, then we can add a new mapping using compose.override.yaml services: web: build: context: ./docker/web ports: – "8081:80" The compose.override.yaml file is merged with the compose.yaml file and the effective configuration is now: services: web:… continue reading.

A short primer on SPF, DKIM and DMARC

I use FastMail for my email and as I control my own domain, I needed to set up SFP, DKIM and DMARC on it. These are DNS records that help the email servers put the emails that I send into my recipient's inbox and to mark any forged emails as spam. These are my tidied up notes so that I can find them again when I next need them. SPF SPF is a DNS record… continue reading.

Indenting output in a shell script with pr

When printing output in a shell script, it's quite useful to be able to indent information to group it. The easiest way to do this is with pr, like this: body=$(curl -s "http://localhost:8888/") echo "Response:" echo "$body" | pr -to 4 This generates: Response: { "links": { "games": "/games" } } Given that pr appeared in version 1 of AT&T Unix, I'm late to the party with this one, but it's a useful thing to… continue reading.

Using Monolog's TestHandler

When I write integration tests with PHPUnit, I find it helpful use Monolog's TestHandler to check that the logs I expect are generated. It's reasonably common that classes that write to a log take a PSR\Log\LoggerInterface in their constructor and then use that for logging. This usually logs to the error handler for pushing to Sentry or whatnot. This makes testing easy! For testing we can use the TestHandler which stores the logs it receives… continue reading.