起因:因为历史原因,在本项目中composer.json基本算废了,没法直接使用composer管理扩展,今天尝试修复一下composer.json。
历史文件,可以看出来已经很久没有维护了,我们主要是恢复require的信息
{"name": "yiisoft/yii2-app-advanced","description": "Yii 2 Advanced Project Template","keywords": ["yii2", "framework", "advanced", "project template"],"homepage": "http://www.yiiframework.com/","type": "project","license": "BSD-3-Clause","support": {"issues": "https://github.com/yiisoft/yii2/issues?state=open","forum": "http://www.yiiframework.com/forum/","wiki": "http://www.yiiframework.com/wiki/","irc": "irc://irc.freenode.net/yii","source": "https://github.com/yiisoft/yii2"},"minimum-stability": "stable","require": {"php": ">=5.4.0","yiisoft/yii2": ">=2.0.27","yiisoft/yii2-bootstrap": "*","yiisoft/yii2-swiftmailer": "*","linslin/yii2-curl": "*","firebase/php-jwt": "^5.0","yiisoft/yii2-elasticsearch": "^2.0","yiisoft/yii2-redis": "^2.0","workerman/workerman": "^4.0","workerman/gateway-worker": "^3.0.0"},"require-dev": {"yiisoft/yii2-debug": "*","yiisoft/yii2-gii": "*","yiisoft/yii2-faker": "*"},"config": {"process-timeout": 1800},"extra": {"asset-installer-paths": {"npm-asset-library": "vendor/npm","bower-asset-library": "vendor/bower"}},"repositories": {"packagist": {"type": "composer","url": "https://php.cnpkg.org"}}
}
第一步:
写一个脚本通过composer show --installed 获取已存在的扩展包信息
<?php/*** 通过文件恢复composer.json*/
declare (strict_types=1);$content = `composer show --installed`;preg_match_all('#([A-z0-9\/\-\.]+)\s+([A-z0-9\.\-]+)[\S\s]+?\n#', $content, $matches);$packages = ['php' => '^7.4',
];foreach ($matches[1] as $key => $name) {$packages[$name] = $matches[2][$key];
}$packages = json_encode($packages);#下面的内容需要可以从老的composer.json中获取,如果没有可以自己定义
$data = <<<JSON
{"name": "yiisoft/yii2-app-advanced","description": "Yii 2 Advanced Project Template","keywords": ["yii2", "framework", "advanced", "project template"],"homepage": "http://www.yiiframework.com/","type": "project","license": "BSD-3-Clause","support": {"issues": "https://github.com/yiisoft/yii2/issues?state=open","forum": "http://www.yiiframework.com/forum/","wiki": "http://www.yiiframework.com/wiki/","irc": "irc://irc.freenode.net/yii","source": "https://github.com/yiisoft/yii2"},"minimum-stability": "stable","require": {$packages},"extra": {"asset-installer-paths": {"npm-asset-library": "vendor/npm","bower-asset-library": "vendor/bower"}}
}
JSON;$data = json_encode(json_decode($data), JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);file_put_contents('./composer.json', stripslashes($data));
第二步:
通过运行上面的脚本我们可以得到一个包含大部分扩展包的信息,网上大部分文档也都截止于此,这个命令主要是通过 /vendor/composer/installed.json 文件生成的,所以如果文件内本身有缺失就得人为处理
我操作方案虽然简单但很实用,首先备份一下原来的vendor,然后通过生成好的composer.json 运行安装新文件,使用文件对比工具对比两个vendor的文件差异找出缺失的安装包,手动添加在composer.json文件中
#迁移文件备份
mv vendor ../vendor#安装扩展
composer require
第三步:
勾选对比规则,这样就能一目了然的看到两个包的差异了。比较复杂的是找到这个扩展包对应的版本号,如果实在找不到可以先用*代替,以下是几个找版本号的方法
第四步:
手动添加完成后,我们再重新对比文件根据经验查看是否新生成的正常可用
当然了同样的版本号更新安装的扩展包也并不一定完全相同,就像下面这种,所以需要自己去验证,不影响正常使用那就代表我们这次还原成功。