Skip to content

Commit 056e374

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

5 files changed

Lines changed: 210 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 (created_at TIMESTAMP(0) WITHOUT TIME ZONE NOT NULL, last_modified_at TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL, 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, 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: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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
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+
private \DateTimeImmutable $releasedAt;
26+
27+
#[ORM\Column(type: Types::TEXT, nullable: true)]
28+
private ?string $source = null;
29+
30+
public function getId(): ?int
31+
{
32+
return $this->id;
33+
}
34+
35+
public function getVersion(): ?Version
36+
{
37+
return $this->version;
38+
}
39+
40+
public function setVersion(Version $version): void
41+
{
42+
$this->version = $version;
43+
}
44+
45+
public function getReference(): string
46+
{
47+
return $this->reference;
48+
}
49+
50+
public function setReference(string $reference): void
51+
{
52+
$this->reference = $reference;
53+
}
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+
}
84+
}

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;
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: 19 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,13 @@ 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+
125+
$this->distributionRepository->save($distribution, true);
126+
117127
return true;
118128
}
119129

@@ -138,6 +148,15 @@ private function mirror(Version $version, ?string $reference, ?string $type): bo
138148
$httpDownloader = $this->composer->createHttpDownloader();
139149
$httpDownloader->copy($distributionUrl, $distributionPath);
140150

151+
$distribution = new Distribution();
152+
$distribution->setVersion($version);
153+
$distribution->setReference($reference);
154+
$distribution->setType($type);
155+
$distribution->setReleasedAt($version->getReleasedAt());
156+
$distribution->setSource($distributionUrl);
157+
158+
$this->distributionRepository->save($distribution, true);
159+
141160
return true;
142161
}
143162
}

0 commit comments

Comments
 (0)