From 2744418e7c361972d6e872b4b0003e021e7807b6 Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Mon, 6 Jun 2022 10:19:02 +0200 Subject: [PATCH 1/2] Fix PHP warning when using Composer 1 --- src/Flex.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Flex.php b/src/Flex.php index cdb52c8be..94d37e696 100644 --- a/src/Flex.php +++ b/src/Flex.php @@ -304,7 +304,7 @@ public function configureInstaller() $this->installer = $trace['object']->setSuggestedPackagesReporter(new SuggestedPackagesReporter(new NullIO())); $updateAllowList = \Closure::bind(function () { - return $this->updateWhitelist ?? $this->updateAllowList; + return $this->updateWhitelist ?? $this->updateAllowList ?? null; }, $this->installer, $this->installer)(); if (['php' => 0] === $updateAllowList) { From 9c612796a68de4196fff4bc159db5071aa62d428 Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Mon, 6 Jun 2022 15:07:15 +0200 Subject: [PATCH 2/2] Read from symfony.lock when checking for recipe updates --- src/Command/RecipesCommand.php | 17 ++++++++++---- src/Command/UpdateRecipesCommand.php | 34 +++++++++++----------------- src/Downloader.php | 1 - src/Flex.php | 12 ++++++---- 4 files changed, 32 insertions(+), 32 deletions(-) diff --git a/src/Command/RecipesCommand.php b/src/Command/RecipesCommand.php index 2aaf88e31..022fe52e5 100644 --- a/src/Command/RecipesCommand.php +++ b/src/Command/RecipesCommand.php @@ -13,6 +13,7 @@ use Composer\Command\BaseCommand; use Composer\Downloader\TransportException; +use Composer\Package\Package; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; @@ -61,19 +62,25 @@ protected function execute(InputInterface $input, OutputInterface $output) // Inspect one or all packages $package = $input->getArgument('package'); if (null !== $package) { - $packages = [0 => ['name' => strtolower($package)]]; + $packages = [strtolower($package)]; } else { $locker = $this->getComposer()->getLocker(); $lockData = $locker->getLockData(); // Merge all packages installed - $packages = array_merge($lockData['packages'], $lockData['packages-dev']); + $packages = array_column(array_merge($lockData['packages'], $lockData['packages-dev']), 'name'); + $packages = array_unique(array_merge($packages, array_keys($this->symfonyLock->all()))); } $operations = []; - foreach ($packages as $value) { - if (null === $pkg = $installedRepo->findPackage($value['name'], '*')) { - $this->getIO()->writeError(sprintf('Package %s is not installed', $value['name'])); + foreach ($packages as $name) { + $pkg = $installedRepo->findPackage($name, '*'); + + if (!$pkg && $this->symfonyLock->has($name)) { + $pkgVersion = $this->symfonyLock->get($name)['version']; + $pkg = new Package($name, $pkgVersion, $pkgVersion); + } elseif (!$pkg) { + $this->getIO()->writeError(sprintf('Package %s is not installed', $name)); continue; } diff --git a/src/Command/UpdateRecipesCommand.php b/src/Command/UpdateRecipesCommand.php index 9a663cc7c..942aca35c 100644 --- a/src/Command/UpdateRecipesCommand.php +++ b/src/Command/UpdateRecipesCommand.php @@ -13,6 +13,7 @@ use Composer\Command\BaseCommand; use Composer\IO\IOInterface; +use Composer\Package\Package; use Composer\Util\ProcessExecutor; use Symfony\Component\Console\Exception\RuntimeException; use Symfony\Component\Console\Formatter\OutputFormatterStyle; @@ -122,7 +123,9 @@ protected function execute(InputInterface $input, OutputInterface $output): int return 1; } - $originalRecipe = $this->getRecipe($packageName, $recipeRef, $recipeVersion); + $installedRepo = $this->getComposer()->getRepositoryManager()->getLocalRepository(); + $package = $installedRepo->findPackage($packageName, '*') ?? new Package($packageName, $packageLockData['version'], $packageLockData['version']); + $originalRecipe = $this->getRecipe($package, $recipeRef, $recipeVersion); if (null === $originalRecipe) { $io->writeError([ @@ -134,7 +137,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int return 1; } - $newRecipe = $this->getRecipe($packageName); + $newRecipe = $this->getRecipe($package); if ($newRecipe->getRef() === $originalRecipe->getRef()) { $io->write(sprintf('This recipe for %s is already at the latest version.', $packageName)); @@ -259,13 +262,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int return 0; } - private function getRecipe(string $packageName, string $recipeRef = null, string $recipeVersion = null): ?Recipe + private function getRecipe(Package $package, string $recipeRef = null, string $recipeVersion = null): ?Recipe { - $installedRepo = $this->getComposer()->getRepositoryManager()->getLocalRepository(); - $package = $installedRepo->findPackage($packageName, '*'); - if (null === $package) { - throw new RuntimeException(sprintf('Could not find package "%s". Try running "composer install".', $packageName)); - } $operation = new InformationOperation($package); if (null !== $recipeRef) { $operation->setSpecificRecipeVersion($recipeRef, $recipeVersion); @@ -278,10 +276,10 @@ private function getRecipe(string $packageName, string $recipeRef = null, string return new Recipe( $package, - $packageName, + $package->getName(), $operation->getOperationType(), - $recipes['manifests'][$packageName], - $recipes['locks'][$packageName] ?? [] + $recipes['manifests'][$package->getName()], + $recipes['locks'][$package->getName()] ?? [] ); } @@ -358,19 +356,13 @@ private function generateChangelog(Recipe $originalRecipe): ?array private function askForPackage(IOInterface $io, Lock $symfonyLock): ?string { $installedRepo = $this->getComposer()->getRepositoryManager()->getLocalRepository(); - $locker = $this->getComposer()->getLocker(); - $lockData = $locker->getLockData(); - - // Merge all packages installed - $packages = array_merge($lockData['packages'], $lockData['packages-dev']); $operations = []; - foreach ($packages as $value) { - if (null === $pkg = $installedRepo->findPackage($value['name'], '*')) { - continue; + foreach ($symfonyLock->all() as $name => $lock) { + if (isset($lock['recipe']['ref'])) { + $package = $installedRepo->findPackage($name, '*') ?? new Package($name, $lock['version'], $lock['version']); + $operations[] = new InformationOperation($package); } - - $operations[] = new InformationOperation($pkg); } $recipes = $this->flex->fetchRecipes($operations, false); diff --git a/src/Downloader.php b/src/Downloader.php index 4e2195a52..d03f6b8b4 100644 --- a/src/Downloader.php +++ b/src/Downloader.php @@ -245,7 +245,6 @@ public function getRecipes(array $operations): array } if (null !== $this->endpoints) { - $data['locks'][$package->getName()]['version'] = $version; continue; } diff --git a/src/Flex.php b/src/Flex.php index 94d37e696..3a861970b 100644 --- a/src/Flex.php +++ b/src/Flex.php @@ -382,7 +382,7 @@ public function recordOperations(InstallerEvent $event) }, null, Transaction::class)(); foreach ($transation->getOperations() as $operation) { - if ($this->shouldRecordOperation($operation, $event->isDevMode(), $event->getComposer())) { + if (!$operation instanceof UninstallOperation && $this->shouldRecordOperation($operation, $event->isDevMode(), $event->getComposer())) { $this->operations[] = $operation; } } @@ -822,9 +822,7 @@ public function fetchRecipes(array $operations, bool $reset): array } else { $recipes[$name] = $recipe; } - } - - if (!isset($manifests[$name])) { + } else { $bundles = []; if (null === $devPackages) { @@ -842,6 +840,10 @@ public function fetchRecipes(array $operations, bool $reset): array 'manifest' => ['bundles' => $bundles], ]; $recipes[$name] = new Recipe($package, $name, $job, $manifest); + + if ($operation instanceof InstallOperation) { + $this->lock->set($name, ['version' => $package->getPrettyVersion()]); + } } } } @@ -1058,6 +1060,7 @@ public static function getSubscribedEvents(): array } $events = [ + PackageEvents::POST_PACKAGE_UNINSTALL => 'record', ScriptEvents::POST_CREATE_PROJECT_CMD => 'configureProject', ScriptEvents::POST_INSTALL_CMD => 'install', ScriptEvents::PRE_UPDATE_CMD => 'configureInstaller', @@ -1069,7 +1072,6 @@ public static function getSubscribedEvents(): array $events += [ PackageEvents::POST_PACKAGE_INSTALL => [['recordFlexInstall'], ['record']], PackageEvents::POST_PACKAGE_UPDATE => [['record'], ['enableThanksReminder']], - PackageEvents::POST_PACKAGE_UNINSTALL => 'record', InstallerEvents::PRE_DEPENDENCIES_SOLVING => [['populateProvidersCacheDir', \PHP_INT_MAX]], InstallerEvents::POST_DEPENDENCIES_SOLVING => [['populateFilesCacheDir', \PHP_INT_MAX]], PackageEvents::PRE_PACKAGE_INSTALL => [['populateFilesCacheDir', ~\PHP_INT_MAX]],