在我的作曲家.json中,我有以下内容:
"repositories": [
{
"type": "package",
"package": {
"name": "aaemnnosttv/wordpress-core-phpunit-includes",
"type": "wordpress-tests-core",
"version": "5.0.2",
"source": {
"type": "svn",
"url": "https://develop.svn.wordpress.org/",
"reference": "tags/5.0.2/tests/phpunit/includes/"
},
"require": {
"aaemnnosttv/wordpress-tests-core-installer": "~0.1"
}
}
}
],
"require": {
"aaemnnosttv/wordpress-core-phpunit-includes": "5.0.2"
}
不幸的是,我必须重复自己指定软件包的版本。一次在"版本"行,一次在"参考"行,一次在"aaemnnosttv/wordpress-core-phpunit-include"行。
有没有办法使用作曲家变量,这样我就不必重复自己?
作曲家文件是json,JSON不允许变量引用。
但是因为您在问题中使用了PHP标签,所以我建议使用一个例子来处理它,也许它可以为您提供帮助。
创建PHP文件以设置作曲家的环境数组。
$variable = "aaemnnosttv/wordpress-core-phpunit-includes";
$version = "5.0.2";
$composer = [
"repositories" => [
[
"type" => "package",
"package" => [
"name" => $variable
// whatever is in
]
]
],
"require" => [
$variable => $version
]
];
然后将数组另存为 Composer.json 文件中的 JSON。
file_put_contents('compose.json', json_encode($composer));
如果您只想编辑作曲家文件,则必须阅读它并编辑:
$composer = json_decode(file_get_contents('compose.json', true));
$variable = "aaemnnosttv/wordpress-core-phpunit-includes";
$version = "5.0.2";
$composer["repositories"][0]["package"]["name"] = $variable;
$composer["require"][$variable] = $version;