├── phpstan.neon.dist ├── LICENSE ├── README.md ├── composer.json └── src └── MetadataMinifier.php /phpstan.neon.dist: -------------------------------------------------------------------------------- 1 | parameters: 2 | level: 8 3 | paths: 4 | - src 5 | - tests 6 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (C) 2021 Composer 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of 4 | this software and associated documentation files (the "Software"), to deal in 5 | the Software without restriction, including without limitation the rights to 6 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 7 | of the Software, and to permit persons to whom the Software is furnished to do 8 | so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in all 11 | copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 | SOFTWARE. 20 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | composer/metadata-minifier 2 | ========================== 3 | 4 | Small utility library that handles metadata minification and expansion. 5 | 6 | This is used by [Composer](https://github.com/composer/composer)'s 2.x repository metadata protocol. 7 | 8 | 9 | Installation 10 | ------------ 11 | 12 | Install the latest version with: 13 | 14 | ```bash 15 | composer require composer/metadata-minifier 16 | ``` 17 | 18 | 19 | Requirements 20 | ------------ 21 | 22 | * PHP 5.3.2 is required but using the latest version of PHP is highly recommended. 23 | 24 | 25 | Basic usage 26 | ----------- 27 | 28 | ### `Composer\MetadataMinifier\MetadataMinifier` 29 | 30 | - `MetadataMinifier::expand()`: Expands an array of minified versions back to their original format 31 | - `MetadataMinifier::minify()`: Minifies an array of versions into a set of version diffs 32 | 33 | For example to expand the dev versions (note the ~dev in the URL) of Monolog you would do: 34 | 35 | ```php 36 | $packageName = 'monolog/monolog'; 37 | $url = 'https://repo.packagist.org/p2/' . $packageName . '~dev.json'; 38 | $json = json_decode(file_get_contents($url), true); 39 | $versions = \Composer\MetadataMinifier\MetadataMinifier::expand($json['packages'][$packageName]); 40 | ``` 41 | 42 | License 43 | ------- 44 | 45 | composer/metadata-minifier is licensed under the MIT License, see the LICENSE file for details. 46 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "composer/metadata-minifier", 3 | "description": "Small utility library that handles metadata minification and expansion.", 4 | "type": "library", 5 | "license": "MIT", 6 | "keywords": [ 7 | "compression", 8 | "composer" 9 | ], 10 | "authors": [ 11 | { 12 | "name": "Jordi Boggiano", 13 | "email": "j.boggiano@seld.be", 14 | "homepage": "http://seld.be" 15 | } 16 | ], 17 | "support": { 18 | "issues": "https://github.com/composer/metadata-minifier/issues" 19 | }, 20 | "require": { 21 | "php": "^5.3.2 || ^7.0 || ^8.0" 22 | }, 23 | "require-dev": { 24 | "symfony/phpunit-bridge": "^4.2 || ^5 || ^6 || ^7", 25 | "phpstan/phpstan": "^1", 26 | "composer/composer": "^2" 27 | }, 28 | "autoload": { 29 | "psr-4": { 30 | "Composer\\MetadataMinifier\\": "src" 31 | } 32 | }, 33 | "autoload-dev": { 34 | "psr-4": { 35 | "Composer\\Test\\MetadataMinifier\\": "tests" 36 | } 37 | }, 38 | "extra": { 39 | "branch-alias": { 40 | "dev-main": "1.x-dev" 41 | } 42 | }, 43 | "scripts": { 44 | "test": "SYMFONY_PHPUNIT_REMOVE_RETURN_TYPEHINT=1 vendor/bin/simple-phpunit", 45 | "phpstan": "vendor/bin/phpstan analyse" 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/MetadataMinifier.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace Composer\MetadataMinifier; 13 | 14 | class MetadataMinifier 15 | { 16 | /** 17 | * Expands an array of minified versions back to their original format 18 | * 19 | * @param list $versions A list of minified version arrays 20 | * @return list A list of version arrays 21 | */ 22 | public static function expand(array $versions) 23 | { 24 | $expanded = array(); 25 | $expandedVersion = null; 26 | foreach ($versions as $versionData) { 27 | if (!$expandedVersion) { 28 | $expandedVersion = $versionData; 29 | $expanded[] = $expandedVersion; 30 | continue; 31 | } 32 | 33 | // add any changes from the previous version to the expanded one 34 | foreach ($versionData as $key => $val) { 35 | if ($val === '__unset') { 36 | unset($expandedVersion[$key]); 37 | } else { 38 | $expandedVersion[$key] = $val; 39 | } 40 | } 41 | 42 | $expanded[] = $expandedVersion; 43 | } 44 | 45 | return $expanded; 46 | } 47 | 48 | /** 49 | * Minifies an array of versions into a set of version diffs 50 | * 51 | * @param list $versions A list of version arrays 52 | * @return list A list of versions minified with each array only containing the differences to the previous one 53 | */ 54 | public static function minify(array $versions) 55 | { 56 | $minifiedVersions = array(); 57 | 58 | $lastKnownVersionData = null; 59 | foreach ($versions as $version) { 60 | if (!$lastKnownVersionData) { 61 | $lastKnownVersionData = $version; 62 | $minifiedVersions[] = $version; 63 | continue; 64 | } 65 | 66 | $minifiedVersion = array(); 67 | 68 | // add any changes from the previous version 69 | foreach ($version as $key => $val) { 70 | if (!isset($lastKnownVersionData[$key]) || $lastKnownVersionData[$key] !== $val) { 71 | $minifiedVersion[$key] = $val; 72 | $lastKnownVersionData[$key] = $val; 73 | } 74 | } 75 | 76 | // store any deletions from the previous version for keys missing in current one 77 | foreach ($lastKnownVersionData as $key => $val) { 78 | if (!isset($version[$key])) { 79 | $minifiedVersion[$key] = "__unset"; 80 | unset($lastKnownVersionData[$key]); 81 | } 82 | } 83 | 84 | $minifiedVersions[] = $minifiedVersion; 85 | } 86 | 87 | return $minifiedVersions; 88 | } 89 | } 90 | --------------------------------------------------------------------------------