diff --git a/src/Composer/Command/ReplaceBuildCommand.php b/src/Composer/Command/ReplaceBuildCommand.php index da8514c..2cd3dda 100644 --- a/src/Composer/Command/ReplaceBuildCommand.php +++ b/src/Composer/Command/ReplaceBuildCommand.php @@ -25,6 +25,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int $output->writeln($message); } + $output->writeln('Your "composer.json" file has been updated. Remove the "composer.lock" file and "vendor/" folder and run "composer install" to rebuild your composer dependencies'); return Command::SUCCESS; } } diff --git a/src/Composer/Exception/EmptyBulkException.php b/src/Composer/Exception/EmptyBulkException.php new file mode 100644 index 0000000..3a6d0f2 --- /dev/null +++ b/src/Composer/Exception/EmptyBulkException.php @@ -0,0 +1,7 @@ +getComposerName()]; } - $client = new Client(); - $response = $client->get('https://repo.packagist.org/p2/'.$this->composerName.'.json'); - if ($response->getStatusCode() !== 200) { - throw new HttpClientException('Packagist call failed: '.$response->getReasonPhrase()); + $io = new BufferIO(); + $composerConfig = Factory::createConfig($io, getcwd()); + + $localComposerFile = getcwd().'/composer.json'; + if (file_exists($localComposerFile)) { + $localComposerConfig = json_decode(file_get_contents($localComposerFile), true); + $composerConfig->merge($localComposerConfig); } - $body = $response->getBody(); - $data = json_decode($body->getContents(), true); - if (empty($data) || empty($data['packages']) || empty($data['packages'][$this->composerName])) { - throw new PackageException('Unknown package "'.$this->composerName.'"'); + $httpDownloader = Factory::createHttpDownloader($io, $composerConfig); + $repositoryManager = RepositoryFactory::manager($io, $composerConfig, $httpDownloader); + + $composerRepositories = $composerConfig->getRepositories(); + $bulkReplacementPackage = null; + foreach ($composerRepositories as $composerRepository) { + //echo $composerRepository['url']."\n"; // @todo: Add debugging output + $repository = $repositoryManager->createRepository($composerRepository['type'], $composerRepository); + $bulkReplacementPackage = $repository->findPackage($this->composerName, '*'); + if ($bulkReplacementPackage instanceof BasePackage) { + break; + } } $collection = new ReplacementCollection; - if (empty($data['packages'][$this->composerName][0]['replace'])) { - return $collection; + if (false === $bulkReplacementPackage instanceof BasePackage) { + throw new EmptyBulkException('No bulk package found with name "'.$this->composerName.'"'); } - foreach ($data['packages'][$this->composerName][0]['replace'] as $package => $version) { - $collection->add(new Replacement($package, $version)); + foreach ($bulkReplacementPackage->getReplaces() as $replace) { + $collection->add(new Replacement($replace->getTarget(), $replace->getPrettyConstraint())); } $collections[$this->getComposerName()] = $collection; + //@todo echo $io->getOutput(); + return $collection; } } diff --git a/src/Composer/Service/ReplaceBuilder.php b/src/Composer/Service/ReplaceBuilder.php index 5020204..bd3f1a8 100644 --- a/src/Composer/Service/ReplaceBuilder.php +++ b/src/Composer/Service/ReplaceBuilder.php @@ -399,7 +399,7 @@ public function build(): array * @return array * @throws FilesystemException */ - private function readJsonData(): array + public function readJsonData(): array { return json_decode($this->filesystem->read($this->composerFile), true); } @@ -409,7 +409,7 @@ private function readJsonData(): array * @return void * @throws FilesystemException */ - private function writeJsonData(array $jsonData) + public function writeJsonData(array $jsonData) { $contents = json_encode($jsonData, JSON_PRETTY_PRINT + JSON_UNESCAPED_SLASHES); $this->filesystem->write($this->composerFile, $contents);