mirror of
https://github.com/friendica/friendica
synced 2024-12-23 12:00:16 +00:00
Merge pull request #9425 from MrPetovan/task/router-recompute-filemtime
Add routes file recompute on last modification time change
This commit is contained in:
commit
fb3dcb41f5
3 changed files with 23 additions and 5 deletions
|
@ -93,6 +93,10 @@ class Router
|
||||||
$this->routeCollector = isset($routeCollector) ?
|
$this->routeCollector = isset($routeCollector) ?
|
||||||
$routeCollector :
|
$routeCollector :
|
||||||
new RouteCollector(new Std(), new GroupCountBased());
|
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 = [];
|
$dispatchData = [];
|
||||||
|
|
||||||
if ($this->baseRoutesFilepath && file_exists($this->baseRoutesFilepath)) {
|
if ($this->baseRoutesFilepath) {
|
||||||
$dispatchData = require $this->baseRoutesFilepath;
|
$dispatchData = require $this->baseRoutesFilepath;
|
||||||
if (!is_array($dispatchData)) {
|
if (!is_array($dispatchData)) {
|
||||||
throw new HTTPException\InternalServerErrorException('Invalid base routes file');
|
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
|
* The cached "routerDispatchData" lasts for a day, and must be cleared manually when there
|
||||||
* is any changes in the enabled addons list.
|
* 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
|
* @return array|mixed
|
||||||
* @throws HTTPException\InternalServerErrorException
|
* @throws HTTPException\InternalServerErrorException
|
||||||
*/
|
*/
|
||||||
private function getCachedDispatchData()
|
private function getCachedDispatchData()
|
||||||
{
|
{
|
||||||
$routerDispatchData = $this->cache->get('routerDispatchData');
|
$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;
|
return $routerDispatchData;
|
||||||
}
|
}
|
||||||
|
|
||||||
$routerDispatchData = $this->getDispatchData();
|
$routerDispatchData = $this->getDispatchData();
|
||||||
|
|
||||||
$this->cache->set('routerDispatchData', $routerDispatchData, Duration::DAY);
|
$this->cache->set('routerDispatchData', $routerDispatchData, Duration::DAY);
|
||||||
|
if (!empty($routesFileModifiedTime)) {
|
||||||
|
$this->cache->set('lastRoutesFileMtime', $routesFileModifiedTime, Duration::MONTH);
|
||||||
|
}
|
||||||
|
|
||||||
return $routerDispatchData;
|
return $routerDispatchData;
|
||||||
}
|
}
|
||||||
|
|
|
@ -178,7 +178,8 @@ class ModuleTest extends DatabaseTest
|
||||||
|
|
||||||
$cache = \Mockery::mock(ICache::class);
|
$cache = \Mockery::mock(ICache::class);
|
||||||
$cache->shouldReceive('get')->with('routerDispatchData')->andReturn('')->atMost()->once();
|
$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));
|
$router = (new App\Router([], __DIR__ . '/../../../static/routes.config.php', $l10n, $cache));
|
||||||
|
|
||||||
|
|
|
@ -185,7 +185,7 @@ class RouterTest extends TestCase
|
||||||
],
|
],
|
||||||
],
|
],
|
||||||
'/post' => [
|
'/post' => [
|
||||||
'/it' => [Module\NodeInfo::class, [Router::POST]],
|
'/it' => [Module\WellKnown\NodeInfo::class, [Router::POST]],
|
||||||
],
|
],
|
||||||
'/double' => [Module\Profile\Index::class, [Router::GET, 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);
|
], '', $this->l10n, $this->cache))->loadRoutes($routes);
|
||||||
|
|
||||||
// Don't find GET
|
// 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'));
|
$this->assertEquals(Module\Profile\Index::class, $router->getModuleClass('/double'));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue