From 4b9200f6f7a571c251ef89599e1af9e25e2e75f4 Mon Sep 17 00:00:00 2001
From: Bart Visscher <bartv@thisnet.nl>
Date: Thu, 2 Aug 2012 21:51:31 +0200
Subject: [PATCH] Routing: combine all routes into one set

---
 lib/api.php    |  1 +
 lib/router.php | 22 ++++++++++++++--------
 ocs/v1.php     |  4 +---
 3 files changed, 16 insertions(+), 11 deletions(-)

diff --git a/lib/api.php b/lib/api.php
index 1ef4e090e37..05d34ffe870 100644
--- a/lib/api.php
+++ b/lib/api.php
@@ -44,6 +44,7 @@ class OC_API {
 		$name = strtolower($method).$url;
 		$name = str_replace(array('/', '{', '}'), '_', $name);
 		if(!isset(self::$actions[$name])){
+			OC::getRouter()->useCollection('ocs');
 			OC::getRouter()->create($name, $url.'.{_format}')
 				->method($method)
 				->defaults(array('_format' => 'xml') + $defaults)
diff --git a/lib/router.php b/lib/router.php
index 5c5171cf82d..12cd55df414 100644
--- a/lib/router.php
+++ b/lib/router.php
@@ -15,32 +15,38 @@ use Symfony\Component\Routing\Exception\ResourceNotFoundException;
 class OC_Router {
 	protected $collections = array();
 	protected $collection = null;
-
-	public function __construct() {
-		// TODO cache
-		$this->loadRoutes();
-	}
+	protected $root = null;
 
 	/**
 	 * loads the api routes
 	 */
 	public function loadRoutes() {
 		// TODO cache
+		$this->root = $this->getCollection('root');
 		foreach(OC_APP::getEnabledApps() as $app){
 			$file = OC_App::getAppPath($app).'/appinfo/routes.php';
 			if(file_exists($file)){
+				$this->useCollection($app);
 				require_once($file);
+				$collection = $this->getCollection($app);
+				$this->root->addCollection($collection, '/apps/'.$app);
 			}
 		}
 		// include ocs routes
 		require_once(OC::$SERVERROOT.'/ocs/routes.php');
+		$collection = $this->getCollection('ocs');
+		$this->root->addCollection($collection, '/ocs');
 	}
 
-	public function useCollection($name) {
+	protected function getCollection($name) {
 		if (!isset($this->collections[$name])) {
 			$this->collections[$name] = new RouteCollection();
 		}
-		$this->collection = $this->collections[$name];
+		return $this->collections[$name];
+	}
+
+	public function useCollection($name) {
+		$this->collection = $this->getCollection($name);
 	}
 
 	public function create($name, $pattern, array $defaults = array(), array $requirements = array()) {
@@ -51,7 +57,7 @@ class OC_Router {
 
     	public function match($url) {
 		$context = new RequestContext($_SERVER['REQUEST_URI'], $_SERVER['REQUEST_METHOD']);
-		$matcher = new UrlMatcher($this->collection, $context);
+		$matcher = new UrlMatcher($this->root, $context);
 		$parameters = $matcher->match($url);
 		if (isset($parameters['action'])) {
 			$action = $parameters['action'];
diff --git a/ocs/v1.php b/ocs/v1.php
index 938a57009f7..ce6bad3d450 100644
--- a/ocs/v1.php
+++ b/ocs/v1.php
@@ -25,10 +25,8 @@ require_once('../lib/base.php');
 use Symfony\Component\Routing\Exception\ResourceNotFoundException;
 use Symfony\Component\Routing\Exception\MethodNotAllowedException;
 
-OC::getRouter()->useCollection('ocs');
-
 try {
-	OC::getRouter()->match($_SERVER['PATH_INFO']);
+	OC::getRouter()->match('/ocs'.$_SERVER['PATH_INFO']);
 } catch (ResourceNotFoundException $e) {
 	OC_OCS::notFound();
 } catch (MethodNotAllowedException $e) {
-- 
GitLab