Skip to content

Instantly share code, notes, and snippets.

@mindplay-dk
mindplay-dk / response.md
Last active Jul 16, 2022
Named registrations in dependency injection containers
View response.md

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
mindplay-dk / README.md
Created Sep 7, 2020
Monkey-patching/polyfill detector
View README.md

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
mindplay-dk / find-var.js
Created Aug 5, 2020
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)) {
return;
}
seen.set(obj, true);
@mindplay-dk
mindplay-dk / image-url.ts
Last active Jan 15, 2022
Rotate image preview to compensate for EXIF orientation (Javascript / Typescript)
View image-url.ts
// Based on: https://stackoverflow.com/a/46814952/283851
/**
* 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
mindplay-dk / README.md
Created Aug 17, 2017
Environment abstractions for PHP
View README.md

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
mindplay-dk / ubuntu.md
Last active Feb 27, 2021
Ubuntu on Windows
View ubuntu.md

Introduction

⚠️ 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
mindplay-dk / collections.md
Last active Jun 6, 2021
Linear collection workflow
View collections.md

You can have a linear workflow with the array functions.

The following is unreadable:

$output = array_reduce(
  array_map(
    function($n) { return $n ** 2; }
    array_filter($input, function($n) { return $n % 2 == 0; })
 ),
@mindplay-dk
mindplay-dk / middleware-host.php
Created Aug 16, 2016
An ultra-naiive universal middleware host
View middleware-host.php
<?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[]
*/
View MiddlewareDelegateInterface.php
<?php
namespace Psr\Middleware;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
/**
* This interface defines the formal method common to HTTP Middleware components.
*/
View scribe-plugin-smart-lists.js
/**
* Automatically create ordered/unordered lists when detecting content like "* " or "1. "
*/
define(function () {
"use strict";
return function () {
return function (scribe) {
/**