Skip to content

Instantly share code, notes, and snippets.

@jm42
jm42 / app.php
Created October 5, 2015 23:14
Container Builder
<?php
namespace A;
use A\Container\Builder as ContainerBuilder;
class App {
public $root; /** root directory given when initilizated */
public $container; /** an array-like object to retrieve services from */
@jm42
jm42 / adr.php
Created October 5, 2015 05:21
ADR Example
<?php
class App {
public $queue = [];
public $routes = [];
public $container;
public $resolver;
<?php
class Router implements RouterInterface {
private $root;
private $routes = [];
public function __construct($root) {
$this->root = $root;
}
<?php
class Emitter implements EmitterInterface {
private $listeners = [];
private $order = PHP_INT_MAX;
public function on($event, callable $listener, $priority=0)
{
if (array_key_exists($event, $this->listeners) === false) {
$this->listeners[$event] = new \SplPriorityQueue;
<?php
interface ContainerInterface {
function get($key, array $arguments=[]);
function has($key);
}
/**
* Dependency Injection Container.
*/
@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];
}
@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 / 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 / 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 / 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;