Skip to content

Add support for composer 2 #617

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ sudo: false
matrix:
include:
- php: 7.1
env: COMPOSER_FLAGS='--prefer-lowest'
env: COMPOSER_FLAGS='--prefer-lowest --prefer-stable'
- php: 7.2
- php: 7.3
- php: 7.4
Expand All @@ -27,8 +27,13 @@ before_install:
- composer validate

install:
- composer require --no-update composer/composer:^1.0.2
- composer update $COMPOSER_FLAGS

script:
- ./vendor/bin/simple-phpunit
- composer self-update --snapshot
- composer require --no-update composer/composer:^2
- composer update $COMPOSER_FLAGS
- ./vendor/bin/simple-phpunit
- find src/ -name '*.php' | xargs -n1 php -l
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@
"minimum-stability": "dev",
"require": {
"php": ">=7.1",
"composer-plugin-api": "^1.0"
"composer-plugin-api": "^1.0|^2.0"
},
"require-dev": {
"composer/composer": "^1.0.2",
"composer/composer": "^1.0.2|^2.0",
"symfony/dotenv": "^4.4|^5.0",
"symfony/phpunit-bridge": "^4.4|^5.0",
"symfony/process": "^4.4|^5.0"
"symfony/process": "^3.4|^4.4|^5.0"
},
"autoload": {
"psr-4": {
Expand All @@ -27,7 +27,7 @@
},
"extra": {
"branch-alias": {
"dev-master": "1.7-dev"
"dev-master": "1.8-dev"
},
"class": "Symfony\\Flex\\Flex"
}
Expand Down
10 changes: 7 additions & 3 deletions src/Command/RecipesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@

use Composer\Command\BaseCommand;
use Composer\Downloader\TransportException;
use Composer\Util\HttpDownloader;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Flex\InformationOperation;
use Symfony\Flex\Lock;
use Symfony\Flex\ParallelDownloader;
use Symfony\Flex\Recipe;

/**
Expand All @@ -32,7 +32,7 @@ class RecipesCommand extends BaseCommand
private $symfonyLock;
private $downloader;

public function __construct(/* cannot be type-hinted */ $flex, Lock $symfonyLock, ParallelDownloader $downloader)
public function __construct(/* cannot be type-hinted */ $flex, Lock $symfonyLock, $downloader)
{
$this->flex = $flex;
$this->symfonyLock = $symfonyLock;
Expand Down Expand Up @@ -354,7 +354,11 @@ private function findRecipeCommitDataFromTreeRef(string $package, string $repo,

private function requestGitHubApi(string $path)
{
$contents = $this->downloader->getContents('api.github.com', $path, false);
if ($this->downloader instanceof HttpDownloader) {
$contents = $this->downloader->get($path)->getBody();
} else {
$contents = $this->downloader->getContents('api.github.com', $path, false);
}

return json_decode($contents, true);
}
Expand Down
11 changes: 9 additions & 2 deletions src/Command/UnpackCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,17 +114,24 @@ protected function execute(InputInterface $input, OutputInterface $output)
$lockFile->write($lockData);

// force removal of files under vendor/
$locker = new Locker($io, $lockFile, $composer->getRepositoryManager(), $composer->getInstallationManager(), file_get_contents($json->getPath()));
if (version_compare('2.0.0', PluginInterface::PLUGIN_API_VERSION, '>')) {
$locker = new Locker($io, $lockFile, $composer->getRepositoryManager(), $composer->getInstallationManager(), file_get_contents($json->getPath()));
} else {
$locker = new Locker($io, $lockFile, $composer->getInstallationManager(), file_get_contents($json->getPath()));
}
$composer->setLocker($locker);
$install = Installer::create($io, $composer);
$install
->setDevMode(true)
->setDumpAutoloader(false)
->setRunScripts(false)
->setSkipSuggest(true)
->setIgnorePlatformRequirements(true)
;

if (method_exists($install, 'setSkipSuggest')) {
$install->setSkipSuggest(true);
}

return $install->run();
}
}
52 changes: 42 additions & 10 deletions src/Downloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
use Composer\Downloader\TransportException;
use Composer\IO\IOInterface;
use Composer\Json\JsonFile;
use Composer\Util\HttpDownloader;
use function React\Promise\all;

/**
* @author Fabien Potencier <[email protected]>
Expand All @@ -39,7 +41,7 @@ class Downloader
private $flexId;
private $enabled = true;

public function __construct(Composer $composer, IoInterface $io, ParallelDownloader $rfs)
public function __construct(Composer $composer, IoInterface $io, $rfs)
{
if (getenv('SYMFONY_CAFILE')) {
$this->caFile = getenv('SYMFONY_CAFILE');
Expand Down Expand Up @@ -85,6 +87,10 @@ public function getVersions()
*/
public function getRecipes(array $operations): array
{
if ($this->enabled && self::$DEFAULT_ENDPOINT !== $this->endpoint) {
$this->io->writeError('<warning>Using "'.$this->endpoint.'" as the Symfony endpoint</>');
}

$paths = [];
$chunk = '';
foreach ($operations as $i => $operation) {
Expand Down Expand Up @@ -129,16 +135,27 @@ public function getRecipes(array $operations): array
$paths[] = ['/p/'.$chunk];
}

if ($this->enabled && self::$DEFAULT_ENDPOINT !== $this->endpoint) {
$this->io->writeError('<warning>Using "'.$this->endpoint.'" as the Symfony endpoint</>');
}

$bodies = [];
$this->rfs->download($paths, function ($path) use (&$bodies) {
if ($body = $this->get($path, [], false)->getBody()) {
$bodies[] = $body;
if ($this->rfs instanceof HttpDownloader) {
$jobs = [];
foreach ($paths as $path) {
$this->rfs->add($this->endpoint.$path[0]);
}
});
$this->rfs->wait();
$bodies = [];
all($jobs)->then(static function (array $responses) use (&$bodies) {
foreach ($responses as $response) {
$bodies[] = json_decode($response->getBody(), true);
}
}, function (\Exception $e) {
$this->io->writeError('<warning>Failed to download recipes: '.$e->getMessage().'</>');
});
} else {
$this->rfs->download($paths, function ($path) use (&$bodies) {
if ($body = $this->get($path, [], false)->getBody()) {
$bodies[] = $body;
}
});
}

$data = [];
foreach ($bodies as $body) {
Expand Down Expand Up @@ -189,6 +206,12 @@ private function fetchFile(string $url, string $cacheKey, array $headers): Respo
$retries = 3;
while ($retries--) {
try {
if ($this->rfs instanceof HttpDownloader) {
$response = $this->rfs->get($url, $options);

return $this->parseJson($response->getBody(), $url, $cacheKey, $response->getHeaders());
}

$json = $this->rfs->getContents($this->endpoint, $url, false, $options);

return $this->parseJson($json, $url, $cacheKey, $this->rfs->getLastHeaders());
Expand Down Expand Up @@ -220,6 +243,15 @@ private function fetchFileIfLastModified(string $url, string $cacheKey, string $
$retries = 3;
while ($retries--) {
try {
if ($this->rfs instanceof HttpDownloader) {
$response = $this->rfs->get($url, $options);
if (304 === $response->getStatusCode()) {
return new Response($response->getBody(), $response->getHeaders(), 304);
}

return $this->parseJson($response->getBody(), $url, $cacheKey, $response->getHeaders());
}

$json = $this->rfs->getContents($this->endpoint, $url, false, $options);
if (304 === $this->rfs->findStatusCode($this->rfs->getLastHeaders())) {
return new Response('', $this->rfs->getLastHeaders(), 304);
Expand Down
Loading