diff --git a/src/Composer/Repository/FilesystemRepository.php b/src/Composer/Repository/FilesystemRepository.php index bf2440bc8..71a8fa6fe 100644 --- a/src/Composer/Repository/FilesystemRepository.php +++ b/src/Composer/Repository/FilesystemRepository.php @@ -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', 'dumpVersion($versions) . ';'."\n"); + $fs->filePutContentsIfModified($repoDir.'/installed.php', '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); diff --git a/tests/Composer/Test/InstalledVersionsTest.php b/tests/Composer/Test/InstalledVersionsTest.php index bfd7feb78..80947fc02 100644 --- a/tests/Composer/Test/InstalledVersionsTest.php +++ b/tests/Composer/Test/InstalledVersionsTest.php @@ -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')); + } } diff --git a/tests/Composer/Test/Repository/Fixtures/installed.php b/tests/Composer/Test/Repository/Fixtures/installed.php index 0f6283c7e..a66c75e50 100644 --- a/tests/Composer/Test/Repository/Fixtures/installed.php +++ b/tests/Composer/Test/Repository/Fixtures/installed.php @@ -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', ),