├── .dockerignore ├── Dockerfile ├── LICENSE ├── README.md ├── composer-diff ├── composer-diff.php ├── composer.json └── src ├── Commands └── DiffAction.php ├── Comparator.php ├── ComposerLock.php ├── Diff.php ├── Exception.php ├── Package.php ├── Renders ├── AbstractRender.php ├── Console.php ├── JsonOutput.php └── Markdown.php └── Url.php /.dockerignore: -------------------------------------------------------------------------------- 1 | # 2 | # JBZoo Toolbox - CI-Report-Converter. 3 | # 4 | # This file is part of the JBZoo Toolbox project. 5 | # For the full copyright and license information, please view the LICENSE 6 | # file that was distributed with this source code. 7 | # 8 | # @license MIT 9 | # @copyright Copyright (C) JBZoo.com, All rights reserved. 10 | # @see https://github.com/JBZoo/CI-Report-Converter 11 | # 12 | 13 | .git 14 | .idea 15 | .github 16 | build 17 | tests 18 | vendor 19 | .DS_Store 20 | .editorconfig 21 | .gitattributes 22 | .gitignore 23 | .phan.php 24 | .phpunit.result.cache 25 | .travis.yml 26 | action.yml 27 | box.json.dist 28 | phpunit.xml.dist 29 | Makefile 30 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | # 2 | # JBZoo Toolbox - Composer-Diff. 3 | # 4 | # This file is part of the JBZoo Toolbox project. 5 | # For the full copyright and license information, please view the LICENSE 6 | # file that was distributed with this source code. 7 | # 8 | # @license MIT 9 | # @copyright Copyright (C) JBZoo.com, All rights reserved. 10 | # @see https://github.com/JBZoo/Composer-Diff 11 | # 12 | 13 | FROM php:8.1-cli-alpine 14 | RUN mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini" 15 | 16 | ADD https://github.com/mlocati/docker-php-extension-installer/releases/latest/download/install-php-extensions /usr/local/bin/ 17 | RUN chmod +x /usr/local/bin/install-php-extensions \ 18 | && sync \ 19 | && install-php-extensions \ 20 | opcache \ 21 | gd \ 22 | zip \ 23 | @composer 24 | 25 | ENV COMPOSER_ALLOW_SUPERUSER=1 26 | COPY . /app 27 | RUN cd /app \ 28 | && composer install --no-dev --optimize-autoloader --no-progress \ 29 | && composer clear-cache 30 | 31 | # Experimental. Forced colored output 32 | ENV TERM_PROGRAM=Hyper 33 | 34 | ENTRYPOINT ["php", "/app/composer-diff.php"] 35 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 JBZoo Toolbox for developers 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # JBZoo / Composer-Diff 2 | 3 | [![CI](https://github.com/JBZoo/Composer-Diff/actions/workflows/main.yml/badge.svg?branch=master)](https://github.com/JBZoo/Composer-Diff/actions/workflows/main.yml?query=branch%3Amaster) [![Coverage Status](https://coveralls.io/repos/github/JBZoo/Composer-Diff/badge.svg?branch=master)](https://coveralls.io/github/JBZoo/Composer-Diff?branch=master) [![Psalm Coverage](https://shepherd.dev/github/JBZoo/Composer-Diff/coverage.svg)](https://shepherd.dev/github/JBZoo/Composer-Diff) [![Psalm Level](https://shepherd.dev/github/JBZoo/Composer-Diff/level.svg)](https://shepherd.dev/github/JBZoo/Composer-Diff) [![CodeFactor](https://www.codefactor.io/repository/github/jbzoo/composer-diff/badge)](https://www.codefactor.io/repository/github/jbzoo/composer-diff/issues) 4 | [![Stable Version](https://poser.pugx.org/jbzoo/composer-diff/version)](https://packagist.org/packages/jbzoo/composer-diff/) [![Total Downloads](https://poser.pugx.org/jbzoo/composer-diff/downloads)](https://packagist.org/packages/jbzoo/composer-diff/stats) [![Dependents](https://poser.pugx.org/jbzoo/composer-diff/dependents)](https://packagist.org/packages/jbzoo/composer-diff/dependents?order_by=downloads) [![GitHub License](https://img.shields.io/github/license/jbzoo/composer-diff)](https://github.com/JBZoo/Composer-Diff/blob/master/LICENSE) 5 | 6 | 7 | 8 | * [Why?](#why) 9 | * [Installation](#installation) 10 | * [Usage](#usage) 11 | * [Help Description](#help-description) 12 | * [Output Examples](#output-examples) 13 | * [Default view (--output=console)](#default-view---outputconsole) 14 | * [Markdown Output (--output=markdown)](#markdown-output---outputmarkdown) 15 | * [JSON Output (--output=json)](#json-output---outputjson) 16 | * [Roadmap](#roadmap) 17 | * [Unit tests and check code style](#unit-tests-and-check-code-style) 18 | * [License](#license) 19 | * [See Also](#see-also) 20 | 21 | 22 | 23 | ## Why? 24 | 25 | See what packages have been changed after you run `composer update` by comparing `composer.lock` to the `git show HEAD:composer.lock`. 26 | 27 | 28 | ## Installation 29 | 30 | ```shell 31 | composer require jbzoo/composer-diff # For specific project 32 | composer global require jbzoo/composer-diff # As global tool 33 | 34 | # OR use phar file. 35 | wget https://github.com/JBZoo/Composer-Diff/releases/latest/download/composer-diff.phar 36 | ``` 37 | 38 | ## Usage 39 | 40 | ```bash 41 | composer update 42 | 43 | # if it's installed via composer 44 | php ./vendor/bin/composer-diff 45 | 46 | # OR (if installed globally) 47 | composer-diff 48 | 49 | # OR (if you downloaded phar file) 50 | php composer-diff.phar 51 | ``` 52 | 53 | 54 | ## Help Description 55 | ``` 56 | ./vendor/bin/composer-diff --help 57 | 58 | Description: 59 | Show difference between two versions of composer.lock files 60 | 61 | Usage: 62 | diff [options] 63 | 64 | Options: 65 | --source=SOURCE The file, git ref, or git ref with filename to compare FROM [default: "HEAD:composer.lock"] 66 | --target=TARGET The file, git ref, or git ref with filename to compare TO [default: "./composer.lock"] 67 | --env=ENV Show only selected environment. Available options: both, require, require-dev [default: "both"] 68 | --output=OUTPUT Output format. Available options: console, markdown, json [default: "console"] 69 | --no-links Hide all links in tables 70 | --strict Return exit code if you have any difference 71 | --no-progress Disable progress bar animation for logs. It will be used only for text output format. 72 | --mute-errors Mute any sort of errors. So exit code will be always "0" (if it's possible). 73 | It has major priority then --non-zero-on-error. It's on your own risk! 74 | --stdout-only For any errors messages application will use StdOut instead of StdErr. It's on your own risk! 75 | --non-zero-on-error None-zero exit code on any StdErr message. 76 | --timestamp Show timestamp at the beginning of each message.It will be used only for text output format. 77 | --profile Display timing and memory usage information. 78 | --output-mode=OUTPUT-MODE Output format. Available options: 79 | text - Default text output format, userfriendly and easy to read. 80 | cron - Shortcut for crontab. It's basically focused on human-readable logs output. 81 | It's combination of --timestamp --profile --stdout-only --no-progress -vv. 82 | logstash - Logstash output format, for integration with ELK stack. 83 | [default: "text"] 84 | --cron Alias for --output-mode=cron. Deprecated! 85 | -h, --help Display help for the given command. When no command is given display help for the diff command 86 | -q, --quiet Do not output any message 87 | -V, --version Display this application version 88 | --ansi|--no-ansi Force (or disable --no-ansi) ANSI output 89 | -n, --no-interaction Do not ask any interactive question 90 | -v|vv|vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug 91 | ``` 92 | 93 | 94 | ## Output Examples 95 | 96 | ### Default view (--output=console) 97 | 98 | 99 | 100 | ``` 101 | PHP Production Dependencies (require) 102 | +-------------------+------------+--------------------+---------------------+---------------------------------------------------------------+ 103 | | Package | Action | Old Version | New Version | Details | 104 | +-------------------+------------+--------------------+---------------------+---------------------------------------------------------------+ 105 | | vendor/downgraded | Downgraded | 2.0.0 | 1.0.0 | https://gitlab.com/vendor/downgraded/compare/2.0.0...1.0.0 | 106 | | vendor/new | New | - | 1.0.0 | | 107 | | vendor/no-tag | Changed | dev-master@bbc0fba | dev-master@f2f9280 | https://gitlab.com/vendor/package-1/compare/bbc0fba...f2f9280 | 108 | | vendor/no-tag-new | New | - | dev-develop@a999014 | | 109 | | vendor/removed | Removed | 1.0.0 | - | | 110 | | vendor/upgraded | Upgraded | 1.0.0 | 2.0.0 | https://gitlab.com/vendor/upgraded/compare/1.0.0...2.0.0 | 111 | +-------------------+------------+--------------------+---------------------+---------------------------------------------------------------+ 112 | ``` 113 | 114 | Rendered in your terminal: 115 | ![Dummy example](https://raw.githubusercontent.com/JBZoo/Composer-Diff/master/resources/dummy.png) 116 | 117 | Also, see [colored example in travis-ci](https://travis-ci.org/github/JBZoo/Composer-Diff/jobs/705031632#L414) 118 | ![Real project](https://raw.githubusercontent.com/JBZoo/Composer-Diff/master/resources/drupal.png) 119 | 120 | 121 | 122 | ### Markdown Output (--output=markdown) 123 | 124 | Source code: 125 | ```markdown 126 | ## PHP Production Dependencies (require) 127 | 128 | | Package | Action | Old Version | New Version | | 129 | |:-----------------------------------------------------------|:-----------|-------------------:|--------------------:|:-----------------------------------------------------------------------------| 130 | | [vendor/downgraded](https://gitlab.com/vendor/downgraded) | Downgraded | 2.0.0 | 1.0.0 | [See details](https://gitlab.com/vendor/downgraded/compare/2.0.0...1.0.0) | 131 | | [vendor/new](https://gitlab.com/vendor/new) | New | - | 1.0.0 | | 132 | | [vendor/no-tag](https://gitlab.com/vendor/package-1) | Changed | dev-master@bbc0fba | dev-master@f2f9280 | [See details](https://gitlab.com/vendor/package-1/compare/bbc0fba...f2f9280) | 133 | | [vendor/no-tag-new](https://gitlab.com/vendor-1/package-1) | New | - | dev-develop@a999014 | | 134 | | [vendor/removed](https://gitlab.com/vendor/removed) | Removed | 1.0.0 | - | | 135 | | [vendor/upgraded](https://gitlab.com/vendor/upgraded) | Upgraded | 1.0.0 | 2.0.0 | [See details](https://gitlab.com/vendor/upgraded/compare/1.0.0...2.0.0) | 136 | ``` 137 | 138 | Rendered in your readme or PR/MR description: 139 | 140 | | Package | Action | Old Version | New Version | | 141 | |:-----------------------------------------------------------|:-----------|-------------------:|--------------------:|:-----------------------------------------------------------------------------| 142 | | [vendor/downgraded](https://gitlab.com/vendor/downgraded) | Downgraded | 2.0.0 | 1.0.0 | [See details](https://gitlab.com/vendor/downgraded/compare/2.0.0...1.0.0) | 143 | | [vendor/new](https://gitlab.com/vendor/new) | New | - | 1.0.0 | | 144 | | [vendor/no-tag](https://gitlab.com/vendor/package-1) | Changed | dev-master@bbc0fba | dev-master@f2f9280 | [See details](https://gitlab.com/vendor/package-1/compare/bbc0fba...f2f9280) | 145 | | [vendor/no-tag-new](https://gitlab.com/vendor-1/package-1) | New | - | dev-develop@a999014 | | 146 | | [vendor/removed](https://gitlab.com/vendor/removed) | Removed | 1.0.0 | - | | 147 | | [vendor/upgraded](https://gitlab.com/vendor/upgraded) | Upgraded | 1.0.0 | 2.0.0 | [See details](https://gitlab.com/vendor/upgraded/compare/1.0.0...2.0.0) | 148 | 149 | 150 | 151 | ### JSON Output (--output=json) 152 | 153 | ```json 154 | { 155 | "require": { 156 | "vendor\/downgraded": { 157 | "name": "vendor\/downgraded", 158 | "url": "https:\/\/gitlab.com\/vendor\/downgraded", 159 | "version_from": "2.0.0", 160 | "version_to": "1.0.0", 161 | "mode": "Downgraded", 162 | "compare": "https:\/\/gitlab.com\/vendor\/downgraded\/compare\/2.0.0...1.0.0" 163 | }, 164 | "vendor\/new": { 165 | "name": "vendor\/new", 166 | "url": "https:\/\/gitlab.com\/vendor\/new", 167 | "version_from": null, 168 | "version_to": "1.0.0", 169 | "mode": "New", 170 | "compare": null 171 | }, 172 | "vendor\/no-tag": { 173 | "name": "vendor\/no-tag", 174 | "url": "https:\/\/gitlab.com\/vendor\/package-1", 175 | "version_from": "dev-master@bbc0fba", 176 | "version_to": "dev-master@f2f9280", 177 | "mode": "Changed", 178 | "compare": "https:\/\/gitlab.com\/vendor\/package-1\/compare\/bbc0fba...f2f9280" 179 | }, 180 | "vendor\/no-tag-new": { 181 | "name": "vendor\/no-tag-new", 182 | "url": "https:\/\/gitlab.com\/vendor-1\/package-1", 183 | "version_from": null, 184 | "version_to": "dev-develop@a999014", 185 | "mode": "New", 186 | "compare": null 187 | }, 188 | "vendor\/removed": { 189 | "name": "vendor\/removed", 190 | "url": "https:\/\/gitlab.com\/vendor\/removed", 191 | "version_from": "1.0.0", 192 | "version_to": null, 193 | "mode": "Removed", 194 | "compare": null 195 | }, 196 | "vendor\/upgraded": { 197 | "name": "vendor\/upgraded", 198 | "url": "https:\/\/gitlab.com\/vendor\/upgraded", 199 | "version_from": "1.0.0", 200 | "version_to": "2.0.0", 201 | "mode": "Upgraded", 202 | "compare": "https:\/\/gitlab.com\/vendor\/upgraded\/compare\/1.0.0...2.0.0" 203 | } 204 | } 205 | } 206 | ``` 207 | 208 | 209 | ## Roadmap 210 | 211 | * [ ] Supporting Drupal repos. [For example](https://git.drupalcode.org/project/fast_404). 212 | * [ ] Add action in the composer via API like `composer lock-diff`. 213 | * [ ] Fixes [the same issue](https://github.com/davidrjonas/composer-lock-diff/issues/26) with complex/custom name of tag. 214 | * [ ] Auto-detecting alias name of branch. 215 | * [ ] No warp links for Markdown format. 216 | * [ ] (?) Support MS Windows... 217 | 218 | 219 | ## Unit tests and check code style 220 | ```sh 221 | make build 222 | make test-all 223 | ``` 224 | 225 | 226 | ## License 227 | 228 | MIT 229 | 230 | 231 | ## See Also 232 | 233 | - [CI-Report-Converter](https://github.com/JBZoo/CI-Report-Converter) - Converting different error reports for deep compatibility with popular CI systems. 234 | - [Composer-Graph](https://github.com/JBZoo/Composer-Graph) - Dependency graph visualization of composer.json based on mermaid-js. 235 | - [Mermaid-PHP](https://github.com/JBZoo/Mermaid-PHP) - Generate diagrams and flowcharts with the help of the mermaid script language. 236 | - [Utils](https://github.com/JBZoo/Utils) - Collection of useful PHP functions, mini-classes, and snippets for every day. 237 | - [Image](https://github.com/JBZoo/Image) - Package provides object-oriented way to manipulate with images as simple as possible. 238 | - [Data](https://github.com/JBZoo/Data) - Extended implementation of ArrayObject. Use files as config/array. 239 | - [Retry](https://github.com/JBZoo/Retry) - Tiny PHP library providing retry/backoff functionality with multiple backoff strategies and jitter support. 240 | - [SimpleTypes](https://github.com/JBZoo/SimpleTypes) - Converting any values and measures - money, weight, exchange rates, length, ... 241 | 242 | Special thanks to the project [davidrjonas/composer-lock-diff](https://github.com/davidrjonas/composer-lock-diff) which inspired me to make a great utility :) 243 | -------------------------------------------------------------------------------- /composer-diff: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env php 2 | registerCommandsByPath(__DIR__ . '/src/Commands', __NAMESPACE__); 51 | $application->setDefaultCommand('diff'); 52 | $application->run(); 53 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name" : "jbzoo/composer-diff", 3 | "type" : "library", 4 | "description" : "See what has changed after a composer update.", 5 | 6 | "keywords" : [ 7 | "composer", "php", "composer.lock", "composer.json", "diff", "composer diff", "composer-diff" 8 | ], 9 | 10 | "license" : "MIT", 11 | 12 | "authors" : [ 13 | { 14 | "name" : "Denis Smetannikov", 15 | "email" : "admin@jbzoo.com", 16 | "role" : "lead" 17 | }, 18 | { 19 | "name" : "David Jonas", 20 | "homepage" : "https://github.com/davidrjonas", 21 | "role" : "Original Idea" 22 | } 23 | ], 24 | 25 | "bin" : ["composer-diff"], 26 | 27 | "minimum-stability" : "dev", 28 | "prefer-stable" : true, 29 | 30 | "require" : { 31 | "php" : "^8.1", 32 | "ext-json" : "*", 33 | "ext-filter" : "*", 34 | 35 | "jbzoo/data" : "^7.1", 36 | "jbzoo/markdown" : "^7.0", 37 | "jbzoo/cli" : "^7.1.1", 38 | 39 | "symfony/console" : ">=6.4", 40 | "symfony/process" : ">=6.4", 41 | "composer/semver" : ">=3.4" 42 | }, 43 | 44 | "require-dev" : { 45 | "jbzoo/toolbox-dev" : "^7.1", 46 | "roave/security-advisories" : "dev-master", 47 | "composer/composer" : ">=2.7.7" 48 | }, 49 | 50 | "autoload" : { 51 | "psr-4" : {"JBZoo\\ComposerDiff\\" : "src"} 52 | }, 53 | 54 | "autoload-dev" : { 55 | "psr-4" : {"JBZoo\\PHPUnit\\" : "tests"} 56 | }, 57 | 58 | "config" : { 59 | "optimize-autoloader" : true, 60 | "allow-plugins" : {"composer/package-versions-deprecated" : true}, 61 | "platform-check" : true 62 | }, 63 | 64 | "extra" : { 65 | "branch-alias" : { 66 | "dev-master" : "7.x-dev" 67 | } 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /src/Commands/DiffAction.php: -------------------------------------------------------------------------------- 1 | setName('diff') 39 | ->setDescription('Show difference between two versions of composer.lock files') 40 | // File paths 41 | ->addOption( 42 | 'source', 43 | null, 44 | InputOption::VALUE_REQUIRED, 45 | 'The file, git ref, or git ref with filename to compare FROM', 46 | 'HEAD:composer.lock', 47 | ) 48 | ->addOption( 49 | 'target', 50 | null, 51 | InputOption::VALUE_REQUIRED, 52 | 'The file, git ref, or git ref with filename to compare TO', 53 | './composer.lock', 54 | ) 55 | // Options 56 | ->addOption( 57 | 'env', 58 | null, 59 | InputOption::VALUE_REQUIRED, 60 | "Show only selected environment. Available options: {$envs}", 61 | Comparator::ENV_BOTH, 62 | ) 63 | ->addOption( 64 | 'output', 65 | null, 66 | InputOption::VALUE_REQUIRED, 67 | "Output format. Available options: {$outputFormats}", 68 | AbstractRender::CONSOLE, 69 | ) 70 | ->addOption('no-links', null, InputOption::VALUE_NONE, 'Hide all links in tables') 71 | ->addOption('strict', null, InputOption::VALUE_NONE, 'Return exit code if you have any difference'); 72 | 73 | parent::configure(); 74 | } 75 | 76 | /** 77 | * {@inheritDoc} 78 | */ 79 | protected function executeAction(): int 80 | { 81 | $sourcePath = $this->getOptString('source'); 82 | $targetPath = $this->getOptString('target'); 83 | $outputFormat = \strtolower($this->getOptString('output')); 84 | $env = \strtolower($this->getOptString('env')); 85 | $isStrictMode = $this->getOptBool('strict'); 86 | 87 | $params = [ 88 | 'show-links' => !$this->getOptBool('no-links'), 89 | 'strict-mode' => $isStrictMode, 90 | ]; 91 | 92 | $fullChangeLog = Comparator::compare($sourcePath, $targetPath); 93 | 94 | $errorCode = 0; 95 | 96 | if (\in_array($env, [Comparator::ENV_BOTH, Comparator::ENV_PROD], true)) { 97 | $changeLog = $fullChangeLog[Comparator::ENV_PROD]; 98 | $this->renderOutput($outputFormat, $changeLog, Comparator::ENV_PROD, $params); 99 | if ($isStrictMode && \count($changeLog) > 0) { 100 | $errorCode++; 101 | } 102 | } 103 | 104 | if (\in_array($env, [Comparator::ENV_BOTH, Comparator::ENV_DEV], true)) { 105 | $changeLog = $fullChangeLog[Comparator::ENV_DEV]; 106 | $this->renderOutput($outputFormat, $changeLog, Comparator::ENV_DEV, $params); 107 | if ($isStrictMode && \count($changeLog) > 0) { 108 | $errorCode++; 109 | } 110 | } 111 | 112 | return $errorCode; 113 | } 114 | 115 | private function renderOutput(string $outputFormat, array $fullChangeLog, string $env, array $params): void 116 | { 117 | AbstractRender::factory($outputFormat, $params) 118 | ->setFullChangeLog($fullChangeLog) 119 | ->setEnv($env) 120 | ->render($this->outputMode->getOutput()); 121 | } 122 | } 123 | -------------------------------------------------------------------------------- /src/Comparator.php: -------------------------------------------------------------------------------- 1 | self::diff(self::ENV_PROD, $sourceData, $targetData), 37 | self::ENV_DEV => self::diff(self::ENV_DEV, $sourceData, $targetData), 38 | ]; 39 | } 40 | 41 | /** 42 | * @return Diff[] 43 | */ 44 | private static function diff(string $type, ComposerLock $sourceData, ComposerLock $targetData): array 45 | { 46 | if ($type === self::ENV_PROD) { 47 | $sourcePackages = $sourceData->getRequired(); 48 | $targetPackages = $targetData->getRequired(); 49 | } else { 50 | $sourcePackages = $sourceData->getRequiredDev(); 51 | $targetPackages = $targetData->getRequiredDev(); 52 | } 53 | 54 | /** @var Diff[] $resultDiff */ 55 | $resultDiff = []; 56 | 57 | foreach ($sourcePackages as $package) { 58 | $resultDiff[$package->getName()] = (new Diff($package))->setMode(Diff::MODE_REMOVED); 59 | } 60 | 61 | foreach ($targetPackages as $targetName => $targetPackage) { 62 | if (!\array_key_exists($targetName, $resultDiff)) { 63 | $resultDiff[$targetName] = (new Diff())->setMode(Diff::MODE_NEW); 64 | } 65 | 66 | $resultDiff[$targetName]->compareWithPackage($targetPackage); 67 | } 68 | 69 | $resultDiff = \array_filter( 70 | $resultDiff, 71 | static fn (Diff $diff) => $diff->getMode() !== Diff::MODE_SAME, 72 | \ARRAY_FILTER_USE_BOTH, 73 | ); 74 | 75 | \ksort($resultDiff, \SORT_NATURAL); 76 | 77 | return $resultDiff; 78 | } 79 | 80 | private static function load(string $composerFile): ComposerLock 81 | { 82 | if ( 83 | Url::isUrl($composerFile) 84 | && !\in_array(\parse_url($composerFile, \PHP_URL_SCHEME), \stream_get_wrappers(), true) 85 | ) { 86 | throw new Exception("There is no stream wrapper to open \"{$composerFile}\""); 87 | } 88 | 89 | if (\file_exists($composerFile)) { 90 | $json = json(\file_get_contents($composerFile)); 91 | 92 | return new ComposerLock($json->getArrayCopy()); 93 | } 94 | 95 | if (\str_contains($composerFile, ':')) { 96 | $json = json(self::exec('git show ' . \escapeshellarg($composerFile))); 97 | 98 | return new ComposerLock($json->getArrayCopy()); 99 | } 100 | 101 | throw new Exception("Composer lock file \"{$composerFile}\" not found"); 102 | } 103 | 104 | private static function exec(string $command): string 105 | { 106 | $process = Process::fromShellCommandline($command); 107 | $process->run(); 108 | 109 | if ($process->isSuccessful()) { 110 | return $process->getOutput(); 111 | } 112 | 113 | throw new ProcessFailedException($process); 114 | } 115 | } 116 | -------------------------------------------------------------------------------- /src/ComposerLock.php: -------------------------------------------------------------------------------- 1 | getArray('packages') as $packageData) { 34 | $package = new Package($packageData); 35 | 36 | $this->listRequired[$package->getName()] = $package; 37 | } 38 | 39 | foreach ($data->getArray('packages-dev') as $packageData) { 40 | $package = new Package($packageData); 41 | 42 | $this->listRequiredDev[$package->getName()] = $package; 43 | } 44 | } 45 | 46 | /** 47 | * @return Package[] 48 | */ 49 | public function getRequired(): array 50 | { 51 | return $this->listRequired; 52 | } 53 | 54 | /** 55 | * @return Package[] 56 | */ 57 | public function getRequiredDev(): array 58 | { 59 | return $this->listRequiredDev; 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /src/Diff.php: -------------------------------------------------------------------------------- 1 | source = $sourcePackage; 39 | } 40 | 41 | public function setMode(string $newMode): self 42 | { 43 | $this->mode = $newMode; 44 | 45 | return $this; 46 | } 47 | 48 | public function getMode(): string 49 | { 50 | return $this->mode; 51 | } 52 | 53 | public function toArray(): array 54 | { 55 | if ($this->source !== null) { 56 | return [ 57 | 'name' => $this->source->getName(), 58 | 'url' => $this->source->getPackageUrl(), 59 | 'version_from' => $this->source->getVersion(true), 60 | 'version_to' => $this->target?->getVersion(true), 61 | 'mode' => $this->mode, 62 | 'compare' => $this->comparingUrl, 63 | ]; 64 | } 65 | 66 | if ($this->target !== null) { 67 | return [ 68 | 'name' => $this->target->getName(), 69 | 'url' => $this->target->getPackageUrl(), 70 | 'version_from' => null, 71 | 'version_to' => $this->target->getVersion(true), 72 | 'mode' => $this->mode, 73 | 'compare' => $this->comparingUrl, 74 | ]; 75 | } 76 | 77 | throw new Exception('Source and target packages are not defined'); 78 | } 79 | 80 | public function compareWithPackage(Package $targetPackage): self 81 | { 82 | $this->target = $targetPackage; 83 | 84 | if ($this->source === null) { 85 | return $this->setMode(self::MODE_NEW); 86 | } 87 | 88 | if ($this->source->getName() !== $this->target->getName()) { 89 | throw new Exception( 90 | "Can't compare versions of different packages. " . 91 | "Source:{$this->source->getName()}; Target:{$this->target->getName()};", 92 | ); 93 | } 94 | 95 | $sourceVersion = $this->source->getVersion(); 96 | $targetVersion = $this->target->getVersion(); 97 | $this->comparingUrl = $this->getComparingUrl($sourceVersion, $targetVersion); 98 | 99 | if ($sourceVersion === $targetVersion) { 100 | return $this->setMode(self::MODE_SAME); 101 | } 102 | 103 | if (self::isHashVersion($sourceVersion) || self::isHashVersion($targetVersion)) { 104 | return $this->setMode(self::MODE_CHANGED); 105 | } 106 | 107 | $parser = new VersionParser(); 108 | 109 | $normalizedSource = $parser->normalize($sourceVersion); 110 | $normalizedTarget = $parser->normalize($targetVersion); 111 | 112 | if (Comparator::greaterThan($normalizedSource, $normalizedTarget)) { 113 | return $this->setMode(self::MODE_DOWNGRADED); 114 | } 115 | 116 | if (Comparator::lessThan($normalizedSource, $normalizedTarget)) { 117 | return $this->setMode(self::MODE_UPGRADED); 118 | } 119 | 120 | return $this->setMode(self::MODE_CHANGED); 121 | } 122 | 123 | public function getComparingUrl(?string $fromVersion, ?string $toVersion): ?string 124 | { 125 | if (\in_array($fromVersion, [self::MODE_REMOVED, self::MODE_NEW], true)) { 126 | return ''; 127 | } 128 | 129 | if (\in_array($toVersion, [self::MODE_REMOVED, self::MODE_NEW], true)) { 130 | return ''; 131 | } 132 | 133 | if ($this->source !== null) { 134 | return Url::getCompareUrl($this->source->getSourceUrl(), $fromVersion, $toVersion); 135 | } 136 | 137 | if ($this->target !== null) { 138 | return Url::getCompareUrl($this->target->getSourceUrl(), $fromVersion, $toVersion); 139 | } 140 | 141 | throw new Exception('Source and target packages are not defined'); 142 | } 143 | 144 | private static function isHashVersion(string $version): bool 145 | { 146 | return \strlen($version) === Package::HASH_LENGTH && !\str_contains($version, '.'); 147 | } 148 | } 149 | -------------------------------------------------------------------------------- /src/Exception.php: -------------------------------------------------------------------------------- 1 | data = data($packageDate); 32 | 33 | if ($this->data->count() === 0) { 34 | throw new Exception("Can't parse package data"); 35 | } 36 | } 37 | 38 | public function getName(): string 39 | { 40 | return $this->data->getString('name'); 41 | } 42 | 43 | public function getVersion(bool $prettyPrint = false): string 44 | { 45 | $version = $this->data->getString('version'); 46 | if ($prettyPrint) { 47 | $version = (string)\preg_replace('#^v\.#i', '', $version); 48 | $version = (string)\preg_replace('#^v#i', '', $version); 49 | } 50 | 51 | $reference = $this->data->findString('source.reference'); 52 | 53 | if (\strlen($reference) >= self::HASH_LENGTH && \str_starts_with($version, 'dev-')) { 54 | $version = \substr($reference, 0, self::HASH_LENGTH); 55 | if ($prettyPrint) { 56 | $version = "{$this->data->getString('version')}@{$version}"; 57 | } 58 | } 59 | 60 | return $version; 61 | } 62 | 63 | public function getSourceUrl(): string 64 | { 65 | return $this->data->findString('source.url'); 66 | } 67 | 68 | public function getPackageUrl(): ?string 69 | { 70 | $url = $this->getSourceUrl(); 71 | if ($url !== '') { 72 | return Url::getPackageUrl($url); 73 | } 74 | 75 | return null; 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /src/Renders/AbstractRender.php: -------------------------------------------------------------------------------- 1 | params = new Data( 44 | \array_merge([ 45 | 'show-links' => true, 46 | 'strict-mode' => false, 47 | ], $params), 48 | ); 49 | } 50 | 51 | /** 52 | * @param Diff[] $fullChangeLog 53 | */ 54 | public function setFullChangeLog(array $fullChangeLog): self 55 | { 56 | $this->fullChangeLog = $fullChangeLog; 57 | 58 | return $this; 59 | } 60 | 61 | public function setEnv(string $env): self 62 | { 63 | $this->env = $env; 64 | 65 | return $this; 66 | } 67 | 68 | public function render(OutputInterface $output): bool 69 | { 70 | if (\count($this->fullChangeLog) === 0) { 71 | $output->writeln("There is no difference ({$this->env})"); 72 | 73 | return false; 74 | } 75 | 76 | if (\in_array($this->env, [Comparator::ENV_BOTH, Comparator::ENV_PROD], true)) { 77 | $this->renderOneEnv($output, $this->fullChangeLog, Comparator::ENV_PROD); 78 | } 79 | 80 | if (\in_array($this->env, [Comparator::ENV_BOTH, Comparator::ENV_DEV], true)) { 81 | $this->renderOneEnv($output, $this->fullChangeLog, Comparator::ENV_DEV); 82 | } 83 | 84 | return true; 85 | } 86 | 87 | public static function factory(string $outputFormat, array $params): self 88 | { 89 | $outputFormat = \strtolower(\trim($outputFormat)); 90 | 91 | if ($outputFormat === self::CONSOLE) { 92 | return new Console($params); 93 | } 94 | 95 | if ($outputFormat === self::MARKDOWN) { 96 | return new Markdown($params); 97 | } 98 | 99 | if ($outputFormat === self::JSON) { 100 | return new JsonOutput($params); 101 | } 102 | 103 | throw new Exception("Output format \"{$outputFormat}\" not found"); 104 | } 105 | 106 | protected function showLinks(): bool 107 | { 108 | return (bool)$this->params->get('show-links'); 109 | } 110 | 111 | /** 112 | * @phan-suppress PhanPluginPossiblyStaticProtectedMethod 113 | */ 114 | protected function getTitle(string $env): string 115 | { 116 | return $env === Comparator::ENV_PROD 117 | ? 'PHP Production Dependencies' 118 | : 'PHP Dev Dependencies'; 119 | } 120 | } 121 | -------------------------------------------------------------------------------- /src/Renders/Console.php: -------------------------------------------------------------------------------- 1 | showLinks(); 32 | 33 | $output->writeln(" {$this->getTitle($env)} ({$env})"); 34 | 35 | $table = (new Table($output)) 36 | ->setColumnStyle(2, (new TableStyle())->setPadType(\STR_PAD_LEFT)) 37 | ->setColumnStyle(3, (new TableStyle())->setPadType(\STR_PAD_LEFT)); 38 | 39 | if ($showLinks) { 40 | $table->setHeaders(['Package', 'Action', 'Old Version', 'New Version', 'Details']); 41 | } else { 42 | $table->setHeaders(['Package', 'Action', 'Old Version', 'New Version']); 43 | } 44 | 45 | foreach ($changeLog as $diff) { 46 | $row = $diff->toArray(); 47 | 48 | $mode = $row['mode']; 49 | 50 | if ($mode === Diff::MODE_NEW) { 51 | $mode = "{$mode}"; 52 | } elseif ($mode === Diff::MODE_REMOVED) { 53 | $mode = "{$mode}"; 54 | } elseif ($mode === Diff::MODE_UPGRADED) { 55 | $mode = "{$mode}"; 56 | } elseif ($mode === Diff::MODE_DOWNGRADED) { 57 | $mode = "{$mode}"; 58 | } elseif ($mode === Diff::MODE_CHANGED) { 59 | $mode = "{$mode}"; 60 | } 61 | 62 | $fromVersion = $row['version_from'] ?? '-'; 63 | $toVersion = $row['version_to'] ?? '-'; 64 | 65 | if ($showLinks) { 66 | $table->addRow([$row['name'], $mode, $fromVersion, $toVersion, $row['compare']]); 67 | } else { 68 | $table->addRow([$row['name'], $mode, $fromVersion, $toVersion]); 69 | } 70 | } 71 | 72 | $table->render(); 73 | $output->writeln(' '); 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /src/Renders/JsonOutput.php: -------------------------------------------------------------------------------- 1 | []]; 28 | 29 | foreach ($changeLog as $diff) { 30 | $row = $diff->toArray(); 31 | $dataForJson[$env][$row['name']] = $row; 32 | } 33 | 34 | $output->writeln((string)json($dataForJson)); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/Renders/Markdown.php: -------------------------------------------------------------------------------- 1 | setMinCellLength(3); 28 | 29 | if ($this->showLinks()) { 30 | $table 31 | ->setHeaders(['Package', 'Action', 'Old Version', 'New Version', '']) 32 | ->setAlignments([ 33 | Table::ALIGN_LEFT, 34 | Table::ALIGN_LEFT, 35 | Table::ALIGN_RIGHT, 36 | Table::ALIGN_RIGHT, 37 | Table::ALIGN_LEFT, 38 | ]); 39 | } else { 40 | $table 41 | ->setHeaders(['Package', 'Action', 'Old Version', 'New Version']) 42 | ->setAlignments([ 43 | Table::ALIGN_LEFT, 44 | Table::ALIGN_LEFT, 45 | Table::ALIGN_RIGHT, 46 | Table::ALIGN_RIGHT, 47 | ]); 48 | } 49 | 50 | foreach ($changeLog as $diff) { 51 | $row = $diff->toArray(); 52 | 53 | $fromVersion = $row['version_from'] ?? '-'; 54 | $toVersion = $row['version_to'] ?? '-'; 55 | 56 | if ($this->showLinks()) { 57 | $table->appendRow([ 58 | self::getLink($row['name'], $row['url']), 59 | $row['mode'], 60 | $fromVersion, 61 | $toVersion, 62 | self::getLink('See details', $row['compare']), 63 | ]); 64 | } else { 65 | $table->appendRow([$row['name'], $row['mode'], $fromVersion, $toVersion]); 66 | } 67 | } 68 | 69 | $output->writeln("## {$this->getTitle($env)} ({$env})"); 70 | $output->writeln(''); 71 | $output->write($table->render()); 72 | $output->writeln(''); 73 | } 74 | 75 | protected static function getLink(string $title, ?string $url = null): string 76 | { 77 | return ($url !== '' && $url !== null) ? "[{$title}]({$url})" : ''; 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /src/Url.php: -------------------------------------------------------------------------------- 1 | 0) { 95 | return self::GITHUB; 96 | } 97 | 98 | if (\preg_match('/^git@gitlab\..+:.+\.git$/', $url) > 0) { 99 | return self::GITLAB; 100 | } 101 | 102 | if (\preg_match('/^git@bitbucket\..+:.+\.git$/', $url) > 0) { 103 | return self::BITBUCKET; 104 | } 105 | 106 | if (\stripos($url, 'http') !== 0) { 107 | return self::UNKNOWN; 108 | } 109 | 110 | $host = \strtolower((string)\parse_url($url, \PHP_URL_HOST)); 111 | 112 | if (\str_contains($host, 'github')) { 113 | return self::GITHUB; 114 | } 115 | 116 | if (\str_contains($host, 'bitbucket')) { 117 | return self::BITBUCKET; 118 | } 119 | 120 | if (\str_contains($host, 'gitlab')) { 121 | return self::GITLAB; 122 | } 123 | 124 | return self::UNKNOWN; 125 | } 126 | } 127 | --------------------------------------------------------------------------------