composer私有仓库


需求分析

  • 管理项目中已经安装的php组件包
  • 管理项目中的私有组件包
  • 解决国外网站访问慢的问题
  • github上的组件包成千上万个,只对常用的组件进行私有管理

软件设计

  • 解析composer.lock文件
  • 下载composer包文件
  • 生成配置文件
  • 生成网页

编码

{
    "type": "git",
    "url": "git@aliyun-php-sdk-core.git"
},
{
    "type": "vcs",
    "url": "https://github.com/asm89/stack-cors.git",
    "v": {
        "v2.0.3": "9cb795bf30988e8c96dd3c40623c48a877bc6714"
    }
}
$curl = curl_init();
curl_setopt_array($curl, [
    CURLOPT_URL => $url,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => "",
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 300,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => "GET",
    CURLOPT_POSTFIELDS => "",
    CURLOPT_HTTPHEADER => [
        "Postman-Token: 8709bb7f-47c3-457e-bfb4-82a7d57cd1f3",
        "cache-control: no-cache",
    ],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
// 保留所有分支
foreach ($gitDriver->getBranches() as $version => $reference) {
    $data['version'] = $version;
    $data['source'] = ['type' => 'git', 'url' => $gitDriver->getUrl(), 'reference' => $reference];
    $package = $this->loader->load($data);
    array_push($this->result, $package);
}
// 生成composer.json文件
$packagesBuilder = new PackagesBuilder($this->output, $outputDir, (array) Factory::createConfig(), false);
$packagesBuilder->dump($this->result);
// 生成网页
$config = ['twig-template' => base_path('satis/views/index.html.twig')];
$webBuilder = new WebBuilder($this->output, $outputDir, $config, false);
$rootPackage = new RootPackage('刘华世的Composer私有仓库', '2.1.0.0', 'v2.1');
$rootPackage->setHomepage($this->homePage);
$rootPackage->setDescription($this->homePage);
$webBuilder->setRootPackage($rootPackage);
$webBuilder->dump($this->result);