![](/img/trans.png)
[英]How to install the latest version of a package using composer in the command line?
[英]Get version of Composer package(s) in PHP, without using the command line
有沒有辦法獲取與 Composer 一起安裝的軟件包的版本,並且當前由應用程序使用,而無需調用composer show -i
或任何類似的東西?
我想確定應用程序當前使用的版本,並在需要更新某些包並最終自動更新時顯示警報。
對於當前的任務,我認為合適的解決方案如下。
Composer 在vendor/composer下創建一個installed.json文件,其中包含有關已安裝軟件包的所有信息,如定義的那樣。
該文件看起來與此類似。
[
{
"name": "vendor_X/package_Y",
"version": "1.0.0",
"version_normalized": "1.1.0.0",
"source": {},
"dist": {},
"require": {},
"require-dev": {},
other data about the package
},
{"..other package's data..": ""},
{"...": ""}
]
一個簡單的解決方案是使用以下代碼。
$data = array();
$packages = json_decode(file_get_contents('../vendor/composer/installed.json'));
// Assuming that the project root is one level above the web root.
foreach ($packages as $package) {
$data[$package['name']] = $package['version'];
}
// Make a cURL request to packagist.org to get the package data
// https://packagist.org/packages/vendor_X/package_Y.json
// The output is something like the following
/*
{
"package": {
"name": "omnipay/dummy",
"versions": {
"dev-master": {},
"v1.1.0": {},
"v1.0.0": {}
},
"type": "library",
}
}
*/
$packagist = json_decode($packagist_reponse_as_json);
if (strcmp($data['vendor_X/package_Y'], $packagist['package']['versions'][1]) < 0) {
// Fire a composer update, send email alert, show notification, or call the president.
}
上面的解決方案是最簡單的,也有點丑,但它展示了關鍵點,即從哪里獲取包的本地版本以及如何從Composer獲取包版本。
更實用的解決方案應該使用緩存,而不應該在正常的應用程序操作期間進行; 更好的解決方案是使用 cron 作業。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.