如何在composer上发表自己的组件

第一步: 首先在本地创建一个测试目录.

# mkdir demodir					创建组件目录
# cd demodir				       进入组件目录

1.1 创建一个composer.json来做一个php依赖包管理

# composer init

填写个人基本信息后,一路回车到结束

{
    "name": "myfirst/assemble",
    "description": "我的第一个组件",
    "authors": [
        {
            "name": "Jiawen Chen",
            "email": "604989282@qq.com"
        }
    ],
    "require": {}
}

1.2 创建自己的库文件和测试文件(用目录区分)

测试文件的目录结构为:example/test.php;

<?php

include '../src/MyLibrary.php';

$obj = new  myfirst\assemble\MyLibrary();

$obj -> func();

资源库文件的目录结构为:src/MyLibrary.php;

<?php 
namespace myfirst\assemble;

class MyLibrary{
    public function func(){

	echo "加载我的组件\n";
    }
}

此刻可以正常运行我们的测试文件example/test.php;

1.3 然后让我们的目录结构符合psr4规范

{
    "name": "myfirst/assemble",
    "description": "我的第一个组件",
    "authors": [
        {
            "name": "Jiawen Chen",
            "email": "604989282@qq.com"
        }
    ],
    "require": {
    
    },
    "autoload": {
		"psr-4": {
		    "myfirst\\assemble\\": "src/"
		}
    }
}

注意:这个psr-4设置特点是:所有的myfirst\assemble全部指向这个composer.json相对的src目录下.

第二部 上传到github上,并同步到composer上.

2.1 在github上新建一个 repository

2.1.1创建一个New organization,和你的项目名称前缀对应,比如此项目中的前缀myfirst(composer.json中name值的前缀).

2.1.2 创建一个新的githun仓库,其中选择上面新建的organization,Repository name对应本测试组件的assemble(composer.json中name值的后缀).

2.1.3 选择公开单选框,并勾选Initialize this repository with a README,创建仓库时,并在一级文档创建README.MD

2.2 为本地项目添加远程仓库,并同步到github上

2.2.1 在github的下载中,选择ssh地址

git@github.com:cjwFirst/assemble.git

2.2.2 把本地推送到远程仓库

克隆github的远程仓库

#git clone git@github.com:cjwFirst/assemble.git
#cp -r demodir/* ./assemble/

然后上面代码推向远程仓库....(注意:本地文件是否有推送权限,github的公钥是否可写)

2.3 把github上内容发布到composer上

  1. 登陆composer上
  2. 然后点击submit按钮
  3. 把github上仓库的https链接复制到表单中
https://github.com/cjwFirst/assemble.git

第三部分 使用composer 拉去组件

1.把源切换到composer官网上,因为中国镜像不回那么快的同步过来

composer config -g repo.packagist composer https://packagist.org

2.此刻,就可以使用composer拉去自己的组件了.

#mkdir testMyComposer			另外起一个工作目录,进行测试

#cd  testMyComposer		

#composer require myfirst/assemble

3.此刻会报错,因为根本不是正式版本(发布版本),必须带上开发分支;

#composer require myfirst/assemble:dev-master

第四部分 对拉下的组件进行测试

  1. 新建test.php文件,并且引入vendor下的自动加载文件,如下所示
<?php

include 'vendor/autoload.php';

$obj = new myfirst\assemble\MyLibrary();

$obj -> func();

2.正确的输出了我们想要的结果

加载我的组件

第五部分 大体归纳

vendor目录下的组件,一级目录代码组织,二级目录代表github上的仓库名

这个组件是没有表明开源协议,我们可以这样做:

在composer.json中这样标明
{
	....
	"license": "MIT",
	....
}

并且同composer.json目录下新建LICENSE文件,写MIT的原文(注意更改事件和所属人);

Copyright (C) <year>时间 <copyright holders>所属人

  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
  
  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

3.如果是对组件的二次开发.请在readme.md中说明

4.当然我们使用composer拉包时,可以指定对应的版本号,也可以指定对应的分支。

5.当然我们也可以对外公开一个版本,在项目中releases释放一个版本(版本branch旁边一个按钮);

比如:1.0.1 @ Target:master

ps:从中我们可以发现,公开一个版本相当于一个版本的快照

6.当然,公开一个新版本后,我们必须让其生效:需要使用curl请求一下:

curl -XPOST -H'content-type:application/json' 'https://packagist.org/api/update-package?username=???????&apiToken=??????' -d'{"repository":{"url":"https://github.com/cjwFirst/assemble.git"}}'

返回{“status”:”success”}表示成功。

7.怎么获取apiToken? (https://packagist.org/)

在composer的包管理的首页中————profile中

8.当然,我们的组件需要多一段时间才能在中国镜像里面拉取。

您的支持是对我最大的鼓励!

发表于: 作者:吕倡
博主经历空降兵部队8年军旅生涯,退伍后到北京IT兄弟连学习软件编程开发,工作不到一年后IT进入兄弟会,一晃在IT行业混迹了5、6年的时间。。。从一名小白也进入到了IT管理者的位置。博客,是博主学习知识分享以及私人笔记(未发布的博客)留存的地方,方便随时观看。
我的站点 Github 新浪微博 Email联系我