开发自己的composer包

composer简介

1
Composer 是 PHP 的一个依赖管理工具。它允许你申明项目所依赖的代码库,它会在你的项目中为你安装他们。

准备工作

1.一个github账号
2.一个packagist账号
3.一台安装composer的开发机

packagist注册链接 github注册链接 composer注册链接
# 发布流程

1.首先在github上创建一个项目

2.把项目克隆到本地

3.进入项目根目录初始化项目

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
➜  xdpframework git:(master) composer init


Welcome to the Composer config generator



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

Package name (<vendor>/<name>) [shiwenyuan/xdpframework]:
Description []: a xdp framework
Author [北行10000 <13341007105@163.com>, n to skip]:
Minimum Stability []: dev
Package Type (e.g. library, project, metapackage, composer-plugin) []:
License []: MIT

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:

{
"name": "shiwenyuan/xdpframework",
"description": "a xdp framework",
"license": "MIT",
"authors": [
{
"name": "北行10000",
"email": "13341007105@163.com"
}
],
"minimum-stability": "dev",
"require": {}
}

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

此时目录下回程车一个composer.json文件,文件内容形如

1
2
3
4
5
6
7
8
9
10
11
12
13
{
"name": "shiwenyuan/xdpframework",
"description": "a xdp framework",
"license": "MIT",
"authors": [
{
"name": "北行10000",
"email": "13341007105@163.com"
}
],
"minimum-stability": "dev",
"require": {}
}

4.编写自己的composer包

4.1 此处我创建了一个src目录并实现了SayHello类

1
2
3
4
5
6
➜  xdpframework git:(master) ✗ tree
.
├── README.md
├── composer.json
└── src
└── SayHello.php

4.2 修改composer.json

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
{
"name": "shiwenyuan/xdpframework",
"description": "a xdp framework",
"license": "MIT",
"authors": [
{
"name": "北行10000",
"email": "13341007105@163.com"
}
],
"minimum-stability": "dev",
"require": {},
"autoload": {
"psr-4": {
"XdpFrameWork\\": "src/"
}
}
}

4.3 自动加载src目录

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
在4.2添加了src的加载运行composer install实现自动加载
➜ xdpframework git:(master) ✗ composer install
Loading composer repositories with package information
Updating dependencies (including require-dev)
Nothing to install or update
Generating autoload files
此时目录中会生成一个vendor 它实现了加载方法
➜ xdpframework git:(master) ✗ tree
.
├── README.md
├── composer.json
├── src
│   └── SayHello.php
└── vendor
├── autoload.php
└── composer
├── ClassLoader.php
├── LICENSE
├── autoload_classmap.php
├── autoload_namespaces.php
├── autoload_psr4.php
├── autoload_real.php
├── autoload_static.php
└── installed.json

4.4 测试结果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
创建test.php
<?php
/**
* Created by PhpStorm.
* User: shiwenyuan
* Date: 2018/8/2 13341007105@163.com
* Time: 下午8:56
*/
require_once __DIR__."/vendor/autoload.php";
use XdpFrameWork\SayHello;
SayHello::world();
命令行中运行
➜ xdpframework git:(master) ✗ php test.php
hello world

4.5 忽略vendor、composer.json文件

1
2
修改.gitignore增加下面两行
/vendor/

4.6提交代码到 github


5. Packagist配置

5.1 github关联到Packagist

1
2
3
4
5
6
1.首先要在Packagist上登录
2.点击顶部导航条中的Summit按钮
3.在输入框中输入github上的仓库地址,如:https://github.com/shiwenyuan/xdpfarmwork
4.然后点击Check按钮
5.Packagist会去检测此仓库地址的代码是否符合Composer的Package包的要求
6.检测正常的话,会出现Submit按钮,再点击一下Submit按钮,我们的包就提交到Packagist上了

5.2 配置自动同步

1
2
3
1.从Packagist点击个人中心点击profile获取token然后复制
2.到github项目首页上点击settings、点击webhooks、点击addwebhook
3.把https://packagist.org/api/bitbucket?username=USERNAME&apiToken=TOKEN 复制到Payload URL,在下方输入密码后保存

5.3 打tag测试


引用包

1
composer require shiwenyuan/xdpframework

注意事项

1
2
3
如果下载不下来到话就换一下镜像
composer config -g repo.packagist composer https://packagist.laravel-china.org
刚发布上去可能会下载不到,没有同步过来-- 可以等一会在试试看