Skip to content

Commit

Permalink
Merge pull request #288 from vtsykun/fix/proxy-refactor
Browse files Browse the repository at this point in the history
Refactor composer proxy feat
  • Loading branch information
vtsykun authored Nov 2, 2024
2 parents 7441203 + 9ad786f commit 412b00f
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 127 deletions.
8 changes: 8 additions & 0 deletions src/Composer/Repository/ComposerProxyRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

class ComposerProxyRepository extends ComposerRepository implements PacketonRepositoryInterface
{
private string $packageName;

public function __construct(
protected array $repoConfig,
protected IOInterface $io,
Expand All @@ -21,6 +23,7 @@ public function __construct(
) {
parent::__construct($repoConfig, $io, $config, $httpDownloader);

$this->packageName = $this->repoConfig['packageName'];
$this->process ??= new ProcessExecutor($this->io);
}

Expand All @@ -29,6 +32,11 @@ public function getHttpDownloader(): HttpDownloader
return $this->httpDownloader;
}

public function getPackages(): array
{
return $this->findPackages($this->packageName);
}

public function getProcessExecutor(): ProcessExecutor
{
return $this->process;
Expand Down
25 changes: 0 additions & 25 deletions src/Filter/VersionFilter.php

This file was deleted.

2 changes: 1 addition & 1 deletion src/Form/Type/Package/ProxyPackageType.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
'label' => 'Packages.json',
'attr' => [
'class' => 'package-repo-info',
'placeholder' => 'e.g.: https://repo.magento.com/packages.json',
'placeholder' => 'e.g.: https://repo.magento.com',
],
]);

Expand Down
58 changes: 0 additions & 58 deletions src/Model/ComposerProxyPackageManager.php

This file was deleted.

2 changes: 1 addition & 1 deletion src/Package/RepTypes.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public static function isNotAutoCrawled(): array
public static function isBuildInDist(?string $type): bool
{
return match ($type) {
self::ARTIFACT, self::CUSTOM, self::VIRTUAL => true, self::PROXY => true,
self::ARTIFACT, self::CUSTOM, self::VIRTUAL => true,
default => false,
};
}
Expand Down
40 changes: 10 additions & 30 deletions src/Package/Updater.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\Persistence\ManagerRegistry;
use Packeton\Composer\PackagistFactory;
use Packeton\Composer\Repository\ComposerProxyRepository;
use Packeton\Composer\Repository\PacketonRepositoryInterface;
use Packeton\Entity\Author;
use Packeton\Entity\Package;
Expand All @@ -42,7 +41,6 @@
use Packeton\Entity\SuggestLink;
use Packeton\Event\SecurityAdvisoryEvent;
use Packeton\Event\UpdaterEvent;
use Packeton\Mirror\Service\ProxyHttpDownloader;
use Packeton\Model\ProviderManager;
use Packeton\Repository\VersionRepository;
use Packeton\Service\DistConfig;
Expand Down Expand Up @@ -97,7 +95,6 @@ public function __construct(
protected ProviderManager $providerManager,
protected EventDispatcherInterface $dispatcher,
protected DistManager $distManager,
protected ProxyHttpDownloader $downloader,
) {
ErrorHandler::register();
}
Expand Down Expand Up @@ -137,12 +134,7 @@ public function update(IOInterface $io, Config $config, Package $package, Reposi
$flags |= $addFlags;
}

if ($repository instanceof ComposerProxyRepository) {
$versions = PacketonUtils::sort($repository->findPackages($package->getName()));
} else {
$versions = PacketonUtils::sort($repository->getPackages());
}

$versions = PacketonUtils::sort($repository->getPackages());
/** @var VersionRepository $versionRepository */
$versionRepository = $this->doctrine->getRepository(Version::class);
if (null === $rootIdentifier && ($probe = end($versions))) {
Expand Down Expand Up @@ -571,7 +563,7 @@ private function updateArchive(PackageInterface $data, Package $package): ?array
// Process local path repos
if (is_string($distUrl = $data->getDistUrl())
&& (str_starts_with($distUrl, '/') || $distUrl === DistConfig::HOSTNAME_PLACEHOLDER)
&& (empty($data->getSourceUrl()) || in_array($package->getRepoType(), [RepTypes::CUSTOM, RepTypes::VIRTUAL, RepTypes::PROXY], true))
&& (empty($data->getSourceUrl()) || in_array($package->getRepoType(), [RepTypes::CUSTOM, RepTypes::VIRTUAL], true))
) {
return [
'url' => $this->distConfig->generateRoute($data->getName(), $data->getDistReference(), $data->getDistType()),
Expand Down Expand Up @@ -602,38 +594,26 @@ private function updateArchive(PackageInterface $data, Package $package): ?array
];
}

$dist['type'] = $this->distConfig->getArchiveFormat();
$dist['url'] = $this->distConfig->generateRoute($data->getName(), $data->getSourceReference());
$dist['reference'] = $data->getSourceReference();
if ($package->getRepoType() === RepTypes::PROXY) {
$dist['reference'] = PacketonUtils::buildHashReference($data);
}

if ($this->distConfig->isPreBuild()) {
$path = null;
try {
$path = $this->distManager->buildAndWriteArchive(
$data->getSourceReference() ?: $data->getDistReference(),
$dist['reference'] ?? $data->getDistReference(),
$package,
$data->getVersion()
);
} catch (\Throwable $e) {
}

$dist['shasum'] = $this->distConfig->isIncludeArchiveChecksum() && is_string($path) && file_exists($path) ? \hash_file('sha1', $path) : null;
}

$dist['type'] = $this->distConfig->getArchiveFormat();

$url = $data->getSourceReference();
if (null === $url) {
$url = $data->getDistReference();
}

if (null === $url) {
$url = sha1($data->getVersion());
}

$dist['url'] = $this->distConfig->generateRoute($data->getName(), $url);
$dist['reference'] = $url;

if ($package->getRepoType() === RepTypes::PROXY) {
$dist['proxy_url'] = $data->getDistUrl();
}

return $dist;
}

Expand Down
38 changes: 26 additions & 12 deletions src/Service/DistManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@
use Composer\Package\CompletePackageInterface;
use Composer\Package\Loader\ArrayLoader;
use Composer\Package\PackageInterface;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Persistence\ManagerRegistry;
use League\Flysystem\FilesystemOperator;
use Packeton\Composer\PackagistFactory;
use Packeton\Composer\Repository\ComposerProxyRepository;
use Packeton\Composer\Repository\PacketonRepositoryInterface;
use Packeton\Entity\Package;
use Packeton\Entity\Version;
use Packeton\Integrations\IntegrationRegistry;
use Packeton\Integrations\ZipballInterface;
use Packeton\Model\ComposerProxyPackageManager;
use Packeton\Model\UploadZipballStorage;
use Packeton\Model\VirtualPackageManager;
use Packeton\Package\RepTypes;
use Packeton\Util\PacketonUtils;
use Symfony\Component\Filesystem\Filesystem;

class DistManager
Expand All @@ -37,7 +37,6 @@ public function __construct(
private readonly IntegrationRegistry $integrations,
private readonly FilesystemOperator $baseStorage,
private readonly Filesystem $fs,
private readonly ComposerProxyPackageManager $composerProxyPackageManager,
private readonly VirtualPackageManager $virtualPackageManager,
) {
}
Expand Down Expand Up @@ -78,6 +77,7 @@ public function buildArchive(string $reference, Package $package, Version|string
return match (true) {
$package->getRepoType() === RepTypes::INTEGRATION => $this->downloadUsingIntegration($reference, $package, $versionName),
RepTypes::isBuildInDist($package->getRepoType()) => $this->downloadArtifact($reference, $package),
$package->getRepoType() === RepTypes::PROXY => $this->downloadProxy($reference, $package, $versionName),
default => $this->downloadVCS($reference, $package, $versionName)
};
}
Expand Down Expand Up @@ -172,6 +172,27 @@ private function guessCompletePackage(string $reference, array $versions): ?Comp
return null;
}

private function downloadProxy(string $reference, Package $package, ?string $versionName): ?string
{
$repository = $this->createRepositoryAndIo($package);
$packages = new ArrayCollection($repository->getPackages());

$repoPackage = $packages->findFirst(fn ($k, PackageInterface $data) => PacketonUtils::buildHashReference($data) === $reference)
?? $packages->findFirst(fn ($k, PackageInterface $data) => $data->getSourceReference() === $reference);

if (null === $repoPackage) {
return null;
}

$archiveManager = $this->packagistFactory->createArchiveManager($repository->getIO(), $repository);
$archiveManager->setOverwriteFiles(false);

$targetDir = $this->config->generateTargetDir($package->getName());
$fileName = $this->config->getFileName($reference, $versionName ?: self::EMPTY_VERSION_NAME);

return $archiveManager->archive($repoPackage, $this->config->getArchiveFormat(), $targetDir, $fileName);
}

private function downloadArtifact(string $reference, Package $package): ?string
{
if ($package->getRepoType() === RepTypes::VIRTUAL) {
Expand All @@ -183,15 +204,8 @@ private function downloadArtifact(string $reference, Package $package): ?string
}

$repository = $this->createRepositoryAndIo($package);

if (!$repository instanceof ComposerProxyRepository) {
$packages = $repository->getPackages();
$found = array_filter($packages, static fn($p) => $reference === $p->getDistReference());
}

if ($package->getRepoType() === RepTypes::PROXY) {
return $this->composerProxyPackageManager->buildArchive($package, $repository, $reference);
}
$packages = $repository->getPackages();
$found = array_filter($packages, static fn($p) => $reference === $p->getDistReference());

/** @var PackageInterface $pkg */
if ($pkg = reset($found)) {
Expand Down
17 changes: 17 additions & 0 deletions src/Util/PacketonUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,23 @@

class PacketonUtils
{
public static function buildHashReference(PackageInterface $package): string
{
$reference = $package->getSourceReference();
if (preg_match('/^[a-f0-9]{40}$/', $reference)) {
return $reference;
}

$proxyDist = [
'type' => $package->getDistType(),
'reference' => $package->getDistReference() ?? $package->getSourceReference(),
'pretty_version' => $package->getPrettyVersion(),
'url' => $package->getDistUrl(),
];

return sha1(json_encode($proxyDist));
}


/**
* @param array $packages
Expand Down

0 comments on commit 412b00f

Please sign in to comment.