diff --git a/src/Symfony/Installer/NewCommand.php b/src/Symfony/Installer/NewCommand.php index c40d2c9..1806d97 100644 --- a/src/Symfony/Installer/NewCommand.php +++ b/src/Symfony/Installer/NewCommand.php @@ -435,16 +435,64 @@ protected function getRemoteFileUrl() } /** - * Updates the 'hash' value stored in composer.lock to avoid out-of-sync + * Updates the hash values stored in composer.lock to avoid out-of-sync * problems when the composer.json file contents are changed. */ private function syncComposerLockFile() { - $composerFileContents = file_get_contents($this->projectDir.'/composer.json'); - $lockFileContents = json_decode(file_get_contents($this->projectDir.'/composer.lock'), true); + $composerJsonFileContents = file_get_contents($this->projectDir.'/composer.json'); + $composerLockFileContents = json_decode(file_get_contents($this->projectDir.'/composer.lock'), true); - $lockFileContents['hash'] = md5($composerFileContents); + if (array_key_exists('hash', $composerLockFileContents)) { + $composerLockFileContents['hash'] = md5($composerJsonFileContents); + } + + if (array_key_exists('content-hash', $composerLockFileContents)) { + $composerLockFileContents['content-hash'] = $this->getComposerContentHash($composerJsonFileContents); + } + + file_put_contents($this->projectDir.'/composer.lock', json_encode($composerLockFileContents, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)."\n"); + } + + /** + * Returns the md5 hash of the sorted content of the composer file. + * + * @see https://github.com/composer/composer/blob/master/src/Composer/Package/Locker.php (getContentHash() method) + * + * @param string $composerJsonFileContents The contents of the composer.json file. + * + * @return string + */ + private function getComposerContentHash($composerJsonFileContents) + { + $content = json_decode($composerJsonFileContents, true); + + $relevantKeys = array( + 'name', + 'version', + 'require', + 'require-dev', + 'conflict', + 'replace', + 'provide', + 'minimum-stability', + 'prefer-stable', + 'repositories', + 'extra', + ); + + $relevantContent = array(); + + foreach (array_intersect($relevantKeys, array_keys($content)) as $key) { + $relevantContent[$key] = $content[$key]; + } + + if (isset($content['config']['platform'])) { + $relevantContent['config']['platform'] = $content['config']['platform']; + } + + ksort($relevantContent); - file_put_contents($this->projectDir.'/composer.lock', json_encode($lockFileContents, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)."\n"); + return md5(json_encode($relevantContent)); } }