Hi all,
As part of the work toward releasing v2 packages, I present the new Aura.Dispatcher package:
<
https://github.com/auraphp/Aura.Dispatcher>
(As a side-note, it uses PSR-4 autoloading instead of PSR-0, which means the src/ and tests/ directories are shallower. The README has some different front-matter as well, and the tests directory is organized a bit differently.)
In putting together the v1 framework built from Aura packages, we embedded some things that turn out to be de-couple-able from the framework. For example, in the existing framework, we have a front controller that maps controller names to factories; it reads the routing information and then picks a controller to invoke.
Aura.Dispatcher is an extracted version of that front-controller/factory system, modified to allow for micro-framework closure controllers as well as for recursive dispatching. Instantiating a dispatcher is dead easy:
// the dispatcher instance
$dispatcher = new Dispatcher;
// look in this param for the object to dispatch to
$dispatcher->setObjectParam('controller');
// look in this param for the method to invoke, if needed
$dispatcher->setMethodParam('action');
Here's a micro-framework invocation style:
$params = [
'controller' => function ($id) {
return "Read blog entry $id";
},
'id' => 88,
];
$result = $dispatcher->__invoke($params);
echo $result; // Read blog entry 88
And here's a full-stack invocation style; note that the call to the dispatcher is the same as with the micro-framework style:
// the controller class
class Blog
{
public function browse()
{
// ...
}
public function read($id)
{
return "Read blog entry $id";
}
public function edit($id)
{
// ...
}
public function add()
{
// ...
}
public function delete($id)
{
// ...
}
}
// add to the dispatcher a factory closure that returns a Blog instance;
// could just as well be $di->lazyNew('Blog') or $di->newFactory('Blog')
$dispatcher->setObject('blog', function () {
return new Blog;
});
// $params taken from Aura\Router\Route->values
$params = [
'controller' => 'blog',
'action' => 'read',
'id' => 88,
];
$result = $dispatcher->__invoke($params);
echo $result; // "Read blog entry 88"
Take a look at the new Aura.Dispatcher stuff and let us know what you think.
<
https://github.com/auraphp/Aura.Dispatcher>
Thanks!
--
Paul M. Jones
pmjo...@gmail.com
http://paul-m-jones.com