[Pupcake] Microframework
Moderator: General Moderators
-
superjimthunderbird
- Forum Newbie
- Posts: 6
- Joined: Mon Jun 04, 2012 11:37 pm
[Pupcake] Microframework
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
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
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.
-
superjimthunderbird
- Forum Newbie
- Posts: 6
- Joined: Mon Jun 04, 2012 11:37 pm
Re: [Pupcake] Microframework
Thanks for the feedback. I will plan to see the best way to add this feature.
Best Regards,
Jim
Best Regards,
Jim
-
superjimthunderbird
- Forum Newbie
- Posts: 6
- Joined: Mon Jun 04, 2012 11:37 pm
Re: [Pupcake] Microframework
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/Pupcaketr0gd0rr 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.
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();
Best Regards,
Jim
Re: [Pupcake] Microframework
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.
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.
-
superjimthunderbird
- Forum Newbie
- Posts: 6
- Joined: Mon Jun 04, 2012 11:37 pm
Re: [Pupcake] Microframework
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
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
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();-
superjimthunderbird
- Forum Newbie
- Posts: 6
- Joined: Mon Jun 04, 2012 11:37 pm
Re: [Pupcake] Microframework
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
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