Skip to content

Commit

Permalink
Merge pull request #60 from small1/master
Browse files Browse the repository at this point in the history
Added Toro::hasHandlerFor function to check if a route is served or not
  • Loading branch information
martinbean committed Sep 24, 2013
2 parents 9feb915 + a1b820d commit 31491bb
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion src/Toro.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,35 @@

class Toro
{

private static $used_routes;
private static $used_tokens;

public static function hasHandlerFor($path_info)
{
if (isset(self::$used_routes[$path_info])) {
return true;
}

foreach (self::$used_routes as $pattern => $handler_name) {
$pattern = strtr($pattern, self::$used_tokens);
if (preg_match('#^/?' . $pattern . '/?$#', $path_info, $matches)) {
return true;
}
}
return false;
}

public static function serve($routes)
{
self::$used_routes = $routes;
self::$used_tokens = array(
':string' => '([a-zA-Z]+)',
':number' => '([0-9]+)',
':alpha' => '([a-zA-Z0-9-_]+)'
);
foreach ($extratokens as $key => $value) self::$used_tokens[$key]="($value)";

ToroHook::fire('before_request', compact('routes'));

$request_method = strtolower($_SERVER['REQUEST_METHOD']);
Expand Down Expand Up @@ -33,7 +60,7 @@ public static function serve($routes)
':alpha' => '([a-zA-Z0-9-_]+)'
);
foreach ($routes as $pattern => $handler_name) {
$pattern = strtr($pattern, $tokens);
$pattern = strtr($pattern, self::$used_tokens);
if (preg_match('#^/?' . $pattern . '/?$#', $path_info, $matches)) {
$discovered_handler = $handler_name;
$regex_matches = $matches;
Expand Down

0 comments on commit 31491bb

Please sign in to comment.