-
-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Extract the ZIP utility functions from ArtifactRepository #7994
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
4d85e21
Extract the ZIP utility functions from ArtifactRepository
aschempp 9de07be
Fixed docblocks
aschempp 05d6b21
Use self:: for private method
aschempp 0d0cb53
Adjust Zip Util to only find the root composer.json
aschempp a91fd20
Return the composer.json content instead of a zip:// path
aschempp 0e2215d
Added full unit test coverage
aschempp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
Extract the ZIP utility functions from ArtifactRepository
- Loading branch information
commit 4d85e217c30ebcc67618e0978cd99544f7aeb634
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,6 +16,7 @@ | |
| use Composer\Json\JsonFile; | ||
| use Composer\Package\Loader\ArrayLoader; | ||
| use Composer\Package\Loader\LoaderInterface; | ||
| use Composer\Util\Zip; | ||
|
|
||
| /** | ||
| * @author Serge Smertin <[email protected]> | ||
|
|
@@ -80,73 +81,14 @@ private function scanDirectory($path) | |
| } | ||
| } | ||
|
|
||
| /** | ||
| * Find a file by name, returning the one that has the shortest path. | ||
| * | ||
| * @param \ZipArchive $zip | ||
| * @param string $filename | ||
| * @return bool|int | ||
| */ | ||
| private function locateFile(\ZipArchive $zip, $filename) | ||
| { | ||
| $indexOfShortestMatch = false; | ||
| $lengthOfShortestMatch = -1; | ||
|
|
||
| for ($i = 0; $i < $zip->numFiles; $i++) { | ||
| $stat = $zip->statIndex($i); | ||
| if (strcmp(basename($stat['name']), $filename) === 0) { | ||
| $directoryName = dirname($stat['name']); | ||
| if ($directoryName == '.') { | ||
| //if composer.json is in root directory | ||
| //it has to be the one to use. | ||
| return $i; | ||
| } | ||
|
|
||
| if (strpos($directoryName, '\\') !== false || | ||
| strpos($directoryName, '/') !== false) { | ||
| //composer.json files below first directory are rejected | ||
| continue; | ||
| } | ||
|
|
||
| $length = strlen($stat['name']); | ||
| if ($indexOfShortestMatch === false || $length < $lengthOfShortestMatch) { | ||
| //Check it's not a directory. | ||
| $contents = $zip->getFromIndex($i); | ||
| if ($contents !== false) { | ||
| $indexOfShortestMatch = $i; | ||
| $lengthOfShortestMatch = $length; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return $indexOfShortestMatch; | ||
| } | ||
|
|
||
| private function getComposerInformation(\SplFileInfo $file) | ||
| { | ||
| $zip = new \ZipArchive(); | ||
| if ($zip->open($file->getPathname()) !== true) { | ||
| return false; | ||
| } | ||
|
|
||
| if (0 == $zip->numFiles) { | ||
| $zip->close(); | ||
| $composerFile = Zip::findFile($file->getPathname(), 'composer.json'); | ||
|
|
||
| if (null === $composerFile) { | ||
| return false; | ||
| } | ||
|
|
||
| $foundFileIndex = $this->locateFile($zip, 'composer.json'); | ||
| if (false === $foundFileIndex) { | ||
| $zip->close(); | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| $configurationFileName = $zip->getNameIndex($foundFileIndex); | ||
| $zip->close(); | ||
|
|
||
| $composerFile = "zip://{$file->getPathname()}#$configurationFileName"; | ||
| $json = file_get_contents($composerFile); | ||
|
|
||
| $package = JsonFile::parseJson($json, $composerFile); | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of Composer. | ||
| * | ||
| * (c) Nils Adermann <[email protected]> | ||
| * Jordi Boggiano <[email protected]> | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| namespace Composer\Util; | ||
|
|
||
| /** | ||
| * @author Jordi Boggiano <[email protected]> | ||
| */ | ||
| class Zip | ||
| { | ||
| /** | ||
| * Finds the path to a file inside a ZIP archive. | ||
| * | ||
| * @param $pathToZip | ||
| * @param $filename | ||
| * | ||
| * @return string|null | ||
| */ | ||
| public static function findFile($pathToZip, $filename) | ||
| { | ||
| $zip = new \ZipArchive(); | ||
| if ($zip->open($pathToZip) !== true) { | ||
| return null; | ||
| } | ||
|
|
||
| if (0 == $zip->numFiles) { | ||
| $zip->close(); | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| $foundFileIndex = static::locateFile($zip, $filename); | ||
aschempp marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if (false === $foundFileIndex) { | ||
| $zip->close(); | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| $configurationFileName = $zip->getNameIndex($foundFileIndex); | ||
| $zip->close(); | ||
|
|
||
| return "zip://{$pathToZip}#$configurationFileName"; | ||
| } | ||
|
|
||
| /** | ||
| * Find a file by name, returning the one that has the shortest path. | ||
| * | ||
| * @param \ZipArchive $zip | ||
| * @param string $filename | ||
| * @return bool|int | ||
| */ | ||
| private static function locateFile(\ZipArchive $zip, $filename) | ||
| { | ||
| $indexOfShortestMatch = false; | ||
| $lengthOfShortestMatch = -1; | ||
|
|
||
| for ($i = 0; $i < $zip->numFiles; $i++) { | ||
| $stat = $zip->statIndex($i); | ||
| if (strcmp(basename($stat['name']), $filename) === 0) { | ||
| $directoryName = dirname($stat['name']); | ||
| if ($directoryName == '.') { | ||
| //if composer.json is in root directory | ||
| //it has to be the one to use. | ||
| return $i; | ||
| } | ||
|
|
||
| if (strpos($directoryName, '\\') !== false || | ||
| strpos($directoryName, '/') !== false) { | ||
| //composer.json files below first directory are rejected | ||
aschempp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| continue; | ||
| } | ||
|
|
||
| $length = strlen($stat['name']); | ||
| if ($indexOfShortestMatch === false || $length < $lengthOfShortestMatch) { | ||
| //Check it's not a directory. | ||
| $contents = $zip->getFromIndex($i); | ||
| if ($contents !== false) { | ||
| $indexOfShortestMatch = $i; | ||
| $lengthOfShortestMatch = $length; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return $indexOfShortestMatch; | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.