手把手教你制作属于自己的composer包
PHP

手把手教你制作属于自己的composer包

远航
2022-05-13 / 0 评论 / 272 阅读 / 正在检测是否收录...
准备工作
// 先建个项目文件夹 我的叫easyUpload 然后执行 composer init
[root@localhost easyUpload]# composer init

  // 欢迎使用composer配置生成器
  Welcome to the Composer config generator

// 教你创建配置文件
This command will guide you through creating your composer.json config.
// 请输入包名 一定要 (<vendor>/<name>) 格式 一般都有默认值的不改就直接回车
Package name (<vendor>/<name>) [yuanhang/easy-upload]:
// 描述 接收一下你这个高级的包
Description []: Integrate Qiniu and Ali OSS,Compatible with ThinkPhp 5,5.1,6 Laravel
// 作者一般都有默认值的不改就直接回车
Author [Voyage <Voyage@Voyage.com>, n to skip]:
// 最小稳定版本 stable, RC, beta, alpha, dev 我填dev
Minimum Stability []: dev
// 包类型,我是类库所以填 library
Package Type (e.g. library, project, metapackage, composer-plugin) []: library
// 开源协议
License []: Apache-2.0
// 定义你的依赖
Define your dependencies.
// 你这个包需要依赖吗,不需要就直接回车我这里有需要就填上
Would you like to define your dependencies (require) interactively [yes]? yes
// 搜索我需要依赖的包 这里搜索php
Search for a package: php
// 依赖包的版本 这里>=7.4
Enter the version constraint to require (or leave blank to use the latest version):  >=7.4
// 继续添加依赖 没有就直接回车
Search for a package:qiniu/php-sdk
Enter the version constraint to require (or leave blank to use the latest version):  >=7.3
Search for a package:aliyuncs/oss-sdk-php
Enter the version constraint to require (or leave blank to use the latest version):  >=2.3
// 继续添加依赖 没有就直接回车 我添加了三个包
Search for a package:
// 继续添加依赖 没有就直接回车
Would you like to define your dev dependencies (require-dev) interactively [yes]?
// 继续添加依赖 没有就直接回车
Search for a package:
// 映射自动加载命名空间,就是 就是某个文件夹下的src会自动加载 我这里没问题就直接回车
Add PSR-4 autoload mapping? Maps namespace "EasyUpload" to the entered relative path. [src/, n to skip]:
// 生成预览文件
{
    "name": "yuanhang/easy-upload",
    "description": "Integrate Qiniu and Ali OSS,Compatible with ThinkPhp 5,5.1,6 Laravel",
    "type": "library",
    "require": {
        "php": ">=7.4",
        "qiniu/php-sdk": ">=7.3",
        "aliyuncs/oss-sdk-php": ">=2.3"
    },
    "license": "Apache-2.0",
    "autoload": {
        "psr-4": {
            "EasyUpload\\": "src/"
        }
    },
    "authors": [
        {
            "name": "Voyage",
            "email": "Voyage@Voyage.com"
        }
    ],
    "minimum-stability": "dev"
}
// 确认一下有没有问题,这里不管有没有问题都回车
Do you confirm generation [yes]?
// 你要安装依赖吗 执行composer install的意思
Would you like to install dependencies now [yes]?
// 回车后下面执行东西不用管
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
PSR-4 autoloading configured. Use "namespace Yuanhang\EasyUpload;" in src/
Include the Composer autoloader with: require 'vendor/autoload.php';
生成的composer.json文件你也可以自己更改,刚刚的步骤只是为了生成这个文件
{
    "name": "yuanhang/easy-upload",
    "description": "Integrate Qiniu and Ali OSS,Compatible with ThinkPhp 5,5.1,6 Laravel",
    "type": "library",
    "require": {
        "php": ">=7.4",
        "qiniu/php-sdk": ">=7.3",
        "aliyuncs/oss-sdk-php": ">=2.3"
    },
    "license": "Apache-2.0",
    "autoload": {
        "psr-4": {
            "EasyUpload\\": "src/"
        }
    },
    "authors": [
        {
            "name": "Voyage",
            "email": "Voyage@Voyage.com"
        }
    ],
    "minimum-stability": "dev"
}
然后在src目录下按规范编写你的高级代码

iShot_2022-05-13_11.30.54.png

提交到github并新建个标签 v1.0.0 这就是你的第一个版本啦

然后打开 composer官网(点击🚀直达) 使用github登录

因为我已经上传过了,所以会提示存在同名包

iShot_2022-05-13_11.41.29.png
如果不同名你们的按钮会变成Submit
iShot_2022-05-13_11.43.02.png
点击Submit,这样你的composer包就可以用啦

配置自动更新 打开这个 地址(点击🚀直达)复制里面的信息

iShot_2022-05-13_11.52.36.png

获取Secret

iShot_2022-05-13_11.55.51.png

打开github你这个项目

iShot_2022-05-13_11.53.44.png

添加WebHook

iShot_2022-05-13_11.57.48.png

这样就可以提交git自动更新composer库啦,但是记得发布版本哦,就是建一个git标签

iShot_2022-05-13_11.59.38.png

0

评论

博主关闭了当前页面的评论