From c9c0ea7c7ba55002f7d7fc44ce1b669ed3608b5f Mon Sep 17 00:00:00 2001 From: Uladzimir Tsykun Date: Sat, 2 Mar 2024 15:00:18 +0100 Subject: [PATCH] Allow archive a package to make it not visible for composer --- src/Entity/PackageSerializedTrait.php | 13 +++++++++++++ src/Form/Type/Package/SettingsPackageType.php | 4 ++++ src/Package/InMemoryDumper.php | 8 ++++++-- src/Repository/PackageRepository.php | 17 ++++++++++++----- templates/package/viewPackage.html.twig | 6 ++++++ 5 files changed, 41 insertions(+), 7 deletions(-) diff --git a/src/Entity/PackageSerializedTrait.php b/src/Entity/PackageSerializedTrait.php index 5a25036d..d0745ac6 100644 --- a/src/Entity/PackageSerializedTrait.php +++ b/src/Entity/PackageSerializedTrait.php @@ -208,4 +208,17 @@ public function setDisabledUpdate(?bool $flag): void { $this->setSerialized('disabled_update', $flag); } + + public function isArchived(): bool + { + return (bool) ($this->serializedFields['archived'] ?? false); + } + + public function setArchived(?bool $flag): void + { + $this->setSerialized('archived', $flag); + if (true === $flag) { + $this->setAbandoned(true); + } + } } diff --git a/src/Form/Type/Package/SettingsPackageType.php b/src/Form/Type/Package/SettingsPackageType.php index 43159ecb..ac1299c1 100644 --- a/src/Form/Type/Package/SettingsPackageType.php +++ b/src/Form/Type/Package/SettingsPackageType.php @@ -32,6 +32,10 @@ public function buildForm(FormBuilderInterface $builder, array $options): void ->add('fullVisibility', CheckboxType::class, [ 'required' => false, 'label' => 'Visible for all users', + ]) + ->add('archived', CheckboxType::class, [ + 'required' => false, + 'label' => 'Mark package as archived', ]); if ($options['has_active_integration']) { diff --git a/src/Package/InMemoryDumper.php b/src/Package/InMemoryDumper.php index 3477beb7..86dae8dd 100644 --- a/src/Package/InMemoryDumper.php +++ b/src/Package/InMemoryDumper.php @@ -58,7 +58,7 @@ public function dumpPackage(?UserInterface $user, $package, array $versionData = $package = $this->getPackageRepo()->findOneByName($package); } - if (!$package instanceof Package) { + if (!$package instanceof Package || $package->isArchived()) { return []; } @@ -143,6 +143,8 @@ private function dumpUserPackages(UserInterface $user = null, int $apiVersion = $availablePackages = $this->getPackageRepo()->getPackageNames($allowed); $availablePackages = $subRepo ? $subRepo->filterAllowed($availablePackages) : $availablePackages; + $availablePackages = $this->getPackageRepo()->filterByJson($availablePackages, static fn($data) => !($data['archived'] ?? false)); + return [null, [], $availablePackages]; } @@ -151,10 +153,12 @@ private function dumpUserPackages(UserInterface $user = null, int $apiVersion = ->getAllowedPackagesForUser($user) : $this->getPackageRepo()->findAll(); + $packages = array_values(array_filter($packages, static fn(Package $pkg) => !$pkg->isArchived())); + $providers = $packagesData = []; $versionData = $this->getVersionData($packages); - $availablePackages = array_map(fn(Package $pkg) => $pkg->getName(), $packages); + $availablePackages = array_map(static fn(Package $pkg) => $pkg->getName(), $packages); $availablePackages = $subRepo ? $subRepo->filterAllowed($availablePackages) : $availablePackages; $keys = array_flip($availablePackages); diff --git a/src/Repository/PackageRepository.php b/src/Repository/PackageRepository.php index b8a221d5..5e64fcf1 100644 --- a/src/Repository/PackageRepository.php +++ b/src/Repository/PackageRepository.php @@ -194,11 +194,18 @@ public function filterByJson(array $packagesIds, callable $filter): array return []; } - $packages = $this->getConn()->fetchAllAssociative( - "SELECT p.id, p.serialized_data FROM package p WHERE p.id IN (:ids)", - ['ids' => $packagesIds], - ['ids' => ArrayParameterType::INTEGER] - ); + $needUseIntegerIdentifier = is_numeric(reset($packagesIds)); + $packages = $needUseIntegerIdentifier ? + $this->getConn()->fetchAllAssociative( + "SELECT p.id, p.serialized_data FROM package p WHERE p.id IN (:ids)", + ['ids' => $packagesIds], + ['ids' => ArrayParameterType::INTEGER] + ) : + $this->getConn()->fetchAllAssociative( + "SELECT p.name as id, p.serialized_data FROM package p WHERE p.name IN (:ids)", + ['ids' => $packagesIds], + ['ids' => ArrayParameterType::STRING] + ); $packages = PacketonUtils::buildChoices($packages, 'id', 'serialized_data'); foreach ($packagesIds as $i => $packageId) { diff --git a/templates/package/viewPackage.html.twig b/templates/package/viewPackage.html.twig index 6d366f99..4b84b98c 100644 --- a/templates/package/viewPackage.html.twig +++ b/templates/package/viewPackage.html.twig @@ -70,6 +70,12 @@ {% endif %} {% endif %} + {% if package.archived %} +
+ This package is ARCHIVED and will not be available in metadata. +
+ {% endif %} + {% if package.abandoned %}
This package is abandoned and no longer maintained.