Skip to content

Instantly share code, notes, and snippets.

View zanbaldwin's full-sized avatar
🦀

Zan Baldwin zanbaldwin

🦀
View GitHub Profile
@zanbaldwin
zanbaldwin / Makefile
Last active February 8, 2022 02:11
Drop this in "/etc/nginx/conf.d", or use nginx-proxy-manager Docker image instead.
SHELL := bash
.SHELLFLAGS := -eu -o pipefail -c
.ONESHELL:
.DELETE_ON_ERROR:
MAKEFLAGS += --warn-undefined-variables
MAKEFLAGS += --no-builtin-rules
ifeq ($(origin .RECIPEPREFIX), undefined)
$(error This Make does not support .RECIPEPREFIX; Please use GNU Make 4.0 or later)
endif
.RECIPEPREFIX = >
@zanbaldwin
zanbaldwin / OpenApiLoader.php
Last active January 28, 2022 18:15
(Extremely) Simple OpenAPI Specification Route Loader for Symfony
<?php declare(strict_types=1);
namespace App\Routing\Loader;
use League\JsonReference\Dereferencer;
use League\JsonReference\Loader\ArrayLoader;
use Symfony\Component\Config\FileLocatorInterface;
use Symfony\Component\Config\Loader\FileLoader;
use Symfony\Component\Config\Resource\FileResource;
use Symfony\Component\Routing\Route;
<?php
function camelCaseToSnakeCase(string $input): string {
preg_match_all('!(^[a-z\d]+)|(?:_?)([a-z\d]+|[A-Z][a-z\d]+|[A-Z][A-Z\d]*(?=_|$|[A-Z][a-z\d]))!', $input, $matches);
return strtolower(implode('_', array_map(function (string $part): string {
return trim($part, '_');
}, $matches[0])));
}
@zanbaldwin
zanbaldwin / Makefile
Last active June 8, 2023 10:24
Automatic Makefile Usage Generator
SHELL := bash
.SHELLFLAGS := -eu -o pipefail -c
.ONESHELL:
.DELETE_ON_ERROR:
MAKEFLAGS += --warn-undefined-variables
MAKEFLAGS += --no-builtin-rules
ifeq ($(origin .RECIPEPREFIX), undefined)
$(error This Make does not support .RECIPEPREFIX; Please use GNU Make 4.0 or later)
endif
.RECIPEPREFIX = >
@zanbaldwin
zanbaldwin / string-regex.md
Last active May 24, 2019 00:49
Match strings, allowing for escape characters.

Regular expression to match either a single or double quoted string, taking into account escape characters: /(["'])((?:(?!\1|\\).|\\.)*?)\1/

It's recommended that you remove escape characters from the resulting string (replace \\((?=\%s|\\).) with \1 substituting %s for the matched quote character):

preg_match('/(["\'])((?:(?!\\1|\\\\).|\\\\.)*?)\\1/', $string, $matches);
$extractedStringValue = preg_replace(sprintf('/\\\\((?=\\\\|\\%s).)/', $matches[1]), '\1', $matches[2]);
@zanbaldwin
zanbaldwin / konami.js
Created May 21, 2019 18:08
Konami Code in Javascript/TypeScript
function konami(callback) {
var codes = [38, 38, 40, 40, 37, 39, 37, 39, 66, 65],
position = 0;
document.addEventListener('keydown', function (event) {
if (event.keyCode === codes[position]) {
position++;
if (position === codes.length) {
position = 0;
callback();
}
@zanbaldwin
zanbaldwin / Dockerfile
Last active December 20, 2019 19:50
Compressed Docker Image (~14MiB) for Hugo Site Builder (v0.50)
FROM "golang:1.13.5-alpine3.10" AS builder
ARG HUGO_VERSION="v0.61.0"
RUN apk add --no-cache --virtual .build-deps gcc g++ git musl-dev upx \
&& git clone git://github.com/gohugoio/hugo.git \
&& cd hugo \
&& git checkout "${HUGO_VERSION}" \
&& go build -o "/tmp/hugo" -ldflags "-s -w" -tags "extended" \
&& upx --brute -o "/sbin/hugo" "/tmp/hugo" \
&& apk del .build-deps
@zanbaldwin
zanbaldwin / StreamMode.php
Last active October 1, 2018 13:58
Figuring out file-descriptors in PHP console applications
<?php declare(strict_types=1);
namespace Darsyn\FD;
class StreamMode implements StreamModeInterface
{
/** @var resource $fp */
private $fp;
/** @var integer $mode */
private $mode;
<?php declare(strict_types=1);
if (!\function_exists('array_map_recursive')) {
function array_map_recursive(array $array, callable $function): array
{
$out = [];
foreach ($array as $key => $value) {
$out[$key] = \is_array($value)
? array_map_recursive($value, $function)
: \call_user_func($function, $value, $key);
<?php declare(strict_types=1);
namespace Symfony\Component\Validator;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Constraints\Collection;
use Symfony\Component\Validator\Constraints\Type;
use Symfony\Component\Validator\Mapping\ClassMetadataInterface;
use Symfony\Component\Validator\Mapping\MetadataInterface;
use Symfony\Component\Validator\Validator\RecursiveValidator;