初心 - packagist
type: channel
link: https://mayanpeng.cn/tag/packagist/
items: 提交自定义包到githb并设置自动composer包自动更新

提交自定义包到githb并设置自动composer包自动更新
type: item
date: 2022-12-28T15:20:41+08:00
description: 项目中频繁使用的代码片段，通常会被封装成全局方法或类，以提高程序的复用性。但初始化新项目，手动引入会相对麻烦，此时可以提交到composer上实现自动安装。大致步骤创建Git代码仓库使用composer在本地初始化提交代码到线上Git仓库，如Git、GitLab等在packagist平台提交git版本库设置packagist自动更新1. 创建Git仓库各Git托管平台操作类似，以GitHub为例:2. 初始化composer创建完成Git仓库后，克隆仓库到本地，在仓库根目录执行composer init进行初始化composer init按照命令要求依次录入项目命名空间、项目描述、作者信息、最低稳定版本、项目类型、授权类型、依赖信息等信息，生成composer.json文件 Welcome to the Composer config generator This command will guide you through creating your composer.json config. Package name (<vendor>/<name>) [ponycool/ponycool-library]: Description []: PonyCool Library is an extension library for the CodeIgniter framework Author [Pony <pony@ponycool.com>, n to skip]: n Minimum Stability []: Package Type (e.g. library, project, metapackage, composer-plugin) []: library License []: MIT Define your dependencies. Would you like to define your dependencies (require) interactively [yes]? yes Search for a package: php Enter the version constraint to require (or leave blank to use the latest version): 8.0 Search for a package: Would you like to define your dev dependencies (require-dev) interactively [yes]? yes Search for a package: Add PSR-4 autoload mapping? Maps namespace "Pony\PonycoolLibrary" to the entered relative path. [src/, n to skip]: { "name": "Ponycool/ponycool-library", "description": "PonyCool Library is an extension library for the CodeIgniter framework", "type": "library", "require": { "php": "8.0" }, "license": "MIT", "autoload": { "psr-4": { "Ponycool\\PonycoolLibrary\\": "src/" } } } Do you confirm generation [yes]? yes Would you like the vendor directory added to your .gitignore [yes]? yes Would you like to install dependencies now [yes]? yescomposer.json文件的详细参数可以参考此链接3. 提交代码到代码仓库提交代码到线上代码平台操作非常简单，在这不再赘述。线上代码仓库尽量选择GitHub、GitLab、Bitbucket、Gitea。目前packagist官方暂时只支持以上4个平台的webhook，其他代码平台需自行实现update-packages4. 在packagist平台提交git版本库打开packagist，通过用户名/密码登录到首页后，点击首页上方的submit按钮提交代码仓库录入Git仓库的仓库地址，点击check，如果仓库中缺少composer.json文件是不能提交的。会提示以下内容：Warning:No composer.json was found in the main branch…