【问题标题】:When "Key repositories is a duplicate in ./composer.json", which one is used by composer?当“密钥存储库在 ./composer.json 中重复”时,composer 使用哪一个?
【发布时间】:2020-06-21 11:25:29
【问题描述】:

我注意到我的 composer.json 中有重复的“存储库”键,用于我正在开发的包。

软件包正在运行。所以,我想执行“重构”,因为我想在不改变行为的情况下修复代码。

如果我想修复错误,我将不得不删除未使用的重复密钥。

所以,问题是:

当“Key repositories is a duplicate in ./composer.json”时,composer使用哪个?

换句话说,

  • composer 是否使用它找到的第一个键并忽略以后的键?
  • 它是否忽略以前的键并使用它找到的最后一个键?或者
  • 它是否以某种巧妙的方式合并了键的子项?

这是我正在运行的命令:

$ composer validate
Key repositories is a duplicate in ./composer.json at line 16
...

composer.json 看起来像这样:

{
  "name": "foo/bar",
  "description": "foo bar",
  "license": "Apache-2.0",
  "type": "library",
  "repositories": [
    {"type": "composer", "url": "foo"},
    {"packagist.org": false}
  ],

  "repositories": [
    {
      "type": "vcs",
      "url": "bar"
    }
  ],

  ...
 }

【问题讨论】:

    标签: php json composer-php operator-precedence


    【解决方案1】:

    Composer 使用 seld/jsonlintdetect 重复键,但它使用 uses json_decode 解码 JSON。

    json_decode 使用它找到的最后一个值作为键:

    >>> $json = '{"a":1,"a":10,"a":100}';
    => "{"a":1,"a":10,"a":100}"
    >>> $decoded = json_decode($json)
    => {#3260
         +"a": 100,
       }
    >>> $decoded->a
    => 100
    

    【讨论】:

      猜你喜欢
      • 2019-03-10
      • 2013-06-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-20
      相关资源
      最近更新 更多