Skip to content
This repository was archived by the owner on Nov 14, 2019. It is now read-only.

Updated the hash generator for the new content-hash Composer property #196

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 53 additions & 5 deletions src/Symfony/Installer/NewCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}