Skip to content
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

Merged
merged 4 commits into from
Oct 2, 2015
Merged

Dispatcher must be a callable #73

merged 4 commits into from
Oct 2, 2015

Conversation

gianarb
Copy link
Contributor

@gianarb gianarb commented Oct 1, 2015

This is a good way to standardize dispatcher
implementation and to improve future implementations

/cc @wdalmut @ezimuel 👍

@fntlnz
Copy link
Contributor

fntlnz commented Oct 1, 2015

Interesting approach

@@ -130,7 +132,7 @@ public function run($request = null, $response = null)
$httpFlow = $this->getEventManager();

try {
$routerInfo = $dispatcher->dispatch($request);
$routerInfo = $dispatcher($request);
Copy link
Contributor

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

Copy link
Member

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

Copy link
Contributor

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)

Copy link
Member

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

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gianarb I created PR #78 for it

@wdalmut
Copy link
Contributor

wdalmut commented Oct 1, 2015

👍 @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
  }
}

@gianarb
Copy link
Contributor Author

gianarb commented Oct 1, 2015

Yes @wdalmut, right your first two examples already work

At the moment after $dispatcher($router) I wait a callable..
Before call it I load controller by Di..
https://github.com/pennyphp/penny/blob/master/src/App.php#L151

This is a good way to standardize dispatcher
implementation and to improve future implementations
@wdalmut
Copy link
Contributor

wdalmut commented Oct 1, 2015

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 Here is my skeleton

@gianarb
Copy link
Contributor Author

gianarb commented Oct 1, 2015

Easy! :)
You can add this into the Penny\Container\PimpeFactory

$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
gianarb pushed a commit that referenced this pull request Oct 2, 2015
@gianarb gianarb merged commit fcecbdf into master Oct 2, 2015
@gianarb gianarb deleted the feature/dispatch-callable branch October 5, 2015 08:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants