Skip to content

Commit d3ae6af

Browse files
committed
Only load routes of the app which is requested
* Add fallback to load all routes if needed * Move partial loaded routes test to proper place The change for the routes to be loaded via include instead of include_once is to allow it to properl load them in the unit tests as they re-initiate the routes during the same execution cycle. Signed-off-by: Morris Jobke <hey@morrisjobke.de>
1 parent 52af709 commit d3ae6af

File tree

3 files changed

+91
-7
lines changed

3 files changed

+91
-7
lines changed

lib/private/Route/Router.php

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -328,13 +328,27 @@ public function getGenerator() {
328328
public function generate($name,
329329
$parameters = [],
330330
$absolute = false) {
331+
$referenceType = UrlGenerator::ABSOLUTE_URL;
332+
if ($absolute === false) {
333+
$referenceType = UrlGenerator::ABSOLUTE_PATH;
334+
}
335+
$name = $this->fixLegacyRootName($name);
336+
if (strpos($name, '.') !== false) {
337+
list($appName, $other) = explode('.', $name, 3);
338+
// OCS routes are prefixed with "ocs."
339+
if ($appName === 'ocs') {
340+
$appName = $other;
341+
}
342+
$this->loadRoutes($appName);
343+
try {
344+
return $this->getGenerator()->generate($name, $parameters, $referenceType);
345+
} catch (RouteNotFoundException $e) {
346+
}
347+
}
348+
349+
// Fallback load all routes
331350
$this->loadRoutes();
332351
try {
333-
$referenceType = UrlGenerator::ABSOLUTE_URL;
334-
if ($absolute === false) {
335-
$referenceType = UrlGenerator::ABSOLUTE_PATH;
336-
}
337-
$name = $this->fixLegacyRootName($name);
338352
return $this->getGenerator()->generate($name, $parameters, $referenceType);
339353
} catch (RouteNotFoundException $e) {
340354
$this->logger->logException($e);
@@ -377,7 +391,7 @@ protected function fixLegacyRootName(string $routeName): string {
377391
* @param string $appName
378392
*/
379393
private function requireRouteFile($file, $appName) {
380-
$this->setupRoutes(include_once $file, $appName);
394+
$this->setupRoutes(include $file, $appName);
381395
}
382396

383397

tests/lib/Route/RouterTest.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
declare(strict_types=1);
3+
/**
4+
* @copyright Copyright (c) 2019 Morris Jobke <hey@morrisjobke.de>
5+
*
6+
* @license GNU AGPL version 3 or any later version
7+
*
8+
* This program is free software: you can redistribute it and/or modify
9+
* it under the terms of the GNU Affero General Public License as
10+
* published by the Free Software Foundation, either version 3 of the
11+
* License, or (at your option) any later version.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
* GNU Affero General Public License for more details.
17+
*
18+
* You should have received a copy of the GNU Affero General Public License
19+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
20+
*
21+
*/
22+
23+
namespace Test\Route;
24+
25+
use OC\Route\Router;
26+
use OCP\ILogger;
27+
use Test\TestCase;
28+
29+
/**
30+
* Class RouterTest
31+
*
32+
* @package Test\Route
33+
*/
34+
class RouterTest extends TestCase {
35+
public function generateRouteProvider(): array {
36+
return [
37+
['files.view.index', '/index.php/apps/files/'],
38+
// the OCS route is the prefixed one for the AppFramework - see /ocs/v1.php for routing details
39+
['ocs.dav.direct.getUrl', '/index.php/ocsapp/apps/dav/api/v1/direct'],
40+
// special route name - should load all apps and then find the route
41+
['files_ajax_list', '/index.php/apps/files/ajax/list.php'],
42+
];
43+
}
44+
45+
/**
46+
* @dataProvider generateRouteProvider
47+
* @param $routeName
48+
* @param $expected
49+
*/
50+
public function testGenerate($routeName, $expected): void {
51+
/** @var ILogger $logger */
52+
$logger = $this->createMock(ILogger::class);
53+
$router = new Router($logger);
54+
55+
$this->assertEquals($expected, $router->generate($routeName));
56+
}
57+
58+
public function testGenerateTwice(): void {
59+
/** @var ILogger $logger */
60+
$logger = $this->createMock(ILogger::class);
61+
$router = new Router($logger);
62+
63+
$this->assertEquals('/index.php/apps/files/', $router->generate('files.view.index'));
64+
$this->assertEquals('/index.php/ocsapp/apps/dav/api/v1/direct', $router->generate('ocs.dav.direct.getUrl'));
65+
$this->assertEquals('/index.php/apps/files/ajax/list.php', $router->generate('files_ajax_list'));
66+
$this->assertEquals('/index.php/apps/files/', $router->generate('files.view.index'));
67+
}
68+
}

tests/lib/UrlGeneratorTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515

1616
/**
1717
* Class UrlGeneratorTest
18+
*
19+
* @package Test
1820
*/
1921
class UrlGeneratorTest extends \Test\TestCase {
2022

@@ -90,8 +92,8 @@ public function testLinkToRouteAbsolute($route, $expected) {
9092

9193
public function provideRoutes() {
9294
return [
93-
['files_ajax_list', 'http://localhost/nextcloud/index.php/apps/files/ajax/list.php'],
9495
['core.Preview.getPreview', 'http://localhost/nextcloud/index.php/core/preview.png'],
96+
['cloud_federation_api.requesthandlercontroller.addShare', 'http://localhost/nextcloud/index.php/ocm/shares'],
9597
];
9698
}
9799

0 commit comments

Comments
 (0)