发布一个自己的composer扩展[实战系列]

第一步新建文件夹

mkdir httptool 
cd httptool

初始化composer

composer init


Welcome to the Composer config generator  


# 交互引导初始化
This command will guide you through creating your composer.json config.

# 填写包名,最好填写github名/包名
Package name (<vendor>/<name>) [afishpapa/httptool]: afishpapa/httptool

# 包描述
Description []: a simple httptool

# 作者
Author [laoxie <116265858@qq.com>, n to skip]: 
laoxie <116265858@qq.com>



# 最低稳定性, 是指你可以接受的最低稳定性级别
# dev级别最低,表示你的项目可以安装全部稳定性级别的扩展
# stable级别最高,表示只能安装stable的扩展
# 级别从低到高排序:dev,alpha,beta,RC,stable,^,~
Minimum Stability []: stable

# 包类型,,项目,还是composer插件,metapackage是一组依赖包的集合,安装这种包就等于安装一组包
Package Type (e.g. library, project, metapackage, composer-plugin) []: project

# 开源协议
License []: MIT

# 定义依赖,这里没有
Define your dependencies.

Would you like to define your dependencies (require) interactively [yes]? no

# 定义开发依赖,这里没有
Would you like to define your dev dependencies (require-dev) interactively [yes]? no

# 添加PSR-4 映射 默认 src/
Add PSR-4 autoload mapping? Maps namespace "Afishpapa\Httptool" to the entered relative path. [src/, n to skip]: src/

# 至此结束,最终在根目录生成以下文件: 
PS D:\workspace\composertest\httptool> ls
Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d-----          2023/4/7     22:10                .idea
d-----          2023/4/7     22:02                src
d-----          2023/4/7     22:02                vendor
-a----          2023/4/7     22:02            388 composer.json

# 其中composer.json
{
    "name": "afishpapa/httptool",
    "description": "simple httptool",
    "type": "project",
    "license": "MIT",
    "autoload": {
        "psr-4": {
            "Afishpapa\\Httptool\\": "src/"
        }
    },
    "authors": [
        {
            "name": "laoxie",
            "email": "116265858@qq.com"
        }
    ],
    "minimum-stability": "stable",
    "require": {}
}

Do you confirm generation [yes]? yes
Generating autoload files
Generated autoload files
PSR-4 autoloading configured. Use "namespace Afishpapa\Httptool;" in src/
Include the Composer autoloader with: require 'vendor/autoload.php';

添加内容

PS D:\workspace\composertest\httptool> ls      
Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d-----          2023/4/7     22:25                .idea
d-----          2023/4/7     22:26                example  //demo代码,介绍你这个包怎么调用
d-----          2023/4/7     22:25                src       //核心科技
d-----          2023/4/7     22:26                test     //测试单例
d-----          2023/4/7     22:02                vendor   
-a----          2023/4/7     22:13            378 composer.json
-a----          2023/4/7     22:13                README.md  //读我

github新建一个空仓库,上传刚建的扩展包

发布一个自己的composer扩展[实战系列]

echo "# httptool" >> README.md
git init
git add README.md
git add .
git commit -m "first commit"
git branch -M main
git remote add origin
# 这里填写你自己的
git@github.com:afishpapa/httptool.git
git push -u origin main

注册packagist.org账号

可以通过oauth2.0使用GITHUB账号登录packagist.org
点击submit,输入github的链接

发布一个自己的composer扩展[实战系列]

check通过之后就可以了,网站会去自动去下载代码,
如果以后包有改动需要再来这里点击update
发布一个自己的composer扩展[实战系列]

composer create-project试试看看

PS  composer create-project afishpapa/httptool
Creating a "afishpapa/httptool" project at "./httptool"

In CreateProjectCommand.php line 424:

  Could not find package afishpapa/httptool with stability stable.  

这个时候还不行,因为github还没有发布稳定版本

github release

先去打个tag

PS D:\workspace\composertest\httptool> git tag 1.0.0

PS D:\workspace\composertest\httptool> git push --tags
Total 0 (delta 0), reused 0 (delta 0), pack-reused 0
To github.com:afishpapa/httptool.git
 * [new tag]         1.0.0 -> 1.0.0

发布一个自己的composer扩展[实战系列]

发布一个自己的composer扩展[实战系列]

发布一个自己的composer扩展[实战系列]

打好包的

composer create-project 再试试看

PS > composer create-project afishpapa/httptool test1
Creating a "afishpapa/httptool" project at "./test1"
Info from https://repo.packagist.org: #StandWithUkraine
Installing afishpapa/httptool (1.0.0)
  - Downloading afishpapa/httptool (1.0.0)
  - Installing afishpapa/httptool (1.0.0): Extracting archive
Created project in D:\workspace\composertest\httptool\test1
Loading composer repositories with package information
Updating dependencies
Nothing to modify in lock file
Writing lock file
Installing dependencies from lock file (including require-dev)
Nothing to install, update or remove
Generating autoload files
No installed packages - skipping audit.

得,nice

本作品采用《CC 协议》,转载必须注明作者和本文链接
遇强则强,太强另说
《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
讨论数量: 3

这个教程不够好,把最关键的怎么调试本地包漏掉了,还有src的映射最好是手动添加执行

11个月前 评论
竖横山 (楼主) 11个月前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!