Skip to content

Commit

Permalink
Enable nette/di lazy mode (#465)
Browse files Browse the repository at this point in the history
See the [blog post](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.

Needed a bit of a refactoring because `RouterFactory::getLocaleRouters()` returned an empty list in tests when the lazy mode was enabled because `RouterFactory::createRouter()` has not been called yet.
  • Loading branch information
spaze authored Jan 12, 2025
2 parents f7797bd + f4a6d14 commit 36c50f8
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 8 deletions.
1 change: 1 addition & 0 deletions app/config/common.neon
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ di:
export:
parameters: false
tags: false
lazy: true

latte:
strictParsing: true
Expand Down
1 change: 1 addition & 0 deletions app/config/services.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions app/psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
<UnusedClass>
<errorLevel type="suppress">
<referencedClass name="*Presenter" />
<referencedClass name="MichalSpacekCz\Application\Routing\RouterFactory" /> <!-- Used in services.neon -->
<referencedClass name="MichalSpacekCz\Formatter\Placeholders\*" /> <!-- An array of these is passed to MichalSpacekCz\Formatter\TexyFormatter::__construct() by the DIC -->
<referencedClass name="MichalSpacekCz\Test\*\Null*" /> <!-- Used in tests.neon -->
<referencedClass name="MichalSpacekCz\Tls\CertificateMonitor" /> <!-- Used in bin/certmonitor.php but can't analyze bin because https://github.com/vimeo/psalm/issues/10143 -->
Expand Down
6 changes: 3 additions & 3 deletions app/src/Application/Locale/LocaleLinkGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -26,7 +26,7 @@ class LocaleLinkGenerator
* @param array<string, array{code:string, name:string}> $languages
*/
public function __construct(
private readonly RouterFactory $routerFactory,
private readonly LocaleRouter $localeRouter,
private readonly IRequest $httpRequest,
private readonly IPresenterFactory $presenterFactory,
private readonly LinkGenerator $linkGenerator,
Expand All @@ -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)));
Expand Down
35 changes: 35 additions & 0 deletions app/src/Application/Routing/LocaleRouter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
declare(strict_types = 1);

namespace MichalSpacekCz\Application\Routing;

use Nette\Application\Routers\RouteList;

readonly class LocaleRouter
{

/**
* @param array<string, RouteList> $localeRouters
*/
public function __construct(
private array $localeRouters,
private RouteList $routeList,
) {
}


/**
* @return array<string, RouteList>
*/
public function getLocaleRouters(): array
{
return $this->localeRouters;
}


public function getRouteList(): RouteList
{
return $this->routeList;
}

}
4 changes: 2 additions & 2 deletions app/src/Application/Routing/RouterFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public function getLocaleRouters(): array
/**
* @noinspection RequiredAttributes Because <param> is not an HTML tag here
*/
public function createRouter(): RouteList
public function createRouter(): LocaleRouter
{
$router = new RouteList();
foreach ($this->availableLocales as $locale) {
Expand Down Expand Up @@ -147,7 +147,7 @@ public function createRouter(): RouteList
new RouterFactoryRoute('<presenter>', 'Homepage', 'default'), // Intentionally no action, use presenter-specific route if you need actions
]);

return $router;
return new LocaleRouter($this->localeRouters, $router);
}


Expand Down
6 changes: 3 additions & 3 deletions app/tests/Application/Locale/LocaleLinkGeneratorTest.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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',
Expand Down

0 comments on commit 36c50f8

Please sign in to comment.