Composer:从 fork 扩展另一个的包

Composer: Package that extends another from a fork

我有以下情况:GitHub 和 Packagist 上有包 author/package1。我决定将它分叉到 tcb13/package1 中以添加一些代码(最终将合并到原始项目中,但这需要时间)。

后来,我开发了另一个包,tcb13/package1-extension,通过继承 fork tcb13/package1 的一些方法到新的 类...

在我的 tcb13/package1-extensioncomposer.json 我写道:

{
    "name": "tcb13/package1-extension",
    "description": "...",
    "keywords": [

    ],
    "homepage": "https://github.com/tcb13/package1-extension",
    "license": "MIT",
    "authors": [

    ],
    "repositories":
    [
        {
            "type": "vcs",
            "url": "https://github.com/tcb13/package1/"
        }
    ],
    "require": {
        "author/package1": "dev-master"
    },

我告诉 composer 我的扩展包需要 author/package1 并且还有一个额外的存储库 https://github.com/tcb13/package1/ 并将包版本设置为 dev-master 这样 composer 就会下载 author/package1从我修改过的叉子,而不是原始包...(如果我告诉它从 tcb13/package1 下载它就不会工作)。

我将上面的代码提交给 GitHub 并试图在一个新项目中要求 tcb13/package1-extension,composer 是这样说的:

Your requirements could not be resolved to an installable set of packages.

  Problem 1
    - Installation request for tcb13/package1-extension dev-master -> satisfiable by tcb13/package1-extension[dev-master].
    -  tcb13/package1-extension dev-master requires `author/package1` dev-master -> no matching package found.

为什么会报错?当我开发我的扩展包时,我能够 require author/package1 as dev-master 因为它指向了我的 fork repo,但是当我尝试 require tcb13/package1-extension 时它看起来不像为了我的叉子并尝试去原来的项目寻找分支...为什么?

此外,这是我应该使用 fork 作为依赖项的方式吗?还有别的办法吗?我最初尝试将 fork 要求为 tcb13/package1 但它会失败,但是要求它为 author/package1 还告诉作曲家我有一个额外的存储库工作到现在。

谢谢。

我设法解决了这个问题。根据 composer 文档,这不是错误,这是出于安全考虑而设计的。

来自 GitHub comment:

Consider this: you have a project that depends on one of my packages. I depend on a package that is critical to your application. I happen to make a fork to introduce a new feature and accidentally tag a release with the repository in my composer.json. You happen to update your package and get my fork that is potentially broken and definitely not what you were expecting when you defined your package dependency.

为了能够做我想做的事,我不得不手动将 tcb13/final-projectauthor/package1 分支的 URL 添加到我的最终项目中(与我一样在 tcb13/package1-extension 上做了),例如:

{
    "repositories":
    [
        {
            "type": "vcs",
            "url": "https://github.com/tcb13/package1/" // Fork of author/package1
        },
        {
            "type": "vcs",
            "url": "https://github.com/tcb13/package1-extension"
        }
    ],
    "require": {
        "author/package1": "dev-master", // Reference to the original package, composer will fetch this from my fork defined above...
        "tcb13/package1-extension": "dev-master" // My current extension package
    }
}

composer update 之后,一切都按预期进行。