Skip to content

Commit 1d017c6

Browse files
committed
Refactor source and dist fields in Metadata entity to individual fields
1 parent e9f2330 commit 1d017c6

5 files changed

Lines changed: 203 additions & 58 deletions

File tree

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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 Version20260707081705 extends AbstractMigration
11+
{
12+
public function getDescription(): string
13+
{
14+
return 'Replace the source and dist json fields of metadata with individual fields';
15+
}
16+
17+
public function up(Schema $schema): void
18+
{
19+
$this->addSql(<<<'SQL'
20+
ALTER TABLE metadata ADD source_type VARCHAR(255) DEFAULT NULL
21+
SQL);
22+
$this->addSql(<<<'SQL'
23+
ALTER TABLE metadata ADD source_url VARCHAR(255) DEFAULT NULL
24+
SQL);
25+
$this->addSql(<<<'SQL'
26+
ALTER TABLE metadata ADD source_reference VARCHAR(255) DEFAULT NULL
27+
SQL);
28+
$this->addSql(<<<'SQL'
29+
ALTER TABLE metadata ADD distribution_type VARCHAR(255) DEFAULT NULL
30+
SQL);
31+
$this->addSql(<<<'SQL'
32+
ALTER TABLE metadata ADD distribution_url VARCHAR(255) DEFAULT NULL
33+
SQL);
34+
$this->addSql(<<<'SQL'
35+
ALTER TABLE metadata ADD distribution_reference VARCHAR(255) DEFAULT NULL
36+
SQL);
37+
$this->addSql(<<<'SQL'
38+
ALTER TABLE metadata ADD distribution_sha1_checksum VARCHAR(255) DEFAULT NULL
39+
SQL);
40+
$this->addSql(<<<'SQL'
41+
UPDATE metadata SET
42+
source_type = source ->> 'type',
43+
source_url = source ->> 'url',
44+
source_reference = source ->> 'reference',
45+
distribution_type = dist ->> 'type',
46+
distribution_url = dist ->> 'url',
47+
distribution_reference = dist ->> 'reference',
48+
distribution_sha1_checksum = dist ->> 'shasum'
49+
SQL);
50+
$this->addSql(<<<'SQL'
51+
ALTER TABLE metadata DROP source
52+
SQL);
53+
$this->addSql(<<<'SQL'
54+
ALTER TABLE metadata DROP dist
55+
SQL);
56+
}
57+
58+
public function down(Schema $schema): void
59+
{
60+
$this->addSql(<<<'SQL'
61+
ALTER TABLE metadata ADD source JSON DEFAULT NULL
62+
SQL);
63+
$this->addSql(<<<'SQL'
64+
ALTER TABLE metadata ADD dist JSON DEFAULT NULL
65+
SQL);
66+
$this->addSql(<<<'SQL'
67+
UPDATE metadata SET source = json_build_object(
68+
'type', source_type,
69+
'url', source_url,
70+
'reference', source_reference
71+
)
72+
WHERE source_type IS NOT NULL
73+
SQL);
74+
$this->addSql(<<<'SQL'
75+
UPDATE metadata SET dist = json_build_object(
76+
'type', distribution_type,
77+
'url', distribution_url,
78+
'reference', distribution_reference,
79+
'shasum', distribution_sha1_checksum
80+
)
81+
WHERE distribution_type IS NOT NULL
82+
SQL);
83+
$this->addSql(<<<'SQL'
84+
ALTER TABLE metadata DROP source_type
85+
SQL);
86+
$this->addSql(<<<'SQL'
87+
ALTER TABLE metadata DROP source_url
88+
SQL);
89+
$this->addSql(<<<'SQL'
90+
ALTER TABLE metadata DROP source_reference
91+
SQL);
92+
$this->addSql(<<<'SQL'
93+
ALTER TABLE metadata DROP distribution_type
94+
SQL);
95+
$this->addSql(<<<'SQL'
96+
ALTER TABLE metadata DROP distribution_url
97+
SQL);
98+
$this->addSql(<<<'SQL'
99+
ALTER TABLE metadata DROP distribution_reference
100+
SQL);
101+
$this->addSql(<<<'SQL'
102+
ALTER TABLE metadata DROP distribution_sha1_checksum
103+
SQL);
104+
}
105+
}

src/Doctrine/Entity/Metadata.php

Lines changed: 83 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,25 @@ class Metadata extends TrackedEntity implements \Stringable
5454
private ?string $targetDir = null;
5555

5656
#[ORM\Column(nullable: true)]
57-
private ?array $source = null;
57+
private ?string $sourceType = null;
5858

5959
#[ORM\Column(nullable: true)]
60-
private ?array $dist = null;
60+
private ?string $sourceUrl = null;
61+
62+
#[ORM\Column(nullable: true)]
63+
private ?string $sourceReference = null;
64+
65+
#[ORM\Column(nullable: true)]
66+
private ?string $distributionType = null;
67+
68+
#[ORM\Column(nullable: true)]
69+
private ?string $distributionUrl = null;
70+
71+
#[ORM\Column(nullable: true)]
72+
private ?string $distributionReference = null;
73+
74+
#[ORM\Column(nullable: true)]
75+
private ?string $distributionSha1Checksum = null;
6176

6277
#[ORM\Column(nullable: true)]
6378
private ?array $autoload = null;
@@ -282,26 +297,6 @@ public function setTargetDir(?string $targetDir): void
282297
$this->targetDir = $targetDir;
283298
}
284299

285-
public function getSource(): ?array
286-
{
287-
return $this->source;
288-
}
289-
290-
public function setSource(?array $source): void
291-
{
292-
$this->source = $source;
293-
}
294-
295-
public function getDist(): ?array
296-
{
297-
return $this->dist;
298-
}
299-
300-
public function setDist(?array $dist): void
301-
{
302-
$this->dist = $dist;
303-
}
304-
305300
public function getAutoload(): ?array
306301
{
307302
return $this->autoload;
@@ -483,47 +478,87 @@ public function isCurrentMetadata(): bool
483478

484479
public function getReference(): ?string
485480
{
486-
return $this->getSourceReference() ?? $this->getDistReference();
481+
return $this->getSourceReference() ?? $this->getDistributionReference();
487482
}
488483

489484
public function hasSource(): bool
490485
{
491-
return null !== $this->source;
486+
return null !== $this->sourceType;
492487
}
493488

494-
public function getSourceReference(): ?string
489+
public function getSourceType(): ?string
495490
{
496-
return $this->source['reference'] ?? null;
491+
return $this->sourceType;
497492
}
498493

499-
public function getSourceType(): ?string
494+
public function setSourceType(?string $sourceType): void
500495
{
501-
return $this->source['type'] ?? null;
496+
$this->sourceType = $sourceType;
502497
}
503498

504499
public function getSourceUrl(): ?string
505500
{
506-
return $this->source['url'] ?? null;
501+
return $this->sourceUrl;
502+
}
503+
504+
public function setSourceUrl(?string $sourceUrl): void
505+
{
506+
$this->sourceUrl = $sourceUrl;
507+
}
508+
509+
public function getSourceReference(): ?string
510+
{
511+
return $this->sourceReference;
512+
}
513+
514+
public function setSourceReference(?string $sourceReference): void
515+
{
516+
$this->sourceReference = $sourceReference;
517+
}
518+
519+
public function hasDistribution(): bool
520+
{
521+
return null !== $this->distributionType;
522+
}
523+
524+
public function getDistributionType(): ?string
525+
{
526+
return $this->distributionType;
527+
}
528+
529+
public function setDistributionType(?string $distributionType): void
530+
{
531+
$this->distributionType = $distributionType;
532+
}
533+
534+
public function getDistributionUrl(): ?string
535+
{
536+
return $this->distributionUrl;
537+
}
538+
539+
public function setDistributionUrl(?string $distributionUrl): void
540+
{
541+
$this->distributionUrl = $distributionUrl;
507542
}
508543

509-
public function hasDist(): bool
544+
public function getDistributionReference(): ?string
510545
{
511-
return null !== $this->dist;
546+
return $this->distributionReference;
512547
}
513548

514-
public function getDistReference(): ?string
549+
public function setDistributionReference(?string $distributionReference): void
515550
{
516-
return $this->dist['reference'] ?? null;
551+
$this->distributionReference = $distributionReference;
517552
}
518553

519-
public function getDistType(): ?string
554+
public function getDistributionSha1Checksum(): ?string
520555
{
521-
return $this->dist['type'] ?? null;
556+
return $this->distributionSha1Checksum;
522557
}
523558

524-
public function getDistUrl(): ?string
559+
public function setDistributionSha1Checksum(?string $distributionSha1Checksum): void
525560
{
526-
return $this->dist['url'] ?? null;
561+
$this->distributionSha1Checksum = $distributionSha1Checksum;
527562
}
528563

529564
public function hasVersionAlias(): bool
@@ -637,8 +672,17 @@ public function toComposerArray(): array
637672
'version_normalized' => $this->normalizedVersionName,
638673
'license' => $this->license,
639674
'authors' => $this->getAuthorsSorted(),
640-
'source' => $this->source,
641-
'dist' => $this->dist,
675+
'source' => !$this->hasSource() ? null : [
676+
'type' => $this->sourceType,
677+
'url' => $this->sourceUrl,
678+
'reference' => $this->sourceReference,
679+
],
680+
'dist' => !$this->hasDistribution() ? null : [
681+
'type' => $this->distributionType,
682+
'url' => $this->distributionUrl,
683+
'reference' => $this->distributionReference,
684+
'shasum' => $this->distributionSha1Checksum,
685+
],
642686
'type' => $this->type,
643687
'autoload' => $this->autoload,
644688
];

src/Package/PackageDistributionResolver.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,17 @@ public function resolve(Version $version, string $reference, string $type): bool
4545

4646
$metadata = $version->getCurrentMetadata();
4747

48-
if ($reference !== $metadata->getDistReference() || $type !== $metadata->getDistType()) {
48+
if ($reference !== $metadata->getDistributionReference() || $type !== $metadata->getDistributionType()) {
4949
return false;
5050
}
5151

52-
$distUrl = $metadata->getDistUrl();
52+
$distributionUrl = $metadata->getDistributionUrl();
5353
$path = $this->path($packageName, $versionName, $reference, $type);
5454

5555
$this->filesystem->mkdir(dirname($path));
5656

5757
$httpDownloader = $this->composer->createHttpDownloader();
58-
$httpDownloader->copy($distUrl, $path);
58+
$httpDownloader->copy($distributionUrl, $path);
5959

6060
return true;
6161
}

src/Package/PackageMetadataResolver.php

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -303,20 +303,16 @@ private function createMetadata(Version $version, CompletePackageInterface $data
303303
}
304304

305305
if ($data->getSourceType()) {
306-
$metadata->setSource([
307-
'type' => $data->getSourceType(),
308-
'url' => static::optimizeRepositoryUrl($data->getSourceUrl()),
309-
'reference' => $data->getSourceReference(),
310-
]);
306+
$metadata->setSourceType($data->getSourceType());
307+
$metadata->setSourceUrl(static::optimizeRepositoryUrl($data->getSourceUrl()));
308+
$metadata->setSourceReference($data->getSourceReference());
311309
}
312310

313-
if ($data->getDistType()) {
314-
$metadata->setDist([
315-
'type' => $data->getDistType(),
316-
'url' => $data->getDistUrl(),
317-
'reference' => $data->getDistReference(),
318-
'shasum' => $data->getDistSha1Checksum(),
319-
]);
311+
if ($data->getDistributionType()) {
312+
$metadata->setDistributionType($data->getDistributionType());
313+
$metadata->setDistributionUrl($data->getDistributionUrl());
314+
$metadata->setDistributionReference($data->getDistributionReference());
315+
$metadata->setDistributionSha1Checksum($data->getDistSha1Checksum());
320316
}
321317

322318
// Handle links

templates/dashboard/packages/package_info.html.twig

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -205,18 +205,18 @@
205205
{% endif %}
206206
</div>
207207
<div class="col-md-6">
208-
{% if metadata.hasDist() %}
208+
{% if metadata.hasDistribution() %}
209209
<div class="h5">
210210
{{ 'Distribution'|trans }}
211-
<span class="badge text-bg-secondary">{{ metadata.distType }}</span>
211+
<span class="badge text-bg-secondary">{{ metadata.distributionType }}</span>
212212
</div>
213213

214214
<div class="mb-2">
215-
{{ 'Reference'|trans }}: <code>{{ metadata.distReference }}</code>
215+
{{ 'Reference'|trans }}: <code>{{ metadata.distributionReference }}</code>
216216
</div>
217217

218218
<div>
219-
<a class="btn btn-sm btn-secondary" href="{{ metadata.distUrl }}" download>
219+
<a class="btn btn-sm btn-secondary" href="{{ metadata.distributionUrl }}" download>
220220
<span class="fa-solid fa-download me-1" aria-hidden="true"></span>
221221
<span class="btn-label">{{ 'Download' }}</span>
222222
</a>

0 commit comments

Comments
 (0)