Skip to content

Instantly share code, notes, and snippets.

View zanbaldwin's full-sized avatar
🦀

Zan Baldwin zanbaldwin

🦀
View GitHub Profile
@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;
@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 / 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 / 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 / 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 = >
<?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 / 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;
@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 / UnicodeCharacter.php
Last active February 16, 2022 13:03
I got bored and played with the UTF-8 standard.
<?php declare(strict_types=1);
class UnicodeCharacter
{
private string $binary;
/**********************************\
| CONSTRUCTORS AND FACTORY METHODS |
\**********************************/
@zanbaldwin
zanbaldwin / DoctrineArgumentResolver.php
Last active February 11, 2022 16:48
This will be useful at some point.
<?php declare(strict_types=1);
namespace App\Request\ArgumentResolver;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Controller\ArgumentValueResolverInterface;
use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;