如何建立自己的composer包

参考文档:https://www.jianshu.com/p/280acb6b0b22
参考文档:https://blog.csdn.net/m_nanle_xiaobudiu/article/details/104231469

准备工作

1.在github上创建一个仓库并克隆来到本地(不会的自行百度)
2.初始化composer

直接看命令操作步骤,操作完得到一个composer.json

D:\phpstudy_pro\WWW\lock>composer init


  Welcome to the Composer config generator



This command will guide you through creating your composer.json config.

Package name (<vendor>/<name>) [dajx/lock]: hcg/lock
Description []: Redisb并发锁
Author [, n to skip]: HuangCuiGang <532508307@qq.com>
Minimum Stability []: dev
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]? no
Would you like to define your dev dependencies (require-dev) interactively [yes]? no

{
    "name": "hcg/lock",
    "description": "Redis并发锁",
    "type": "library",
    "license": "MIT",
    "authors": [
        {
            "name": "HuangCuiGang",
            "email": "532508307@qq.com"
        }
    ],
    "minimum-stability": "dev",
    "require": {}
}

Do you confirm generation [yes]? yes
Would you like the vendor directory added to your .gitignore [yes]? yes

3.补充composer自动加载

因为我们这个包要遵循PSR-4自动加载规范,项目中composer引入这个包才能自动加载
顺便把require依赖也一起配置了,保存后执行下安装依赖文件(composer install)

{
    "name": "hcg/lock",
    "description": "Redis并发锁",
    "type": "library",
    "license": "MIT",
    "authors": [
        {
            "name": "HuangCuiGang",
            "email": "532508307@qq.com"
        }
    ],
    "minimum-stability": "dev",
    "require": {
        "php": ">=7.0",
        "predis/predis": "^1.1"
    },
    "autoload": {
        "psr-4": {
            "Lock\\": "src/",
            "Lock\\Lock\\": "src/Lock"
        }
    }
}

4.写个测试文件测试一下,保证这个包可用

代码比较简单,直接上截图吧

1596684995-ab3ce794ec5d22c50b85c07c95ed32c.png插图

1596685206-d784200a66cebfc73ecebce99a798d9.png插图(1)

测试一下,ok的

1596685060-aefea0ad6c987b660e42930dfc192d2.png插图(2)

5.写好忽略文件
1596685313-e1a263fd66dc8662957c9b38f179ab2.png插图(3)

6.提交代码到github仓库中
1596685452-d6c4d0cdc0bc5eb7a8429dbb04ab563.png插图(4)

提交代码到packagist.org

1.到packagist.org上注册并登陆好
2.进入包提交页提交包

如图, 获取github仓库的https的仓库地址填入地址栏 点击 check 之后点击 submit
这里操作完,包就已经上传完成了,此时,如果你想用这个包,是不可以的,因为我们还没有发布任何版本

1596686882-2bf7a6bfb752cb62626c9a5134592f1.png插图(5)
1596686896-9bcdcfcde7197a257191f44b147c7ed.png插图(6)

3.给代码打个版本Tag
1596687069-d6443ea3363f488f8f0fdb34d19a974.png插图(7)

4.到GitHub发布一个版本

到这里,咋们的composer包,就做好了
1596687132-cf2837b348f3df1f73701546237f81d.png插图(8)


1596689060-cdbfa51e6d49967d964889c41bbfb16.png插图(9)


1596687149-2ab5e1c3dd37d86b9df9fce072291c6.png插图(10)

随便找个项目引入一下包,测试一下,OK了

1596689181-80e499797c127dd35d1bea1bd082af8.png插图(11)


1596689737-07776e140b947393fc6b2a94577294d.png插图(12)


1596689499-f67b5caea4c59e86d09e750f53f6f1a.png插图(13)

相关推荐

发表评论

电子邮件地址不会被公开。 必填项已用*标注

微信扫一扫,分享到朋友圈

如何建立自己的composer包
返回顶部

显示

忘记密码?

显示

显示

获取验证码

Close