Skip to content

Commit 4487a8c

Browse files
committed
Fix circular dependency issue with configs using environment variables
The issue was introduced with nette/di update. The generated code changed in a way that the circular dependency started appearing. The easiest solution was to get rid of "setup" method of EnvironmentConfig service and extract the configs to specific classes. remp/remp#1299
1 parent af6ad25 commit 4487a8c

11 files changed

+123
-70
lines changed

CHANGELOG.md

+7
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p
66

77
## [Unreleased]
88

9+
### [Mailer]
10+
11+
- **BREAKING**: Removed `EnvironmentConfig::setParam()` and `EnvironmentConfig::getParam()` methods. remp/remp#1299
12+
- Use of these could lead to circular dependency issues if values were read by environment config itself.
13+
- We recommend the extraction of these values to their separate config classes.
14+
- Fixed circular dependency issue with configs using environment variables. remp/remp#1299
15+
916
## Archive
1017

1118
- [v3.3](./changelogs/CHANGELOG-v3.3.md)

Mailer/composer.lock

+7-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Remp\MailerModule\Models\Config;
5+
6+
class EditorConfig
7+
{
8+
public const EDITOR_CODEMIRROR = 'codemirror';
9+
public const EDITOR_WYSIWYG = 'wysiwyg';
10+
11+
private string $templateEditor = self::EDITOR_CODEMIRROR;
12+
13+
public function setTemplateEditor(string $editor): void
14+
{
15+
$this->templateEditor = match ($editor) {
16+
self::EDITOR_CODEMIRROR => self::EDITOR_CODEMIRROR,
17+
self::EDITOR_WYSIWYG => self::EDITOR_WYSIWYG,
18+
default => throw new \Exception('Unsupported editor configured: ' . $editor),
19+
};
20+
}
21+
22+
public function getTemplateEditor(): string
23+
{
24+
return $this->templateEditor;
25+
}
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace Remp\MailerModule\Models\Config;
4+
5+
class LinkedServices
6+
{
7+
private array $linkedServices = [];
8+
9+
public function linkService(string $code, ?string $url, ?string $icon): void
10+
{
11+
if (empty($url)) {
12+
return;
13+
}
14+
$this->linkedServices[$code] = [
15+
'url' => $url,
16+
'icon' => $icon,
17+
];
18+
}
19+
20+
public function getServices(): array
21+
{
22+
return $this->linkedServices;
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Remp\MailerModule\Models\Config;
5+
6+
class SearchConfig
7+
{
8+
private int $maxResultCount = 5;
9+
10+
public function setMaxResultCount(int $maxResultCount): void
11+
{
12+
$this->maxResultCount = $maxResultCount;
13+
}
14+
15+
public function getMaxResultCount(): int
16+
{
17+
return $this->maxResultCount;
18+
}
19+
}

Mailer/extensions/mailer-module/src/Models/EnvironmentConfig.php

-30
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,6 @@
55

66
class EnvironmentConfig
77
{
8-
private $linkedServices = [];
9-
10-
private $params = [];
11-
12-
public function linkService(string $code, ?string $url, ?string $icon): void
13-
{
14-
if (empty($url)) {
15-
return;
16-
}
17-
$this->linkedServices[$code] = [
18-
'url' => $url,
19-
'icon' => $icon,
20-
];
21-
}
22-
23-
public function getLinkedServices(): array
24-
{
25-
return $this->linkedServices;
26-
}
27-
288
public function get(string $key): ?string
299
{
3010
if (!isset($_ENV[$key])) {
@@ -67,14 +47,4 @@ public function getBool(string $key): ?bool
6747
}
6848
return filter_var($value, FILTER_VALIDATE_BOOLEAN);
6949
}
70-
71-
public function setParam(string $key, ?string $value): void
72-
{
73-
$this->params[$key] = $value;
74-
}
75-
76-
public function getParam(string $key, $default = null): ?string
77-
{
78-
return $this->params[$key] ?? $default;
79-
}
8050
}

Mailer/extensions/mailer-module/src/Presenters/BasePresenter.php

+6-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Remp\MailerModule\Components\ApplicationStatus\IApplicationStatusFactory;
99
use Remp\MailerModule\Forms\IFormFactory;
1010
use Remp\MailerModule\Models\Auth\PermissionManager;
11+
use Remp\MailerModule\Models\Config\LinkedServices;
1112
use Remp\MailerModule\Models\Config\LocalizationConfig;
1213
use Remp\MailerModule\Models\EnvironmentConfig;
1314

@@ -24,6 +25,9 @@ abstract class BasePresenter extends Presenter
2425

2526
/** @var LocalizationConfig @inject */
2627
public $localizationConfig;
28+
29+
/** @var LinkedServices @inject */
30+
public $linkedServices;
2731

2832
public function startup(): void
2933
{
@@ -34,8 +38,8 @@ public function startup(): void
3438
}
3539

3640
$this->template->currentUser = $this->getUser();
37-
$this->template->linkedServices = $this->environmentConfig->getLinkedServices();
38-
$this->template->locale = $this->environmentConfig->getParam('locale');
41+
$this->template->linkedServices = $this->linkedServices->getServices();
42+
$this->template->locale = $this->localizationConfig->getDefaultLocale();
3943
$this->template->langs = $this->localizationConfig->getSecondaryLocales();
4044
}
4145

Mailer/extensions/mailer-module/src/Presenters/Error4xxPresenter.php

+9-5
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,16 @@
66
use Nette;
77
use Remp\MailerModule\Components\MissingConfiguration\IMissingConfigurationFactory;
88
use Remp\MailerModule\Components\MissingConfiguration\MissingConfiguration;
9-
use Remp\MailerModule\Models\EnvironmentConfig;
9+
use Remp\MailerModule\Models\Config\LinkedServices;
10+
use Remp\MailerModule\Models\Config\LocalizationConfig;
1011

1112
class Error4xxPresenter extends BasePresenter
1213
{
13-
/** @var EnvironmentConfig @inject */
14-
public $environmentConfig;
14+
/** @var LocalizationConfig @inject */
15+
public $localizationConfig;
16+
17+
/** @var LinkedServices @inject */
18+
public $linkedServices;
1519

1620
public function startup(): void
1721
{
@@ -25,8 +29,8 @@ public function startup(): void
2529
public function renderDefault(Nette\Application\BadRequestException $exception): void
2630
{
2731
$this->template->currentUser = $this->getUser();
28-
$this->template->linkedServices = $this->environmentConfig->getLinkedServices();
29-
$this->template->locale = $this->environmentConfig->getParam('locale');
32+
$this->template->linkedServices = $this->linkedServices->getServices();
33+
$this->template->locale = $this->localizationConfig->getDefaultLocale();
3034

3135
// load template 403.latte or 404.latte or ... 4xx.latte
3236
$file = __DIR__ . "/templates/Error/{$exception->getCode()}.latte";

Mailer/extensions/mailer-module/src/Presenters/SearchPresenter.php

+7-19
Original file line numberDiff line numberDiff line change
@@ -3,39 +3,27 @@
33

44
namespace Remp\MailerModule\Presenters;
55

6-
use Remp\MailerModule\Repositories\BatchTemplatesRepository;
6+
use Remp\MailerModule\Models\Config\SearchConfig;
77
use Remp\MailerModule\Repositories\JobsRepository;
88
use Remp\MailerModule\Repositories\LayoutsRepository;
99
use Remp\MailerModule\Repositories\ListsRepository;
1010
use Remp\MailerModule\Repositories\TemplatesRepository;
1111

1212
final class SearchPresenter extends BasePresenter
1313
{
14-
private $templatesRepository;
15-
16-
private $layoutsRepository;
17-
18-
private $listsRepository;
19-
20-
private $jobsRepository;
21-
2214
public function __construct(
23-
TemplatesRepository $templatesRepository,
24-
LayoutsRepository $layoutsRepository,
25-
ListsRepository $listsRepository,
26-
JobsRepository $jobsRepository
15+
private TemplatesRepository $templatesRepository,
16+
private LayoutsRepository $layoutsRepository,
17+
private ListsRepository $listsRepository,
18+
private JobsRepository $jobsRepository,
19+
private SearchConfig $searchConfig,
2720
) {
2821
parent::__construct();
29-
30-
$this->templatesRepository = $templatesRepository;
31-
$this->layoutsRepository = $layoutsRepository;
32-
$this->listsRepository = $listsRepository;
33-
$this->jobsRepository = $jobsRepository;
3422
}
3523

3624
public function actionDefault($term): void
3725
{
38-
$limit = (int) $this->environmentConfig->getParam('max_result_count', '5');
26+
$limit = $this->searchConfig->getMaxResultCount();
3927
$layouts = array_values($this->layoutsRepository->search($term, $limit));
4028
$lists = array_values($this->listsRepository->search($term, $limit));
4129

Mailer/extensions/mailer-module/src/Presenters/TemplatePresenter.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Remp\MailerModule\Components\MailLinkStats\MailLinkStats;
1111
use Remp\MailerModule\Forms\IFormFactory;
1212
use Remp\MailerModule\Models\Config\Config;
13+
use Remp\MailerModule\Models\Config\EditorConfig;
1314
use Remp\MailerModule\Models\ContentGenerator\GeneratorInputFactory;
1415
use Remp\MailerModule\Models\ContentGenerator\Replace\RtmClickReplace;
1516
use Remp\MailerModule\Repositories\ActiveRow;
@@ -46,6 +47,7 @@ public function __construct(
4647
private TemplateTranslationsRepository $templateTranslationsRepository,
4748
private MailLinkStats $mailLinkStats,
4849
private Config $config,
50+
private EditorConfig $editorConfig,
4951
) {
5052
parent::__construct();
5153
}
@@ -263,7 +265,7 @@ public function renderNew(): void
263265
$this->template->layouts = $layouts;
264266
$this->template->snippets = $snippets;
265267
$this->template->lists = $lists;
266-
$this->template->templateEditor = $this->environmentConfig->getParam('template_editor', 'codemirror');
268+
$this->template->templateEditor = $this->editorConfig->getTemplateEditor();
267269
$this->template->editedLocale = null;
268270
}
269271

@@ -291,7 +293,7 @@ public function renderEdit($id, string $editedLocale = null): void
291293
$this->template->layouts = $layouts;
292294
$this->template->snippets = $snippets;
293295
$this->template->lists = $lists;
294-
$this->template->templateEditor = $this->environmentConfig->getParam('template_editor', 'codemirror');
296+
$this->template->templateEditor = $this->editorConfig->getTemplateEditor();
295297
$this->template->editedLocale = $editedLocale;
296298
}
297299

Mailer/extensions/mailer-module/src/config/config.neon

+14-5
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,23 @@
11
services:
22
router: Remp\MailerModule\Models\RouterFactory::createRouter
3-
environmentConfig:
4-
factory: Remp\MailerModule\Models\EnvironmentConfig
3+
environmentConfig: Remp\MailerModule\Models\EnvironmentConfig
4+
5+
linkedServices:
6+
factory: Remp\MailerModule\Models\Config\LinkedServices
57
setup:
68
- linkService(beam, %remp.beam.web_addr%, album)
79
- linkService(campaign, %remp.campaign.web_addr%, trending-up)
810
- linkService(mailer, /, email)
9-
- setParam(locale, %locale%)
10-
- setParam(max_result_count, %max_result_count%)
11-
- setParam(template_editor, %template_editor%)
11+
12+
searchConfig:
13+
factory: Remp\MailerModule\Models\Config\SearchConfig
14+
setup:
15+
- setMaxResultCount(beam, %max_result_count%)
16+
17+
editorConfig:
18+
factory: Remp\MailerModule\Models\Config\EditorConfig
19+
setup:
20+
- setTemplateEditor(%template_editor%)
1221

1322
localizationConfig:
1423
factory: Remp\MailerModule\Models\Config\LocalizationConfig(@environmentConfig::get('LOCALE'))

0 commit comments

Comments
 (0)