From 5f3b72a4d9cdd98f17d243daae357d516c1d53d5 Mon Sep 17 00:00:00 2001 From: Sergey Lukin Date: Tue, 17 Dec 2013 19:14:50 -0500 Subject: [PATCH 1/3] feature-static-parameters: Implement static parameters in Handler methods --- README.md | 35 +++++++++++++++++++++++++++++++++ src/Toro.php | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+) diff --git a/README.md b/README.md index c3a3210..0d90059 100644 --- a/README.md +++ b/README.md @@ -89,6 +89,41 @@ class ProductHandler { ``` +## Static parameters + +It's also possible to pass static parameters to Handler method/hook, for example: + +```php + "ExampleHandler?param1=foo¶m2=bar", + "/catalog/page/([0-9]+)" => "CatalogHandler?param1=foo", +)); + +class ExampleHandler { + function get($a, $b) { + echo $a; // Output: foo + echo $b; // Output: bar + } +} + +class CatalogHandler { + function __construct() { + ToroHook::add("before_handler", function($arr) { + echo $arr['regex_matches'][2]; // Output: foo + } + } + + function get($page, $param) { + echo $param; // Output: foo + } +} +``` + +Note that static parameters don't affect any HTTP data passed with request. + + ## RESTful Handlers ```php diff --git a/src/Toro.php b/src/Toro.php index 6e4787d..ff67e1b 100644 --- a/src/Toro.php +++ b/src/Toro.php @@ -42,6 +42,61 @@ public static function serve($routes) } } + /* + |-------------------------------------------------------------------------- + | Handler static parameters + |-------------------------------------------------------------------------- + | + | Populate Handler methods(get(), post(), etc.) and hooks with parameters + | defined in Query String style Handler name suffix + | + | For example: + | + | Toro::serve(array( + | "/:string" => "MainHandler?param1=foo¶m2=bar", + | )); + | + | ..with the route example above you can access `param1` and `param2` + | via 2 last parameters respectively (parameters names are not + | preserved). Assuming GET HTTP request is sent to `/woot`, + | Handler may look like following: + | + | class MainHandler { + | function __construct() { + | ToroHook::add("before_handler", function($arr) { + | echo $arr['regex_matches'][1]; // Output: woot + | echo $arr['regex_matches'][2]; // Output: foo + | echo $arr['regex_matches'][3]; // Output: bar + | } + | } + | + | function get($a, $b, $c) { + | echo '$a - '.$a; // Output: $a - woot + | echo '$b - '.$b; // Output: $b - foo + | echo '$c - '.$c; // Output: $c - bar + | } + | } + | + */ + + if (is_string($discovered_handler) && + preg_match('/^[\w\\\]*\?([\w=&]*)$/', $discovered_handler, $matches)) { + + // Because first item in array returned by preg_match() is + // cut before it's passed to Handler/Hook method, we add a + // "duck" + if( count($regex_matches) === 0 ) { + array_push($regex_matches, null); + } + + array_walk(explode('&', $matches[1]), function($value, $key) use(&$regex_matches) { + list($param, $value) = array_values(explode('=', $value)); + $regex_matches[] = $value; + }); + $discovered_handler = strstr($discovered_handler, '?', true); + + } + $result = null; $handler_instance = null; From 9f0f478a540763fcd3838db9aad15544d0b788f0 Mon Sep 17 00:00:00 2001 From: Sergey Lukin Date: Wed, 18 Dec 2013 09:59:24 -0500 Subject: [PATCH 2/3] feature-static-parameters: Pass static parameters as a key-value array in the end of $regex_matches array --- README.md | 17 +++++++++-------- src/Toro.php | 43 +++++++++++++++++++++++++++---------------- 2 files changed, 36 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index 0d90059..b0a2079 100644 --- a/README.md +++ b/README.md @@ -102,26 +102,27 @@ Toro::serve(array( )); class ExampleHandler { - function get($a, $b) { - echo $a; // Output: foo - echo $b; // Output: bar + function get($a) { + echo $a['param1']; // Output: foo + echo $a['param2']; // Output: bar } } class CatalogHandler { function __construct() { - ToroHook::add("before_handler", function($arr) { - echo $arr['regex_matches'][2]; // Output: foo + ToroHook::add("before_handler", function($toro) { + $static_parameters = end($toro['regex_matches']); + echo $static_parameters['param1']; // Output: foo } } - function get($page, $param) { - echo $param; // Output: foo + function get($page, $static_params) { + echo $static_params['param1']; // Output: foo } } ``` -Note that static parameters don't affect any HTTP data passed with request. +Note that static parameters don't affect any HTTP data passed with the request. ## RESTful Handlers diff --git a/src/Toro.php b/src/Toro.php index ff67e1b..97ecf38 100644 --- a/src/Toro.php +++ b/src/Toro.php @@ -57,23 +57,23 @@ public static function serve($routes) | )); | | ..with the route example above you can access `param1` and `param2` - | via 2 last parameters respectively (parameters names are not - | preserved). Assuming GET HTTP request is sent to `/woot`, + | via last parameter using their names in array fashion. + | Assuming GET HTTP request is sent to `/woot`, | Handler may look like following: | | class MainHandler { | function __construct() { - | ToroHook::add("before_handler", function($arr) { - | echo $arr['regex_matches'][1]; // Output: woot - | echo $arr['regex_matches'][2]; // Output: foo - | echo $arr['regex_matches'][3]; // Output: bar + | ToroHook::add("before_handler", function($toro) { + | $static_params = end($toro['regex_matches]); + | echo $static_params['param1']; // Output: foo + | echo $static_params['param2']; // Output: bar | } | } | - | function get($a, $b, $c) { - | echo '$a - '.$a; // Output: $a - woot - | echo '$b - '.$b; // Output: $b - foo - | echo '$c - '.$c; // Output: $c - bar + | function get($a, $b) { + | echo $a; // Output: woot + | echo $b['param1']; // Output: foo + | echo $b['param2']; // Output: bar | } | } | @@ -82,17 +82,28 @@ public static function serve($routes) if (is_string($discovered_handler) && preg_match('/^[\w\\\]*\?([\w=&]*)$/', $discovered_handler, $matches)) { - // Because first item in array returned by preg_match() is - // cut before it's passed to Handler/Hook method, we add a - // "duck" + // Because first item in array with dynamic parameters returned by + // preg_match() is cut before array is passed to Handler/Hook + // method, we add a "duck" that will be cut instead of + // static parameters array (that we'll add) in case array with + // dynamic parameters is empty if( count($regex_matches) === 0 ) { array_push($regex_matches, null); } - array_walk(explode('&', $matches[1]), function($value, $key) use(&$regex_matches) { - list($param, $value) = array_values(explode('=', $value)); - $regex_matches[] = $value; + $static_parameters = explode('&', $matches[1]); + array_walk($static_parameters, function($value, $key) use(&$static_parameters) { + unset($static_parameters[$key]); + list($param_name, $param_value) = array_values(explode('=', $value)); + $static_parameters[$param_name] = $param_value; }); + + // Add array with static parameters to the end of array with + // dynamic parameters + array_push($regex_matches, $static_parameters); + + // Well, we can't leave Handler namespace name with query string + // like that, right? Let's trim all the static parameters syntax off of it $discovered_handler = strstr($discovered_handler, '?', true); } From 92dfab954490a55ae9bbd56bfe895e1368930789 Mon Sep 17 00:00:00 2001 From: Sergey Lukin Date: Sun, 29 Dec 2013 11:49:19 -0500 Subject: [PATCH 3/3] Use simple foreach to walk through array and rearrange its contents --- src/Toro.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Toro.php b/src/Toro.php index 97ecf38..f8cb95b 100644 --- a/src/Toro.php +++ b/src/Toro.php @@ -80,7 +80,7 @@ public static function serve($routes) */ if (is_string($discovered_handler) && - preg_match('/^[\w\\\]*\?([\w=&]*)$/', $discovered_handler, $matches)) { + preg_match('/^[\w\\\]*\?([\w=&]+)$/', $discovered_handler, $matches)) { // Because first item in array with dynamic parameters returned by // preg_match() is cut before array is passed to Handler/Hook @@ -92,11 +92,11 @@ public static function serve($routes) } $static_parameters = explode('&', $matches[1]); - array_walk($static_parameters, function($value, $key) use(&$static_parameters) { - unset($static_parameters[$key]); + foreach( $static_parameters as $key => $value ) { list($param_name, $param_value) = array_values(explode('=', $value)); $static_parameters[$param_name] = $param_value; - }); + unset($static_parameters[$key]); + } // Add array with static parameters to the end of array with // dynamic parameters