Merge pull request #14635 from Art4/remove-dice-from-router

Remove DICE from router
This commit is contained in:
Hypolite Petovan 2024-12-28 20:07:45 -05:00 committed by GitHub
commit f484dc8bd8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 31 additions and 32 deletions

View file

@ -15,6 +15,7 @@ use Friendica\App\Page;
use Friendica\App\Request; use Friendica\App\Request;
use Friendica\App\Router; use Friendica\App\Router;
use Friendica\Capabilities\ICanCreateResponses; use Friendica\Capabilities\ICanCreateResponses;
use Friendica\Capabilities\ICanHandleRequests;
use Friendica\Content\Nav; use Friendica\Content\Nav;
use Friendica\Core\Config\Factory\Config; use Friendica\Core\Config\Factory\Config;
use Friendica\Core\KeyValueStorage\Capability\IManageKeyValuePairs; use Friendica\Core\KeyValueStorage\Capability\IManageKeyValuePairs;
@ -170,7 +171,6 @@ class App
$this->mode->setExecutor(Mode::INDEX); $this->mode->setExecutor(Mode::INDEX);
$this->runFrontend( $this->runFrontend(
$this->container->create(Router::class),
$this->container->create(IManagePersonalConfigValues::class), $this->container->create(IManagePersonalConfigValues::class),
$this->container->create(Page::class), $this->container->create(Page::class),
$this->container->create(Nav::class), $this->container->create(Nav::class),
@ -529,7 +529,6 @@ class App
* *
* This probably should change to limit the size of this monster method. * This probably should change to limit the size of this monster method.
* *
* @param Router $router
* @param IManagePersonalConfigValues $pconfig * @param IManagePersonalConfigValues $pconfig
* @param Page $page The Friendica page printing container * @param Page $page The Friendica page printing container
* @param ModuleHTTPException $httpException The possible HTTP Exception container * @param ModuleHTTPException $httpException The possible HTTP Exception container
@ -539,7 +538,6 @@ class App
* @throws \ImagickException * @throws \ImagickException
*/ */
private function runFrontend( private function runFrontend(
Router $router,
IManagePersonalConfigValues $pconfig, IManagePersonalConfigValues $pconfig,
Page $page, Page $page,
Nav $nav, Nav $nav,
@ -677,11 +675,11 @@ class App
// The "view" module is required to show the theme CSS // The "view" module is required to show the theme CSS
if (!$this->mode->isInstall() && !$this->mode->has(Mode::MAINTENANCEDISABLED) && $moduleName !== 'view') { if (!$this->mode->isInstall() && !$this->mode->has(Mode::MAINTENANCEDISABLED) && $moduleName !== 'view') {
$module = $router->getModule(Maintenance::class); $module = $this->createModuleInstance(Maintenance::class);
} else { } else {
// determine the module class and save it to the module instance // determine the module class and save it to the module instance
// @todo there's an implicit dependency due SESSION::start(), so it has to be called here (yet) // @todo there's an implicit dependency due SESSION::start(), so it has to be called here (yet)
$module = $router->getModule(); $module = $this->createModuleInstance(null);
} }
// Display can change depending on the requested language, so it shouldn't be cached whole // Display can change depending on the requested language, so it shouldn't be cached whole
@ -712,6 +710,31 @@ class App
$page->logRuntime($this->config, 'runFrontend'); $page->logRuntime($this->config, 'runFrontend');
} }
private function createModuleInstance(?string $moduleClass = null): ICanHandleRequests
{
/** @var Router $router */
$router = $this->container->create(Router::class);
$moduleClass = $moduleClass ?? $router->getModuleClass();
$parameters = $router->getParameters();
$dice_profiler_threshold = $this->config->get('system', 'dice_profiler_threshold', 0);
$stamp = microtime(true);
/** @var ICanHandleRequests $module */
$module = $this->container->create($moduleClass, $parameters);
if ($dice_profiler_threshold > 0) {
$dur = floatval(microtime(true) - $stamp);
if ($dur >= $dice_profiler_threshold) {
$this->logger->notice('Dice module creation lasts too long.', ['duration' => round($dur, 3), 'module' => $moduleClass, 'parameters' => $parameters]);
}
}
return $module;
}
/** /**
* Log slow page executions * Log slow page executions
* *

View file

@ -89,12 +89,6 @@ class Router
/** @var bool */ /** @var bool */
private $isLocalUser; private $isLocalUser;
/** @var float */
private $dice_profiler_threshold;
/** @var Dice */
private $dice;
/** @var string */ /** @var string */
private $baseRoutesFilepath; private $baseRoutesFilepath;
@ -113,11 +107,10 @@ class Router
* @param IManageConfigValues $config * @param IManageConfigValues $config
* @param Arguments $args * @param Arguments $args
* @param LoggerInterface $logger * @param LoggerInterface $logger
* @param Dice $dice
* @param IHandleUserSessions $userSession * @param IHandleUserSessions $userSession
* @param RouteCollector|null $routeCollector * @param RouteCollector|null $routeCollector
*/ */
public function __construct(array $server, string $baseRoutesFilepath, L10n $l10n, ICanCache $cache, ICanLock $lock, IManageConfigValues $config, Arguments $args, LoggerInterface $logger, Dice $dice, IHandleUserSessions $userSession, RouteCollector $routeCollector = null) public function __construct(array $server, string $baseRoutesFilepath, L10n $l10n, ICanCache $cache, ICanLock $lock, IManageConfigValues $config, Arguments $args, LoggerInterface $logger, IHandleUserSessions $userSession, RouteCollector $routeCollector = null)
{ {
$this->baseRoutesFilepath = $baseRoutesFilepath; $this->baseRoutesFilepath = $baseRoutesFilepath;
$this->l10n = $l10n; $this->l10n = $l10n;
@ -125,11 +118,9 @@ class Router
$this->lock = $lock; $this->lock = $lock;
$this->args = $args; $this->args = $args;
$this->config = $config; $this->config = $config;
$this->dice = $dice;
$this->server = $server; $this->server = $server;
$this->logger = $logger; $this->logger = $logger;
$this->isLocalUser = !empty($userSession->getLocalUserId()); $this->isLocalUser = !empty($userSession->getLocalUserId());
$this->dice_profiler_threshold = $config->get('system', 'dice_profiler_threshold', 0);
$this->routeCollector = $routeCollector ?? new RouteCollector(new Std(), new GroupCountBased()); $this->routeCollector = $routeCollector ?? new RouteCollector(new Std(), new GroupCountBased());
@ -328,23 +319,9 @@ class Router
} }
} }
public function getModule(?string $module_class = null): ICanHandleRequests public function getParameters(): array
{ {
$moduleClass = $module_class ?? $this->getModuleClass(); return $this->parameters;
$stamp = microtime(true);
/** @var ICanHandleRequests $module */
$module = $this->dice->create($moduleClass, $this->parameters);
if ($this->dice_profiler_threshold > 0) {
$dur = floatval(microtime(true) - $stamp);
if ($dur >= $this->dice_profiler_threshold) {
$this->logger->notice('Dice module creation lasts too long.', ['duration' => round($dur, 3), 'module' => $moduleClass, 'parameters' => $this->parameters]);
}
}
return $module;
} }
/** /**

View file

@ -219,7 +219,6 @@ return (function(string $basepath, array $getVars, array $serverVars, array $coo
'constructParams' => [ 'constructParams' => [
$serverVars, $serverVars,
__DIR__ . '/routes.config.php', __DIR__ . '/routes.config.php',
[Dice::INSTANCE => Dice::SELF],
null null
], ],
], ],