0%

开发composer包

迟迟不愿动这块儿,感觉没有什么好封装成工具的,github上面的轮子太多了,不想重复的去造。此文仅以最基础代码进行演练测试,特此记录!

一、开干

  1. 注册github账号,配置ssh-key等,此处不再赘述
  2. 登录github,新建一个仓库,如my-first-packagist
  3. 回到本机,克隆刚新建的仓库 git clone https://github.com/liusirdotnet/my-first-packagist.git
  4. 切换到仓库目录 cd my-first-packagist
  5. 初始化composer.json文件 composer init
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
$ composer init

Welcome to the Composer config generator

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

// 1. 输入项目命名空间,默认【用户名/仓库名】
Package name (<vendor>/<name>) [dell/htdocs]: yourname/projectname

// 2. 项目描述
Description []: 这是一个测试

// 3. 输入作者信息,可以直接回车,默认当前git配置的信息
Author [test <123@qq.com>, n to skip]:

// 4. 输入最低稳定版本,stable, RC, beta, alpha, dev
Minimum Stability []: dev

// 5. 输入项目类型,
Package Type (e.g. library, project, metapackage, composer-plugin) []: library

// 6. 输入授权类型
License []: MIT //github建仓库时可指定,不写的话packagist会报错

> Define your dependencies.
// 7. 输入依赖信息
Would you like to define your dependencies (require) interactively [yes]?

// 7.1如果需要依赖,则输入要安装的依赖
Search for a package: php

// 7.2输入版本号,如需多个则重复7.1和7.2
Enter the version constraint to require (or leave blank to use the latest version): >=5.6

// 8. 是否需要require-dev,操作同7
Would you like to define your dev dependencies (require-dev) interactively [yes]?

{
"name": "yourname/projectname",
"description": "这是一个测试",
"type": "library",
"require": {
"php": ">=5.6"
},
"require-dev": {
"php": ">=5.6"
},
"license": "",
"authors": [
{
"name": "test",
"email": "test@qq.com"
}
],
"minimum-stability": "dev"
}

// 9. 是否生成composer.json
Do you confirm generation [yes]? yes
  1. 编辑composer.json文件,添加自动加载
1
2
3
4
5
"autoload": {
"psr-4": {
"namespace_name\\": "src/" //自定义命名空间名称,和类文件命名空间保持一致
}
}
  1. 新建src/Test.php
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
<?php

/**
* This is a test class
*
* @detail null
* @author Name <email@email.com>
* @date 2018-06-17
* @version v1.0
*/

namespace Liusir; // 注意命名空间与composer.json中的一致

class Test
{
/**
* This is a test function
*
* @access public
* @param string $arg1 参数一的说明
* @param int $arg2 参数二的说明
* @param mixed $mixed 这是一个混合类型
* @return array 返回类型
* @author Name <email@email.com>
* @date 2018-06-17
*/
public function testFunc()
{
echo 'This is a test script';
}
}
  1. 生成vendor目录 composer install
  2. 跟src同级目录新建demo.php
1
2
3
4
5
6
7
require './vendor/autoload.php';

use Liusir\Test;

$test = new Test();

$test->testFunc();
  1. 提交并推送到github
1
2
3
git add .
git commit -m 'first commit'
git push
  1. 发布到packagist

    • 注册packagist
    • 点击submit
    • 将github仓库输入到输入框,Check && Submit
  2. 切换到另一个项目目录,安装刚发布的包 composer require liusirdotnet/my-first-packagist,报错

1
2
3
4
  [InvalidArgumentException]                                                                                                                
Could not find a matching version of package liuyulong/second-packagist. Check the package spelling, your version constraint and that th
e package is available in a stability which matches your minimum-stability (stable).
require [--dev] [--prefer-source] [--prefer-dist] [--no-progress] [--no-suggest] [--no-update] [--no-scripts] [--update-no-dev] [--update-with-dependencies] [--update-with-all-dependencies] [--ignore-platform-reqs] [--prefer-stable] [--prefer-lowest] [--sort-packages] [-o|--optimize-autoloader] [-a|--classmap-authoritative] [--apcu-autoloader] [--] [<packages>]...
  1. 打一个tag并推送到github
1
2
3
git tag -a v1.0 -m 'first tag'
git push
git push origin v1.0
  1. 回到github后台并进入当前仓库,点击release选项,点击Draft a new release按钮,勾选下面的This is a pre-release,最后点击Publish release按钮

  2. 切换到另一个项目目录,安装刚发布的包 composer require liusirdotnet/my-first-packagist

  3. 新建index.php

1
2
3
4
5
6
<?php
require './vendor/autoload.php';
use RabbitMQ\RabbitMQ;

$ob = new RabbitMQ;
$ob->test();
  1. 设置更新composer包自动更新packagist【github停止此服务了】
    • 在packagist后台获取 API Token
    • 进入当前仓库,点击settings,点击add service,选择packagist,输入表单后添加add service按钮,最后点击Update service

Note: GitHub Services have been deprecated. Please contact your integrator for more information on how to migrate or replace a service with webhooks or GitHub Apps.

二、参考

  1. 如何建立自己的composer包
  2. 开发一个自己的composer包