Only load routes from the apps we need

This commit is contained in:
Robin Appelman 2014-03-24 15:41:46 +01:00
parent 0d0aac6fff
commit f17674fef2
2 changed files with 37 additions and 13 deletions

View File

@ -47,6 +47,8 @@ class Router implements IRouter {
protected $loaded = false; protected $loaded = false;
protected $loadedApps = array();
public function __construct() { public function __construct() {
$baseUrl = \OC_Helper::linkTo('', 'index.php'); $baseUrl = \OC_Helper::linkTo('', 'index.php');
if (!\OC::$CLI) { if (!\OC::$CLI) {
@ -93,18 +95,34 @@ class Router implements IRouter {
/** /**
* loads the api routes * loads the api routes
*/ */
public function loadRoutes() { public function loadRoutes($app = null) {
if ($this->loaded) { if ($this->loaded) {
return; return;
} }
if (is_null($app)) {
$this->loaded = true; $this->loaded = true;
foreach ($this->getRoutingFiles() as $app => $file) { $routingFiles = $this->getRoutingFiles();
} else {
if (isset($this->loadedApps[$app])) {
return;
}
$this->loadedApps[$app] = true;
$file = \OC_App::getAppPath($app) . '/appinfo/routes.php';
if (file_exists($file)) {
$routingFiles = array($file);
} else {
$routingFiles = array();
}
}
foreach ($routingFiles as $app => $file) {
$this->useCollection($app); $this->useCollection($app);
require_once $file; require_once $file;
$collection = $this->getCollection($app); $collection = $this->getCollection($app);
$collection->addPrefix('/apps/' . $app); $collection->addPrefix('/apps/' . $app);
$this->root->addCollection($collection); $this->root->addCollection($collection);
} }
if (!isset($this->loadedApps['core'])) {
$this->loadedApps['core'] = true;
$this->useCollection('root'); $this->useCollection('root');
require_once 'settings/routes.php'; require_once 'settings/routes.php';
require_once 'core/routes.php'; require_once 'core/routes.php';
@ -115,6 +133,7 @@ class Router implements IRouter {
$collection->addPrefix('/ocs'); $collection->addPrefix('/ocs');
$this->root->addCollection($collection); $this->root->addCollection($collection);
} }
}
/** /**
* @param string $name * @param string $name
@ -158,7 +177,12 @@ class Router implements IRouter {
* @throws \Exception * @throws \Exception
*/ */
public function match($url) { public function match($url) {
if (substr($url, 0, 6) === '/apps/') {
list(, , $app,) = explode('/', $url, 4);
$this->loadRoutes($app);
} else {
$this->loadRoutes(); $this->loadRoutes();
}
$matcher = new UrlMatcher($this->root, $this->context); $matcher = new UrlMatcher($this->root, $this->context);
$parameters = $matcher->match($url); $parameters = $matcher->match($url);
if (isset($parameters['action'])) { if (isset($parameters['action'])) {

View File

@ -22,7 +22,7 @@ interface IRouter {
/** /**
* loads the api routes * loads the api routes
*/ */
public function loadRoutes(); public function loadRoutes($app = null);
/** /**
* Sets the collection to use for adding routes * Sets the collection to use for adding routes