Skip to content

Instantly share code, notes, and snippets.

View colinodell's full-sized avatar

Colin O'Dell colinodell

View GitHub Profile
@colinodell
colinodell / CustomParagraphRenderer.php
Created February 6, 2017 14:40
CommonMark - Overriding default renderers
<?php
use League\CommonMark\Block\Element\AbstractBlock;
use League\CommonMark\ElementRendererInterface;
class CustomParagraphRenderer implements BlockRendererInterface
{
public function render(AbstractBlock $block, ElementRendererInterface $htmlRenderer, $inTightList = false)
{
return $htmlRenderer->renderInlines($block->children());
@colinodell
colinodell / benchmark.php
Created May 30, 2016 00:46
array_change_keys() benchmark
<?php
$a = range(1, 10000000);
echo "Test 1 - Using foreach\n";
$start = microtime(true);
$b = [];
foreach ($a as $k => $v) {
$b[md5($v)] = $v;
@colinodell
colinodell / _description.md
Last active May 28, 2016 14:34
array_change_keys()

Problem

AFAIK, there are no core PHP methods which allow you to re-key an array.

Okay, that's not entirely true - there is array_change_key_case(), but whenever I need to change an array's keys it's never been this use case.

You could do it with a foreach loop:

$newArray = [];
@colinodell
colinodell / ParagraphDirProcessor.php
Created March 10, 2016 16:29
Adding 'dir' attribute to all paragraphs (UNTESTED)
<?php
// TODO: Set your "namespace" and "use" statements here
// Also see this documentation: http://commonmark.thephpleague.com/customization/abstract-syntax-tree/#document-processor
class ParagraphDirProcessor implements DocumentProcessorInterface
{
public function processDocument(Document $document)
{
$walker = $document->walker();
@colinodell
colinodell / portVarKeywords.php
Last active September 28, 2016 12:24 — forked from nikic/portAlternativeTags.php
Tool for changing "var" keywords to "public" in PHP
<?php
/*
* Note: This script will directly modify the .php files in the given directory.
* It is assumed that the code is under version control, so you can easily review
* the changes using `git diff` or similar.
*/
function usageError() {
die("Usage: php portVarKeywords.php dir/\n");
@colinodell
colinodell / services.yml
Created August 20, 2015 22:59
Guzzle with logger
services:
guzzle.client:
class: Guzzle\Http\Client
calls:
- [addSubscriber, ["@guzzle.logger"]]
guzzle.logger:
class: Guzzle\Log\MonologLogAdapter
arguments:
- "@logger"
@colinodell
colinodell / create-multiple.sh
Created August 20, 2015 15:33
Create Drupal upgrade patches
#!/bin/bash
# A sample script for batch-creating multiple patches at once
# Generate 6.x patches
for i in `seq 0 36`
do
./create-patch.sh 6.$i 6.37
done
# Generate 7.x patches
@colinodell
colinodell / AbstractEnum.php
Created August 18, 2015 15:09
Enum-like class for PHP
<?php
/**
* Base class used to build enums
*/
abstract class AbstractEnum
{
/**
* @var array Options array, suitable for a select box
*/
@colinodell
colinodell / rateLimit.js
Created July 8, 2015 15:52
JS Rate Limiting
// Returns a function that won't fire more than
// once every `wait` milliseconds.
//
// Example usage:
//
// $('.button').click(rateLimit(function(){
// console.log('clicked!');
// }));
//
function rateLimit(func, wait) {
@colinodell
colinodell / console.js
Created April 1, 2015 17:45
/r/thebutton
// Super-simple console script to push the button only when the timer gets low
var h;
var armed = false;
var lowestSoFar = 9999999999;
h = setInterval(function() {
if (r.thebutton._msLeft <= 0) {
console.log('Game over :-/');
clearInterval(h);
return;