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

Commit bdb1067

Browse files
committed
feature #196 Updated the hash generator for the new content-hash Composer property (javiereguiluz)
This PR was squashed before being merged into the 1.0-dev branch (closes #196). Discussion ---------- Updated the hash generator for the new content-hash Composer property This fixes #195. Commits ------- 9222bcc Updated the hash generator for the new content-hash Composer property
2 parents 36e46b3 + 9222bcc commit bdb1067

File tree

1 file changed

+53
-5
lines changed

1 file changed

+53
-5
lines changed

src/Symfony/Installer/NewCommand.php

Lines changed: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -435,16 +435,64 @@ protected function getRemoteFileUrl()
435435
}
436436

437437
/**
438-
* Updates the 'hash' value stored in composer.lock to avoid out-of-sync
438+
* Updates the hash values stored in composer.lock to avoid out-of-sync
439439
* problems when the composer.json file contents are changed.
440440
*/
441441
private function syncComposerLockFile()
442442
{
443-
$composerFileContents = file_get_contents($this->projectDir.'/composer.json');
444-
$lockFileContents = json_decode(file_get_contents($this->projectDir.'/composer.lock'), true);
443+
$composerJsonFileContents = file_get_contents($this->projectDir.'/composer.json');
444+
$composerLockFileContents = json_decode(file_get_contents($this->projectDir.'/composer.lock'), true);
445445

446-
$lockFileContents['hash'] = md5($composerFileContents);
446+
if (array_key_exists('hash', $composerLockFileContents)) {
447+
$composerLockFileContents['hash'] = md5($composerJsonFileContents);
448+
}
449+
450+
if (array_key_exists('content-hash', $composerLockFileContents)) {
451+
$composerLockFileContents['content-hash'] = $this->getComposerContentHash($composerJsonFileContents);
452+
}
453+
454+
file_put_contents($this->projectDir.'/composer.lock', json_encode($composerLockFileContents, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)."\n");
455+
}
456+
457+
/**
458+
* Returns the md5 hash of the sorted content of the composer file.
459+
*
460+
* @see https://github.com/composer/composer/blob/master/src/Composer/Package/Locker.php (getContentHash() method)
461+
*
462+
* @param string $composerJsonFileContents The contents of the composer.json file.
463+
*
464+
* @return string
465+
*/
466+
private function getComposerContentHash($composerJsonFileContents)
467+
{
468+
$content = json_decode($composerJsonFileContents, true);
469+
470+
$relevantKeys = array(
471+
'name',
472+
'version',
473+
'require',
474+
'require-dev',
475+
'conflict',
476+
'replace',
477+
'provide',
478+
'minimum-stability',
479+
'prefer-stable',
480+
'repositories',
481+
'extra',
482+
);
483+
484+
$relevantContent = array();
485+
486+
foreach (array_intersect($relevantKeys, array_keys($content)) as $key) {
487+
$relevantContent[$key] = $content[$key];
488+
}
489+
490+
if (isset($content['config']['platform'])) {
491+
$relevantContent['config']['platform'] = $content['config']['platform'];
492+
}
493+
494+
ksort($relevantContent);
447495

448-
file_put_contents($this->projectDir.'/composer.lock', json_encode($lockFileContents, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)."\n");
496+
return md5(json_encode($relevantContent));
449497
}
450498
}

0 commit comments

Comments
 (0)