Browse Source

Only sort routes when new paths are added.

Remove complexity and nested statements where possible.
mark_story 11 years ago
parent
commit
839282cc3e
1 changed files with 16 additions and 25 deletions
  1. 16 25
      src/Routing/RouteCollection.php

+ 16 - 25
src/Routing/RouteCollection.php

@@ -78,14 +78,6 @@ class RouteCollection {
 		$this->_paths[$path][] = $route;
 
 		/*
-		// Index scopes by path (for parsing)
-		if (empty(static::$_pathScopes[$path])) {
-			static::$_pathScopes[$path] = $collection;
-			krsort(static::$_pathScopes);
-		} else {
-			static::$_pathScopes[$path]->merge($collection);
-		}
-
 		// Index scopes by key params (for reverse routing).
 		$plugin = isset($params['plugin']) ? $params['plugin'] : '';
 		$prefix = isset($params['prefix']) ? $params['prefix'] : '';
@@ -104,27 +96,26 @@ class RouteCollection {
  * @return array An array of request parameters parsed from the url.
  */
 	public function parse($url) {
-		krsort($this->_paths);
-		foreach ($this->_paths as $path => $collection) {
-			if (strpos($url, $path) !== 0) {
-				continue;
+		foreach (array_keys($this->_paths) as $path) {
+			if (strpos($url, $path) === 0) {
+				break;
 			}
+		}
 
-			$queryParameters = null;
-			if (strpos($url, '?') !== false) {
-				list($url, $queryParameters) = explode('?', $url, 2);
-				parse_str($queryParameters, $queryParameters);
+		$queryParameters = null;
+		if (strpos($url, '?') !== false) {
+			list($url, $queryParameters) = explode('?', $url, 2);
+			parse_str($queryParameters, $queryParameters);
+		}
+		foreach ($this->_paths[$path] as $route) {
+			$r = $route->parse($url);
+			if ($r === false) {
+				continue;
 			}
-			foreach ($collection as $route) {
-				$r = $route->parse($url);
-				if ($r === false) {
-					continue;
-				}
-				if ($queryParameters) {
-					$r['?'] = $queryParameters;
-				}
-				return $r;
+			if ($queryParameters) {
+				$r['?'] = $queryParameters;
 			}
+			return $r;
 		}
 		throw new MissingRouteException(['url' => $url]);
 	}