Skip to content

Implement static parameters in Handler methods #70

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,42 @@ class ProductHandler {
```


## Static parameters

It's also possible to pass static parameters to Handler method/hook, for example:

```php
<?php

Toro::serve(array(
"/" => "ExampleHandler?param1=foo&param2=bar",
"/catalog/page/([0-9]+)" => "CatalogHandler?param1=foo",
));

class ExampleHandler {
function get($a) {
echo $a['param1']; // Output: foo
echo $a['param2']; // Output: bar
}
}

class CatalogHandler {
function __construct() {
ToroHook::add("before_handler", function($toro) {
$static_parameters = end($toro['regex_matches']);
echo $static_parameters['param1']; // 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 the request.


## RESTful Handlers

```php
Expand Down
66 changes: 66 additions & 0 deletions src/Toro.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,72 @@ 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&param2=bar",
| ));
|
| ..with the route example above you can access `param1` and `param2`
| 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($toro) {
| $static_params = end($toro['regex_matches]);
| echo $static_params['param1']; // Output: foo
| echo $static_params['param2']; // Output: bar
| }
| }
|
| function get($a, $b) {
| echo $a; // Output: woot
| echo $b['param1']; // Output: foo
| echo $b['param2']; // Output: bar
| }
| }
|
*/

if (is_string($discovered_handler) &&
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
// 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);
}

$static_parameters = explode('&', $matches[1]);
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
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);

}

$result = null;

$handler_instance = null;
Expand Down