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) { if ($this->dumpVersions) {
$versions = $this->generateInstalledVersions($installationManager, $installPaths, $devMode, $repoDir); $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'); $installedVersionsClass = file_get_contents(__DIR__.'/../InstalledVersions.php');
// while not strictly needed since https://github.com/composer/composer/pull/9635 - we keep this for BC // 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 // 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. // 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); $fs->filePutContentsIfModified($repoDir.'/InstalledVersions.php', $installedVersionsClass);
\Composer\InstalledVersions::reload($versions); \Composer\InstalledVersions::reload($versions);
} }
} }
private function dumpVersion(array $array = array(), $level = 0) private function dumpToPhpCode(array $array = array(), $level = 0)
{ {
$lines = "array(\n"; $lines = "array(\n";
$level++; $level++;
@ -154,31 +155,26 @@ class FilesystemRepository extends WritableArrayRepository
if (is_array($value)) { if (is_array($value)) {
if (!empty($value)) { if (!empty($value)) {
$lines .= self::dumpVersion($value, $level); $lines .= $this->dumpToPhpCode($value, $level);
} else { } else {
$lines .= "array(),\n"; $lines .= "array(),\n";
} }
} elseif (is_null($value)) { } elseif ($key === 'install_path') {
$lines .= 'null'; $lines .= "__DIR__ . " . var_export('/' . $value, true) . ",\n";
$lines .= ",\n";
} elseif (is_bool($value)) {
$lines .= $value ? 'true' : 'false';
$lines .= ",\n";
} else { } else {
$stringContent = str_replace(array('\\', '\''), array('\\\\', '\\\''), $value); $lines .= var_export($value, true) . ",\n";
$folder = $key === 'install_path' ? '__DIR__ . DIRECTORY_SEPARATOR . ' : '';
$lines .= $folder . "'" . $stringContent . "',\n";
} }
} }
$lines .= str_repeat(' ', $level - 1) . ')' . ($level - 1 == 0 ? '' : ",\n"); $lines .= str_repeat(' ', $level - 1) . ')' . ($level - 1 == 0 ? '' : ",\n");
return $lines; return $lines;
} }
/** /**
* @return ?array * @return ?array
*/ */
private function generateInstalledVersions(InstallationManager $installationManager, $installPaths, $devMode, $repoDir) private function generateInstalledVersions(InstallationManager $installationManager, array $installPaths, $devMode, $repoDir)
{ {
if (!$this->dumpVersions) { if (!$this->dumpVersions) {
return null; return null;
@ -207,7 +203,7 @@ class FilesystemRepository extends WritableArrayRepository
$reference = ($package->getSourceReference() ?: $package->getDistReference()) ?: null; $reference = ($package->getSourceReference() ?: $package->getDistReference()) ?: null;
} }
if($package instanceof RootPackageInterface) { if ($package instanceof RootPackageInterface) {
$fs = new Filesystem(); $fs = new Filesystem();
$to = getcwd(); $to = getcwd();
$installPath = $fs->findShortestPath($repoDir, $to, true); $installPath = $fs->findShortestPath($repoDir, $to, true);

View File

@ -193,7 +193,7 @@ class InstalledVersionsTest extends TestCase
'pretty_version' => 'dev-master', 'pretty_version' => 'dev-master',
'version' => 'dev-master', 'version' => 'dev-master',
'type' => 'library', 'type' => 'library',
'install_path' => $this->root . DIRECTORY_SEPARATOR . './', 'install_path' => $this->root . '/./',
'aliases' => array( 'aliases' => array(
'1.10.x-dev', '1.10.x-dev',
), ),
@ -243,4 +243,11 @@ class InstalledVersionsTest extends TestCase
$this->assertSame($names, \Composer\InstalledVersions::getInstalledPackagesByType('library')); $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', 'version' => 'dev-master',
'type' => 'library', 'type' => 'library',
// @phpstan-ignore-next-line // @phpstan-ignore-next-line
'install_path' => $dir . DIRECTORY_SEPARATOR . './', 'install_path' => $dir . '/./',
'aliases' => array( 'aliases' => array(
'1.10.x-dev', '1.10.x-dev',
), ),
@ -30,57 +30,57 @@ return array(
'version' => 'dev-master', 'version' => 'dev-master',
'type' => 'library', 'type' => 'library',
// @phpstan-ignore-next-line // @phpstan-ignore-next-line
'install_path' => $dir . DIRECTORY_SEPARATOR . './', 'install_path' => $dir . '/./',
'aliases' => array( 'aliases' => array(
'1.10.x-dev', '1.10.x-dev',
), ),
'reference' => 'sourceref-by-default', 'reference' => 'sourceref-by-default',
'dev-requirement' => false, 'dev_requirement' => false,
), ),
'a/provider' => array( 'a/provider' => array(
'pretty_version' => '1.1', 'pretty_version' => '1.1',
'version' => '1.1.0.0', 'version' => '1.1.0.0',
'type' => 'library', 'type' => 'library',
// @phpstan-ignore-next-line // @phpstan-ignore-next-line
'install_path' => $dir . DIRECTORY_SEPARATOR . '/foo/bar/vendor/woop/woop', 'install_path' => $dir . '/foo/bar/vendor/woop/woop',
'aliases' => array(), 'aliases' => array(),
'reference' => 'distref-as-no-source', 'reference' => 'distref-as-no-source',
'dev-requirement' => false, 'dev_requirement' => false,
), ),
'a/provider2' => array( 'a/provider2' => array(
'pretty_version' => '1.2', 'pretty_version' => '1.2',
'version' => '1.2.0.0', 'version' => '1.2.0.0',
'type' => 'library', 'type' => 'library',
// @phpstan-ignore-next-line // @phpstan-ignore-next-line
'install_path' => $dir . DIRECTORY_SEPARATOR . '/foo/bar/vendor/woop/woop', 'install_path' => $dir . '/foo/bar/vendor/woop/woop',
'aliases' => array( 'aliases' => array(
'1.4', '1.4',
), ),
'reference' => 'distref-as-installed-from-dist', 'reference' => 'distref-as-installed-from-dist',
'dev-requirement' => false, 'dev_requirement' => false,
), ),
'b/replacer' => array( 'b/replacer' => array(
'pretty_version' => '2.2', 'pretty_version' => '2.2',
'version' => '2.2.0.0', 'version' => '2.2.0.0',
'type' => 'library', 'type' => 'library',
// @phpstan-ignore-next-line // @phpstan-ignore-next-line
'install_path' => $dir . DIRECTORY_SEPARATOR . '/foo/bar/vendor/woop/woop', 'install_path' => $dir . '/foo/bar/vendor/woop/woop',
'aliases' => array(), 'aliases' => array(),
'reference' => null, 'reference' => null,
'dev-requirement' => false, 'dev_requirement' => false,
), ),
'c/c' => array( 'c/c' => array(
'pretty_version' => '3.0', 'pretty_version' => '3.0',
'version' => '3.0.0.0', 'version' => '3.0.0.0',
'type' => 'library', 'type' => 'library',
// @phpstan-ignore-next-line // @phpstan-ignore-next-line
'install_path' => $dir . DIRECTORY_SEPARATOR . '/foo/bar/vendor/woop/woop', 'install_path' => $dir . '/foo/bar/vendor/woop/woop',
'aliases' => array(), 'aliases' => array(),
'reference' => null, 'reference' => null,
'dev-requirement' => true, 'dev_requirement' => true,
), ),
'foo/impl' => array( 'foo/impl' => array(
'dev-requirement' => false, 'dev_requirement' => false,
'provided' => array( 'provided' => array(
'^1.1', '^1.1',
'1.2', '1.2',
@ -89,7 +89,7 @@ return array(
), ),
), ),
'foo/impl2' => array( 'foo/impl2' => array(
'dev-requirement' => false, 'dev_requirement' => false,
'provided' => array( 'provided' => array(
'2.0', '2.0',
), ),
@ -98,7 +98,7 @@ return array(
), ),
), ),
'foo/replaced' => array( 'foo/replaced' => array(
'dev-requirement' => false, 'dev_requirement' => false,
'replaced' => array( 'replaced' => array(
'^3.0', '^3.0',
), ),