如何在包而不是项目上设置包的安装目录?

How to set the install directory for a package on the package, instead of on the project?

我正在创建自定义作曲家包。 composer 包必须安装到自定义文件夹而不是 vendors/ 目录,我希望它安装在 packages/ 中。这就是我的 composer.json 的样子:

{
"name": "demo/contentfeed",
"description": "This is yet another Lumen composer package wrapper",
"type": "lumen-plugin",
"version": "1.1.5",
"keywords": ["demo","lumen","drupal"],
"homepage": "https://github.com/gauravmehrasrijan/lumen-feeds",
"require": {
    "composer/installers": "^1.0.24"
},
"autoload": {
    "psr-4": {
        "demo\Contentfeed\": "/src"
    }
},
"extra": {
    "installer-name": "packages",
    "installer-paths": {
        "packages": ["demo/contentfeed"]
    }
},
"license": "MIT",
"minimum-stability": "dev",
"authors": [
    {
        "name": "Gaurav Mehra",
        "email": "gauravmehra1987@gmail.com"
    }
]
}

在跳到这里之前,我也尝试了中发布的解决方案,但它对我不起作用,我额外添加了 installer-name 密钥,但没有成功。

您可以将配置部分添加到您的 composer.json,并在其中定义 "vendor-dir"

{
"name": "demo/contentfeed",
"description": "This is yet another Lumen composer package wrapper",
"type": "lumen-plugin",
"version": "1.1.5",
"keywords": ["demo","lumen","drupal"],
"homepage": "https://github.com/gauravmehrasrijan/lumen-feeds",
"require": {
    "composer/installers": "^1.0.24"
},
"autoload": {
    "psr-4": {
        "demo\Contentfeed\": "/src"
    }
},
"license": "MIT",
"minimum-stability": "dev",
"authors": [
    {
        "name": "Gaurav Mehra",
        "email": "gauravmehra1987@gmail.com"
    }
],
"config": {
    "vendor-dir": "packages/"
}
}

参考:

https://getcomposer.org/doc/06-config.md#vendor-dir

这是不可能的,文档says so explicitly:

[...] The ability for a package author to determine where a package will be installed either through setting the path directly in their composer.json or through a dynamic package type: "type": "framework-install-here".

It has been proposed many times. Even implemented once early on and then removed. Installers won't do this because it would allow a single package author to wipe out entire folders without the user's consent. That user would then come here to yell at us.

(强调我的)

您使用的两个键(installer-pathsinstaller-name)的用途与您想象的不同:

  • installer-name:允许包作者(您)说您的包应该安装在与 vendor/name 不同的目录下。在您的情况下,不是安装在 vendor/demo/contentfeed 上,而是安装在 vendor/demo/packages 下(因为您在 composer.json 中的设置)
  • installer-paths:允许包 consumer 为某个包或包或包系列设置自定义安装路径。对一个包composer.json没有影响,这个设置只针对项目配置。