Skip to content

Commit be7db88

Browse files
committed
Save distributions in the database [temporary]
1 parent 389f7d6 commit be7db88

9 files changed

Lines changed: 201 additions & 11 deletions

File tree

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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+
/**
11+
* Auto-generated Migration: Please modify to your needs!
12+
*/
13+
final class Version20250609105944 extends AbstractMigration
14+
{
15+
public function getDescription(): string
16+
{
17+
return '';
18+
}
19+
20+
public function up(Schema $schema): void
21+
{
22+
// this up() migration is auto-generated, please modify it to your needs
23+
$this->addSql(<<<'SQL'
24+
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))
25+
SQL);
26+
$this->addSql(<<<'SQL'
27+
CREATE INDEX IDX_A44837814BBC2705 ON distribution (version_id)
28+
SQL);
29+
$this->addSql(<<<'SQL'
30+
ALTER TABLE distribution ADD CONSTRAINT FK_A44837814BBC2705 FOREIGN KEY (version_id) REFERENCES version (id) NOT DEFERRABLE INITIALLY IMMEDIATE
31+
SQL);
32+
}
33+
34+
public function down(Schema $schema): void
35+
{
36+
// this down() migration is auto-generated, please modify it to your needs
37+
$this->addSql(<<<'SQL'
38+
ALTER TABLE distribution DROP CONSTRAINT FK_A44837814BBC2705
39+
SQL);
40+
$this->addSql(<<<'SQL'
41+
DROP TABLE distribution
42+
SQL);
43+
}
44+
}

src/Controller/ApiController.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
use Symfony\Component\HttpFoundation\JsonResponse;
1818
use Symfony\Component\HttpFoundation\Request;
1919
use Symfony\Component\HttpFoundation\Response;
20-
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
2120
use Symfony\Component\Messenger\Envelope;
2221
use Symfony\Component\Messenger\MessageBusInterface;
2322
use Symfony\Component\Messenger\Stamp\TransportNamesStamp;
@@ -79,13 +78,13 @@ public function packageMetadata(string $packageName): Response
7978
$basePackageName = u($packageName)->trimSuffix('~dev')->toString();
8079

8180
if (null === $package = $this->findPackage($basePackageName)) {
82-
throw new NotFoundHttpException();
81+
throw $this->createNotFoundException();
8382
}
8483

8584
$this->messenger->dispatch(new UpdatePackage($package->getId()));
8685

8786
if (!$this->providerManager->exists($packageName)) {
88-
throw new NotFoundHttpException();
87+
throw $this->createNotFoundException();
8988
}
9089

9190
return new BinaryFileResponse($this->providerManager->path($packageName), headers: ['Content-Type' => 'application/json']);
@@ -105,26 +104,26 @@ public function packageMetadata(string $packageName): Response
105104
public function packageDistribution(string $packageName, string $packageVersion, string $reference, string $type): Response
106105
{
107106
if (!$this->getParameter('dirigent.dist_mirroring.enabled')) {
108-
throw new NotFoundHttpException();
107+
throw $this->createNotFoundException();
109108
}
110109

111110
if (!$this->distributionResolver->exists($packageName, $packageVersion, $reference, $type)) {
112111
if (null === $package = $this->packageRepository->findOneBy(['name' => $packageName])) {
113-
throw new NotFoundHttpException();
112+
throw $this->createNotFoundException();
114113
}
115114

116115
if (null === $version = $this->versionRepository->findOneBy(['package' => $package, 'normalizedVersion' => $packageVersion])) {
117-
throw new NotFoundHttpException();
116+
throw $this->createNotFoundException();
118117
}
119118

120119
if ($version->isDevelopment() && !$this->getParameter('dirigent.dist_mirroring.dev_packages')) {
121-
throw new NotFoundHttpException();
120+
throw $this->createNotFoundException();
122121
}
123122

124123
$this->messenger->dispatch(new UpdatePackage($package->getId()));
125124

126125
if (!$this->distributionResolver->resolve($version, $reference, $type)) {
127-
throw new NotFoundHttpException();
126+
throw $this->createNotFoundException();
128127
}
129128
}
130129

src/Controller/Dashboard/DashboardPackagesInfoController.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace CodedMonkey\Dirigent\Controller\Dashboard;
44

55
use CodedMonkey\Dirigent\Attribute\IsGrantedAccess;
6+
use CodedMonkey\Dirigent\Doctrine\Entity\Distribution;
67
use CodedMonkey\Dirigent\Doctrine\Entity\Package;
78
use CodedMonkey\Dirigent\Doctrine\Entity\PackageProvideLink;
89
use CodedMonkey\Dirigent\Doctrine\Entity\PackageRequireLink;
@@ -35,7 +36,7 @@ public function info(string $packageName): Response
3536
return $this->versionInfo($packageName, $version->getNormalizedVersion());
3637
}
3738

38-
#[Route('/packages/{packageName}/v/{packageVersion}', name: 'dashboard_packages_version_info', requirements: ['packageName' => '[a-z0-9_.-]+/[a-z0-9_.-]+'])]
39+
#[Route('/packages/{packageName}/v/{packageVersion}', name: 'dashboard_packages_version_info', requirements: ['packageName' => '[a-z0-9_.-]+/[a-z0-9_.-]+', 'packageVersion' => '.*'])]
3940
#[IsGrantedAccess]
4041
public function versionInfo(string $packageName, string $packageVersion): Response
4142
{
@@ -73,6 +74,21 @@ public function versions(string $packageName): Response
7374
]);
7475
}
7576

77+
#[Route('/packages/{packageName}/distributions', name: 'dashboard_packages_distributions', requirements: ['packageName' => '[a-z0-9_.-]+/[a-z0-9_.-]+'])]
78+
#[IsGrantedAccess]
79+
public function distributions(string $packageName): Response
80+
{
81+
$package = $this->packageRepository->findOneBy(['name' => $packageName]);
82+
83+
$distributionRepository = $this->entityManager->getRepository(Distribution::class);
84+
$distributions = $distributionRepository->findByPackage($package);
85+
86+
return $this->render('dashboard/packages/package_distributions.html.twig', [
87+
'package' => $package,
88+
'distributions' => $distributions,
89+
]);
90+
}
91+
7692
#[Route('/packages/{packageName}/dependents', name: 'dashboard_packages_dependents', requirements: ['packageName' => '[a-z0-9_.-]+/[a-z0-9_.-]+'])]
7793
#[IsGrantedAccess]
7894
public function dependents(Request $request, string $packageName): Response
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace CodedMonkey\Dirigent\Doctrine\Entity;
4+
5+
use CodedMonkey\Dirigent\Doctrine\Repository\DistributionRepository;
6+
use Doctrine\ORM\Mapping as ORM;
7+
8+
#[ORM\Entity(repositoryClass: DistributionRepository::class)]
9+
class Distribution
10+
{
11+
#[ORM\Column, ORM\GeneratedValue, ORM\Id]
12+
private ?int $id = null;
13+
14+
#[ORM\ManyToOne]
15+
#[ORM\JoinColumn(nullable: false)]
16+
private ?Version $version = null;
17+
18+
#[ORM\Column]
19+
private ?string $reference = null;
20+
21+
public function getId(): ?int
22+
{
23+
return $this->id;
24+
}
25+
26+
public function getVersion(): ?Version
27+
{
28+
return $this->version;
29+
}
30+
31+
public function setVersion(Version $version): void
32+
{
33+
$this->version = $version;
34+
}
35+
36+
public function getReference(): ?string
37+
{
38+
return $this->reference;
39+
}
40+
41+
public function setReference(string $reference): void
42+
{
43+
$this->reference = $reference;
44+
}
45+
}
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: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
namespace CodedMonkey\Dirigent\Package;
44

55
use CodedMonkey\Dirigent\Composer\ComposerClient;
6+
use CodedMonkey\Dirigent\Doctrine\Entity\Distribution;
67
use CodedMonkey\Dirigent\Doctrine\Entity\Version;
8+
use CodedMonkey\Dirigent\Doctrine\Repository\DistributionRepository;
79
use Symfony\Component\DependencyInjection\Attribute\Autowire;
810
use Symfony\Component\Filesystem\Filesystem;
911

@@ -14,6 +16,7 @@
1416

1517
public function __construct(
1618
private ComposerClient $composer,
19+
private DistributionRepository $distributionRepository,
1720
#[Autowire(param: 'dirigent.storage.path')]
1821
string $storagePath,
1922
) {
@@ -53,6 +56,12 @@ public function resolve(Version $version, string $reference, string $type): bool
5356
$httpDownloader = $this->composer->createHttpDownloader();
5457
$httpDownloader->copy($distUrl, $path);
5558

59+
$distribution = new Distribution();
60+
$distribution->setVersion($version);
61+
$distribution->setReference($reference);
62+
63+
$this->distributionRepository->save($distribution, true);
64+
5665
return true;
5766
}
5867
}

templates/dashboard/packages/_package_header.html.twig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
<a {% if currentPage == 'versions' %}class="nav-link active" aria-current="page"{% else %}class="nav-link text-primary"{% endif%} href="{{ packageVersionsUrl }}">{{ 'Versions'|trans }}</a>
1111
</li>
1212
<li class="nav-item">
13-
{% set packageDependentsUrl = path('dashboard_packages_dependents', {packageName: package.name}) %}
14-
<a {% if currentPage == 'dependents' %}class="nav-link active" aria-current="page"{% else %}class="nav-link text-primary"{% endif%} href="{{ packageDependentsUrl }}">{{ 'Dependents'|trans }}</a>
13+
{% set packageDistributionsUrl = path('dashboard_packages_distributions', {packageName: package.name}) %}
14+
<a {% if currentPage == 'distributions' %}class="nav-link active" aria-current="page"{% else %}class="nav-link text-primary"{% endif%} href="{{ packageDistributionsUrl }}">{{ 'Distributions'|trans }}</a>
1515
</li>
1616
<li class="nav-item">
1717
{% set packageStatisticsUrl = path('dashboard_packages_statistics', {packageName: package.name}) %}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{% extends 'dashboard/packages/package_base.html.twig' %}
2+
3+
{% block page_title %}{{ package.name }} <small>{{ 'Distributions'|trans }}</small>{% endblock %}
4+
5+
{% block page_content %}
6+
{{ include('dashboard/packages/_package_header.html.twig', {currentPage: 'distributions', package: package}) }}
7+
8+
<div class="list-group list-group-flush mb-3">
9+
{% for distribution in distributions %}
10+
{% set packageVersionInfoUrl = path('dashboard_packages_version_info', {packageName: package.name, packageVersion: distribution.version.version}) %}
11+
<a href="{{ packageVersionInfoUrl }}" class="list-group-item">
12+
<div class="d-flex justify-content-between">
13+
<span>
14+
{{ distribution.version.version }} ({{ distribution.reference }})
15+
</span>
16+
</div>
17+
</a>
18+
{% endfor %}
19+
</div>
20+
{% endblock %}

translations/messages.en.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ Administration: Administration
33
Credits: Credits
44
Dashboard: Dashboard
55
Dependents: Dependents
6+
Distributions: Distributions
67
Documentation: Documentation
78
Implementations: Implementations
89
Info: Info

0 commit comments

Comments
 (0)