Skip to content

Commit

Permalink
DI for Router
Browse files Browse the repository at this point in the history
Signed-off-by: Robin Appelman <robin@icewind.nl>
  • Loading branch information
icewind1991 committed Feb 13, 2023
1 parent b68be79 commit b911da3
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 27 deletions.
27 changes: 17 additions & 10 deletions lib/private/Route/CachingRouter.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,27 @@
*/
namespace OC\Route;

use OCP\Diagnostics\IEventLogger;
use OCP\ICache;
use OCP\ICacheFactory;
use OCP\IConfig;
use OCP\IRequest;
use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;

class CachingRouter extends Router {
/**
* @var \OCP\ICache
*/
protected $cache;
protected ICache $cache;

/**
* @param \OCP\ICache $cache
*/
public function __construct($cache, LoggerInterface $logger) {
$this->cache = $cache;
parent::__construct($logger);
public function __construct(
ICacheFactory $cacheFactory,
LoggerInterface $logger,
IRequest $request,
IConfig $config,
IEventLogger $eventLogger,
ContainerInterface $container
) {
$this->cache = $cacheFactory->createLocal('route');
parent::__construct($logger, $request, $config, $eventLogger, $container);
}

/**
Expand Down
26 changes: 19 additions & 7 deletions lib/private/Route/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,11 @@
use OC\AppFramework\Routing\RouteParser;
use OCP\AppFramework\App;
use OCP\Diagnostics\IEventLogger;
use OCP\IConfig;
use OCP\IRequest;
use OCP\Route\IRouter;
use OCP\Util;
use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
use Symfony\Component\Routing\Exception\RouteNotFoundException;
Expand Down Expand Up @@ -66,25 +69,34 @@ class Router implements IRouter {
/** @var RequestContext */
protected $context;
private IEventLogger $eventLogger;

public function __construct(LoggerInterface $logger) {
private IConfig $config;
private ContainerInterface $container;

public function __construct(
LoggerInterface $logger,
IRequest $request,
IConfig $config,
IEventLogger $eventLogger,
ContainerInterface $container
) {
$this->logger = $logger;
$this->config = $config;
$baseUrl = \OC::$WEBROOT;
if (!(\OC::$server->getConfig()->getSystemValue('htaccess.IgnoreFrontController', false) === true || getenv('front_controller_active') === 'true')) {
if (!($config->getSystemValue('htaccess.IgnoreFrontController', false) === true || getenv('front_controller_active') === 'true')) {
$baseUrl .= '/index.php';
}
if (!\OC::$CLI && isset($_SERVER['REQUEST_METHOD'])) {
$method = $_SERVER['REQUEST_METHOD'];
} else {
$method = 'GET';
}
$request = \OC::$server->getRequest();
$host = $request->getServerHost();
$schema = $request->getServerProtocol();
$this->context = new RequestContext($baseUrl, $method, $host, $schema);
// TODO cache
$this->root = $this->getCollection('root');
$this->eventLogger = \OC::$server->get(IEventLogger::class);
$this->eventLogger = $eventLogger;
$this->container = $container;
}

/**
Expand Down Expand Up @@ -253,7 +265,7 @@ public function findMatchingRoute(string $url): array {
$this->loadRoutes('settings');
} elseif (substr($url, 0, 6) === '/core/') {
\OC::$REQUESTEDAPP = $url;
if (!\OC::$server->getConfig()->getSystemValueBool('maintenance') && !Util::needUpgrade()) {
if (!$this->config->getSystemValueBool('maintenance') && !Util::needUpgrade()) {
\OC_App::loadApps();
}
$this->loadRoutes('core');
Expand Down Expand Up @@ -441,7 +453,7 @@ private function getApplicationClass(string $appName) {
$applicationClassName = $appNameSpace . '\\AppInfo\\Application';

if (class_exists($applicationClassName)) {
$application = \OC::$server->query($applicationClassName);
$application = $this->container->get($applicationClassName);
} else {
$application = new App($appName);
}
Expand Down
6 changes: 3 additions & 3 deletions lib/private/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@
use OC\Remote\Api\ApiFactory;
use OC\Remote\InstanceFactory;
use OC\RichObjectStrings\Validator;
use OC\Route\CachingRouter;
use OC\Route\Router;
use OC\Security\Bruteforce\Throttler;
use OC\Security\CertificateManager;
Expand Down Expand Up @@ -819,11 +820,10 @@ public function __construct($webRoot, \OC\Config $config) {

$this->registerService(Router::class, function (Server $c) {
$cacheFactory = $c->get(ICacheFactory::class);
$logger = $c->get(LoggerInterface::class);
if ($cacheFactory->isLocalCacheAvailable()) {
$router = new \OC\Route\CachingRouter($cacheFactory->createLocal('route'), $logger);
$router = $c->resolve(CachingRouter::class);
} else {
$router = new \OC\Route\Router($logger);
$router = $c->resolve(Router::class);
}
return $router;
});
Expand Down
52 changes: 46 additions & 6 deletions tests/lib/AppFramework/Routing/RoutingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@
use OC\AppFramework\Routing\RouteConfig;
use OC\Route\Route;
use OC\Route\Router;
use OCP\Diagnostics\IEventLogger;
use OCP\IConfig;
use OCP\IRequest;
use OCP\Route\IRouter;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;

class RoutingTest extends \Test\TestCase {
Expand Down Expand Up @@ -133,7 +137,13 @@ public function testSimpleRouteWithBrokenName() {
/** @var IRouter|MockObject $router */
$router = $this->getMockBuilder(Router::class)
->onlyMethods(['create'])
->setConstructorArgs([$this->createMock(LoggerInterface::class)])
->setConstructorArgs([
$this->createMock(LoggerInterface::class),
$this->createMock(IRequest::class),
$this->createMock(IConfig::class),
$this->createMock(IEventLogger::class),
$this->createMock(ContainerInterface::class)
])
->getMock();

// load route configuration
Expand All @@ -154,7 +164,13 @@ public function testSimpleOCSRouteWithBrokenName() {
/** @var IRouter|MockObject $router */
$router = $this->getMockBuilder(Router::class)
->onlyMethods(['create'])
->setConstructorArgs([$this->createMock(LoggerInterface::class)])
->setConstructorArgs([
$this->createMock(LoggerInterface::class),
$this->createMock(IRequest::class),
$this->createMock(IConfig::class),
$this->createMock(IEventLogger::class),
$this->createMock(ContainerInterface::class)
])
->getMock();

// load route configuration
Expand Down Expand Up @@ -214,7 +230,13 @@ private function assertSimpleRoute($routes, $name, $verb, $url, $controllerName,
/** @var IRouter|MockObject $router */
$router = $this->getMockBuilder(Router::class)
->onlyMethods(['create'])
->setConstructorArgs([$this->createMock(LoggerInterface::class)])
->setConstructorArgs([
$this->createMock(LoggerInterface::class),
$this->createMock(IRequest::class),
$this->createMock(IConfig::class),
$this->createMock(IEventLogger::class),
$this->createMock(ContainerInterface::class)
])
->getMock();

// we expect create to be called once:
Expand Down Expand Up @@ -264,7 +286,13 @@ private function assertSimpleOCSRoute($routes,
/** @var IRouter|MockObject $router */
$router = $this->getMockBuilder(Router::class)
->onlyMethods(['create'])
->setConstructorArgs([$this->createMock(LoggerInterface::class)])
->setConstructorArgs([
$this->createMock(LoggerInterface::class),
$this->createMock(IRequest::class),
$this->createMock(IConfig::class),
$this->createMock(IEventLogger::class),
$this->createMock(ContainerInterface::class)
])
->getMock();

// we expect create to be called once:
Expand All @@ -291,7 +319,13 @@ private function assertOCSResource($yaml, $resourceName, $url, $controllerName,
/** @var IRouter|MockObject $router */
$router = $this->getMockBuilder(Router::class)
->onlyMethods(['create'])
->setConstructorArgs([$this->createMock(LoggerInterface::class)])
->setConstructorArgs([
$this->createMock(LoggerInterface::class),
$this->createMock(IRequest::class),
$this->createMock(IConfig::class),
$this->createMock(IEventLogger::class),
$this->createMock(ContainerInterface::class)
])
->getMock();

// route mocks
Expand Down Expand Up @@ -338,7 +372,13 @@ private function assertResource($yaml, $resourceName, $url, $controllerName, $pa
/** @var IRouter|MockObject $router */
$router = $this->getMockBuilder(Router::class)
->onlyMethods(['create'])
->setConstructorArgs([$this->createMock(LoggerInterface::class)])
->setConstructorArgs([
$this->createMock(LoggerInterface::class),
$this->createMock(IRequest::class),
$this->createMock(IConfig::class),
$this->createMock(IEventLogger::class),
$this->createMock(ContainerInterface::class)
])
->getMock();

// route mocks
Expand Down
12 changes: 11 additions & 1 deletion tests/lib/Route/RouterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
namespace Test\Route;

use OC\Route\Router;
use OCP\Diagnostics\IEventLogger;
use OCP\IConfig;
use OCP\IRequest;
use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;
use Test\TestCase;

Expand All @@ -44,7 +48,13 @@ function (string $message, array $data) {
$this->fail('Unexpected info log: '.(string)($data['exception'] ?? $message));
}
);
$router = new Router($logger);
$router = new Router(
$logger,
$this->createMock(IRequest::class),
$this->createMock(IConfig::class),
$this->createMock(IEventLogger::class),
$this->createMock(ContainerInterface::class),
);

$this->assertEquals('/index.php/apps/files/', $router->generate('files.view.index'));

Expand Down

0 comments on commit b911da3

Please sign in to comment.