Skip to content

Commit 4bd4f3c

Browse files
committed
Merge branch 'fetch-strategy-source'
2 parents 3d85fa2 + 844c295 commit 4bd4f3c

21 files changed

Lines changed: 233 additions & 42 deletions

‎config/reference.php‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1649,7 +1649,8 @@
16491649
* dev_packages?: bool|Param, // Default: false
16501650
* },
16511651
* metadata?: array{
1652-
* mirror_vcs_repositories?: bool|Param, // Fetch mirrored packages from their VCS repositories by default when possible. // Default: false
1652+
* default_fetch_strategy?: value-of<\CodedMonkey\Dirigent\Entity\PackageFetchStrategy>|\CodedMonkey\Dirigent\Entity\PackageFetchStrategy|Param, // Available values are "mirror" (default, fetch from the mirror), "source" (fetch from the VCS source) and "vcs" (fetch complete VCS repository) // Default: "mirror"
1653+
* mirror_vcs_repositories?: bool|Param, // Deprecated: The node "mirror_vcs_repositories" at path "dirigent.metadata.mirror_vcs_repositories" is deprecated. Use the "default_fetch_strategy" option instead. // Fetch mirrored packages from their VCS repositories by default when possible. // Default: false
16531654
* retain_pruned_versions?: bool|array{
16541655
* enabled?: bool|Param, // Retain pruned package versions. // Default: true
16551656
* tagged_versions?: bool|Param, // Retain pruned tagged package versions. // Default: true

‎docs/configuration-reference.md‎

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ dirigent:
3434
preferred: true
3535
dev_packages: false
3636
metadata:
37-
mirror_vcs_repositories: false
37+
default_fetch_strategy: 'mirror'
3838
retain_pruned_versions:
3939
enabled: true
4040
tagged_versions: true
@@ -131,13 +131,22 @@ Whether to enable or disable distribution mirroring
131131

132132
## metadata
133133

134-
### mirror_vcs_repositories
134+
### default_fetch_strategy
135135

136-
Type: `boolean` | Default: `false`
136+
Type: `string` | Default: `mirror`
137137

138-
Fetch mirrored packages from their VCS repositories by default when possible.
138+
Configure the default fetch strategy for new packages:
139139

140-
Sets the fetch strategy of new mirrored packages to **Fetch from VCS**.
140+
**mirror** (Fetch from mirror)
141+
Always try to mirror package metadata from mirror registries when possible. Only metadata from the project's
142+
`composer.json` is available. If mirroring is not possible, it defaults to `source` instead.
143+
**source** (Fetch from source)
144+
Fetch the package metadata directly from the source, but it doesn't have to include VCS data. For example, when the
145+
package is hosted on GitHub the API is used instead, which saves on storage and bandwidth but limits the amount of
146+
metadata that's available.
147+
**vcs** (Fetch from VCS)
148+
Fetch the package metadata directly from the source through VCS. Package metadata is created directly from the
149+
source code.
141150

142151
### retain_pruned_versions
143152

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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 Version20260713143513 extends AbstractMigration
11+
{
12+
public function getDescription(): string
13+
{
14+
return 'Convert the vcs package fetch strategy to source to preserve behavior of existing packages';
15+
}
16+
17+
public function up(Schema $schema): void
18+
{
19+
// Previously the vcs fetch strategy used the default Composer drivers (using APIs where
20+
// possible), which is now the behavior of the source fetch strategy. The vcs fetch
21+
// strategy now always clones repositories instead.
22+
$this->addSql(<<<'SQL'
23+
UPDATE package SET fetch_strategy = 'source' WHERE fetch_strategy = 'vcs'
24+
SQL);
25+
// Packages without a fetch strategy or mirror registry fell back to the vcs fetch strategy.
26+
$this->addSql(<<<'SQL'
27+
UPDATE package SET fetch_strategy = 'source' WHERE fetch_strategy IS NULL AND mirror_registry_id IS NULL
28+
SQL);
29+
}
30+
31+
public function down(Schema $schema): void
32+
{
33+
$this->addSql(<<<'SQL'
34+
UPDATE package SET fetch_strategy = 'vcs' WHERE fetch_strategy = 'source'
35+
SQL);
36+
}
37+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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 Version20260714131735 extends AbstractMigration
11+
{
12+
public function getDescription(): string
13+
{
14+
return 'Make the package fetch strategy required';
15+
}
16+
17+
public function up(Schema $schema): void
18+
{
19+
// Make the package fetch strategy required while preserving the previous default for existing packages
20+
$this->addSql(<<<'SQL'
21+
UPDATE package
22+
SET fetch_strategy = CASE WHEN mirror_registry_id IS NULL THEN 'source' ELSE 'mirror' END
23+
WHERE fetch_strategy IS NULL
24+
SQL);
25+
$this->addSql(<<<'SQL'
26+
ALTER TABLE package ALTER fetch_strategy SET NOT NULL
27+
SQL);
28+
}
29+
30+
public function down(Schema $schema): void
31+
{
32+
$this->addSql(<<<'SQL'
33+
ALTER TABLE package ALTER fetch_strategy DROP NOT NULL
34+
SQL);
35+
}
36+
}

‎src/Composer/ComposerClient.php‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Composer\IO\IOInterface;
1212
use Composer\IO\NullIO;
1313
use Composer\Repository\ComposerRepository;
14+
use Composer\Repository\Vcs as ComposerVcs;
1415
use Composer\Repository\VcsRepository;
1516
use Composer\Util\HttpDownloader;
1617

@@ -41,7 +42,18 @@ public function createVcsRepository(Package $package, ?IOInterface $io = null, ?
4142
}
4243
$httpDownloader = $this->createHttpDownloader($io, $config);
4344

44-
return new VcsRepository(['url' => $repoUrl], $io, $config, $httpDownloader);
45+
if ($package->getFetchStrategy()->isVcs()) {
46+
$drivers = [
47+
'git' => ComposerVcs\GitDriver::class,
48+
'hg' => ComposerVcs\HgDriver::class,
49+
'perforce' => ComposerVcs\PerforceDriver::class,
50+
'fossil' => ComposerVcs\FossilDriver::class,
51+
// svn must be last because identifying a subversion server for sure is practically impossible
52+
'svn' => ComposerVcs\SvnDriver::class,
53+
];
54+
}
55+
56+
return new VcsRepository(['url' => $repoUrl], $io, $config, $httpDownloader, drivers: $drivers ?? null);
4557
}
4658

4759
public function createHttpDownloader(?IOInterface $io = null, ?Config $config = null): HttpDownloader

‎src/Controller/ApiController.php‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ public function __construct(
4343
private readonly MessageBusInterface $messenger,
4444
#[Autowire(param: 'dirigent.packages.dynamic_updates')]
4545
private readonly bool $dynamicUpdatesEnabled,
46-
#[Autowire(param: 'dirigent.metadata.mirror_vcs_repositories')]
47-
private readonly bool $mirrorVcsRepositories = false,
46+
#[Autowire(param: 'dirigent.metadata.default_mirror_fetch_strategy')]
47+
private readonly PackageFetchStrategy $defaultMirrorFetchStrategy,
4848
) {
4949
}
5050

@@ -184,10 +184,9 @@ private function findPackage(string $packageName, ?bool $create = false): ?Packa
184184
return null;
185185
}
186186

187-
$package = new Package();
188-
$package->setName($packageName);
187+
$package = new Package($packageName);
189188
$package->setMirrorRegistry($registry);
190-
$package->setFetchStrategy($this->mirrorVcsRepositories ? PackageFetchStrategy::Vcs : PackageFetchStrategy::Mirror);
189+
$package->setFetchStrategy($this->defaultMirrorFetchStrategy);
191190

192191
$this->packageRepository->save($package, true);
193192
}

‎src/Controller/Dashboard/DashboardPackagesController.php‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ public function __construct(
3232
private readonly PackageRepository $packageRepository,
3333
private readonly PackageMetadataResolver $metadataResolver,
3434
private readonly MessageBusInterface $messenger,
35-
#[Autowire(param: 'dirigent.metadata.mirror_vcs_repositories')]
36-
private readonly bool $mirrorVcsRepositories = false,
35+
#[Autowire(param: 'dirigent.metadata.default_mirror_fetch_strategy')]
36+
private readonly PackageFetchStrategy $defaultMirrorFetchStrategy,
3737
) {
3838
}
3939

@@ -108,10 +108,9 @@ public function addMirroring(Request $request): Response
108108
continue;
109109
}
110110

111-
$package = new Package();
112-
$package->setName($packageName);
111+
$package = new Package($packageName);
113112
$package->setMirrorRegistry($registry);
114-
$package->setFetchStrategy($this->mirrorVcsRepositories ? PackageFetchStrategy::Vcs : PackageFetchStrategy::Mirror);
113+
$package->setFetchStrategy($this->defaultMirrorFetchStrategy);
115114

116115
$this->packageRepository->save($package, true);
117116

@@ -149,6 +148,7 @@ public function addVcsRepository(Request $request): Response
149148
if ($form->isSubmitted() && $form->isValid()) {
150149
/** @var Package $package */
151150
$package = $form->getData();
151+
152152
$this->packageRepository->save($package, true);
153153

154154
$this->messenger->dispatch(new UpdatePackage($package->getId(), PackageUpdateSource::Manual));

‎src/DependencyInjection/DirigentConfiguration.php‎

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

55
namespace CodedMonkey\Dirigent\DependencyInjection;
66

7+
use CodedMonkey\Dirigent\Entity\PackageFetchStrategy;
78
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
89
use Symfony\Component\Config\Definition\Builder\NodeDefinition;
910
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
@@ -91,9 +92,19 @@ private function addMetadataSection(ArrayNodeDefinition|NodeDefinition $rootNode
9192
->arrayNode('metadata')
9293
->addDefaultsIfNotSet()
9394
->children()
95+
->enumNode('default_fetch_strategy')
96+
->enumFqcn(PackageFetchStrategy::class)
97+
->defaultValue(PackageFetchStrategy::Mirror)
98+
->info('Available values are "mirror" (default, fetch from the mirror), "source" (fetch from the VCS source) and "vcs" (fetch complete VCS repository)')
99+
->end()
94100
->booleanNode('mirror_vcs_repositories')
95101
->defaultFalse()
96102
->info('Fetch mirrored packages from their VCS repositories by default when possible.')
103+
->setDeprecated(
104+
package: 'codedmonkey/dirigent',
105+
version: '0.8.0',
106+
message: 'The node "%node%" at path "%path%" is deprecated. Use the "default_fetch_strategy" option instead.',
107+
)
97108
->end()
98109
->arrayNode('retain_pruned_versions')
99110
->canBeDisabled('Retain pruned package versions.')

‎src/DependencyInjection/DirigentExtension.php‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace CodedMonkey\Dirigent\DependencyInjection;
66

7+
use CodedMonkey\Dirigent\Entity\PackageFetchStrategy;
78
use Symfony\Component\Config\Definition\ConfigurationInterface;
89
use Symfony\Component\DependencyInjection\ContainerBuilder;
910
use Symfony\Component\HttpKernel\DependencyInjection\ConfigurableExtension;
@@ -58,11 +59,25 @@ private function registerEncryptionConfiguration(array $config, ContainerBuilder
5859
}
5960

6061
/**
61-
* @param array{mirror_vcs_repositories: bool, retain_stale_revisions: array{enabled: bool, tagged_versions: bool, dev_versions: bool}, retain_pruned_versions: array{enabled: bool, tagged_versions: bool, dev_versions: bool}} $config
62+
* @param array{default_fetch_strategy: PackageFetchStrategy, mirror_vcs_repositories: bool, retain_stale_revisions: array{enabled: bool, tagged_versions: bool, dev_versions: bool}, retain_pruned_versions: array{enabled: bool, tagged_versions: bool, dev_versions: bool}} $config
6263
*/
6364
private function registerMetadataConfiguration(array $config, ContainerBuilder $container): void
6465
{
65-
$container->setParameter('dirigent.metadata.mirror_vcs_repositories', $config['mirror_vcs_repositories']);
66+
$defaultFetchStrategy = $config['default_fetch_strategy'];
67+
$mirrorVcsRepositories = $config['mirror_vcs_repositories'];
68+
69+
if ($mirrorVcsRepositories && $defaultFetchStrategy->isMirror()) {
70+
$defaultFetchStrategy = PackageFetchStrategy::Source;
71+
}
72+
73+
$container->setParameter(
74+
name: 'dirigent.metadata.default_mirror_fetch_strategy',
75+
value: $defaultFetchStrategy,
76+
);
77+
$container->setParameter(
78+
name: 'dirigent.metadata.default_vcs_fetch_strategy',
79+
value: false === $defaultFetchStrategy->isMirror() ? $defaultFetchStrategy : PackageFetchStrategy::Source,
80+
);
6681

6782
$retainPrunedVersions = $config['retain_pruned_versions']['enabled'];
6883
$container->setParameter(

‎src/Doctrine/DataFixtures/PackageFixtures.php‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@ public function __construct(
2020
public function load(ObjectManager $manager): void
2121
{
2222
foreach ($this->getPackages() as $packageData) {
23-
$package = new Package();
24-
25-
$package->setName($packageData['name']);
23+
$package = new Package($packageData['name']);
2624
$package->setRepositoryUrl($packageData['repositoryUrl']);
2725
$package->setFetchStrategy(PackageFetchStrategy::Vcs);
2826

0 commit comments

Comments
 (0)