使用Composer嵌套的依赖项和私有存储库 [英] Nested dependencies and private repositories with composer

查看:46
本文介绍了使用Composer嵌套的依赖项和私有存储库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我目前正在工作的公司中,我们最近开始将代码移至不同的私有存储库中,以使其更具可维护性和可重用性(并使其以后更易于开源).

At the company I'm currently working we've recently started to move our code into different private repositories so that it's more maintainable and reusable (and also to make it easier to open-source it later).

每个PHP存储库也是Composer软件包,只要我们需要它就可以在我们的项目中使用.

Every PHP repository is also a Composer package that can be required in our project whenever we need it.

目前,这种方法存在问题:每次我们需要依赖于其他软件包的软件包时,都需要在根 composer.json 中指定这些软件包.

At the moment there's an issue with this approach: every time we need a package that depends on other packages we need to specify those also in the root composer.json.

例如,假设在根 composer.json 中,我们需要两个软件包 company \ b company \ c ,而软件包 company \ c 需要另一个软件包 company \ d .然后,生成的根 composer.json 将如下所示:

For example, let's say that the in the root composer.json we need to require two packages company\b and company\c, and that the package company\c needs another package company\d. Then the resulting root composer.json will look like this:

{
    "require": {
        "company/b": "dev-master",
        "company/c": "dev-master",
        "company/d": "dev-master"
    },
    "autoload": {
        "psr-4": {
            "Company\\" : "src\Company"
        }
    },
    "repositories": [
        {
            "type": "vcs",
            "url":  "git@bitbucket.org:company/b.git"
        },
        {
            "type": "vcs",
            "url": "git@bitbucket.org:company/c.git"
        },
        {
            "type": "vcs",
            "url": "git@bitbucket.org:company/d.git"
        }
    ]
}

有没有一种方法可以避免在根 composer.json 中指定嵌套依赖关系,并在每个软件包中使用 composer.json 中指定的依赖关系?

Is there a way to avoid specifying nested dependencies in the root composer.json and use the ones specified in the composer.json in every package?

修改:我之前说过的所有内容仅对私有软件包有效.如果某个包(例如 company \ b )需要一个可以在Packagist上找到的公共包,那么可以在 company \ b 编写器中指定依赖项.json ,它将被导入.

Edit: Everything I stated before is valid only for the private packages. If a package, let's say company\b, needs a public package that can be found on Packagist then that dependency CAN be specified in the company\b composer.json and it will be imported.

推荐答案

您正确地发现,只有根软件包才能将存储库元数据添加到已知软件包的集合中.

As you correctly found out, only the root package can add repository metadata to the collection of known packages.

我建议您看看 Satis 来创建本地Composer存储库.这仅需要您将此单一存储库添加到所有软件包的所有 composer.json 文件中,它将用作有关所有私有存储库的可更新知识来源.您不再需要在任何地方添加Git存储库列表.

I would suggest you take a look at Satis to create a local Composer repository. This would only require you to add this single repository to all your composer.json files of all packages, and it will be used as an updatable source of knowledge about all your private repositories. You no longer have to add a list of Git repos everywhere.

我已经成功地为我们的IT企业托管了大约120个内部软件包.以此为标志,一旦开始将孤立的任务拆分为一个程序包,您将很快获得更多任务.

I am successfully hosting around 120 internal packages for our IT enterprise that way. Take this as a sign that once you start splitting isolated tasks into a package, you will get more of them pretty fast.

还请注意,认真对待版本控制很重要.停止依赖分支机构-标记软件,发布版本,使用语义版本控制.如果您不这样做,事情会在某些时候破裂,人们会因为您无法使用或弄乱事情而诅咒您(正确)或Composer(错误).

Also note that it is important to take versioning seriously. Stop depending on branches - tag your software, make releases, use semantic versioning. If you don't, things will break at some point, and people will curse you (correct) or Composer (incorrect) for not working or messing things up.

这篇关于使用Composer嵌套的依赖项和私有存储库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆