Skip to content

Commit

Permalink
adds support for args in the controller wrapper (w/i dispatcher)
Browse files Browse the repository at this point in the history
  • Loading branch information
henderjon committed Oct 29, 2014
1 parent 89239ad commit db00f5c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/Dispatcher/Dispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ function dispatch( RouteInterface $route ){
$object = [$object, $method];
}

return call_user_func($object);
return call_user_func_array($object, $args);
};

}
Expand Down
20 changes: 13 additions & 7 deletions tests/PHPUnit/Dispatcher/DispatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class BasicController implements DispatchableInterface {
function __construct($di, $route){}
function __invoke(){ return $this; }
function init(){ $this->called = 323; }
function getCalled(){ return $this->called; }
function getCalled($int = 0){ return $this->called + (int)$int; }
}

class TestLog extends \Psr\Log\AbstractLogger {
Expand Down Expand Up @@ -55,12 +55,18 @@ function test_dispatch(){

$dispatcher = new Dispatcher($di, "Chevron\\Kernel\\");

$controller = call_user_func($dispatcher->dispatch($route));

// note the invokation
$this->assertTrue($controller InstanceOf BasicController);
$this->assertEquals($controller->getCalled() , 323);

$lambda = $dispatcher->dispatch($route);

// note the invokation -- the lambda wraps the routed object (controller)
// simple invokation calls __invoke on the controller
$this->assertTrue($lambda() InstanceOf DispatchableInterface);
// since __invoke returns $this, call a method of our controller
$this->assertEquals($lambda()->getCalled() , 323);
// the lambda acts as the wrapper for our object and allows us to pass method names and args to our controller
$this->assertEquals($lambda("getCalled", [4]) , 327);
// in a frontend controller, your wrapper would call a method (or __invoke) on our desired object and that
// method might in turn return a callable (perhaps a view) that is then sent somewhere else or
// invoked itself.
}


Expand Down

0 comments on commit db00f5c

Please sign in to comment.