Merge pull request #9425 from MrPetovan/task/router-recompute-filemtime

Add routes file recompute on last modification time change
This commit is contained in:
Michael Vogel 2020-10-16 06:13:38 +02:00 committed by GitHub
commit fb3dcb41f5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 5 deletions

View file

@ -93,6 +93,10 @@ class Router
$this->routeCollector = isset($routeCollector) ?
$routeCollector :
new RouteCollector(new Std(), new GroupCountBased());
if ($this->baseRoutesFilepath && !file_exists($this->baseRoutesFilepath)) {
throw new HTTPException\InternalServerErrorException('Routes file path does\'n exist.');
}
}
/**
@ -249,7 +253,7 @@ class Router
{
$dispatchData = [];
if ($this->baseRoutesFilepath && file_exists($this->baseRoutesFilepath)) {
if ($this->baseRoutesFilepath) {
$dispatchData = require $this->baseRoutesFilepath;
if (!is_array($dispatchData)) {
throw new HTTPException\InternalServerErrorException('Invalid base routes file');
@ -268,20 +272,33 @@ class Router
* The cached "routerDispatchData" lasts for a day, and must be cleared manually when there
* is any changes in the enabled addons list.
*
* Additionally, we check for the base routes file last modification time to automatically
* trigger re-computing the dispatch data.
*
* @return array|mixed
* @throws HTTPException\InternalServerErrorException
*/
private function getCachedDispatchData()
{
$routerDispatchData = $this->cache->get('routerDispatchData');
$lastRoutesFileModifiedTime = $this->cache->get('lastRoutesFileModifiedTime');
$forceRecompute = false;
if ($routerDispatchData) {
if ($this->baseRoutesFilepath) {
$routesFileModifiedTime = filemtime($this->baseRoutesFilepath);
$forceRecompute = $lastRoutesFileModifiedTime != $routesFileModifiedTime;
}
if (!$forceRecompute && $routerDispatchData) {
return $routerDispatchData;
}
$routerDispatchData = $this->getDispatchData();
$this->cache->set('routerDispatchData', $routerDispatchData, Duration::DAY);
if (!empty($routesFileModifiedTime)) {
$this->cache->set('lastRoutesFileMtime', $routesFileModifiedTime, Duration::MONTH);
}
return $routerDispatchData;
}

View file

@ -178,7 +178,8 @@ class ModuleTest extends DatabaseTest
$cache = \Mockery::mock(ICache::class);
$cache->shouldReceive('get')->with('routerDispatchData')->andReturn('')->atMost()->once();
$cache->shouldReceive('set')->withAnyArgs()->andReturn(false)->atMost()->once();
$cache->shouldReceive('get')->with('lastRoutesFileModifiedTime')->andReturn('')->atMost()->once();
$cache->shouldReceive('set')->withAnyArgs()->andReturn(false)->atMost()->twice();
$router = (new App\Router([], __DIR__ . '/../../../static/routes.config.php', $l10n, $cache));

View file

@ -185,7 +185,7 @@ class RouterTest extends TestCase
],
],
'/post' => [
'/it' => [Module\NodeInfo::class, [Router::POST]],
'/it' => [Module\WellKnown\NodeInfo::class, [Router::POST]],
],
'/double' => [Module\Profile\Index::class, [Router::GET, Router::POST]]
],
@ -221,7 +221,7 @@ class RouterTest extends TestCase
], '', $this->l10n, $this->cache))->loadRoutes($routes);
// Don't find GET
$this->assertEquals(Module\NodeInfo::class, $router->getModuleClass('/post/it'));
$this->assertEquals(Module\WellKnown\NodeInfo::class, $router->getModuleClass('/post/it'));
$this->assertEquals(Module\Profile\Index::class, $router->getModuleClass('/double'));
}
}