Skip to content

Instantly share code, notes, and snippets.

@fjahn
fjahn / EventEmitter.ts
Created September 27, 2022 13:10
Typesafe Event Emitter
export class OpenEventEmitter<T> implements EventEmitter<T> {
private readonly handlers: { [eventName in keyof T]?: ((value: T[eventName]) => void)[] }
constructor() {
this.handlers = {}
}
getClosedEmitter(): EventEmitter<T> {
return this
}
@fjahn
fjahn / beforeUnload.js
Created April 7, 2022 08:15
Block unload in Browser and Inertia Router using Vue
import { Inertia } from '@inertiajs/inertia'
import { onBeforeMount, onBeforeUnmount } from 'vue'
/**
* @param {() => boolean} shouldBlockUnload
* @param {string} message
* @returns void
*/
export function useBeforeUnload(
shouldBlockUnload,
@fjahn
fjahn / add-cronjob.sh
Created December 15, 2021 10:53
Add Cronjob via bash
#!/bin/bash
# Idea stolen from https://stackoverflow.com/questions/610839/how-can-i-programmatically-create-a-new-cron-job#13355743
addCronjob() {
user=$(whoami)
(crontab -u "$user" -l 2>/dev/null || echo '' ; echo "$1") \
| sort - \
| uniq - \
@fjahn
fjahn / Dockerfile
Created October 5, 2021 11:53
Composer Dockerfile
FROM composer:2
FROM php:8-cli
COPY --from=composer /usr/bin/composer /usr/bin/composer
VOLUME /app
WORKDIR /app
RUN apt update && apt install -y \
@fjahn
fjahn / Dockerfile
Last active October 5, 2021 11:47
Dockerfile for Statamic
FROM php:8-cli
VOLUME /app
WORKDIR /app
RUN apt update && apt install -y \
tini \
libonig-dev \
&& rm -rf /var/lib/apt/lists/*
@fjahn
fjahn / stream_download.php
Created September 9, 2021 07:21
Laravel Stream Download
<?php
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Storage;
use Symfony\Component\HttpFoundation\StreamedResponse;
/**
* Similar to Storage::download, but serves the file as a stream.
*
* Stolen and adapted from stackoverflow.com/questions/44965777/laravel-5-file-downloads-stream-or-download
@fjahn
fjahn / readCsv.php
Last active September 1, 2021 14:54
Parse CSV file
<?php
function readCsv(string $filepath, string $separator = ';'): array
{
$csv = file_get_contents($filepath);
$lines = explode("\n", $csv);
$head = str_getcsv(array_shift($lines), $separator);
$data = [];
$lineNumber = 1;
@fjahn
fjahn / AuthorFilter.php
Created August 31, 2021 07:32
Statamic filter for authors
<?php
namespace App\Scopes;
use Illuminate\Support\Facades\Auth;
use Statamic\Query\Scopes\Filter;
class AuthorFilter extends Filter
{
public $pinned = true;
@fjahn
fjahn / readable-text-mixin.scss
Created May 5, 2021 09:53
Readable Text Mixin
/**
Ensures that text is readable on any background.
Stolen from https://stackoverflow.com/questions/23968961/css-how-can-i-make-a-font-readable-over-any-color#43135326
*/
@mixin readable-shadow($size, $color: white) {
text-shadow:
#{0.01 * $size}em 0 $color,
0 #{0.01 * $size}em $color,
#{-0.01 * $size}em 0 $color,
0 #{-0.01 * $size}em $color,
@fjahn
fjahn / UriBuilder.php
Created April 20, 2021 08:40
Builder class for building clean and simple URIs every time
<?php
namespace App\Util;
class UriBuilder
{
private string|null $protocol;
private string|null $host = null;
private array $path_segments = [];
private array $query_parameters = [];