PHP Composer:是否需要至少一个依赖项或另一个依赖项? [英] PHP Composer: Require at least one dependency or another?

查看:102
本文介绍了PHP Composer:是否需要至少一个依赖项或另一个依赖项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为一个项目编写一个新的composer.json文件,以便可以使用它,也可以使用Packagist或直接引用其GitHub存储库。该项目需要一个或两个HTTP客户端扩展, pecl_http curl 。如果我在 require 下指定了两个软件包,则如果未同时安装这两个软件包,则Composer将给出错误。

I'm writing a new composer.json file for a project so that it can be available or Packagist or direct reference to its GitHub repo. The project requires either one or both of the HTTP client extensions, pecl_http or curl. If I specify both of the packages under require, Composer will give an error if both are not installed.

如何我可以告诉Composer可以只安装这两个扩展之一吗?

How can I tell Composer it's okay to have just one of those two extensions installed?

推荐答案

您不能以您自己的方式定义它

You cannot define it in a way that you only need one of these extensions, but make Composer fail if none are present.

通常的打包方法是将扩展称为建议。您的代码已经必须处理检测到哪个扩展名并选择正确的代码路径,如果全部丢失,则不会产生任何其他问题。

The usual way for packages is to mention extensions as "suggest". Your code already has to deal with detecting which extension is present and select the correct code path, it's no additional problem to emit an error if all are missing.

或者,有两个软件包,它们完全依赖于一个扩展。或在建议两个实现程序包的主程序包之上有一个主程序包,并通过其接口清楚地表明开发人员必须提供至少一个实现程序。

Alternatively, have two packages with their dependency exactly on one extension. Or have a main package on top of that which suggests the two implementation packages and makes it clear via it's interface that the developer has to provide at least one implementation.

另一方面:处理HTTP内容似乎应该依赖PSR-7接口,并让开发人员选择要使用的HTTP客户端实现。

On the other hand: Dealing with HTTP stuff looks like you should probably rely on PSR-7 interfaces, and let the developer choose the HTTP client implementation to use.

这篇关于PHP Composer:是否需要至少一个依赖项或另一个依赖项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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