使用 Composer CLI 将数据添加到额外的 属性

Use Composer CLI to add data to the extra property

根据 extra 属性 的 composer.json schema 的文档,允许设置 "arbitrary extra data for consumption by scripts."

出于脚本目的,如果可以通过命令行将数据添加到 extra 属性,那就太好了。它已尝试 composer config extra.foo bar,但这给出了错误 Setting extra.foo does not exist or is not supported by this command

所以我想知道:有没有办法使用 Composer CLI 向 extra 属性 添加数据?

更新:Composer 1.1.0 添加了对此功能的支持:https://getcomposer.org/doc/03-cli.md#modifying-extra-values 遗憾的是,无法添加布尔值或数值,因为每个值都是作为字符串添加的。另见 issue #5492 of the Composer project

没有办法,原因是这通常与某些不适用于一般受众的非常具体的本地用例相关联。

此处列出了可以受 composer config 影响的所有参数:https://getcomposer.org/doc/06-config.md

如果您想将数据添加到 "extra" 部分,您必须手动编辑它或让您的脚本以其他方式执行。

从 Composer 1.1.0 开始,可以使用 CLI 将 string 值添加到 extra 属性:

composer config extra.foo "some text"
composer config extra.bar 123
composer config extra.baz true

结果如下:

"extra": {
    "foo": "some text",
    "bar": "123",
    "baz": "true"
}

从 Composer 2.0 开始,可以使用 --json--merge 标志在任何 JSON 值类型 中添加值 。这包括添加数字和布尔值的可能性:

composer config --json extra.foo '"some text"'
composer config --json extra.bar 123
composer config --json extra.baz true

结果如下:

"extra": {
    "foo": "some text",
    "bar": 123,
    "baz": true
}

documentation of this feature 展示了如何从 CLI 添加 JSON 对象。