假设我在包中需要ext-oauth
处理OAuth签名生成的代码。
{
"name":"vendor/my-package",
"type":"library",
"require":{
"ext-oauth":"*"
}
}
当我尝试在没有扩展名的系统上安装此软件包时,Composer会按预期引发错误。
$ composer install
Loading composer repositories with package information
Updating dependencies (including require-dev)
Your requirements could not be resolved to an installable set of packages.
Problem 1
- The requested PHP extension ext-oauth * is missing from your system. Install or enable PHP's oauth extension.
显然,此系统上缺少ext-oauth
包,通过composer show --platform
检查确认了这一点。
假设系统无法更新以包含扩展名,虚拟化(通过Docker或Vagrant)也不是一个选项。使用package
provide
ext-oauth
require
{
"name":"vendor/my-package",
"type":"library",
"require":{
"vendor/ext-oauth":"1.0",
"ext-oauth":"*"
},
"repositories":[
{
"type":"package",
"package":{
"name":"vendor/ext-oauth",
"version":"1.0",
"dist":{
"url":"https://example.com/ext-oauth.zip",
"type":"zip"
},
"provide":{
"ext-oauth":"1.0"
},
"autoload":[
"files":[
"src/oauth.php",
"src/oauthexception.php"
]
]
}
}
]
}
扩展或ext-oauth
来自packagist的包是否是一个合理的解决方案?
此合成测试出现即可工作。
$ composer install
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 1 install, 0 updates, 0 removals
- Installing vendor/ext-oauth (1.0): Downloading (100%)
Writing lock file
Generating autoload files
然后,Composer会下载提供的填充程序并安装它,标记{{1}}依赖项已满足。
{{1}}
这感觉不对,但似乎以可维护的方式实现了对依赖的满足。有没有一个很好的理由 - 除了代码味道 - 以避免在无法安装PHP扩展时提供这种填充程序?