如何解决 Composer 依赖项冲突(一种干净的方法)?

How to resolve the Composer dependencies conflicts (a clean way)?

我正在编写一个基于 Symfony v5.0.11 的 PHP 应用程序。现在我想将 Symfony 升级到 ^v5.2。 (Composer 版本为 2.0.12。)

$ composer require symfony/framework-bundle:^5.2
./composer.json has been updated
Running composer update symfony/framework-bundle
Loading composer repositories with package information
Updating dependencies
Your requirements could not be resolved to an installable set of packages.

  Problem 1
    - symfony/framework-bundle[v5.2.0, ..., v5.2.6] require symfony/cache ^5.2 -> found symfony/cache[v5.2.0, ..., v5.2.6] but the package is fixed to v5.0.11 (lock file version) by a partia
l update and that version does not match. Make sure you list it as an argument for the update command.
    - Root composer.json requires symfony/framework-bundle ^5.2 -> satisfiable by symfony/framework-bundle[v5.2.0, ..., v5.2.6].

Use the option --with-all-dependencies (-W) to allow upgrades, downgrades and removals for packages currently locked to specific versions.

Installation failed, reverting ./composer.json and ./composer.lock to their original content.

没用,因为 symfony/cache 包在 composer.lock 中被锁定到 v5.0.11 版本。但是当我尝试使用 --with-all-dependencies 选项时,如错误消息中所示:

$ composer require --update-with-all-dependencies symfony/framework-bundle:^5.2
./composer.json has been updated
Running composer update symfony/framework-bundle --with-all-dependencies
Loading composer repositories with package information
Restricting packages listed in "symfony/symfony" to "5.0.*"
Updating dependencies
Your requirements could not be resolved to an installable set of packages.

  Problem 1
    - symfony/framework-bundle[v5.2.0, ..., v5.2.6] require symfony/cache ^5.2 -> found symfony/cache[v5.2.0, ..., v5.2.6] but these were not loaded, likely because it conflicts with another
 require.
    - Root composer.json requires symfony/framework-bundle ^5.2 -> satisfiable by symfony/framework-bundle[v5.2.0, ..., v5.2.6].


Installation failed, reverting ./composer.json and ./composer.lock to their original content.

现在的问题是,Composer 无法安装所需版本的软件包 symfony/cache,可能是“因为它与另一个要求 冲突”。

如何干净利落地解决这些/这样的 Composer 问题? (“干净”在这里的意思是:不重置项目,运行 全面 composer update,或者制作其他具有不可预测的副作用的残酷东西。)


更新

composer.json

{
    "type": "project",
    "license": "proprietary",
    "require": {
        "php": "^7.3.0 || ^8.0.0",
        "ext-ctype": "*",
        "ext-iconv": "*",
        "knplabs/knp-markdown-bundle": "^1.9",
        "paragonie/sodium_compat": "^1.15",
        "sensio/framework-extra-bundle": "^6.0",
        "sentry/sentry-symfony": "^4.0",
        "symfony/asset": "5.0.*",
        "symfony/console": "5.0.*",
        "symfony/debug-bundle": "5.0.*",
        "symfony/dotenv": "5.0.*",
        "symfony/flex": "^1.3.1",
        "symfony/framework-bundle": "5.0.*",
        "symfony/monolog-bundle": "^3.0",
        "symfony/profiler-pack": "*",
        "symfony/routing": "5.1.*",
        "symfony/twig-pack": "^1.0",
        "symfony/var-dumper": "5.0.*",
        "symfony/webpack-encore-bundle": "^1.7",
        "symfony/yaml": "5.0.*"
    },
    "require-dev": {
        "symfony/maker-bundle": "^1.30",
        "symfony/profiler-pack": "^1.0"
    },
    "config": {
        "preferred-install": {
            "*": "dist"
        },
        "sort-packages": true,
        "platform": {}
    },
    "autoload": {
        "psr-4": {
            "App\": "src/"
        }
    },
    "autoload-dev": {
        "psr-4": {
            "App\Tests\": "tests/"
        }
    },
    "replace": {
        "paragonie/random_compat": "2.*",
        "symfony/polyfill-ctype": "*",
        "symfony/polyfill-iconv": "*",
        "symfony/polyfill-php72": "*",
        "symfony/polyfill-php71": "*",
        "symfony/polyfill-php70": "*",
        "symfony/polyfill-php56": "*"
    },
    "scripts": {
        "auto-scripts": {
            "cache:clear": "symfony-cmd",
            "assets:install %PUBLIC_DIR%": "symfony-cmd"
        },
        "post-install-cmd": [
            "@auto-scripts"
        ],
        "post-update-cmd": [
            "@auto-scripts"
        ]
    },
    "conflict": {
        "symfony/symfony": "*"
    },
    "extra": {
        "symfony": {
            "allow-contrib": true,
            "require": "5.0.*"
        }
    }
}

composer.lock

{
    ...
        {
            "name": "symfony/framework-bundle",
            "version": "v5.0.11",
            "source": {
                "type": "git",
                "url": "https://github.com/symfony/framework-bundle.git",
                "reference": "0fc0a93f8bbe465d0b483e21b087d432baa92c16"
            },
            "dist": {
                "type": "zip",
                "url": "https://api.github.com/repos/symfony/framework-bundle/zipball/0fc0a93f8bbe465d0b483e21b087d432baa92c16",
                "reference": "0fc0a93f8bbe465d0b483e21b087d432baa92c16",
                "shasum": ""
            },
            "require": {
                "ext-xml": "*",
                "php": ">=7.2.5",
                "symfony/cache": "^4.4|^5.0",
                "symfony/config": "^5.0",
                "symfony/dependency-injection": "^5.0.1",
                "symfony/error-handler": "^4.4.1|^5.0.1",
                "symfony/filesystem": "^4.4|^5.0",
                "symfony/finder": "^4.4|^5.0",
                "symfony/http-foundation": "^4.4|^5.0",
                "symfony/http-kernel": "^5.0",
                "symfony/polyfill-mbstring": "~1.0",
                "symfony/routing": "^5.0"
            },
            "conflict": {
                "doctrine/persistence": "<1.3",
                "phpdocumentor/reflection-docblock": "<3.0",
                "phpdocumentor/type-resolver": "<0.2.1",
                "phpunit/phpunit": "<5.4.3",
                "symfony/asset": "<4.4",
                "symfony/browser-kit": "<4.4",
                "symfony/console": "<4.4",
                "symfony/dom-crawler": "<4.4",
                "symfony/dotenv": "<4.4",
                "symfony/form": "<4.4",
                "symfony/http-client": "<4.4",
                "symfony/lock": "<4.4",
                "symfony/mailer": "<4.4",
                "symfony/messenger": "<4.4",
                "symfony/mime": "<4.4",
                "symfony/property-info": "<4.4",
                "symfony/serializer": "<4.4",
                "symfony/stopwatch": "<4.4",
                "symfony/translation": "<5.0",
                "symfony/twig-bridge": "<4.4",
                "symfony/twig-bundle": "<4.4",
                "symfony/validator": "<4.4",
                "symfony/web-profiler-bundle": "<4.4",
                "symfony/workflow": "<4.4"
            },
            "require-dev": {
                "doctrine/annotations": "~1.7",
                "doctrine/cache": "~1.0",
                "paragonie/sodium_compat": "^1.8",
                "phpdocumentor/reflection-docblock": "^3.0|^4.0",
                "symfony/asset": "^4.4|^5.0",
                "symfony/browser-kit": "^4.4|^5.0",
                "symfony/console": "^4.4|^5.0",
                "symfony/css-selector": "^4.4|^5.0",
                "symfony/dom-crawler": "^4.4|^5.0",
                "symfony/dotenv": "^4.4|^5.0",
                "symfony/expression-language": "^4.4|^5.0",
                "symfony/form": "^4.4|^5.0",
                "symfony/http-client": "^4.4|^5.0",
                "symfony/lock": "^4.4|^5.0",
                "symfony/mailer": "^4.4|^5.0",
                "symfony/messenger": "^4.4|^5.0",
                "symfony/mime": "^4.4|^5.0",
                "symfony/polyfill-intl-icu": "~1.0",
                "symfony/process": "^4.4|^5.0",
                "symfony/property-info": "^4.4|^5.0",
                "symfony/security-csrf": "^4.4|^5.0",
                "symfony/security-http": "^4.4|^5.0",
                "symfony/serializer": "^4.4|^5.0",
                "symfony/stopwatch": "^4.4|^5.0",
                "symfony/string": "~5.0.0",
                "symfony/translation": "^5.0",
                "symfony/twig-bundle": "^4.4|^5.0",
                "symfony/validator": "^4.4|^5.0",
                "symfony/web-link": "^4.4|^5.0",
                "symfony/workflow": "^4.4|^5.0",
                "symfony/yaml": "^4.4|^5.0",
                "twig/twig": "^2.10|^3.0"
            },
            "suggest": {
                "ext-apcu": "For best performance of the system caches",
                "symfony/console": "For using the console commands",
                "symfony/form": "For using forms",
                "symfony/property-info": "For using the property_info service",
                "symfony/serializer": "For using the serializer service",
                "symfony/validator": "For using validation",
                "symfony/web-link": "For using web links, features such as preloading, prefetching or prerendering",
                "symfony/yaml": "For using the debug:config and lint:yaml commands"
            },
            "type": "symfony-bundle",
            "extra": {
                "branch-alias": {
                    "dev-master": "5.0-dev"
                }
            },
            "autoload": {
                "psr-4": {
                    "Symfony\Bundle\FrameworkBundle\": ""
                },
                "exclude-from-classmap": [
                    "/Tests/"
                ]
            },
            "notification-url": "https://packagist.org/downloads/",
            "license": [
                "MIT"
            ],
            "authors": [
                {
                    "name": "Fabien Potencier",
                    "email": "fabien@symfony.com"
                },
                {
                    "name": "Symfony Community",
                    "homepage": "https://symfony.com/contributors"
                }
            ],
            "description": "Symfony FrameworkBundle",
            "homepage": "https://symfony.com",
            "support": {
                "source": "https://github.com/symfony/framework-bundle/tree/v5.0.11"
            },
            "funding": [
                {
                    "url": "https://symfony.com/sponsor",
                    "type": "custom"
                },
                {
                    "url": "https://github.com/fabpot",
                    "type": "github"
                },
                {
                    "url": "https://tidelift.com/funding/github/packagist/symfony/symfony",
                    "type": "tidelift"
                }
            ],
            "time": "2020-07-23T08:36:09+00:00"
        },
    ...
}

symfony.lock

{
    ...
    "symfony/framework-bundle": {
        "version": "4.4",
        "recipe": {
            "repo": "github.com/symfony/recipes",
            "branch": "master",
            "version": "4.4",
            "ref": "23ecaccc551fe2f74baf613811ae529eb07762fa"
        },
        "files": [
            "config/bootstrap.php",
            "config/packages/cache.yaml",
            "config/packages/framework.yaml",
            "config/packages/test/framework.yaml",
            "config/routes/dev/framework.yaml",
            "config/services.yaml",
            "public/index.php",
            "src/Controller/.gitignore",
            "src/Kernel.php"
        ]
    },
    ...
}

考虑你的输出:

Restricting packages listed in "symfony/symfony" to "5.0.*"

您目前在 composer.json 中有类似的内容:

"extra": {
    "symfony": {
      "require": "5.0.*"
    }
  },

这意味着您无法更新到打包大于 5.0.* 的 Symfony。

但现在您正尝试在更高版本上安装框架包,但您无法更新各个组件。

“干净”地做到这一点的方法是逐步进行。

首先将您需要的 Symfony 版本更改为 5.2.*,将所有 Symfony 软件包更新为该版本(是的,需要您发布 composer update),处理更新后的任何问题(如果有) ,然后然后安装您要安装的框架包。

您已经在 composer.json 中使用了多个版本的 Symfony 包,例如:

    "symfony/asset": "5.0.*",
    "symfony/console": "5.0.*",
    "symfony/debug-bundle": "5.0.*",
    "symfony/dotenv": "5.0.*",
    "symfony/framework-bundle": "5.0.*",
    "symfony/routing": "5.1.*",
    "symfony/yaml": "5.0.*"

你应该清理它以避免不兼容的包。通过使用 Flex 或手动设置所有包版本来执行此操作


Flex 的使用

将所有与 Symfony 相关的依赖项设置为 "*",像这样配置 Symfony 版本:

"extra": {
  "symfony": {
    "require": "5.2.*"
  }
},

之后,通过composer update "symfony/*"更新Symfony依赖。

任何以后升级到下一个版本只需要在此部分中进行一次更改。


“手动”配置

将所有与 Symfony 相关的依赖设置为 "5.2.*",通过 composer update "symfony/*"

更新它们