Skip to content
This repository was archived by the owner on Mar 16, 2024. It is now read-only.

Commit 821850a

Browse files
author
Kirill Nesmeyanov
committed
Make DB connection lazy initialized
1 parent 842fd69 commit 821850a

File tree

5 files changed

+46
-62
lines changed

5 files changed

+46
-62
lines changed

app/Application/Console/Commands/ArticlesTouchCommand.php

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
namespace App\Application\Console\Commands;
66

77
use App\Domain\Article\ArticleRepositoryInterface;
8-
use App\Infrastructure\Persistence\Doctrine\Repository\ArticleDatabaseRepository;
98
use Doctrine\ORM\EntityManagerInterface;
109
use Psr\Clock\ClockInterface;
1110

@@ -36,24 +35,18 @@ class ArticlesTouchCommand extends Command
3635
*/
3736
protected $description = 'Update articles and execute renderer';
3837

39-
/**
40-
* @param EntityManagerInterface $em
41-
*/
4238
public function __construct(
43-
private readonly EntityManagerInterface $em,
4439
private readonly ClockInterface $clock,
4540
) {
4641
parent::__construct();
4742
}
4843

49-
public function handle(ArticleRepositoryInterface $repository): void
50-
{
44+
public function handle(
45+
EntityManagerInterface $em,
46+
ArticleRepositoryInterface $repository,
47+
): void {
5148
$articles = $repository->all();
52-
$count = $articles instanceof \Countable || \is_array($articles)
53-
? \count($articles)
54-
: \count([...$articles])
55-
;
56-
$progress = $this->progress($count);
49+
$progress = $this->progress($this->count($articles));
5750

5851
foreach ($articles as $article) {
5952
$progress->setMessage($article->title . ' (' . $article->urn . ')');
@@ -64,10 +57,10 @@ public function handle(ArticleRepositoryInterface $repository): void
6457

6558
$article->touch($this->clock);
6659

67-
$this->em->persist($article);
60+
$em->persist($article);
6861
}
6962

7063
$progress->clear();
71-
$this->em->flush();
64+
$em->flush();
7265
}
7366
}

app/Application/Console/Commands/Command.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010
abstract class Command extends BaseCommand
1111
{
1212
/**
13-
* @param int $max
14-
* @return ProgressBar
13+
* @param int<0, max> $max
1514
*/
1615
protected function progress(int $max = 0): ProgressBar
1716
{
@@ -20,4 +19,16 @@ protected function progress(int $max = 0): ProgressBar
2019

2120
return $progress;
2221
}
22+
23+
/**
24+
* @return int<0, max>
25+
*/
26+
protected function count(iterable $items): int
27+
{
28+
if ($items instanceof \Countable || \is_array($items)) {
29+
return \count($items);
30+
}
31+
32+
return \count([...$items]);
33+
}
2334
}

app/Application/Console/Commands/DocsFetchCommand.php

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,9 @@ class DocsFetchCommand extends Command
3939
*/
4040
protected $description = 'Download and update source documentation';
4141

42-
/**
43-
* @var Repository
44-
*/
45-
private Repository $source;
42+
private readonly Repository $source;
4643

47-
/**
48-
* @param Client $client
49-
* @param EntityManagerInterface $em
50-
*/
51-
public function __construct(Client $client, private readonly EntityManagerInterface $em)
44+
public function __construct(Client $client)
5245
{
5346
parent::__construct();
5447

@@ -58,14 +51,15 @@ public function __construct(Client $client, private readonly EntityManagerInterf
5851
}
5952

6053
public function handle(
54+
EntityManagerInterface $em,
6155
VersionRepositoryInterface $versions,
6256
DocumentationRepositoryInterface $docs,
6357
): void {
6458
//
6559
// Синхронизируем все доступные версии из оригинальной документации.
6660
//
6761
$this->info('Versions updating');
68-
$branches = \iterator_to_array($this->loadVersions($versions));
62+
$branches = \iterator_to_array($this->loadVersions($em, $versions));
6963
$this->comment('Versions updated');
7064

7165
//
@@ -81,7 +75,7 @@ public function handle(
8175
foreach ($branches as ['branch' => $branch, 'version' => $version]) {
8276
$this->info(\sprintf('Source %s documentation updating', $branch->getName()));
8377

84-
foreach($this->loadFiles($docs, $branch, $version) as $_);
78+
foreach($this->loadFiles($em, $docs, $branch, $version) as $_);
8579

8680
$this->comment(\sprintf('Source %s documentation updated', $branch->getName()));
8781
}
@@ -90,7 +84,7 @@ public function handle(
9084
/**
9185
* @return \Traversable<non-empty-string, array{branch: BranchInterface, version: Version}>
9286
*/
93-
private function loadVersions(VersionRepositoryInterface $versions): \Traversable
87+
private function loadVersions(EntityManagerInterface $em, VersionRepositoryInterface $versions): \Traversable
9488
{
9589
$branches = $this->source->getBranches();
9690
$progress = $this->progress($branches->count());
@@ -110,19 +104,20 @@ private function loadVersions(VersionRepositoryInterface $versions): \Traversabl
110104
$version = $versions->findByName($branch->getName())
111105
?? new Version($branch->getName());
112106

113-
$this->em->persist($version);
107+
$em->persist($version);
114108

115109
yield $branch->getName() => ['branch' => $branch, 'version' => $version];
116110
}
117111

118-
$this->em->flush();
112+
$em->flush();
119113
$progress->clear();
120114
}
121115

122116
/**
123117
* @return \Traversable<non-empty-string, array{file: FileInterface, page: Documentation}>
124118
*/
125119
private function loadFiles(
120+
EntityManagerInterface $em,
126121
DocumentationRepositoryInterface $docs,
127122
BranchInterface $branch,
128123
Version $ver,
@@ -161,10 +156,10 @@ private function loadFiles(
161156

162157
yield $file->getUrn() => ['file' => $file, 'page' => $page];
163158

164-
$this->em->persist($page);
159+
$em->persist($page);
165160
}
166161

167-
$this->em->flush();
168162
$progress->clear();
163+
$em->flush();
169164
}
170165
}

app/Application/Console/Commands/DocsTouchCommand.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,15 @@ class DocsTouchCommand extends Command
3636
protected $description = 'Update documentation and execute the page renderer';
3737

3838
public function __construct(
39-
private readonly EntityManagerInterface $em,
4039
private readonly ClockInterface $clock,
4140
) {
4241
parent::__construct();
4342
}
4443

45-
public function handle(VersionRepositoryInterface $versions): void
46-
{
44+
public function handle(
45+
EntityManagerInterface $em,
46+
VersionRepositoryInterface $versions
47+
): void {
4748
foreach ($versions->all() as $version) {
4849
$progress = $this->progress($version->docs->count());
4950

@@ -57,10 +58,10 @@ public function handle(VersionRepositoryInterface $versions): void
5758

5859
$documentation->touch($this->clock);
5960

60-
$this->em->persist($documentation);
61+
$em->persist($documentation);
6162
}
6263

63-
$this->em->flush();
64+
$em->flush();
6465
$progress->clear();
6566
}
6667
}

app/Application/Console/Commands/DocsTranslationCommand.php

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -28,34 +28,16 @@ abstract class DocsTranslationCommand extends Command
2828
private const NOTICE_TRANSLATION_EXTRA_FILE =
2929
'<error>Skip the translation file %s, which does not correspond to the source</error>';
3030

31-
/**
32-
* @var Repository
33-
*/
34-
protected Repository $source;
35-
36-
/**
37-
* @var Repository
38-
*/
39-
protected Repository $translation;
31+
protected readonly Repository $source;
4032

41-
/**
42-
* @var EntityManagerInterface
43-
*/
44-
protected EntityManagerInterface $em;
33+
protected readonly Repository $translation;
4534

46-
/**
47-
* @param Client $client
48-
* @param EntityManagerInterface $em
49-
* @param Config $config
50-
*/
51-
public function __construct(Client $client, EntityManagerInterface $em, Config $config)
35+
public function __construct(Client $client, Config $config)
5236
{
5337
parent::__construct();
5438

5539
\ini_set('memory_limit', -1);
5640

57-
$this->em = $em;
58-
5941
$this->source = new Repository(
6042
$client,
6143
$config->get('documentation.laravel-source.user'),
@@ -71,6 +53,7 @@ public function __construct(Client $client, EntityManagerInterface $em, Config $
7153
}
7254

7355
public function handle(
56+
EntityManagerInterface $em,
7457
VersionRepositoryInterface $versions,
7558
DocumentationRepositoryInterface $docs,
7659
): void {
@@ -79,12 +62,13 @@ public function handle(
7962
//
8063
foreach ($this->translation->getBranches() as $branch) {
8164
$this->info(\sprintf('Translation %s updating', $branch->getName()));
82-
$this->loadTranslations($branch, $versions, $docs);
65+
$this->loadTranslations($em, $branch, $versions, $docs);
8366
$this->comment(\sprintf('Translation %s updated', $branch->getName()));
8467
}
8568
}
8669

8770
private function loadTranslations(
71+
EntityManagerInterface $em,
8872
BranchInterface $branch,
8973
VersionRepositoryInterface $versions,
9074
DocumentationRepositoryInterface $docs
@@ -133,12 +117,12 @@ private function loadTranslations(
133117

134118
$this->each($page, $file);
135119

136-
$this->em->persist($page);
137-
$this->em->flush();
120+
$em->persist($page);
121+
$em->flush();
138122
}
139123

140124
$progress->clear();
141-
$this->em->flush();
125+
$em->flush();
142126
}
143127

144128
/**

0 commit comments

Comments
 (0)