diff --git a/src/Composer/Repository/ComposerProxyRepository.php b/src/Composer/Repository/ComposerProxyRepository.php index 3657c7d0..80eb09be 100644 --- a/src/Composer/Repository/ComposerProxyRepository.php +++ b/src/Composer/Repository/ComposerProxyRepository.php @@ -12,6 +12,8 @@ class ComposerProxyRepository extends ComposerRepository implements PacketonRepositoryInterface { + private string $packageName; + public function __construct( protected array $repoConfig, protected IOInterface $io, @@ -21,6 +23,7 @@ public function __construct( ) { parent::__construct($repoConfig, $io, $config, $httpDownloader); + $this->packageName = $this->repoConfig['packageName']; $this->process ??= new ProcessExecutor($this->io); } @@ -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; diff --git a/src/Filter/VersionFilter.php b/src/Filter/VersionFilter.php deleted file mode 100644 index 4b9639d7..00000000 --- a/src/Filter/VersionFilter.php +++ /dev/null @@ -1,25 +0,0 @@ -getName() === $repoName) { - $result[] = $version; - } - } - return $result; - } -} \ No newline at end of file diff --git a/src/Form/Type/Package/ProxyPackageType.php b/src/Form/Type/Package/ProxyPackageType.php index 71d6a540..79d0d9fa 100644 --- a/src/Form/Type/Package/ProxyPackageType.php +++ b/src/Form/Type/Package/ProxyPackageType.php @@ -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', ], ]); diff --git a/src/Model/ComposerProxyPackageManager.php b/src/Model/ComposerProxyPackageManager.php deleted file mode 100644 index 04341d9d..00000000 --- a/src/Model/ComposerProxyPackageManager.php +++ /dev/null @@ -1,58 +0,0 @@ -getVersionByReference($reference) ?: $package->getVersions()->first(); - if (null === $version) { - throw new \RuntimeException("Not found any versions for reference '$reference' of package '{$package->getName()}'"); - } - - $keyName = $this->config->buildName($package->getName(), $version->getReference(), $version->getVersion()); - $cachedName = $this->config->resolvePath($keyName); - if (file_exists($cachedName)) { - return $cachedName; - } - - $selected = []; - $serialized = $package->getCustomVersions(); - foreach ($serialized as $data) { - $verName = $data['version'] ?? null; - if ($verName === $version->getVersion() || $verName === $version->getNormalizedVersion()) { - $selected = $data['definition'] ?? []; - $selected['version'] = $version->getVersion(); - } - } - - $selected['name'] = $package->getName(); - $dir = dirname($cachedName); - if (!is_dir($dir)) { - @mkdir($dir, 0777, true); - } - - $url = $package->getVersionByReference($reference)->getDist()['proxy_url']; - $response = $repository->getHttpDownloader()->get($url); - $body = (string) $response->getBody(); - - $this->baseStorage->write($keyName, $body); - - return $cachedName; - } - -} diff --git a/src/Package/RepTypes.php b/src/Package/RepTypes.php index bf8f19da..b891d67d 100644 --- a/src/Package/RepTypes.php +++ b/src/Package/RepTypes.php @@ -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, }; } diff --git a/src/Package/Updater.php b/src/Package/Updater.php index 925f286e..5c6dc767 100644 --- a/src/Package/Updater.php +++ b/src/Package/Updater.php @@ -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; @@ -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; @@ -97,7 +95,6 @@ public function __construct( protected ProviderManager $providerManager, protected EventDispatcherInterface $dispatcher, protected DistManager $distManager, - protected ProxyHttpDownloader $downloader, ) { ErrorHandler::register(); } @@ -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))) { @@ -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()), @@ -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; } diff --git a/src/Service/DistManager.php b/src/Service/DistManager.php index 5b544e30..fd5b62df 100644 --- a/src/Service/DistManager.php +++ b/src/Service/DistManager.php @@ -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 @@ -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, ) { } @@ -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) }; } @@ -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) { @@ -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)) { diff --git a/src/Util/PacketonUtils.php b/src/Util/PacketonUtils.php index ab030d31..10a43159 100644 --- a/src/Util/PacketonUtils.php +++ b/src/Util/PacketonUtils.php @@ -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