Skip to content

Instantly share code, notes, and snippets.

View al-the-x's full-sized avatar

David Rogers AKA "AL the X" al-the-x

View GitHub Profile
@al-the-x
al-the-x / main.php
Created February 8, 2013 04:26
Roman Numeral converter from OrlandoPHP meeting on 02/07/2013
<?php
class Roman_converter
{
public $numerals = array(
'I' => 1,
'II' => 2,
'V' => 5,
'X' => 10,
'L' => 50,
@al-the-x
al-the-x / main.php
Created April 5, 2013 01:39
Coding Dojo at Orlando PHP 2013-04-04
<?php
/**
* Imagine a hallway (from the Matrix) with 50 doors on each side.
* Take a walk down the hallway and back up, opening every door.
* On a second pass, close all the even numbered doors.
* The third pass, each third door, close it if it's open and open if it's closed.
* Repeat.
* What is the state of the doors after 100 passes?
*/
@al-the-x
al-the-x / main.php
Created May 3, 2013 01:56
Coding Dojo at OrlandoPHP.org: Roman Numerals
<?php
/**
* Given a decimal number like 1, 3 7, 13, 57, 42, convert to its Roman Numeral
* equivalent. For example, "46" is "XLVI", "973" is "CMLXXIII", "1984" is
* "MCMLXXXIV".
*/
function toRoman($decimal){
$romanNumerals = array(
"1"=>"I",
"2"=>"II",
@al-the-x
al-the-x / example.txt
Last active August 29, 2015 13:55
AWK script to find number of occurrences of "foo=" in a very large text file (or stream); supplied is a small example of the format one might expect.
{
foo=1
bar=2
baz=3
}
{
foo=0
}
{
}
@al-the-x
al-the-x / notes.md
Created February 7, 2014 19:11
Tips from @rasmus about atomic deployments...

The only way to have atomic (code) deploys:

  • Don't copy files into current document root
  • Let existing requests finish on old code
  • New requests start on new code
  • Avoid clearing your opcode cache
  • Minimal impact on production traffic

Breakdown:

@al-the-x
al-the-x / router.php
Last active August 29, 2015 14:01
Simple router file for serving legacy PHP projects like Wordpress with `php -S host:port router.php`
<?php
$path = rtrim(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH), '/');
// If that doesn't correspond to an existing path, assume a route...
$path = realpath(__DIR__ . $path) ?: __DIR__ . '/index.php';
extract(pathinfo_safe($path)); // $dirname, $basename, $extension, and $filename
if ( !$extension ) extract(pathinfo_safe($path .= '/index.php'));
@al-the-x
al-the-x / Model.php
Created July 27, 2014 20:00
Another thought about PHP ORMs that occurred to me today, should I ever fall victim to that particular land war in Asia... What if a Model class exposed a static method for configuration of child classes, which accepted a Closure that was invoked upon instantiation, like `Model#initialize` in Ruby?
<?php
class Model
{
static $hooks = [ ];
/**
* Add initialization hook $init to (FIFO) queue, which will be bound to the
* instance of the object at initialization.
*
@al-the-x
al-the-x / FunctionalTestCase.php
Last active August 29, 2015 14:04
I get tired of writing this simple `FunctionalTestCase` over and over again. I should submit to @sbergmann for PHPUnit...
<?php
/**
* A "functional" test is typically one that runs the full application through it's
* paces: constructing a request, triggering a route, generating a response, and
* testing the fully rendered output. This abstract class provides some assertions
* appropriate for testing requests and responses and abstract methods for actually
* fetching requests and responses and routing the application. The developer should
* extend this class for his or her specific framework or application.
*/
@al-the-x
al-the-x / controller.js
Created September 3, 2014 22:19
Using Lodash inside of Angular
angular.module('myModule', [ ], function($rootScope){ // equivalent to .config(function($rootScope){ . . . })
$rootScope._ = _; // Make `_` available in Angular Expressions
}).controller('MyController', function(Something){
var saveSomething = this.saveSomething = Something.save();
$scope.$watch('something', _.debounce(saveSomething, 500));
$scope.something = 'some value';
})
; // END myModule
@al-the-x
al-the-x / check-writing.js
Created October 2, 2014 15:33
Practiced Kata: Check Writing from @TheIronYard--Orlando
var assert = require('assert');
function test(actual, expected, success){
success = success || 'pass!';
assert.equal(actual, expected);
console.log(success);
}