Skip to content

Commit aa40bde

Browse files
authored
Merge pull request #24295 from nextcloud/perf/noid/router
First attempt to check against core routes before loading all app routes
2 parents 2637f92 + 4b1576e commit aa40bde

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

lib/private/Route/Router.php

+14-12
Original file line numberDiff line numberDiff line change
@@ -234,32 +234,29 @@ public function create($name,
234234
* @throws \Exception
235235
* @return array
236236
*/
237-
public function findMatchingRoute(string $url): array {
238-
if (substr($url, 0, 6) === '/apps/') {
237+
public function findMatchingRoute(string $url, bool $loadAll = false): array {
238+
if (strpos($url, '/apps/') === 0) {
239239
// empty string / 'apps' / $app / rest of the route
240240
[, , $app,] = explode('/', $url, 4);
241241

242242
$app = \OC_App::cleanAppId($app);
243243
\OC::$REQUESTEDAPP = $app;
244244
$this->loadRoutes($app);
245-
} elseif (substr($url, 0, 13) === '/ocsapp/apps/') {
245+
} elseif (strpos($url, '/ocsapp/apps/') === 0) {
246246
// empty string / 'ocsapp' / 'apps' / $app / rest of the route
247247
[, , , $app,] = explode('/', $url, 5);
248248

249249
$app = \OC_App::cleanAppId($app);
250250
\OC::$REQUESTEDAPP = $app;
251251
$this->loadRoutes($app);
252-
} elseif (substr($url, 0, 10) === '/settings/') {
252+
} elseif (strpos($url, '/settings/') === 0) {
253253
$this->loadRoutes('settings');
254-
} elseif (substr($url, 0, 6) === '/core/') {
255-
\OC::$REQUESTEDAPP = $url;
256-
if (!\OC::$server->getConfig()->getSystemValueBool('maintenance') && !Util::needUpgrade()) {
257-
\OC_App::loadApps();
258-
}
259-
$this->loadRoutes('core');
260-
} else {
261-
$this->loadRoutes();
262254
}
255+
\OC::$REQUESTEDAPP = $url;
256+
if (!\OC::$server->getConfig()->getSystemValueBool('maintenance') && !Util::needUpgrade()) {
257+
\OC_App::loadApps();
258+
}
259+
$this->loadRoutes('core');
263260

264261
$matcher = new UrlMatcher($this->root, $this->context);
265262
try {
@@ -272,6 +269,11 @@ public function findMatchingRoute(string $url): array {
272269
try {
273270
$parameters = $matcher->match($url . '/');
274271
} catch (ResourceNotFoundException $newException) {
272+
// Attempt to fallback to load all routes if none of the above route patterns matches and the route is not in core
273+
if (!$loadAll) {
274+
$this->loadRoutes();
275+
return $this->findMatchingRoute($url, true);
276+
}
275277
// If we still didn't match a route, we throw the original exception
276278
throw $e;
277279
}

0 commit comments

Comments
 (0)