Skip to content

Instantly share code, notes, and snippets.

@jm42
jm42 / started.php
Created October 22, 2014 18:46
Using `$started || start()`;
<?php
$started = false;
function start() {
global $started;
if ($started) {
print("Already started\n");
return;
@jm42
jm42 / app.php
Created October 23, 2014 06:42
Template for Application class
<?php
interface Module
{
function start();
function stop();
function buildContainer(Container $container);
}
@jm42
jm42 / filter.php
Created October 23, 2014 06:46
Input Filter
<?php
// This class I used several times before:
// @see https://gist.github.com/jm42/e7e788d7e257ce1f93c7
// @see https://gist.github.com/jm42/4fc873e91b3835ade45f
class InputFilter implements \ArrayAccess {
private $input;
private $cache = [];
@jm42
jm42 / http_headers.php
Last active August 29, 2015 14:08
HTTP Headers
<?php
class Headers implements ArrayAccess {
private static $headers = array(
'Accept',
'Accept-Charset',
'Accept-Language',
'Accept-Encoding',
'Authorization',
'Cache-Control',
@jm42
jm42 / GroupIterator.php
Created October 26, 2014 20:32
PHP `itertools.groupby` implementation
<?php
class GroupIterator implements Iterator
{
protected $keyfunc;
protected $iter;
protected $key = null;
protected $value = null;
@jm42
jm42 / app_config.php
Created October 30, 2014 04:31
MVC Framework Test
<?php
$container['db.config'] = array(
'file' => __DIR__ . '/../tmp/mvc.db',
'init' => function(PDO $pdo) {
$pdo->exec('CREATE TABLE user (id INTEGER PRIMARY KEY, name VARCHAR(255))');
$pdo->exec('INSERT INTO user (id, name) VALUES (1, "John Doe")');
$pdo->exec('INSERT INTO user (id, name) VALUES (2, "Jane Doe")');
},
);
@jm42
jm42 / 140.md
Last active December 24, 2019 14:54
140 bytes of PHP code

140 bytes

  • Framework

    • Twitto - A web framework in a tweet.
    • Kernel - A kernel in a tweet.
    • sized140 - Something like a framework in PHP with 6 components of 140 chars.
  • Router

  • mu - A tweet-sized PHP micro-router.
@jm42
jm42 / c.php
Created October 31, 2014 04:31
IoC in a tweet
<?php // php -dallow_url_include=1 c.php
// Compressed 122 bytes
// class C extends ArrayObject { function __get($i) { $s = $this[$i]; $s instanceof Closure && $s = $s($this); return $s; } }
class C extends ArrayObject {
function __get($i) {
$s = $this[$i];
$s instanceof Closure && $s = $s($this);
return $s;
@jm42
jm42 / tr.php
Last active November 4, 2020 23:11
Traversal router in a tweet
<?php
// Compressed 138 bytes
// function m($s,$t,$r){foreach($s as$p){if(!isset($t[$p]))break;$t=$t[$p];}foreach($r as$e)if ($e[1]?:$p==$p&&is_a($t,$e[0]))return[$e,$t];}
function m($s, $t, $r) {
foreach ($s as $p) {
if (!isset($t[$p])) break;
$t = $t[$p];
}
<?php
interface ContainerInterface {
function get($key, array $arguments=[]);
function has($key);
}
/**
* Dependency Injection Container.
*/