Skip to content

Commit dc7a25c

Browse files
committed
Save distributions in the database
Signed-off-by: Tim Goudriaan <tim@codedmonkey.com>
1 parent a644b57 commit dc7a25c

5 files changed

Lines changed: 212 additions & 0 deletions

File tree

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace DoctrineMigrations;
6+
7+
use Doctrine\DBAL\Schema\Schema;
8+
use Doctrine\Migrations\AbstractMigration;
9+
10+
final class Version20250610105944 extends AbstractMigration
11+
{
12+
public function getDescription(): string
13+
{
14+
return 'Create distribution table';
15+
}
16+
17+
public function up(Schema $schema): void
18+
{
19+
$this->addSql(<<<'SQL'
20+
CREATE TABLE distribution (id INT GENERATED BY DEFAULT AS IDENTITY NOT NULL, reference VARCHAR(255) NOT NULL, type VARCHAR(255) NOT NULL, source TEXT DEFAULT NULL, version_id INT NOT NULL, created_at TIMESTAMP(0) WITHOUT TIME ZONE NOT NULL, last_modified_at TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL, PRIMARY KEY(id))
21+
SQL);
22+
$this->addSql(<<<'SQL'
23+
CREATE INDEX IDX_A44837814BBC2705 ON distribution (version_id)
24+
SQL);
25+
$this->addSql(<<<'SQL'
26+
ALTER TABLE distribution ADD CONSTRAINT FK_A44837814BBC2705 FOREIGN KEY (version_id) REFERENCES version (id) NOT DEFERRABLE INITIALLY IMMEDIATE
27+
SQL);
28+
}
29+
30+
public function down(Schema $schema): void
31+
{
32+
$this->addSql(<<<'SQL'
33+
ALTER TABLE distribution DROP CONSTRAINT FK_A44837814BBC2705
34+
SQL);
35+
$this->addSql(<<<'SQL'
36+
DROP TABLE distribution
37+
SQL);
38+
}
39+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
3+
namespace CodedMonkey\Dirigent\Doctrine\Entity;
4+
5+
use CodedMonkey\Dirigent\Doctrine\Repository\DistributionRepository;
6+
use Doctrine\DBAL\Types\Types;
7+
use Doctrine\ORM\Mapping as ORM;
8+
9+
#[ORM\Entity(repositoryClass: DistributionRepository::class)]
10+
class Distribution extends TrackedEntity
11+
{
12+
#[ORM\Column, ORM\GeneratedValue, ORM\Id]
13+
private ?int $id = null;
14+
15+
#[ORM\ManyToOne(inversedBy: 'distributions')]
16+
#[ORM\JoinColumn(nullable: false)]
17+
private ?Version $version = null;
18+
19+
#[ORM\Column]
20+
private ?string $reference = null;
21+
22+
#[ORM\Column]
23+
private ?string $type = null;
24+
25+
#[ORM\Column]
26+
private \DateTimeImmutable $releasedAt;
27+
28+
#[ORM\Column(type: Types::TEXT, nullable: true)]
29+
private ?string $source = null;
30+
31+
public function getId(): ?int
32+
{
33+
return $this->id;
34+
}
35+
36+
public function getVersion(): ?Version
37+
{
38+
return $this->version;
39+
}
40+
41+
public function setVersion(Version $version): void
42+
{
43+
$this->version = $version;
44+
}
45+
46+
public function getReference(): string
47+
{
48+
return $this->reference;
49+
}
50+
51+
public function setReference(string $reference): void
52+
{
53+
$this->reference = $reference;
54+
}
55+
56+
public function getType(): string
57+
{
58+
return $this->type;
59+
}
60+
61+
public function setType(string $type): void
62+
{
63+
$this->type = $type;
64+
}
65+
66+
public function getReleasedAt(): \DateTimeImmutable
67+
{
68+
return $this->releasedAt;
69+
}
70+
71+
public function setReleasedAt(\DateTimeImmutable $releasedAt): void
72+
{
73+
$this->releasedAt = $releasedAt;
74+
}
75+
76+
public function getSource(): string
77+
{
78+
return $this->source;
79+
}
80+
81+
public function setSource(string $source): void
82+
{
83+
$this->source = $source;
84+
}
85+
}

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 extends TrackedEntity
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

@@ -132,6 +135,7 @@ public function __construct()
132135
$this->replace = new ArrayCollection();
133136
$this->suggest = new ArrayCollection();
134137
$this->tags = new ArrayCollection();
138+
$this->distributions = new ArrayCollection();
135139
$this->installations = new VersionInstallations($this);
136140
}
137141

@@ -480,6 +484,14 @@ public function setPackage(Package $package): void
480484
$this->package = $package;
481485
}
482486

487+
/**
488+
* @return Collection<int, Distribution>
489+
*/
490+
public function getDistributions(): Collection
491+
{
492+
return $this->distributions;
493+
}
494+
483495
public function getInstallations(): VersionInstallations
484496
{
485497
return $this->installations;
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
namespace CodedMonkey\Dirigent\Doctrine\Repository;
4+
5+
use CodedMonkey\Dirigent\Doctrine\Entity\Distribution;
6+
use CodedMonkey\Dirigent\Doctrine\Entity\Package;
7+
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
8+
use Doctrine\Persistence\ManagerRegistry;
9+
10+
/**
11+
* @extends ServiceEntityRepository<Distribution>
12+
*
13+
* @method Distribution|null find($id, $lockMode = null, $lockVersion = null)
14+
* @method Distribution[] findAll()
15+
* @method Distribution[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
16+
* @method Distribution|null findOneBy(array $criteria, array $orderBy = null)
17+
*/
18+
class DistributionRepository extends ServiceEntityRepository
19+
{
20+
public function __construct(ManagerRegistry $registry)
21+
{
22+
parent::__construct($registry, Distribution::class);
23+
}
24+
25+
public function save(Distribution $entity, bool $flush = false): void
26+
{
27+
$this->getEntityManager()->persist($entity);
28+
29+
if ($flush) {
30+
$this->getEntityManager()->flush();
31+
}
32+
}
33+
34+
public function remove(Distribution $entity, bool $flush = false): void
35+
{
36+
$this->getEntityManager()->remove($entity);
37+
38+
if ($flush) {
39+
$this->getEntityManager()->flush();
40+
}
41+
}
42+
43+
/**
44+
* @return Distribution[]
45+
*/
46+
public function findByPackage(Package $package): array
47+
{
48+
return $this->createQueryBuilder('distribution')
49+
->leftJoin('distribution.version', 'version')
50+
->leftJoin('version.package', 'package')
51+
->andWhere('package.id = :package')
52+
->setParameter('package', $package->getId())
53+
->getQuery()
54+
->getResult();
55+
}
56+
}

src/Package/PackageDistributionResolver.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
use CodedMonkey\Dirigent\Composer\ComposerClient;
66
use CodedMonkey\Dirigent\Composer\ConfigFactory;
7+
use CodedMonkey\Dirigent\Doctrine\Entity\Distribution;
78
use CodedMonkey\Dirigent\Doctrine\Entity\Version;
9+
use CodedMonkey\Dirigent\Doctrine\Repository\DistributionRepository;
810
use CodedMonkey\Dirigent\Message\ResolveDistribution;
911
use Composer\IO\NullIO;
1012
use Composer\Pcre\Preg;
@@ -26,6 +28,7 @@
2628
public function __construct(
2729
private MessageBusInterface $messenger,
2830
private ComposerClient $composer,
31+
private DistributionRepository $distributionRepository,
2932
#[Autowire(param: 'dirigent.distributions.build')]
3033
private bool $buildDistributions,
3134
#[Autowire(param: 'dirigent.distributions.mirror')]
@@ -114,6 +117,14 @@ private function build(Version $version, ?string $reference, ?string $type): boo
114117
['git', 'archive', '--format=zip', "--output=$distributionPath", $reference],
115118
], $repositoryUrl, $cachePath);
116119

120+
$distribution = new Distribution();
121+
$distribution->setVersion($version);
122+
$distribution->setReference($reference);
123+
$distribution->setType($type);
124+
$distribution->setReleasedAt($version->getReleasedAt());
125+
126+
$this->distributionRepository->save($distribution, flush: true);
127+
117128
return true;
118129
}
119130

@@ -138,6 +149,15 @@ private function mirror(Version $version, ?string $reference, ?string $type): bo
138149
$httpDownloader = $this->composer->createHttpDownloader();
139150
$httpDownloader->copy($distributionUrl, $distributionPath);
140151

152+
$distribution = new Distribution();
153+
$distribution->setVersion($version);
154+
$distribution->setReference($reference);
155+
$distribution->setType($type);
156+
$distribution->setReleasedAt($version->getReleasedAt());
157+
$distribution->setSource($distributionUrl);
158+
159+
$this->distributionRepository->save($distribution, flush: true);
160+
141161
return true;
142162
}
143163
}

0 commit comments

Comments
 (0)