Skip to content

Commit b2141b7

Browse files
MorrisJobkekesselb
authored andcommitted
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 Signed-off-by: Morris Jobke <[email protected]>
1 parent 8bc4295 commit b2141b7

File tree

3 files changed

+78
-5
lines changed

3 files changed

+78
-5
lines changed

lib/private/Route/Router.php

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -330,12 +330,27 @@ public function getGenerator() {
330330
public function generate($name,
331331
$parameters = [],
332332
$absolute = false) {
333+
334+
$referenceType = UrlGenerator::ABSOLUTE_URL;
335+
if ($absolute === false) {
336+
$referenceType = UrlGenerator::ABSOLUTE_PATH;
337+
}
338+
if (strpos($name, '.') !== false) {
339+
list($appName, $other) = explode('.', $name, 3);
340+
// OCS routes are prefixed with "ocs."
341+
if ($appName === 'ocs') {
342+
$appName = $other;
343+
}
344+
$this->loadRoutes($appName);
345+
try {
346+
return $this->getGenerator()->generate($name, $parameters, $referenceType);
347+
} catch (RouteNotFoundException $e) {
348+
}
349+
}
350+
351+
// Fallback load all routes
333352
$this->loadRoutes();
334353
try {
335-
$referenceType = UrlGenerator::ABSOLUTE_URL;
336-
if ($absolute === false) {
337-
$referenceType = UrlGenerator::ABSOLUTE_PATH;
338-
}
339354
return $this->getGenerator()->generate($name, $parameters, $referenceType);
340355
} catch (RouteNotFoundException $e) {
341356
$this->logger->logException($e);
@@ -350,7 +365,7 @@ public function generate($name,
350365
* @param string $appName
351366
*/
352367
private function requireRouteFile($file, $appName) {
353-
$this->setupRoutes(include_once $file, $appName);
368+
$this->setupRoutes(include $file, $appName);
354369
}
355370

356371

tests/lib/Route/RouterTest.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
declare(strict_types=1);
3+
/**
4+
* @copyright Copyright (c) 2019 Morris Jobke <[email protected]>
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+
36+
public function generateRouteProvider(): array {
37+
return [
38+
['files.view.index', '/index.php/apps/files/'],
39+
// the OCS route is the prefixed one for the AppFramework - see /ocs/v1.php for routing details
40+
['ocs.dav.direct.getUrl', '/index.php/ocsapp/apps/dav/api/v1/direct'],
41+
];
42+
}
43+
44+
/**
45+
* @dataProvider generateRouteProvider
46+
* @param $routeName
47+
* @param $expected
48+
*/
49+
public function testGenerate($routeName, $expected): void {
50+
/** @var ILogger $logger */
51+
$logger = $this->createMock(ILogger::class);
52+
$router = new Router($logger);
53+
54+
$this->assertEquals($expected, $router->generate($routeName));
55+
}
56+
}

tests/lib/UrlGeneratorTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
/**
1616
* Class UrlGeneratorTest
17+
*
18+
* @package Test
1719
*/
1820
class UrlGeneratorTest extends \Test\TestCase {
1921

0 commit comments

Comments
 (0)