- Super-fast
- http status code are important
- uses some DDD concept
- Super micro it is yocto
- it works :)
function form($d,$r){foreach(array_intersect_key((array)$r,(array)($c=is_object($d)?$d:new$d))as$a=>$v){$c->$a=$v;}return $c;}
class Is{var$r;function ok($o,$m){$e=[];foreach($o as$p=>$v)foreach($m[$p]as$a)if(!preg_match($this->r[$a]?:$a, $v))$e[$p]=$a;return$e;}}
function render($t,$d=0){if(!$d)return$t;$d=(array)$d;$o=[];array_walk($d,function($i,$k)use(&$o){$o['{'."$k}"]=$i;});return strtr($t,$o);}
function match($u,$s){foreach($s as$a=>$c)if(preg_match("@^{$a}$@D",$u,$m)){array_shift($m);return[$a,$c,$m];}}
function hub($u,$r,$s,$m){$m.=" $u";list(,$a,$c)=match($m,$s);$c[]=$r;return $a?call_user_func_array($a,$c):'404'.header('HTTP/1.1 404');}
class Emit{function bind($e,$f){$this->l[$e][]=$f;}function __invoke($e){foreach($this->l[end(explode('\\',get_class($e)))]as$a)$a($e);}}
There's a running example in the ./Example/index.php file.
- first clone:
git clone [email protected]:liuggio/sized140.git
, and enter in thecd sized140
- just run with
php -S localhost:8080 Example/index.php
open your browser at localhost:8080
hub usage
class Controller{
public function get($slug /*,$request*/) {
return "[$slug]";
}
}
$routes = [
'GET /blog/(\w+)' => '\Sized140\Controller::get', // will match this
'POST /blog' => function($request) {echo "Hi mate";}
];
$output = hub('/blog/titleslug', $request=[], $routes, 'GET'); // [titleslug]
It fills a DTO from a given request, Domain Driven Design Approved.
Form() usage
$httpRequest = array('who'=>'liuggio', 'something we don\'t need');
$kissCommandDTO = form('\sized140\KissCommandDTO', $httpRequest);
echo $kissCommandDTO->who; // liuggio
or given an already instantiated object:
$httpRequest = array('who'=>'liuggio', 'something we don\'t need');
$kissCommandDTO = new \sized140\KissCommandDTO();
$kissCommandDTO->who = 'nobody';
$kissCommandDTO = form($kissCommandDTO, $httpRequest);
echo $kissCommandDTO->who; // liuggio
Given an object, Is->ok
will check if all the proprieties respect the validation as regex on the mappings array
Is->ok() usage
// the object to validate:
class User
{
public $name;
public $email;
}
// The Object mapping
$userMapping = [
// property | assertion types
'name' => ['string', '/^[\S]{3,}$/'],
'email' => ['email']
];
$alice = new User();
$alice->name = 'Alice';
$alice->email = '[email protected]';
// init object
$is = new Is();
// register assertions
$is->r = [
'string' => '/^[\S]+$/',
'email' => '/^\S+@\S+\.\S+$/'
];
// Is the object OK?
$errors = $is->ok($alice, $userMapping);
count($errors) is 0 if there's no error;
Given an array of routes, it find the one matching with the request.
Match usage
$routes = [
'/users/(\w+)/blog/(\w+)' => '\Sized140\UserController::get', // class name or function
'/admin/(\w+)' => function ($id) {echo $id;}
];
$output = match($_SERVER["PATH_INFO"], $routes); //$_SERVER["PATH_INFO"] = "/users/15/blog/38";
// $output is ['/users/(\w+)/blog/(\w+)', '\Sized140\UserController', [15,38]]
// var_dump(return call_user_func_array($output[1],$output[2]));
Contribution provided by @claudio-dalicandro
It replaces variables in a template.
Render usage
$template = "{the} {test}";
$dto = new \StdClass();
$dto->the = "Hello";
$dto->test = "World";
echo render($template, $dto); // "Hello World"
or simply use array:
$dto = array('the'=>'Hello', 'test'=>'world');
echo render($template, $dto); // "Hello World"
Emit usage
$emit = new Emit();
// Binding 1 event to 2 listeners
$emit->bind('FoodOrdered', function(){
echo 'FOOD ORDERED...';
});
$emit->bind('FoodOrdered', function($e){
echo $e->name;
});
$event = new FoodOrdered('Pizza');
// calling the event
$emit($event);
See the ./Tests folder for more examples
- http://twitto.org/ A web framework in a tweet by @fabpot
- http://twittee.org/ A Dependency Injection Container in a Tweet by @fabpot
- https://github.com/lastguest/ev A tweet-sized PHP Event emitter @lastguest
- https://gist.github.com/mathiasverraes/9046427 - A test framework in a tweet by @mathiasverraes