Skip to content

Instantly share code, notes, and snippets.

View patricknelson's full-sized avatar
:shipit:
Pushing 1's and 0's.

Patrick Nelson patricknelson

:shipit:
Pushing 1's and 0's.
View GitHub Profile
@patricknelson
patricknelson / Allow Scale (Mobile browser bookmarklet)
Last active August 29, 2015 14:20
Allows you to take back control from the sweaty greedy clasp of those restricting website overlords... and git yer zoom on!
javascript:document.querySelector(%22meta%5Bname=viewport%5D%22).setAttribute(%22content%22,%20%22%22);
@patricknelson
patricknelson / framework.php
Last active August 29, 2015 14:21
Access SilverStripe framework outside of typical page controller. Rapid prototyping.
<?php
/**
* Include this file if you wish to tie into the SilverStripe framework to test your code in a separate workbench area
* without having to build a page controller, data object and then go through the full request cycle. This is good for
* rapid prototyping before rolling out any sort of final code. NOTE: This *is* suitable for use in the command line as
* well.
*
* @author Patrick Nelson, pat@catchyour.com
* @since 2015-01-06
@patricknelson
patricknelson / example.php
Created July 22, 2015 20:43 — forked from scottsb/example.php
Hack to handle multiple Exception types with a single block
<?php
class Foo extends Exception {}
class Bar extends Foo {} // Bar is a technically different but Foo-like exception.
class Baz extends Exception {}
try {
throw new Foo();
} catch (Foo $e) {
echo 'Case 1';
} catch (Baz $e) {
@patricknelson
patricknelson / cd-git.sh
Created September 29, 2015 14:45
When you're too stupid to realize you need to "git checkout [branch]" and not "cd" into it.
cd() {
if [ -d .git ] && [ ! -d $@ ]; then
git checkout "$@"
else
command cd "$@"
fi
}
@patricknelson
patricknelson / randomColor.js
Created October 16, 2015 21:58
Generate random hex color, for fun.
function randColor() {
var color = "#";
for(var i = 1; i <= 3; i++ ) {
var num = Math.round(Math.random() * 255);
color += num.toString(16);
}
return color;
}
// This has moved to: https://github.com/patricknelson/dubtrack-tweaks
@patricknelson
patricknelson / silverstripe-exceptions.php
Last active April 14, 2016 00:28
Example of how you can ensure you have programmatic encapsulation and capture of all SilverStripe generated errors.
<?php
/**
* SilverStripe doesn't typically use exceptions, but we may still need a programmatic method of encapsulating code
* and catching errors. Here we can wrap code in a callback which will trigger an exception instead of user_error()'s.
*
* @param callable $closure
* @throws Exception
*/
function withExceptions(callable $closure) {
<?php
class Injector extends Nestception {
// ... rest of class...
public static function unnestTemp($callable) {
// Retain current instance so we can revert back to it momentarily.
$revertTo = self::inst();
@patricknelson
patricknelson / silverstripe-versioned-helpers.php
Last active December 29, 2018 02:05
Helper functions to cleanly alter versioned objects in SilverStripe
<?php
/**
* Helper functions to cleanly alter versioned objects in SilverStripe.
*
* Usage:
*
* whileDraft(function() {
* $page = Page::get()->byId(123);
* // ... with great power comes great responsibility...
* });
@patricknelson
patricknelson / README.md
Last active March 12, 2020 07:14
PHP function to split arrays up into chunks to do work on (basically pagination for callbacks).

doChunks()

Very simple but useful PHP function for performing work on large arrays of information. Basically, it's like pagination for callbacks. For example, this can be used for performing bulk database INSERT operations (instead of inserting each item individually). See below for a quick real world example.

Example Usage