Skip to content

Instantly share code, notes, and snippets.

mindplay-dk / psr-middleware.php
Created December 13, 2022 11:32
PSR Handlers as middleware
View psr-middleware.php
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
// There are essentially 3 handler/middlerware patterns:
mindplay-dk /
Created December 12, 2022 10:29
Teaching ChatGPT new PHP syntax

I was curious if we could use ChatGPT to generate a new and better manual for PHP.

This example was interesting enough that I figured it was worth posting.

It looks like ChatGPT does not know PHP 8.1 - the latest version it seems to know is PHP 8.

When asked to explain the first class Callable(...) syntax (introduced in PHP 8.1) it does explain how to generate something that is functionally similar, using e.g. fn($car) => $car->drive() rather than $car->drive(...). So it understands the concept, it just doesn't know about the new syntax.

But here's the wild part: I then proceded to explain the new syntax to it, and asked it to explain again using the new syntax - and it did it. 😮

mindplay-dk /
Last active July 16, 2022 14:45
Named registrations in dependency injection containers

In response to this answer:

The built-in dependency injection container does not support named dependency registrations, and there are no plans to add this at the moment.

One reason for this is that with dependency injection, there is no type-safe way to specify which kind of named instance you would want. You could surely use something like parameter attributes for constructors (or attributes on properties for property injection) but that would be a different kind of complexity that likely wouldn’t be worth it; and it certainly wouldn’t be backed by the type system, which is an important part of how dependency injection works.

I'm not sure what is meant by "type-safe" here?

The way service providers are provisioned in .NET today (via service providers and descriptors) is only "type-safe" in the sense that e.g. constructors specify the required types of dependencies that should be injected - th

mindplay-dk /
Created September 7, 2020 13:32
Monkey-patching/polyfill detector

This script tries to detect monkey-patching, polyfills and other hacks/overrides in the browser.

Paste it into the Chrome (or Edge) console and press ENTER.

Note that this may give false positives for window.location, window.fetch and window.length - this appears to be because these properties aren't correctly reflected by the native browser implementations, but if you know how to fix that, please post a comment.

mindplay-dk / find-var.js
Created August 5, 2020 13:53
Recursively search the global namespace (window) for a variable with a given name
View find-var.js
function findVar(varName) {
let seen = new Map();
function search(obj, prefix = "") {
if (seen.has(obj)) {
seen.set(obj, true);
mindplay-dk / image-url.ts
Last active January 15, 2022 12:06
Rotate image preview to compensate for EXIF orientation (Javascript / Typescript)
View image-url.ts
// Based on:
* Create a Base64 Image URL, with rotation applied to compensate for EXIF orientation, if needed.
* Optionally resize to a smaller maximum width - to improve performance for larger image thumbnails.
export async function getImageUrl(file: File, maxWidth: number|undefined) {
return readOrientation(file).then(orientation => applyRotation(file, orientation || 1, maxWidth || 999999));
mindplay-dk /
Created August 17, 2017 11:22
Environment abstractions for PHP

Draft for a simple PSR providing a trivial abstraction of the local system environment.

Why? So we can stop using hacks to work around dependencies on system state - so that these dependencies can be made visible as (constructor) dependencies, and so we can mock the implementations under test.

mindplay-dk /
Last active February 27, 2021 16:40
Ubuntu on Windows


⚠️ I am no longer actively maintaining this. ⚠️

With the Windows 10 Creators Update comes an awesome new opportunity to run a lighweight Ubuntu Linux environment as a subsystem of Windows. This is officially known as Windows Subsystem for Linux (WSL) or Bash on Windows.

This is great news for PHP developers using Windows - we can now get access to native Linux builds of PHP, PECL extensions, ImageMagick, NGINX and other server components and tools, without having to migrate to a Linux desktop environment!

In this guide, we'll get you set up with WSL itself, a working PHP 7.4 environment with OpCache, XDebug and task/terminal integration with Php Storm, and working NGINX configuration - as well as various tools, including PEAR/PECL, Composer and Node.JS.

mindplay-dk /
Last active June 6, 2021 14:37
Linear collection workflow

You can have a linear workflow with the array functions.

The following is unreadable:

$output = array_reduce(
    function($n) { return $n ** 2; }
    array_filter($input, function($n) { return $n % 2 == 0; })
mindplay-dk / middleware-host.php
Created August 16, 2016 12:52
An ultra-naiive universal middleware host
View middleware-host.php
// an ultra-simple middleware host that works for middleware callables with any signature.
// the last argument is always the `$next` function, which delegates to the next middleware.
class Host
* @var callable[]