From 74b4bcd22e68d0bdc446b37ad82d03d9929cb5cd Mon Sep 17 00:00:00 2001 From: Danack Date: Sun, 18 Aug 2013 22:37:18 +0100 Subject: [PATCH] Fix issue where none root composer.json could be used by ArtifactRepository http://www.php.net/manual/en/ziparchive.locatename.php#85512 --- .../Repository/ArtifactRepository.php | 35 ++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/src/Composer/Repository/ArtifactRepository.php b/src/Composer/Repository/ArtifactRepository.php index 869e4757f..e554cac8b 100644 --- a/src/Composer/Repository/ArtifactRepository.php +++ b/src/Composer/Repository/ArtifactRepository.php @@ -74,6 +74,39 @@ class ArtifactRepository extends ArrayRepository } } + /** + * Find a file by name, returning the one that has the shortest path. + * + * @param \ZipArchive $zip + * @param $filename + * @return bool|int + */ + private function locateFile(\ZipArchive $zip, $filename) { + $shortestIndex = -1; + $shortestIndexLength = -1; + + for ($i = 0; $i < $zip->numFiles; $i++ ){ + $stat = $zip->statIndex($i); + if (strcmp(basename($stat['name']), $filename) === 0){ + $length = strlen($stat['name']); + if ($shortestIndex == -1 || $length < $shortestIndexLength) { + //Check it's not a directory. + $contents = $zip->getFromIndex($i); + if ($contents !== false) { + $shortestIndex = $i; + $shortestIndexLength = $length; + } + } + } + } + + if ($shortestIndex == -1) { + return false; + } + + return $shortestIndex; + } + private function getComposerInformation(\SplFileInfo $file) { $zip = new \ZipArchive(); @@ -83,7 +116,7 @@ class ArtifactRepository extends ArrayRepository return false; } - $foundFileIndex = $zip->locateName('composer.json', \ZipArchive::FL_NODIR); + $foundFileIndex = $this->locateFile($zip, 'composer.json'); if (false === $foundFileIndex) { return false; }