Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Fix parsing of regex route within namespace #41

Merged
merged 1 commit into from

2 participants

@abackstrom

Modify parsing of regex routes within namespaces:

  1. Prepend the namespace to all regexes
  2. Interpret circumflex (^) anchor appropriately in relation to namespace
  3. Ensure that negation only matches hits within the namespace

See #40 for original issue and test code.

Slightly wonky aspect of this: the prepending slash.

<?php

with( '/path', function(){
    respond( '@foo', $cb ); // matches /path/foo, /pathfoo, /pathfood, /path/hoofoot
    respond( '@^foo', $cb ); // matches /pathfoo, /pathfood
    respond( '@^/foo', $cb ); // matches /path/foo, /path/food
});
@abackstrom

== should have been ===, per code already in dispatch().

@chriso
Owner

Thanks. The namespace functionality was a tad rushed.

@chriso chriso merged commit 8b7adac into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on May 9, 2012
  1. @abackstrom
This page is out of date. Refresh to see the latest.
Showing with 24 additions and 1 deletion.
  1. +24 −1 klein.php
View
25 klein.php
@@ -19,8 +19,31 @@ function respond($method, $route = '*', $callback = null) {
$method = null;
}
+ if( $__namespace && $route[0] === '@' || ( $route[0] === '!' && $route[1] === '@' ) ) {
+ if( $route[0] === '!' ) {
+ $negate = true;
+ $route = substr( $route, 2 );
+ } else {
+ $negate = false;
+ $route = substr( $route, 1 );
+ }
+
+ // regex anchored to front of string
+ if( $route[0] === '^' ) {
+ $route = substr( $route, 1 );
+ } else {
+ $route = '.*' . $route;
+ }
+
+ if( $negate ) {
+ $route = '@^' . $__namespace . '(?!' . $route . ')';
+ } else {
+ $route = '@^' . $__namespace . $route;
+ }
+ }
+
// empty route with namespace is a match-all
- if( $__namespace && ( null == $route || '*' == $route ) ) {
+ elseif( $__namespace && ( null == $route || '*' === $route ) ) {
$route = '@^' . $__namespace . '(/|$)';
} else {
$route = $__namespace . $route;
Something went wrong with that request. Please try again.