Page 1 of 1
[Pupcake] Microframework
Posted: Mon Jun 04, 2012 11:43 pm
by superjimthunderbird
Hi All,
I am introducing a minimum microframework, Pupcake.
http://superjimpupcake.github.com/Pupcake/
and it's also on Packagist.
http://packagist.org/packages/Pupcake/Pupcake
It's inspired by Slim and Silex framework but with only a small set of core features, one significant one is it does not support regex in route, it only support named tokens. Other features can be added through the event based system.
I look forward to hearing from all of you on feedbacks so I can improve it.
Thanks.
Best Regards,
Jim
Re: [Pupcake] Microframework
Posted: Wed Jun 06, 2012 2:29 pm
by tr0gd0rr
Very cool. Looks a lot like
Express for NodeJS. I think it would be a lot more robust if the functions took as parameters request and response objects like Express does. But your approach is very simple and straightforward! Awesome ideas.
Re: [Pupcake] Microframework
Posted: Wed Jun 06, 2012 3:21 pm
by superjimthunderbird
Thanks for the feedback. I will plan to see the best way to add this feature.
Best Regards,
Jim
Re: [Pupcake] Microframework
Posted: Thu Jun 07, 2012 7:46 pm
by superjimthunderbird
tr0gd0rr wrote:Very cool. Looks a lot like
Express for NodeJS. I think it would be a lot more robust if the functions took as parameters request and response objects like Express does. But your approach is very simple and straightforward! Awesome ideas.
Hi, I just add a Express service in the Pupcake framework, it will allow you to use express-like apis like you mentioned, you can see more details in
https://github.com/superjimpupcake/Pupcake
Code: Select all
<?php
//Assiming this is public/index.php and the composer vendor directory is ../vendor
require_once __DIR__.'/../vendor/autoload.php';
$app = new Pupcake\Pupcake();
$app->getService("Pupcake\Service\Express");
$app->get("date/:year/:month/:day", function($req, $res){
$output = $req->params('year').'-'.$req->params('month').'-'.$req->params('day');
$res->send($output);
});
$app->run();
It's more of a demo now honestly, cloning all the apis in express is a huge project, so now I only support $res->send and $req->params, probably is a good starting point.
Best Regards,
Jim
Re: [Pupcake] Microframework
Posted: Fri Jun 08, 2012 2:22 pm
by tr0gd0rr
Nice!
PHP 5.3 offers some crazy things. Express passes a third prameter "next" which you can call to defer to the next matching route. A kind of silly example is to have a route that is date/:year/:month/:day and another that is date/:month/:day/:year. In the first handler, if :day is a 4-digit number, calling next() would execute the handler for the second route. If there are no more matching routes, next() executes the handler for the 404 route.
So what is crazy is that you can use PHP's __invoke() magic method to pass an object $next as the third parameter which will call the handler for the next matching route with the proper $req, $res, $next parameters. You could pass the new handler directly as $next, but then it wouldn't receive $req, $res, and the new $next. But if you pass an object with an __invoke() method as $next, your handler could simply call $next() and it would execute the handler that matches the next route and pass the $req, $res, and $next.
I'm thinking you could probably emulate almost all of Express's API, even a lot of the handlers and closures.
Re: [Pupcake] Microframework
Posted: Fri Jun 08, 2012 4:46 pm
by superjimthunderbird
Hi,
Thanks for the feedback, I will think about this down the road. Emulating majority of the Express's API syntax is possible, I will add more of them to it when I have time. Right now I'm adding more and more unit tests to make sure the codebase is solid.
Also, as an update, now when you use the Express service, you can do $res->forward and $res->redirect.
$res->forward might be similar to what you mentioned on the next() concept.
Feel free to use it if you like.
Best Regards,
Jim
Re: [Pupcake] Microframework
Posted: Mon Jun 11, 2012 1:32 pm
by tr0gd0rr
This is all very interesting academically. This is how you could implement $next(): Curry in $req, $res, and $next.
Code: Select all
class CurriedFunction {
public function __construct($callback/*[, $arg1][, $arg2][, $argN]*/) {
$this->args = func_get_args();
$this->callback = array_shift($this->args);
}
public function __invoke(/*[, $arg1][, $arg2][, $argN]*/) {
$moreArgs = func_get_args();
$args = array_merge($this->args, $moreArgs);
return call_user_func_array($this->callback, $args);
}
}
$world = new CurriedFunction('substr', 'Hello World.', 6);
echo $world(); // World.
echo '<br />';
echo $world(1); // W
$next = new CurriedFunction($req, $res, $next);
$next();
Re: [Pupcake] Microframework
Posted: Fri Jun 15, 2012 1:43 am
by superjimthunderbird
Hi,
I finally got some time to implement this $next feature, check out
https://github.com/superjimpupcake/Pupcake
In the readme file, search for "$next function", I explain a bit on how to use it.
Best Regards,
Jim