Skip to content

Commit

Permalink
non-greedy route parser for quanitifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
mrjgreen committed Aug 11, 2014
1 parent b3abc18 commit ca119ce
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 5 deletions.
1 change: 1 addition & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="true"
syntaxCheck="false"
bootstrap="vendor/autoload.php"
>
Expand Down
3 changes: 1 addition & 2 deletions src/Phroute/Dispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,7 @@ private function dispatchVariableRoute($httpMethod, $uri)
while(!isset($data['routeMap'][$count++]));

$routes = $data['routeMap'][$count - 1];



if (!isset($routes[$httpMethod]))
{
$httpMethod = $this->checkFallbacks($routes, $httpMethod);
Expand Down
2 changes: 1 addition & 1 deletion src/Phroute/RouteParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class RouteParser {
"~\{
\s* ([a-zA-Z][a-zA-Z0-9_]*) \s*
(?:
: \s* ([^{]*(?:\{.*\})?)
: \s* ([^{]+(?:\{.*?\})?)
)?
\}\??~x";

Expand Down
24 changes: 22 additions & 2 deletions test/Dispatcher/DispatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -706,15 +706,35 @@ public function provideFoundDispatchCases()
// 12 -------------------------------------------------------------------------------------->
// Test \d{3,4} style quantifiers
$callback = function($r) {
$r->addRoute('GET', 'server/{ip:\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}}/{name}/{newname}?', function($year, $name, $newname = null) {
return trim("$year $name $newname");
$r->addRoute('GET', 'server/{ip:\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}}/{name}/{newname}?', function($ip, $name, $newname = null) {
return trim("$ip $name $newname");
});
};

$cases[] = ['GET', 'server/10.10.10.10/server1', $callback, '10.10.10.10 server1'];
$cases[] = ['GET', 'server/0.0.0.0/server2', $callback, '0.0.0.0 server2'];
$cases[] = ['GET', 'server/123.2.23.111/server3/server4', $callback, '123.2.23.111 server3 server4'];

// 13 -------------------------------------------------------------------------------------->
// Test \d{3,4} style quantifiers
$callback = function($r) {
$r->addRoute('GET', 'date/{year:\d{4}}/{month:\d+}?', function($year, $month) {
return trim("$year $month");
});
};

$cases[] = ['GET', 'date/1990/05', $callback, '1990 05'];

// 14 -------------------------------------------------------------------------------------->
// Test \d{3,4} style quantifiers
$callback = function($r) {
$r->addRoute('GET', 'date/{year:\d{4}}/{month:\d{2}}', function($year, $month) {
return trim("$year $month");
});
};

$cases[] = ['GET', 'date/2010/06', $callback, '2010 06'];

return $cases;
}

Expand Down

0 comments on commit ca119ce

Please sign in to comment.