Skip to content

Commit

Permalink
Restore compat with Composer 1
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas-grekas committed Jun 13, 2020
1 parent 186f05e commit 6d1e73c
Show file tree
Hide file tree
Showing 14 changed files with 1,237 additions and 40 deletions.
5 changes: 3 additions & 2 deletions .travis.yml
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 @@ -24,12 +24,13 @@ env:

before_install:
- ([[ $TRAVIS_PHP_VERSION = nightly ]] && composer config platform.php 7.4.99 || true)
- composer self-update --snapshot
- composer validate

install:
- composer update $COMPOSER_FLAGS

script:
- ./vendor/bin/simple-phpunit
- composer self-update --snapshot
- ./vendor/bin/simple-phpunit
- find src/ -name '*.php' | xargs -n1 php -l
8 changes: 4 additions & 4 deletions composer.json
Expand Up @@ -12,13 +12,13 @@
"minimum-stability": "dev",
"require": {
"php": ">=7.1",
"composer-plugin-api": "^2.0"
"composer-plugin-api": "^1.0|^2.0"
},
"require-dev": {
"composer/composer": "^2.0@dev",
"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": "2.0-dev"
"dev-master": "1.8-dev"
},
"class": "Symfony\\Flex\\Flex"
}
Expand Down
147 changes: 147 additions & 0 deletions src/Cache.php
@@ -0,0 +1,147 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Flex;

use Composer\Cache as BaseCache;
use Composer\IO\IOInterface;
use Composer\Semver\Constraint\Constraint;
use Composer\Semver\VersionParser;

/**
* @author Nicolas Grekas <p@tchwork.com>
*/
class Cache extends BaseCache
{
private $versions;
private $versionParser;
private $symfonyRequire;
private $symfonyConstraints;
private $downloader;
private $io;

public function setSymfonyRequire(string $symfonyRequire, Downloader $downloader, IOInterface $io = null)
{
$this->versionParser = new VersionParser();
$this->symfonyRequire = $symfonyRequire;
$this->symfonyConstraints = $this->versionParser->parseConstraints($symfonyRequire);
$this->downloader = $downloader;
$this->io = $io;
}

public function read($file)
{
$content = parent::read($file);

if (0 === strpos($file, 'provider-symfony$') && \is_array($data = json_decode($content, true))) {
$content = json_encode($this->removeLegacyTags($data));
}

return $content;
}

public function removeLegacyTags(array $data): array
{
if (!$this->symfonyConstraints || !isset($data['packages'])) {
return $data;
}

foreach ($data['packages'] as $name => $versions) {
if (!isset($this->getVersions()['splits'][$name])) {
continue;
}

foreach ($versions as $version => $composerJson) {
if ('dev-master' === $version) {
if (null === $devMasterAlias = $versions['dev-master']['extra']['branch-alias']['dev-master'] ?? null) {
continue;
}

$normalizedVersion = $this->versionParser->normalize($devMasterAlias);
} elseif (!isset($composerJson['version_normalized'])) {
continue;
} else {
$normalizedVersion = $composerJson['version_normalized'];
}

if (!$this->symfonyConstraints->matches(new Constraint('==', $normalizedVersion))) {
if (null !== $this->io) {
$this->io->writeError(sprintf('<info>Restricting packages listed in "symfony/symfony" to "%s"</>', $this->symfonyRequire));
$this->io = null;
}
unset($versions[$version]);
}
}

$data['packages'][$name] = $versions;
}

if (null === $symfonySymfony = $data['packages']['symfony/symfony'] ?? null) {
return $data;
}

foreach ($symfonySymfony as $version => $composerJson) {
if ('dev-master' === $version) {
$normalizedVersion = $this->versionParser->normalize($composerJson['extra']['branch-alias']['dev-master']);
} elseif (!isset($composerJson['version_normalized'])) {
continue;
} else {
$normalizedVersion = $composerJson['version_normalized'];
}

if (!$this->symfonyConstraints->matches(new Constraint('==', $normalizedVersion))) {
unset($symfonySymfony[$version]);
}
}

if ($symfonySymfony) {
$data['packages']['symfony/symfony'] = $symfonySymfony;
}

return $data;
}

private function getVersions(): array
{
if (null !== $this->versions) {
return $this->versions;
}

$versions = $this->downloader->getVersions();
$this->downloader = null;
$okVersions = [];

foreach ($versions['splits'] as $name => $vers) {
foreach ($vers as $i => $v) {
if (!isset($okVersions[$v])) {
$okVersions[$v] = false;

for ($j = 0; $j < 60; ++$j) {
if ($this->symfonyConstraints->matches(new Constraint('==', $v.'.'.$j.'.0'))) {
$okVersions[$v] = true;
break;
}
}
}

if (!$okVersions[$v]) {
unset($vers[$i]);
}
}

if (!$vers || $vers === $versions['splits'][$name]) {
unset($versions['splits'][$name]);
}
}

return $this->versions = $versions;
}
}
10 changes: 7 additions & 3 deletions src/Command/RecipesCommand.php
Expand Up @@ -32,7 +32,7 @@ class RecipesCommand extends BaseCommand
private $symfonyLock;
private $downloader;

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

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

return json_decode($response->getBody(), true);
return json_decode($contents, true);
}
}
10 changes: 9 additions & 1 deletion src/Command/UnpackCommand.php
Expand Up @@ -114,7 +114,11 @@ protected function execute(InputInterface $input, OutputInterface $output)
$lockFile->write($lockData);

// force removal of files under vendor/
$locker = new Locker($io, $lockFile, $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
Expand All @@ -124,6 +128,10 @@ protected function execute(InputInterface $input, OutputInterface $output)
->setIgnorePlatformRequirements(true)
;

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

return $install->run();
}
}
58 changes: 58 additions & 0 deletions src/ComposerRepository.php
@@ -0,0 +1,58 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Flex;

use Composer\Repository\ComposerRepository as BaseComposerRepository;

/**
* @author Nicolas Grekas <p@tchwork.com>
*/
class ComposerRepository extends BaseComposerRepository
{
private $providerFiles;

protected function loadProviderListings($data)
{
if (null !== $this->providerFiles) {
parent::loadProviderListings($data);

return;
}

$data = [$data];

while ($data) {
$this->providerFiles = [];
foreach ($data as $data) {
$this->loadProviderListings($data);
}

$loadingFiles = $this->providerFiles;
$this->providerFiles = null;
$data = [];
$this->rfs->download($loadingFiles, function (...$args) use (&$data) {
$data[] = $this->fetchFile(...$args);
});
}
}

protected function fetchFile($filename, $cacheKey = null, $sha256 = null, $storeLastModifiedTime = false)
{
if (null !== $this->providerFiles) {
$this->providerFiles[] = [$filename, $cacheKey, $sha256, $storeLastModifiedTime];

return [];
}

return parent::fetchFile($filename, $cacheKey, $sha256, $storeLastModifiedTime);
}
}

0 comments on commit 6d1e73c

Please sign in to comment.