-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Dispatcher must be a callable #73
Conversation
Interesting approach |
@@ -130,7 +132,7 @@ public function run($request = null, $response = null) | |||
$httpFlow = $this->getEventManager(); | |||
|
|||
try { | |||
$routerInfo = $dispatcher->dispatch($request); | |||
$routerInfo = $dispatcher($request); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about call_user_func($dispatcher, $request)
here?
Penny still requires only PHP 5.4+ and the notation $dispatcher($request)
will be indipendent only from PHP 7: https://3v4l.org/Rd6qm
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
anyway, we now require php 5.5 #76
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@samsonasik the problem exists for php until 5.6.13 (see the 3v4l link)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yup, seems call_user_func
is for the rescue now
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 @gianarb can you explain here how to use the router callable? using callable we can use something like this? (see below) "router" => function (/*...*/) {
// my router
} or "router" => new RouterFactory(/*...*/),
class RouterFactory
{
public function __invoke(/*...*/) {
}
} Right? Which kind of data type the router callable have to return? Any interface or just another callable (the executable action)? "router" => function (/*...*/) {
return function (/*...*/) {
// Here is my dispatch-able controller
}
} |
Yes @wdalmut, right your first two examples already work At the moment after |
This is a good way to standardize dispatcher implementation and to improve future implementations
fb348a5
to
6561f89
Compare
Here my Penny with a single route and Pimple as a container! (when you fix #77) <?php
require __DIR__ . '/../vendor/autoload.php';
use GianArb\Penny\App;
use Interop\Container\Pimple\PimpleInterop;
use Zend\EventManager\EventManager;
use Zend\Diactoros\Response\SapiEmitter;
$container = new PimpleInterop();
$container["event_manager"] = function() {
return new EventManager();
};
$container["dispatcher"] = function($r) {
return $r["router"];
};
$container["index"] = new Controller();
$container["router"] = function($r) {
return function ($request) {
return [
null,
["index", "index"],
["ok"],
];
};
};
$emitter = new SapiEmitter();
$app = new App($container);
$emitter->emit($app->run());
class Controller {
public function index($request, $response)
{
$response->getBody()->write("Here is my skeleton");
return $response;
}
} You will see |
Easy! :) $container = new PimpleInterop();
$container["event_manager"] = function() {
return new EventManager();
};
$container["dispatcher"] = function($r) {
return $r["router"];
};
$container["index"] = new Controller();
$container["router"] = function($r) {
return function ($request) {
return [
null,
["index", "index"],
["ok"],
];
};
}; To reuse it :D |
apply call_user_func_array to dispatcher invoke
This is a good way to standardize dispatcher
implementation and to improve future implementations
/cc @wdalmut @ezimuel 👍