Skip to content

Commit 0411976

Browse files
committed
Distributions and tracked entities?
1 parent ce6c640 commit 0411976

7 files changed

Lines changed: 121 additions & 6 deletions

File tree

migrations/Version20250610105944.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public function getDescription(): string
1717
public function up(Schema $schema): void
1818
{
1919
$this->addSql(<<<'SQL'
20-
CREATE TABLE distribution (id INT GENERATED BY DEFAULT AS IDENTITY NOT NULL, reference VARCHAR(255) NOT NULL, version_id INT NOT NULL, PRIMARY KEY(id))
20+
CREATE TABLE distribution (id INT GENERATED BY DEFAULT AS IDENTITY NOT NULL, reference VARCHAR(255) NOT NULL, type VARCHAR(255) NOT NULL, source VARCHAR(255) NOT NULL, version_id INT NOT NULL, PRIMARY KEY(id))
2121
SQL);
2222
$this->addSql(<<<'SQL'
2323
CREATE INDEX IDX_A44837814BBC2705 ON distribution (version_id)

src/Controller/ApiController.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,9 @@ public function packageMetadata(string $packageName): Response
8181
throw $this->createNotFoundException();
8282
}
8383

84-
$this->messenger->dispatch(new UpdatePackage($package->getId()));
84+
if ($this->getParameter('dirigent.packages.dynamic_updates')) {
85+
$this->messenger->dispatch(new UpdatePackage($package->getId()));
86+
}
8587

8688
if (!$this->providerManager->exists($packageName)) {
8789
throw $this->createNotFoundException();

src/Doctrine/Entity/Distribution.php

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,30 @@
33
namespace CodedMonkey\Dirigent\Doctrine\Entity;
44

55
use CodedMonkey\Dirigent\Doctrine\Repository\DistributionRepository;
6+
use Doctrine\DBAL\Types\Types;
67
use Doctrine\ORM\Mapping as ORM;
78

89
#[ORM\Entity(repositoryClass: DistributionRepository::class)]
9-
class Distribution
10+
class Distribution extends TrackedEntity
1011
{
1112
#[ORM\Column, ORM\GeneratedValue, ORM\Id]
1213
private ?int $id = null;
1314

14-
#[ORM\ManyToOne]
15+
#[ORM\ManyToOne(inversedBy: 'distributions')]
1516
#[ORM\JoinColumn(nullable: false)]
1617
private ?Version $version = null;
1718

1819
#[ORM\Column]
1920
private ?string $reference = null;
2021

22+
#[ORM\Column]
23+
private ?string $type = null;
24+
25+
private \DateTimeImmutable $releasedAt;
26+
27+
#[ORM\Column(type: Types::TEXT, nullable: true)]
28+
private ?string $source = null;
29+
2130
public function getId(): ?int
2231
{
2332
return $this->id;
@@ -33,7 +42,7 @@ public function setVersion(Version $version): void
3342
$this->version = $version;
3443
}
3544

36-
public function getReference(): ?string
45+
public function getReference(): string
3746
{
3847
return $this->reference;
3948
}
@@ -42,4 +51,34 @@ public function setReference(string $reference): void
4251
{
4352
$this->reference = $reference;
4453
}
54+
55+
public function getType(): string
56+
{
57+
return $this->type;
58+
}
59+
60+
public function setType(string $type): void
61+
{
62+
$this->type = $type;
63+
}
64+
65+
public function getReleasedAt(): \DateTimeImmutable
66+
{
67+
return $this->releasedAt;
68+
}
69+
70+
public function setReleasedAt(\DateTimeImmutable $releasedAt): void
71+
{
72+
$this->releasedAt = $releasedAt;
73+
}
74+
75+
public function getSource(): string
76+
{
77+
return $this->source;
78+
}
79+
80+
public function setSource(string $source): void
81+
{
82+
$this->source = $source;
83+
}
4584
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace CodedMonkey\Dirigent\Doctrine\Entity;
4+
5+
use Doctrine\DBAL\Types\Types;
6+
use Doctrine\ORM\Mapping as ORM;
7+
8+
#[ORM\MappedSuperclass]
9+
abstract class TrackedEntity
10+
{
11+
#[ORM\Column(type: Types::DATETIME_IMMUTABLE)]
12+
protected readonly \DateTimeImmutable $createdAt;
13+
14+
#[ORM\Column(type: Types::DATETIME_IMMUTABLE)]
15+
protected ?\DateTimeImmutable $modifiedAt = null;
16+
17+
public function getCreatedAt(): \DateTimeImmutable
18+
{
19+
return $this->createdAt;
20+
}
21+
22+
#[ORM\PrePersist]
23+
public function setCreatedAt(): void
24+
{
25+
$this->createdAt ??= new \DateTimeImmutable();
26+
}
27+
28+
public function getModifiedAt(): ?\DateTimeImmutable
29+
{
30+
return $this->modifiedAt;
31+
}
32+
33+
#[ORM\PreUpdate]
34+
public function setModifiedAt(): void
35+
{
36+
$this->modifiedAt = new \DateTimeImmutable();
37+
}
38+
}

src/Doctrine/Entity/Version.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,9 @@ class Version
114114
#[ORM\ManyToOne(targetEntity: Package::class, inversedBy: 'versions')]
115115
private ?Package $package = null;
116116

117+
#[ORM\OneToMany(targetEntity: Distribution::class, mappedBy: 'version')]
118+
private Collection $distributions;
119+
117120
#[ORM\OneToOne(mappedBy: 'version', cascade: ['persist', 'detach', 'remove'])]
118121
private VersionInstallations $installations;
119122

@@ -135,6 +138,7 @@ public function __construct()
135138
$this->replace = new ArrayCollection();
136139
$this->suggest = new ArrayCollection();
137140
$this->tags = new ArrayCollection();
141+
$this->distributions = new ArrayCollection();
138142
$this->installations = new VersionInstallations($this);
139143
$this->createdAt = new \DateTime();
140144
}
@@ -484,6 +488,14 @@ public function setPackage(Package $package): void
484488
$this->package = $package;
485489
}
486490

491+
/**
492+
* @return Collection<int, Distribution>
493+
*/
494+
public function getDistributions(): Collection
495+
{
496+
return $this->distributions;
497+
}
498+
487499
public function getInstallations(): VersionInstallations
488500
{
489501
return $this->installations;

src/Package/PackageDistributionResolver.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,28 @@ public function __construct(
4242
$this->distributionStoragePath = "$storagePath/distribution";
4343
}
4444

45+
public function isAvailable(): bool
46+
{
47+
48+
}
49+
50+
/**
51+
* Check if the distribution exists on the filesystem, circumventing the database.
52+
*/
4553
public function exists(string $packageName, string $packageVersion, ?string $reference, ?string $type): bool
4654
{
4755
return null !== $reference && null !== $type && $this->filesystem->exists($this->path($packageName, $packageVersion, $reference, $type));
4856
}
4957

58+
public function findEntity(Version $version, string $reference, string $type): ?Distribution
59+
{
60+
return $this->distributionRepository->findOneBy([
61+
'version' => $version,
62+
'reference' => $reference,
63+
'type' => $type,
64+
]);
65+
}
66+
5067
public function path(string $packageName, string $packageVersion, string $reference, string $type): string
5168
{
5269
return "$this->distributionStoragePath/$packageName/$packageVersion-$reference.$type";
@@ -120,6 +137,7 @@ private function build(Version $version, ?string $reference, ?string $type): boo
120137
$distribution = new Distribution();
121138
$distribution->setVersion($version);
122139
$distribution->setReference($reference);
140+
$distribution->setType($type);
123141

124142
$this->distributionRepository->save($distribution, true);
125143

@@ -150,6 +168,9 @@ private function mirror(Version $version, ?string $reference, ?string $type): bo
150168
$distribution = new Distribution();
151169
$distribution->setVersion($version);
152170
$distribution->setReference($reference);
171+
$distribution->setType($type);
172+
$distribution->setReleasedAt($version->getReleasedAt());
173+
$distribution->setSource($distributionUrl);
153174

154175
$this->distributionRepository->save($distribution, true);
155176

templates/dashboard/packages/package_versions.html.twig

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@
1616
/ {{ version.versionAlias }}
1717
{% endif %}
1818
</span>
19-
<span class="text-muted">{{ version.releasedAt|date }}</span>
19+
<span class="text-muted">
20+
{% dump version.distributions.toArray() %}
21+
{{ version.releasedAt|date }}
22+
</span>
2023
</div>
2124
</a>
2225
{% endfor %}

0 commit comments

Comments
 (0)