- Super-fast
- http status code are important
- uses some DDD concept
- Super micro it is yocto
- it works :)
functionform($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;functionok($o,$m){$e=[];foreach($oas$p=>$v)foreach($m[$p]as$a)if(!preg_match($this->r[$a]?:$a, $v))$e[$p]=$a;return$e;}}
functionrender($t,$d=0){if(!$d)return$t;$d=(array)$d;$o=[];array_walk($d,function($i,$k)use(&$o){$o['{'."$k}"]=$i;});returnstrtr($t,$o);}
functionmatch($u,$s){foreach($sas$a=>$c)if(preg_match("@^{$a}$@D",$u,$m)){array_shift($m);return[$a,$c,$m];}}
functionhub($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{functionbind($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 git@github.com: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{
publicfunctionget($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; // liuggioor 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; // liuggioGiven 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 = newUser();
$alice->name = 'Alice';
$alice->email = 'alice@email.com';
// init object$is = newIs();
// register assertions$is->r = [
'string' => '/^[\S]+$/',
'email' => '/^\S+@\S+\.\S+$/'
];
// Is the object OK?$errors = $is->ok($alice, $userMapping);
count($errors) is 0if 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";
echorender($template, $dto); // "Hello World"or simply use array:
$dto = array('the'=>'Hello', 'test'=>'world');
echorender($template, $dto); // "Hello World"Emit usage
$emit = newEmit();
// Binding 1 event to 2 listeners$emit->bind('FoodOrdered', function(){
echo'FOOD ORDERED...';
});
$emit->bind('FoodOrdered', function($e){
echo$e->name;
});
$event = newFoodOrdered('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