Skip to content

Commit

Permalink
Merge pull request #6 from nicolas-grekas/sync-upstream
Browse files Browse the repository at this point in the history
Sync with v1.10 of upstream package
  • Loading branch information
Seldaek authored Jul 15, 2020
2 parents 7a8001f + 1778079 commit dd51b44
Show file tree
Hide file tree
Showing 10 changed files with 294 additions and 39 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"composer-plugin-api": "^1.1.0 || ^2.0"
},
"replace": {
"ocramius/package-versions": "1.8.99"
"ocramius/package-versions": "1.10.99"
},
"require-dev": {
"phpunit/phpunit": "^6.5 || ^7",
Expand Down
73 changes: 55 additions & 18 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions src/PackageVersions/FallbackVersions.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ private function __construct()
* @throws OutOfBoundsException If a version cannot be located.
* @throws UnexpectedValueException If the composer.lock file could not be located.
*/
public static function getVersion(string $packageName) : string
public static function getVersion(string $packageName): string
{
$versions = iterator_to_array(self::getVersions(self::getPackageData()));

Expand All @@ -56,7 +56,7 @@ public static function getVersion(string $packageName) : string
*
* @throws UnexpectedValueException
*/
private static function getPackageData() : array
private static function getPackageData(): array
{
$checkedPaths = [
// The top-level project's ./vendor/composer/installed.json
Expand Down Expand Up @@ -115,7 +115,7 @@ private static function getPackageData() : array
*
* @psalm-return Generator<string, string>
*/
private static function getVersions(array $packageData) : Generator
private static function getVersions(array $packageData): Generator
{
foreach ($packageData as $package) {
yield $package['name'] => $package['version'] . '@' . (
Expand Down
47 changes: 39 additions & 8 deletions src/PackageVersions/Installer.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@
use Composer\Script\ScriptEvents;
use Generator;
use RuntimeException;

use function array_key_exists;
use function array_merge;
use function chmod;
use function dirname;
use function file_exists;
use function file_put_contents;
use function is_writable;
use function iterator_to_array;
use function rename;
use function sprintf;
Expand All @@ -38,6 +40,7 @@ final class Installer implements PluginInterface, EventSubscriberInterface
namespace PackageVersions;
use Composer\InstalledVersions;
use OutOfBoundsException;
/**
Expand All @@ -50,7 +53,14 @@ final class Installer implements PluginInterface, EventSubscriberInterface
*/
%s
{
/**
* @deprecated please use {@see \Composer\InstalledVersions::getRootPackage()} instead. The
* equivalent expression for this constant's contents is
* `\Composer\InstalledVersions::getRootPackage()['name']`.
* This constant will be removed in version 2.0.0.
*/
const ROOT_PACKAGE_NAME = '%s';
/**
* Array of all available composer packages.
* Dont read this array from your calling code, but use the \PackageVersions\Versions::getVersion() method instead.
Expand All @@ -62,16 +72,25 @@ final class Installer implements PluginInterface, EventSubscriberInterface
private function __construct()
{
class_exists(InstalledVersions::class);
}
/**
* @throws OutOfBoundsException If a version cannot be located.
*
* @psalm-param key-of<self::VERSIONS> $packageName
* @psalm-pure
*
* @psalm-suppress ImpureMethodCall we know that {@see InstalledVersions} interaction does not
* cause any side effects here.
*/
public static function getVersion(string $packageName) : string
public static function getVersion(string $packageName): string
{
if (class_exists(InstalledVersions::class, false)) {
return InstalledVersions::getPrettyVersion($packageName)
. '@' . InstalledVersions::getReference($packageName);
}
if (isset(self::VERSIONS[$packageName])) {
return self::VERSIONS[$packageName];
}
Expand Down Expand Up @@ -102,7 +121,7 @@ public function uninstall(Composer $composer, IOInterface $io)
/**
* {@inheritDoc}
*/
public static function getSubscribedEvents() : array
public static function getSubscribedEvents(): array
{
return [ScriptEvents::POST_AUTOLOAD_DUMP => 'dumpVersionsClass'];
}
Expand Down Expand Up @@ -130,7 +149,7 @@ public static function dumpVersionsClass(Event $composerEvent)
/**
* @param string[] $versions
*/
private static function generateVersionsClass(string $rootPackageName, array $versions) : string
private static function generateVersionsClass(string $rootPackageName, array $versions): string
{
return sprintf(
self::$generatedClassTemplate,
Expand All @@ -148,12 +167,24 @@ private static function writeVersionClassToFile(string $versionClassSource, Comp
$installPath = self::locateRootPackageInstallPath($composer->getConfig(), $composer->getPackage())
. '/src/PackageVersions/Versions.php';

if (! file_exists(dirname($installPath))) {
$installDir = dirname($installPath);
if (! file_exists($installDir)) {
$io->write('<info>composer/package-versions-deprecated:</info> Package not found (probably scheduled for removal); generation of version class skipped.');

return;
}

if (! is_writable($installDir)) {
$io->write(
sprintf(
'<info>composer/package-versions-deprecated:</info> %s is not writable; generation of version class skipped.',
$installDir
)
);

return;
}

$io->write('<info>composer/package-versions-deprecated:</info> Generating version class...');

$installPathTmp = $installPath . '_' . uniqid('tmp', true);
Expand All @@ -170,15 +201,15 @@ private static function writeVersionClassToFile(string $versionClassSource, Comp
private static function locateRootPackageInstallPath(
Config $composerConfig,
RootPackageInterface $rootPackage
) : string {
): string {
if (self::getRootPackageAlias($rootPackage)->getName() === 'composer/package-versions-deprecated') {
return dirname($composerConfig->get('vendor-dir'));
}

return $composerConfig->get('vendor-dir') . '/composer/package-versions-deprecated';
}

private static function getRootPackageAlias(RootPackageInterface $rootPackage) : PackageInterface
private static function getRootPackageAlias(RootPackageInterface $rootPackage): PackageInterface
{
$package = $rootPackage;

Expand All @@ -194,15 +225,15 @@ private static function getRootPackageAlias(RootPackageInterface $rootPackage) :
*
* @psalm-return Generator<string, string>
*/
private static function getVersions(Locker $locker, RootPackageInterface $rootPackage) : Generator
private static function getVersions(Locker $locker, RootPackageInterface $rootPackage): Generator
{
$lockData = $locker->getLockData();

$lockData['packages-dev'] = $lockData['packages-dev'] ?? [];

foreach (array_merge($lockData['packages'], $lockData['packages-dev']) as $package) {
yield $package['name'] => $package['version'] . '@' . (
$package['source']['reference']?? $package['dist']['reference'] ?? ''
$package['source']['reference'] ?? $package['dist']['reference'] ?? ''
);
}

Expand Down
17 changes: 15 additions & 2 deletions src/PackageVersions/Versions.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace PackageVersions;

use Composer\InstalledVersions;
use OutOfBoundsException;
use UnexpectedValueException;

Expand All @@ -17,19 +18,31 @@
*/
final class Versions
{
/**
* @deprecated please use {@see \Composer\InstalledVersions::getRootPackage()} instead. The
* equivalent expression for this constant's contents is
* `\Composer\InstalledVersions::getRootPackage()['name']`.
* This constant will be removed in version 2.0.0.
*/
const ROOT_PACKAGE_NAME = FallbackVersions::ROOT_PACKAGE_NAME;
const VERSIONS = [];

private function __construct()
{
class_exists(InstalledVersions::class);
}

/**
* @throws OutOfBoundsException if a version cannot be located.
* @throws UnexpectedValueException if the composer.lock file could not be located.
*/
public static function getVersion(string $packageName) : string
public static function getVersion(string $packageName): string
{
return FallbackVersions::getVersion($packageName);
if (!class_exists(InstalledVersions::class, false)) {
return FallbackVersions::getVersion($packageName);
}

return InstalledVersions::getPrettyVersion($packageName)
. '@' . InstalledVersions::getReference($packageName);
}
}
Loading

0 comments on commit dd51b44

Please sign in to comment.