1
0
Fork 0

Clean up dump code to avoid reimplementing var export, and remove DIRECTORY_SEPARATOR

pull/9699/head
Jordi Boggiano 2021-05-21 13:25:31 +02:00
parent 5a69a1e483
commit 518b44a810
No known key found for this signature in database
GPG Key ID: 7BBD42C429EC80BC
3 changed files with 33 additions and 30 deletions

View File

@ -130,20 +130,21 @@ class FilesystemRepository extends WritableArrayRepository
if ($this->dumpVersions) {
$versions = $this->generateInstalledVersions($installationManager, $installPaths, $devMode, $repoDir);
$versionsCode = $this->dumpToPhpCode($versions);
$fs->filePutContentsIfModified($repoDir.'/installed.php', '<?php return ' . $this->dumpVersion($versions) . ';'."\n");
$fs->filePutContentsIfModified($repoDir.'/installed.php', '<?php return ' . $versionsCode . ';'."\n");
$installedVersionsClass = file_get_contents(__DIR__.'/../InstalledVersions.php');
// while not strictly needed since https://github.com/composer/composer/pull/9635 - we keep this for BC
// and overall broader compatibility with people that may not use Composer's ClassLoader. They can
// simply include InstalledVersions.php manually and have it working in a basic way.
$installedVersionsClass = str_replace('public static function initializeInstalled() {}', 'public static function initializeInstalled() {' . PHP_EOL . 'self::$installed = ' . $this->dumpVersion($versions) . ';' . PHP_EOL . '}', $installedVersionsClass);
$installedVersionsClass = str_replace('public static function initializeInstalled() {}', 'public static function initializeInstalled() {' . PHP_EOL . 'self::$installed = ' . $versionsCode . ';' . PHP_EOL . '}', $installedVersionsClass);
$fs->filePutContentsIfModified($repoDir.'/InstalledVersions.php', $installedVersionsClass);
\Composer\InstalledVersions::reload($versions);
}
}
private function dumpVersion(array $array = array(), $level = 0)
private function dumpToPhpCode(array $array = array(), $level = 0)
{
$lines = "array(\n";
$level++;
@ -154,31 +155,26 @@ class FilesystemRepository extends WritableArrayRepository
if (is_array($value)) {
if (!empty($value)) {
$lines .= self::dumpVersion($value, $level);
$lines .= $this->dumpToPhpCode($value, $level);
} else {
$lines .= "array(),\n";
}
} elseif (is_null($value)) {
$lines .= 'null';
$lines .= ",\n";
} elseif (is_bool($value)) {
$lines .= $value ? 'true' : 'false';
$lines .= ",\n";
} elseif ($key === 'install_path') {
$lines .= "__DIR__ . " . var_export('/' . $value, true) . ",\n";
} else {
$stringContent = str_replace(array('\\', '\''), array('\\\\', '\\\''), $value);
$folder = $key === 'install_path' ? '__DIR__ . DIRECTORY_SEPARATOR . ' : '';
$lines .= $folder . "'" . $stringContent . "',\n";
$lines .= var_export($value, true) . ",\n";
}
}
$lines .= str_repeat(' ', $level - 1) . ')' . ($level - 1 == 0 ? '' : ",\n");
return $lines;
}
/**
* @return ?array
*/
private function generateInstalledVersions(InstallationManager $installationManager, $installPaths, $devMode, $repoDir)
private function generateInstalledVersions(InstallationManager $installationManager, array $installPaths, $devMode, $repoDir)
{
if (!$this->dumpVersions) {
return null;
@ -207,7 +203,7 @@ class FilesystemRepository extends WritableArrayRepository
$reference = ($package->getSourceReference() ?: $package->getDistReference()) ?: null;
}
if($package instanceof RootPackageInterface) {
if ($package instanceof RootPackageInterface) {
$fs = new Filesystem();
$to = getcwd();
$installPath = $fs->findShortestPath($repoDir, $to, true);

View File

@ -193,7 +193,7 @@ class InstalledVersionsTest extends TestCase
'pretty_version' => 'dev-master',
'version' => 'dev-master',
'type' => 'library',
'install_path' => $this->root . DIRECTORY_SEPARATOR . './',
'install_path' => $this->root . '/./',
'aliases' => array(
'1.10.x-dev',
),
@ -243,4 +243,11 @@ class InstalledVersionsTest extends TestCase
$this->assertSame($names, \Composer\InstalledVersions::getInstalledPackagesByType('library'));
}
public function testGetInstallPath()
{
$this->assertSame($this->root . '/./', \Composer\InstalledVersions::getInstallPath('__root__'));
$this->assertSame($this->root . '/foo/bar/vendor/woop/woop', \Composer\InstalledVersions::getInstallPath('c/c'));
$this->assertNull(\Composer\InstalledVersions::getInstallPath('foo/impl'));
}
}

View File

@ -16,7 +16,7 @@ return array(
'version' => 'dev-master',
'type' => 'library',
// @phpstan-ignore-next-line
'install_path' => $dir . DIRECTORY_SEPARATOR . './',
'install_path' => $dir . '/./',
'aliases' => array(
'1.10.x-dev',
),
@ -30,57 +30,57 @@ return array(
'version' => 'dev-master',
'type' => 'library',
// @phpstan-ignore-next-line
'install_path' => $dir . DIRECTORY_SEPARATOR . './',
'install_path' => $dir . '/./',
'aliases' => array(
'1.10.x-dev',
),
'reference' => 'sourceref-by-default',
'dev-requirement' => false,
'dev_requirement' => false,
),
'a/provider' => array(
'pretty_version' => '1.1',
'version' => '1.1.0.0',
'type' => 'library',
// @phpstan-ignore-next-line
'install_path' => $dir . DIRECTORY_SEPARATOR . '/foo/bar/vendor/woop/woop',
'install_path' => $dir . '/foo/bar/vendor/woop/woop',
'aliases' => array(),
'reference' => 'distref-as-no-source',
'dev-requirement' => false,
'dev_requirement' => false,
),
'a/provider2' => array(
'pretty_version' => '1.2',
'version' => '1.2.0.0',
'type' => 'library',
// @phpstan-ignore-next-line
'install_path' => $dir . DIRECTORY_SEPARATOR . '/foo/bar/vendor/woop/woop',
'install_path' => $dir . '/foo/bar/vendor/woop/woop',
'aliases' => array(
'1.4',
),
'reference' => 'distref-as-installed-from-dist',
'dev-requirement' => false,
'dev_requirement' => false,
),
'b/replacer' => array(
'pretty_version' => '2.2',
'version' => '2.2.0.0',
'type' => 'library',
// @phpstan-ignore-next-line
'install_path' => $dir . DIRECTORY_SEPARATOR . '/foo/bar/vendor/woop/woop',
'install_path' => $dir . '/foo/bar/vendor/woop/woop',
'aliases' => array(),
'reference' => null,
'dev-requirement' => false,
'dev_requirement' => false,
),
'c/c' => array(
'pretty_version' => '3.0',
'version' => '3.0.0.0',
'type' => 'library',
// @phpstan-ignore-next-line
'install_path' => $dir . DIRECTORY_SEPARATOR . '/foo/bar/vendor/woop/woop',
'install_path' => $dir . '/foo/bar/vendor/woop/woop',
'aliases' => array(),
'reference' => null,
'dev-requirement' => true,
'dev_requirement' => true,
),
'foo/impl' => array(
'dev-requirement' => false,
'dev_requirement' => false,
'provided' => array(
'^1.1',
'1.2',
@ -89,7 +89,7 @@ return array(
),
),
'foo/impl2' => array(
'dev-requirement' => false,
'dev_requirement' => false,
'provided' => array(
'2.0',
),
@ -98,7 +98,7 @@ return array(
),
),
'foo/replaced' => array(
'dev-requirement' => false,
'dev_requirement' => false,
'replaced' => array(
'^3.0',
),