Commit 0306e355 authored by Avris's avatar Avris

Dispatcher __call

parent f35b3234
Pipeline #11397721 (#) passed with stage
in 4 seconds
<?namespace Picco;class App{
function __construct($d,$e){session_start();$s=$_SERVER;define('D',$d);$l=strlen($r=@$s['PATH_INFO']);$u=explode('?',$s['REQUEST_URI'])[0];define('F',$l?substr($u,0,-$l):$u);define('R',rtrim($r,'/')?:'/');define('B',preg_replace('#/[^/]+\.php$#','',F).'/');define('E',$e);}
function run(){$c=new Container();$c->router=new Router(\App\Routing::get());$c->view=new View;$q=$c->controllers=new\App\Controllers;$d=$c->dispatcher=new Dispatcher;\App\Services::get($c);
$t=function($f,$m)use($c,$q,$d){$r=(array)call_user_func_array([$q,$f],$m);return$d('response',[$c,&$r])?:$c->view->render($f,$r+['r'=>$c->router]);};
$t=function($f,$m)use($c,$q,$d){$r=(object)call_user_func_array([$q,$f],$m);return$d->response($c,$r)?:$c->view->render($f,(array)$r+['r'=>$c->router]);};
$d->error=function($c,$e)use($t){if(E)throw$e;return$t('error',[$c,$e]);};
set_error_handler(function($t,$m,$f,$l)use($c,$d){if(error_reporting()){echo$d('error',[$c,new\ErrorException("$m in $f line $l",500)]);die;}},E_ALL);
try{list($f,$m)=$c->router->match(R);return$d('request',[$c,$f,$m])?:$t($f,[0=>$c]+$m);}catch(\Exception$e){return$d('error',[$c,$e]);}}}
\ No newline at end of file
set_error_handler(function($t,$m,$f,$l)use($c,$d){if(error_reporting()){echo$d->error($c,new\ErrorException("$m in $f line $l",500));die;}},E_ALL);
try{list($f,$m)=$c->router->match(R);return$d->request($c,$f,$m)?:$t($f,[0=>$c]+$m);}catch(\Exception$e){return$d->error($c,$e);}}}
\ No newline at end of file
<?namespace Picco;class Dispatcher{protected$e;
function __set($n,$c){$this->e[$n][]=$c;}
function __invoke($n,$a){foreach(@$this->e[$n]?:[]as$e)if(null!==$r=call_user_func_array($e,$a))return$r;}}
\ No newline at end of file
function __call($n,$a){foreach(@$this->e[$n]?:[]as$e)if(null!==$r=call_user_func_array($e,$a))return$r;}}
\ No newline at end of file
......@@ -47,7 +47,7 @@ Mock::$services = function(Container $c) { $c->dispatcher->request = function()
a($a->run() === 'blocked');
Mock::$routing = [ 'foo' => '/test' ];
Mock::$services = function(Container $c) { $c->dispatcher->response = function(Container $c, &$vars) { $vars['foo'] = 'overwritten'; }; };
Mock::$services = function(Container $c) { $c->dispatcher->response = function(Container $c, \stdClass $vars) { $vars->foo = 'overwritten'; }; };
$expected = <<<HTML
<p>foo = overwritten</p>
<p>view = foo</p>
......
......@@ -6,19 +6,19 @@ $out = 0;
$d->foo = function($a, $b) use (&$out) { $out += $a + $b; if ($a == 0) { return true; } };
$d('foo', [1,2]);
$d->foo(1,2);
a(3 == $out);
$d('foo', [3,4]);
$d->foo(3,4);
a(10 == $out);
$d('foo', [0,1]);
$d->foo(0,1);
a(11 == $out);
$d->foo = function($a, $b) use (&$out) { $out += 2*($a + $b); };
$d('foo', [1,2]);
$d->foo(1,2);
a(20 == $out); // current 11 + first listener 3 + second listener 6
$d('foo', [0,4]);
$d->foo(0,4);
a(24 == $out); // current 20 + first listener 4, second listener not executed
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment