发布自己的composer包
在我们的开发过程中,你可能有一些非常好用的小工具或者一些非常有特色的功能点,我们可以把它做成composer包,供自己以后使用并分享给大家。
前期准备
- 代码托管网站,例如github、gitee。
- 注册packagist网站:https://packagist.org/。
打包
首先,在托管网站,创建一个仓库git clone到本地。
然后进入目录后执行初始化:
PS E:\project\sheng> composer init
Welcome to the Composer config generator
This command will guide you through creating your composer.json config.
Package name (<vendor>/<name>) [86182/sheng]: sophie/sheng #输入包名称: sophie/sheng。这里面sophie一般是指组织机构名称。sheng是包名称。
Description []: 描述信息
Author [zhangsan <xxxxx@126.com>, n to skip]: #作者信息。直接回车或者自己指定
Minimum Stability []:
Package Type (e.g. library, project, metapackage, composer-plugin) []: library #包类型
License []: #资质许可
Define your dependencies.
Would you like to define your dependencies (require) interactively [yes]? #需要依赖的包,没有就直接回车
Search for a package: #输入你需要的依赖包名
Would you like to define your dev dependencies (require-dev) interactively [yes]? #指定本地开发时需要引入的依赖包名
Search for a package: #指定本地开发时需要引入的依赖包名
# 添加PSR-4自动加载映射?将命名空间“Jackychen\Blog”映射到输入的相对路径,检查没问题直接回车。
Add PSR-4 autoload mapping? Maps namespace "Sophie\Sheng" to the entered relative path. [src/, n to skip]:
{
"name": "sophie/sheng",
"type": "library",
"autoload": {
"psr-4": {
"Sophie\\Sheng\\": "src/"
}
},
"authors": [
{
"name": "zhangsan",
"email": "xxxx@126.com"
}
],
"require": {}
}
Do you confirm generation [yes]? #直接回车生成composer.json
Generating autoload files
Generated autoload files
PSR-4 autoloading configured. Use "namespace Sophie\Sheng;" in src/
Include the Composer autoloader with: require 'vendor/autoload.php';
PS E:\project\sheng>
生成后的composer.json内容和文件目录结构。
我们可以在src文件夹内写我们自己的代码。
这个地方要注意命名空间,否则安装完成后会报class not found。
然后开始提交到远程仓库。
可以忽略这两个文件夹 在.gitignore文件内添加 /vendor/ .idea
git add .
git commit -m 'no message'
git push
git tag 1.1.1 #加上标签,以后每次发布大版本可以打上标签作为版本号
git push --tag
现在我们登录远程托管平台,我使用的是github。
打开packagist,点击右上角submit,打开下面页面。
粘贴刚才复制的项目地址,点击check,检测成功后,点击submit,注意是点了两次。下面界面是提交成功了。
这样每次提交在这个页面点击update就会同步过来了,我们也就可以设置自动同步,放在最后说吧。
现在我们就可以在项目里是用composer来引用了
composer require sophie/sheng
现在可以找一个控制器
<?php
namespace App\Http\Controllers;
use Sophie\Sheng\Index;
class ProductController extends Controller
{
public function index()
{
Index::test();
}
}
这样就完成了咱们得包发布功能,以后如果有更新,咱们可以继续迭代版本,打上标签就可以用composer update来更新了。
自动同步
打开地址:packagist.org/about 拉到下面,复制payload url。
复制token,现在我们就拿到了两个payload url和token;打开github进入我们的项目。点击setting->webhooks->add webhook->
自动同步就完成了,git提交push后会自动同步到packagist。
本作品采用《CC 协议》,转载必须注明作者和本文链接
自动同步 这一步应该是多余的