Skip to content

Commit

Permalink
Allow bulks to be searched from anywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
jissereitsma committed Oct 18, 2023
1 parent 480944d commit b827c75
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 14 deletions.
1 change: 1 addition & 0 deletions src/Composer/Command/ReplaceBuildCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
7 changes: 7 additions & 0 deletions src/Composer/Exception/EmptyBulkException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php declare(strict_types=1);

namespace Yireo\ReplaceTools\Composer\Exception;

use Exception;

class EmptyBulkException extends Exception {}
45 changes: 33 additions & 12 deletions src/Composer/Model/BulkReplacement.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,16 @@

namespace Yireo\ReplaceTools\Composer\Model;

use Composer\Factory;
use Composer\IO\BufferIO;
use Composer\IO\NullIO;
use Composer\Package\BasePackage;
use Composer\Repository\RepositoryFactory;
use Composer\Repository\RepositoryManager;
use Composer\Util\HttpDownloader;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use Yireo\ReplaceTools\Composer\Exception\EmptyBulkException;
use Yireo\ReplaceTools\Composer\Exception\HttpClientException;
use Yireo\ReplaceTools\Composer\Exception\PackageException;

Expand Down Expand Up @@ -73,28 +81,41 @@ public function fetch(): ReplacementCollection
return $collections[$this->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;
}
}
4 changes: 2 additions & 2 deletions src/Composer/Service/ReplaceBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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);
Expand Down

0 comments on commit b827c75

Please sign in to comment.