Two tiny patches

93 views
Skip to first unread message

Etenil

unread,
Apr 27, 2012, 7:32:07 PM4/27/12
to GluePHP
Hi everyone,

I know the project's policy is to not accept patches, but I thought
I'd better share those anyway...

They are two very small patches that I believe comply with the
project's philosophy.

The first one is a change on the method call that makes use of
call_user_func_array to pass URL matches as actual parameters to the
called method. This makes the resulting code a little cleaner (we
don't have to deal with an array).

Here's the patch:

diff --git a/glue.php b/glue.php
index 462056a..0c80ecc 100644
--- a/glue.php
+++ b/glue.php
@@ -58,7 +58,8 @@
if (class_exists($class)) {
$obj = new $class;
if (method_exists($obj, $method)) {
- $obj->$method($matches);
+ call_user_func_array(array($obj, $method),
+ array_slice($matches, 1));
} else {
throw new BadMethodCallException("Method,
$method, not supported.");
}

You'd simply use the result like so:

$urls = array('page/(\d+)' => 'Pages');
class Pages { function GET($page_num) { echo "This is page #
$page_num."; } }
glue::stick($urls);

The second one introduces the ability to specify a prefix to URL
mapping. This is useful when the project gets deployed into a
subfolder, and even works when URL rewriting is turned off!

The patch:

diff --git a/glue.php b/glue.php
index 0c80ecc..ae452b5 100644
--- a/glue.php
+++ b/glue.php
@@ -36,12 +36,13 @@
* the main static function of the glue class.
*
* @param array $urls The regex-based url to
class mapping
+ * @param string $prefix The URL prefix to apply to path
matching
* @throws Exception Thrown if corresponding
class is not found
* @throws Exception Thrown if no match is
found
* @throws BadMethodCallException Thrown if a corresponding
GET,POST is not found
*
*/
- static function stick ($urls) {
+ static function stick ($urls, $prefix = "") {

$method = strtoupper($_SERVER['REQUEST_METHOD']);
$path = $_SERVER['REQUEST_URI'];
@@ -51,7 +52,7 @@
krsort($urls);

foreach ($urls as $regex => $class) {
- $regex = str_replace('/', '\/', $regex);
+ $regex = str_replace('/', '\/', $prefix . $regex);
$regex = '^' . $regex . '\/?$';
if (preg_match("/$regex/i", $path, $matches)) {
$found = true;

Here's how you'd use it if your URL was http://someho.st/gluephp/... :

glue::stick($urls, "/gluephp");

And as a bonus, we also can run gluePHP without URL rewriting:

glue::stick($urls, "/gluephp/index.php");
Reply all
Reply to author
Forward
0 new messages