我尝试创建一个PHP类来安装新的作曲家库。
它将删除composer.json
内的所有其他库
您是否有想法在不删除所有库的情况下添加库?
谢谢
public static function install(string $packages)
{
if ($packages == self::dump()) {
return false;
}
putenv('COMPOSER_HOME=' . BASE_DIRECTORY . 'External/vendor/bin/composer');
self::createComposerJson($packages);
// Setup composer output formatter
$stream = fopen('php://temp', 'w+');
$output = new StreamOutput($stream);
// Programmatically run `composer install`
$application = new Application();
$application->setAutoExit(false);
$application->run(new ArrayInput(array('command' => 'install')), $output);
// remove composer.lock
if (file_exists(static::$root . 'composer.lock')) {
unlink(static::$root . 'composer.lock');
}
// rewind stream to read full contents
rewind($stream);
return stream_get_contents($stream);
}
/**
* @param $packages
* @return false|int
*/
protected static function createComposerJson($packages) {
$array = [
array(
'config' => array('vendor-dir' => self::VENDOR_DIR),
'require' => $packages,
//
// TODO:
// windowsazure requires PEAR repository
//
'repositories' => array(array(
'type' => 'pear',
'url' => 'http://pear.php.net'
)),
'preferred-install' => 'dist'
)
];
$composer_json = str_replace("\/", '/', json_encode($array));
return file_put_contents(static::$root . 'composer.json', $composer_json, FILE_APPEND | LOCK_EX);
}
结果是composon.json的结果。有问题
{
"config": {
"vendor-dir": "includes/ClicShopping/External/vendor"
},
"require": {
"phpmailer/phpmailer": "^6.0",
"guzzlehttp/guzzle": "^6.3",
"setasign/fpdf": "1.8.1",
"phpxmlrpc/phpxmlrpc": "^4.0",
"sensiolabs/security-checker": "^6.0",
"composer/composer": "^1.9"
}
}
[{"config":{"vendor-dir":"vendor"},"require":"stripe/stripe-php","repositories":[{"type":"pear","url":"http://pear.php.net"}],"preferred-install":"dist"}]