Skip to content

Instantly share code, notes, and snippets.

View dave1010's full-sized avatar

Dave Hulbert dave1010

View GitHub Profile
@dave1010
dave1010 / monolog-setup.php
Created April 22, 2014 12:08
Make sure a Monolog logger can log
<?php
$handler = new RotatingFileHandler(ABSPATH . '../logs/log.log', 0, Logger::DEBUG, true, 0777);
try {
$record = array(
'message' => (string) "Write test",
'context' => null,
'level' => Logger::DEBUG,
'level_name' => 'DEBUG',
@dave1010
dave1010 / monolog-setup.php
Created April 22, 2014 13:45
Monolog setup for FingersCrossedHandler, RotatingFileHandler, NativeMailerHandler & BufferHandler
<?php
$logger = new Logger('my logger');
// log DEBUG messages to file when there's at least a NOTICE
$fileHandler = new RotatingFileHandler(ABSPATH . '../logs/log.log', 0, Logger::DEBUG, true, 0777);
$fingersCrossedHandler = new FingersCrossedHandler($fileHandler, Logger::NOTICE, 1000);
$logger->pushHandler($fingersCrossedHandler);
@dave1010
dave1010 / variadics.php
Created April 29, 2014 08:07
Type hinting in PHP variadics
<?php
class Bar{}
$bars = [new Bar, new Bar, new Bar];
$ints = [1, 2, 3];
// without variadics
// lots of manual type checking
function oldFoo($bars)
{
@dave1010
dave1010 / array-map-break-yield.php
Last active September 19, 2017 12:25
array_map in php, with break
<?php
class BreakOut extends Exception {}
function mapGenerator(array $arr, $callback)
{
$ret = [];
foreach ($arr as $val) {
try {
yield $callback($val);
@dave1010
dave1010 / README.md
Last active August 29, 2015 14:06
retry

Improved retry function for igorw/retry, as proposed by @acleon.

Benefits:

  • Appears to use fewer opcodes (This is the 1st time I've used VLD, I could be wrong)
  • Doesn't use goto
@dave1010
dave1010 / interpreter.php
Last active August 29, 2015 14:07
PHP Interpreter
<?php
$stack = [];
$labels = [];
$pointer = 0;
$instructions = [];
/**
* @param $stack
* @param $instructions
@dave1010
dave1010 / repo.php
Created November 18, 2014 08:04
cqs repo with creation on fetch
class Repo
{
public function fetchByName($name)
{
$entity = $this->em->fetch(['name' => $name]);
if (!$entity) {
$entity = new NullEntity($name);
}
$this->dispatcher->dispatch(new EntityRetrievedEvent($entity));
return $entity;
@dave1010
dave1010 / bench1.php
Last active August 29, 2015 14:13
PHP object instantiation benchmark
<?php
class Foo {
public function __construct($i)
{
$this->bar = $i;
}
}
$start = microtime(true);
$count = 1000000;
@dave1010
dave1010 / 0_reuse_code.js
Last active August 29, 2015 14:18
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console
@dave1010
dave1010 / javascript_resources.md
Last active August 29, 2015 14:18 — forked from jookyboi/javascript_resources.md
Here are a set of libraries, plugins and guides which may be useful to your Javascript coding.

Libraries

  • jQuery - The de-facto library for the modern age. It makes things like HTML document traversal and manipulation, event handling, animation, and Ajax much simpler with an easy-to-use API that works across a multitude of browsers.
  • Backbone - Backbone.js gives structure to web applications by providing models with key-value binding and custom events, collections with a rich API of enumerable functions, views with declarative event handling, and connects it all to your existing API over a RESTful JSON interface.
  • AngularJS - Conventions based MVC framework for HTML5 apps.
  • Underscore - Underscore is a utility-belt library for JavaScript that provides a lot of the functional programming support that you would expect in Prototype.js (or Ruby), but without extending any of the built-in JavaScript objects.
  • lawnchair - Key/value store adapter for indexdb, localStorage