From f5abb2ca8b9d20b4c1684a0ac878b37d0bdcf502 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20=C5=A0pa=C4=8Dek?= Date: Sun, 12 Jan 2025 21:46:37 +0100 Subject: [PATCH 1/2] Move RouterFactory::getLocaleRouters() to a separate service Because that only worked when createRoute() was called which may not always be the case (it isn't with the lazy mode enabled for tests as well), so now createRouter() returns LocaleRouter and only then you can get the RouteList or call getLocaleRouters(). --- app/config/services.neon | 1 + app/psalm.xml | 1 + .../Locale/LocaleLinkGenerator.php | 6 ++-- app/src/Application/Routing/LocaleRouter.php | 35 +++++++++++++++++++ app/src/Application/Routing/RouterFactory.php | 4 +-- .../Locale/LocaleLinkGeneratorTest.phpt | 6 ++-- 6 files changed, 45 insertions(+), 8 deletions(-) create mode 100644 app/src/Application/Routing/LocaleRouter.php diff --git a/app/config/services.neon b/app/config/services.neon index ec8c2f055..bcec0a8a8 100644 --- a/app/config/services.neon +++ b/app/config/services.neon @@ -8,6 +8,7 @@ services: - MichalSpacekCz\Application\LinkGenerator localeLinkGenerator: MichalSpacekCz\Application\Locale\LocaleLinkGenerator(languages: %locales.languages%) - MichalSpacekCz\Application\Locale\Locales + - @MichalSpacekCz\Application\Routing\LocaleRouter::getRouteList - MichalSpacekCz\Application\Routing\RouterFactory(supportedLocales: %locales.supported%, rootDomainMapping: %locales.rootDomainMapping%, translatedRoutes: %translatedRoutes.presenters%) - @MichalSpacekCz\Application\Routing\RouterFactory::createRouter - MichalSpacekCz\Application\SanitizedPhpInfo diff --git a/app/psalm.xml b/app/psalm.xml index 2f98f0c44..e32af9ebe 100644 --- a/app/psalm.xml +++ b/app/psalm.xml @@ -85,6 +85,7 @@ + diff --git a/app/src/Application/Locale/LocaleLinkGenerator.php b/app/src/Application/Locale/LocaleLinkGenerator.php index 78afbc686..2c78a5b89 100644 --- a/app/src/Application/Locale/LocaleLinkGenerator.php +++ b/app/src/Application/Locale/LocaleLinkGenerator.php @@ -5,7 +5,7 @@ use Contributte\Translation\Translator; use MichalSpacekCz\Application\LinkGenerator; -use MichalSpacekCz\Application\Routing\RouterFactory; +use MichalSpacekCz\Application\Routing\LocaleRouter; use MichalSpacekCz\ShouldNotHappenException; use Nette\Application\IPresenterFactory; use Nette\Application\LinkGenerator as NetteLinkGenerator; @@ -26,7 +26,7 @@ class LocaleLinkGenerator * @param array $languages */ public function __construct( - private readonly RouterFactory $routerFactory, + private readonly LocaleRouter $localeRouter, private readonly IRequest $httpRequest, private readonly IPresenterFactory $presenterFactory, private readonly LinkGenerator $linkGenerator, @@ -47,7 +47,7 @@ public function __construct( public function links(string $destination, array $params = []): array { $links = []; - foreach ($this->routerFactory->getLocaleRouters() as $locale => $routers) { + foreach ($this->localeRouter->getLocaleRouters() as $locale => $routers) { foreach ($routers->getRouters() as $router) { if (!$router instanceof RouteList) { throw new ShouldNotHappenException(sprintf("The presenter should be a '%s' but it's a %s", RouteList::class, get_debug_type($router))); diff --git a/app/src/Application/Routing/LocaleRouter.php b/app/src/Application/Routing/LocaleRouter.php new file mode 100644 index 000000000..89b24145e --- /dev/null +++ b/app/src/Application/Routing/LocaleRouter.php @@ -0,0 +1,35 @@ + $localeRouters + */ + public function __construct( + private array $localeRouters, + private RouteList $routeList, + ) { + } + + + /** + * @return array + */ + public function getLocaleRouters(): array + { + return $this->localeRouters; + } + + + public function getRouteList(): RouteList + { + return $this->routeList; + } + +} diff --git a/app/src/Application/Routing/RouterFactory.php b/app/src/Application/Routing/RouterFactory.php index 78bfbea92..cd51bd73a 100644 --- a/app/src/Application/Routing/RouterFactory.php +++ b/app/src/Application/Routing/RouterFactory.php @@ -102,7 +102,7 @@ public function getLocaleRouters(): array /** * @noinspection RequiredAttributes Because is not an HTML tag here */ - public function createRouter(): RouteList + public function createRouter(): LocaleRouter { $router = new RouteList(); foreach ($this->availableLocales as $locale) { @@ -147,7 +147,7 @@ public function createRouter(): RouteList new RouterFactoryRoute('', 'Homepage', 'default'), // Intentionally no action, use presenter-specific route if you need actions ]); - return $router; + return new LocaleRouter($this->localeRouters, $router); } diff --git a/app/tests/Application/Locale/LocaleLinkGeneratorTest.phpt b/app/tests/Application/Locale/LocaleLinkGeneratorTest.phpt index b907a4eb8..4e6f27db2 100644 --- a/app/tests/Application/Locale/LocaleLinkGeneratorTest.phpt +++ b/app/tests/Application/Locale/LocaleLinkGeneratorTest.phpt @@ -5,7 +5,7 @@ declare(strict_types = 1); namespace MichalSpacekCz\Application\Locale; use MichalSpacekCz\Application\LinkGenerator; -use MichalSpacekCz\Application\Routing\RouterFactory; +use MichalSpacekCz\Application\Routing\LocaleRouter; use MichalSpacekCz\Test\NoOpTranslator; use MichalSpacekCz\Test\TestCaseRunner; use Nette\Application\IPresenterFactory; @@ -25,12 +25,12 @@ class LocaleLinkGeneratorTest extends TestCase public function __construct( private readonly NoOpTranslator $translator, - RouterFactory $routerFactory, + LocaleRouter $localeRouter, IRequest $httpRequest, IPresenterFactory $presenterFactory, LinkGenerator $linkGenerator, ) { - $this->localeLinkGenerator = new LocaleLinkGenerator($routerFactory, $httpRequest, $presenterFactory, $linkGenerator, $translator, [ + $this->localeLinkGenerator = new LocaleLinkGenerator($localeRouter, $httpRequest, $presenterFactory, $linkGenerator, $translator, [ 'cs_CZ' => [ 'code' => 'cs', 'name' => 'Česky', From f4a6d14fb9f0af413a3d53fee13aa859246cbae7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20=C5=A0pa=C4=8Dek?= Date: Sun, 12 Jan 2025 21:50:09 +0100 Subject: [PATCH 2/2] Enable nette/di lazy mode https://blog.nette.org/en/one-line-in-configuration-will-speed-up-your-nette-application-how-is-that-possible The speedup is tiny in my case, if any at all, as the database connection is already created on-demand only (since 7766444 #14) but it generally seems like a good idea. --- app/config/common.neon | 1 + 1 file changed, 1 insertion(+) diff --git a/app/config/common.neon b/app/config/common.neon index 0486fc19c..e9b858637 100644 --- a/app/config/common.neon +++ b/app/config/common.neon @@ -32,6 +32,7 @@ di: export: parameters: false tags: false + lazy: true latte: strictParsing: true