Skip to content

Instantly share code, notes, and snippets.

Avatar
🤞
reverse-engineering assumptions from code

Sander van Dragt svandragt

🤞
reverse-engineering assumptions from code
View GitHub Profile
@svandragt
svandragt / foreach-object-list.php
Last active Feb 7, 2022
Gotcha: Iterated lists of objects pass by reference in PHP!
View foreach-object-list.php
<?php
// won't be incremented
$a = [
0,
0,
];
// !! will be incremented !!
$b = [
(object)['count' => 0],
@svandragt
svandragt / Gemfile
Created Jan 25, 2022
Combine items from multiple feeds
View Gemfile
gem "byebug", "~> 11.1", :groups => [:development, :test]
gem "rss", "~> 0.2.9"
@svandragt
svandragt / fts.rb
Last active Dec 24, 2021
Full text search a single text file
View fts.rb
#!/usr/bin/env ruby
# v2021-12-24.1
require 'sqlite3'
dbf = '/tmp/plan.db'
File.delete(dbf) if File.exists? dbf
db = SQLite3::Database.open dbf
db.results_as_hash = true
db.execute "CREATE VIRTUAL TABLE IF NOT EXISTS plan USING FTS5(contents,tokenize = porter)"
db.execute "INSERT INTO plan (contents) VALUES(?)", File.open(ARGV[0]).read
View husky-utils.sh
#!/usr/bin/env bash
# Adds the current date (and an optional prefix) to the string to be logged
add_date() {
while IFS= read -r line; do
printf '%s %s\n' "$(date -u +"%Y-%m-%dT%H:%M:%SZ")" "$1 $line";
done
}
# If any dependencies have changed, install them in the background.
@svandragt
svandragt / demo.php
Last active Jan 27, 2021
phpquery maintainable demo
View demo.php
<?php
namespace SvanDragt\Test;
require_once __DIR__ . '/vendor/autoload.php';
use phpQuery;
function do_parse() {
// INITIALIZE IT
// phpQuery::newDocumentHTML($markup);
@svandragt
svandragt / index.php
Created Jan 26, 2021
Altis Local Server index
View index.php
<?php
/**
* Front to the WordPress application. This file doesn't do anything, but loads
* wp-blog-header.php which tells WordPress to load the theme.
*
* @package WordPress
*
* DO NOT EDIT THIS FILE.
*
* phpcs:disable PSR1.Files.SideEffects
@svandragt
svandragt / upgrade.sh
Last active Oct 29, 2020
Basic upgrade script
View upgrade.sh
#!/bin/bash
set -euo pipefail
IFS=$'\n\t'
do_upgrade() {
VERSION_FILE=".bin/upgrade.version"
if [ ! -f $VERSION_FILE ]; then
touch $VERSION_FILE
fi
@svandragt
svandragt / countdown.sh
Last active May 28, 2020
countdown cli
View countdown.sh
#!/usr/bin/env bash
# v3 - 2020-05-28
if [ $# -eq 0 ]; then
echo "countdown.sh <seconds> \"<message>\""
exit 1
fi
secs=$1
msg=${2:-$1 seconds passed.}
while [ $secs -gt 0 ]; do
echo -ne "$secs\033[0K\r"
@svandragt
svandragt / stream_zip.php
Last active Mar 10, 2020
Zip files in a streamable way, with a 2MB memory footprint.
View stream_zip.php
<?php
// composer init; composer require maennchen/zipstream-php
error_reporting(-1);
require_once __DIR__ .'/vendor/autoload.php';
function stream_zip( array $files, string $out_file ) {
$out = fopen( 'file://' . $out_file, 'w' );
$options = new ZipStream\Option\Archive();
$options->setOutputStream( $out );
@svandragt
svandragt / composer.json
Last active Dec 12, 2019
Replace plugin with non-packagist composer version, in custom location.
View composer.json
{
"repositories": [
{
"type": "vcs",
"url": "https://github.com/wp-shortcake/shortcake"
},
],
"require": {
"oomphinc/composer-installers-extender": "^1.1",
"fusioneng/shortcake": "dev-master#0.7.4"