代码语言:javascript复制
1.新建一个git仓库
备注:可以在github建立,如果要建立私有的composer,可以自己搭建git仓库
2.初始化composer文件
执行composer init
参考:https://getcomposer.org/doc/04-schema.md
{
"name": "joyousphper/composer-demo",
"description": "composer demo",
"type": "library",
"license": "MIT",
"authors": [
{
"name": "joyousphper",
"email": "joyousphper@gmail.com"
}
],
"minimum-stability": "stable",
"require": {
"php": ">=5.4.0"
},
"autoload": {
"psr-4": {
"Tools\": "src/Tools/"
}
}
}
3.编写代码并推送git仓库
新建src/Tools/Common.php
<?php
namespace Tools;
class Common
{
public static function demo()
{
return 'hello world';
}
}
添加标签
git tag -a v0.1.1 -m'第二个版本'
git push origin tag v0.1.1
4.提交composer
https://packagist.org/packages/submit
5.查看提交composer包
https://packagist.org/packages/joyousphper/composer-demo
6.设置composer自动更新
6.1打开https://github.com/JoyousPHPer/composer-demo/settings/installations添加一个Packagist服务
6.2https://github.com/JoyousPHPer/composer-demo/settings/hooks设置hook,这里的apitoken可以从https://packagist.org/profile/查看
7.项目引用
{
"name": "joyousphper/demo1",
"description": "composer demo",
"type": "project",
"license": "MIT",
"authors": [
{
"name": "joyousphper",
"email": "joyousphper@gmail.com"
}
],
"minimum-stability": "dev",
"require": {
"joyousphper/composer-demo": "v0.1.*"
}
}
8.使用案例
<?php
require __DIR__ . '/../vendor/autoload.php';
use ToolsCommon;
echo Common::demo();