From 7cb962de82ae7e7a89fc47d0fac637ae3c350c7c Mon Sep 17 00:00:00 2001 From: Art4 Date: Sat, 28 Dec 2024 12:58:09 +0000 Subject: [PATCH 1/5] move Router creation into App::runFrontend() --- src/App.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/App.php b/src/App.php index 0345eb3adf..f673c2eb39 100644 --- a/src/App.php +++ b/src/App.php @@ -170,7 +170,6 @@ class App $this->mode->setExecutor(Mode::INDEX); $this->runFrontend( - $this->container->create(Router::class), $this->container->create(IManagePersonalConfigValues::class), $this->container->create(Page::class), $this->container->create(Nav::class), @@ -529,7 +528,6 @@ class App * * This probably should change to limit the size of this monster method. * - * @param Router $router * @param IManagePersonalConfigValues $pconfig * @param Page $page The Friendica page printing container * @param ModuleHTTPException $httpException The possible HTTP Exception container @@ -539,7 +537,6 @@ class App * @throws \ImagickException */ private function runFrontend( - Router $router, IManagePersonalConfigValues $pconfig, Page $page, Nav $nav, @@ -675,6 +672,9 @@ class App // Initialize module that can set the current theme in the init() method, either directly or via App->setProfileOwner $page['page_title'] = $moduleName; + /** @var Router $router */ + $router = $this->container->create(Router::class); + // The "view" module is required to show the theme CSS if (!$this->mode->isInstall() && !$this->mode->has(Mode::MAINTENANCEDISABLED) && $moduleName !== 'view') { $module = $router->getModule(Maintenance::class); From 182732b1128f2e05890d6a3b70d9f8d531f0e848 Mon Sep 17 00:00:00 2001 From: Art4 Date: Sat, 28 Dec 2024 13:07:07 +0000 Subject: [PATCH 2/5] Extract create module into own method --- src/App.php | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/App.php b/src/App.php index f673c2eb39..a0dd06521c 100644 --- a/src/App.php +++ b/src/App.php @@ -15,6 +15,7 @@ use Friendica\App\Page; use Friendica\App\Request; use Friendica\App\Router; use Friendica\Capabilities\ICanCreateResponses; +use Friendica\Capabilities\ICanHandleRequests; use Friendica\Content\Nav; use Friendica\Core\Config\Factory\Config; use Friendica\Core\KeyValueStorage\Capability\IManageKeyValuePairs; @@ -672,16 +673,13 @@ class App // Initialize module that can set the current theme in the init() method, either directly or via App->setProfileOwner $page['page_title'] = $moduleName; - /** @var Router $router */ - $router = $this->container->create(Router::class); - // The "view" module is required to show the theme CSS if (!$this->mode->isInstall() && !$this->mode->has(Mode::MAINTENANCEDISABLED) && $moduleName !== 'view') { - $module = $router->getModule(Maintenance::class); + $module = $this->createModuleInstance(Maintenance::class); } else { // 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) - $module = $router->getModule(); + $module = $this->createModuleInstance(null); } // Display can change depending on the requested language, so it shouldn't be cached whole @@ -712,6 +710,14 @@ class App $page->logRuntime($this->config, 'runFrontend'); } + private function createModuleInstance(?string $moduleClass = null): ICanHandleRequests + { + /** @var Router $router */ + $router = $this->container->create(Router::class); + + return $router->getModule($moduleClass); + } + /** * Log slow page executions * From fa315cf44af6ce4fb095ab1b61026397ce9a6079 Mon Sep 17 00:00:00 2001 From: Art4 Date: Sat, 28 Dec 2024 13:10:28 +0000 Subject: [PATCH 3/5] remove constructor injection of Dice in Router --- src/App.php | 2 +- src/App/Router.php | 11 +++-------- static/dependencies.config.php | 1 - 3 files changed, 4 insertions(+), 10 deletions(-) diff --git a/src/App.php b/src/App.php index a0dd06521c..68cae80830 100644 --- a/src/App.php +++ b/src/App.php @@ -715,7 +715,7 @@ class App /** @var Router $router */ $router = $this->container->create(Router::class); - return $router->getModule($moduleClass); + return $router->getModule($this->container, $moduleClass); } /** diff --git a/src/App/Router.php b/src/App/Router.php index 31162ec1b6..c0ec7b1e1e 100644 --- a/src/App/Router.php +++ b/src/App/Router.php @@ -92,9 +92,6 @@ class Router /** @var float */ private $dice_profiler_threshold; - /** @var Dice */ - private $dice; - /** @var string */ private $baseRoutesFilepath; @@ -113,11 +110,10 @@ class Router * @param IManageConfigValues $config * @param Arguments $args * @param LoggerInterface $logger - * @param Dice $dice * @param IHandleUserSessions $userSession * @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->l10n = $l10n; @@ -125,7 +121,6 @@ class Router $this->lock = $lock; $this->args = $args; $this->config = $config; - $this->dice = $dice; $this->server = $server; $this->logger = $logger; $this->isLocalUser = !empty($userSession->getLocalUserId()); @@ -328,14 +323,14 @@ class Router } } - public function getModule(?string $module_class = null): ICanHandleRequests + public function getModule(Dice $dice, ?string $module_class = null): ICanHandleRequests { $moduleClass = $module_class ?? $this->getModuleClass(); $stamp = microtime(true); /** @var ICanHandleRequests $module */ - $module = $this->dice->create($moduleClass, $this->parameters); + $module = $dice->create($moduleClass, $this->parameters); if ($this->dice_profiler_threshold > 0) { $dur = floatval(microtime(true) - $stamp); diff --git a/static/dependencies.config.php b/static/dependencies.config.php index 93d9de81ac..28ae277c21 100644 --- a/static/dependencies.config.php +++ b/static/dependencies.config.php @@ -219,7 +219,6 @@ return (function(string $basepath, array $getVars, array $serverVars, array $coo 'constructParams' => [ $serverVars, __DIR__ . '/routes.config.php', - [Dice::INSTANCE => Dice::SELF], null ], ], From 4d0a695588f0d68e1d838d69855a50b983b53f90 Mon Sep 17 00:00:00 2001 From: Art4 Date: Sat, 28 Dec 2024 16:30:33 +0000 Subject: [PATCH 4/5] use $dice_profiler_threshold in Router not as property --- src/App/Router.php | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/App/Router.php b/src/App/Router.php index c0ec7b1e1e..f1f0510867 100644 --- a/src/App/Router.php +++ b/src/App/Router.php @@ -89,9 +89,6 @@ class Router /** @var bool */ private $isLocalUser; - /** @var float */ - private $dice_profiler_threshold; - /** @var string */ private $baseRoutesFilepath; @@ -124,7 +121,6 @@ class Router $this->server = $server; $this->logger = $logger; $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()); @@ -327,14 +323,16 @@ class Router { $moduleClass = $module_class ?? $this->getModuleClass(); + $dice_profiler_threshold = $this->config->get('system', 'dice_profiler_threshold', 0); + $stamp = microtime(true); /** @var ICanHandleRequests $module */ $module = $dice->create($moduleClass, $this->parameters); - if ($this->dice_profiler_threshold > 0) { + if ($dice_profiler_threshold > 0) { $dur = floatval(microtime(true) - $stamp); - if ($dur >= $this->dice_profiler_threshold) { + if ($dur >= $dice_profiler_threshold) { $this->logger->notice('Dice module creation lasts too long.', ['duration' => round($dur, 3), 'module' => $moduleClass, 'parameters' => $this->parameters]); } } From dde8291597cac645494f267b099585c99c7734e9 Mon Sep 17 00:00:00 2001 From: Art4 Date: Sat, 28 Dec 2024 16:36:07 +0000 Subject: [PATCH 5/5] Move getModule() code into App class --- src/App.php | 19 ++++++++++++++++++- src/App/Router.php | 20 ++------------------ 2 files changed, 20 insertions(+), 19 deletions(-) diff --git a/src/App.php b/src/App.php index 68cae80830..e0f1a0e2da 100644 --- a/src/App.php +++ b/src/App.php @@ -715,7 +715,24 @@ class App /** @var Router $router */ $router = $this->container->create(Router::class); - return $router->getModule($this->container, $moduleClass); + $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; } /** diff --git a/src/App/Router.php b/src/App/Router.php index f1f0510867..deb0155cf0 100644 --- a/src/App/Router.php +++ b/src/App/Router.php @@ -319,25 +319,9 @@ class Router } } - public function getModule(Dice $dice, ?string $module_class = null): ICanHandleRequests + public function getParameters(): array { - $moduleClass = $module_class ?? $this->getModuleClass(); - - $dice_profiler_threshold = $this->config->get('system', 'dice_profiler_threshold', 0); - - $stamp = microtime(true); - - /** @var ICanHandleRequests $module */ - $module = $dice->create($moduleClass, $this->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' => $this->parameters]); - } - } - - return $module; + return $this->parameters; } /**