用composer开发自定义包

后端开发
发布日期: 2019-05-13 14:10:44 作者: Stephen 评论: 0

1、注册composer、github账号

2、在github上新增项目,并将项目克隆到本地环境

3、进入目录,新增src目录,新建Test.php文件

<?php

namespace Stephen;

class Test {
	public function __construct(){

	}

	public function getDate(){
		return date('Y-m-d H:i:s');
	}
}

?>

4、composer构建项目

[E:\web\stephen]$ composer init
Welcome to the Composer config generator
This command will guide you through creating your composer.json config.

#项目命名空间
Package name (<vendor>/<name>) [mhzuh/stephen]: cq_stephen/stephen
#项目描述
Description []: composer test
#作者信息
Author [stephen <mhzuhe@163.com>, n to skip]: cq_stephen <775038529@qq.com>
#输入最低稳定版本
Minimum Stability []: dev
#项目类型
Package Type (e.g. library, project, metapackage, composer-plugin) []: library
#授权类型
License []: 
Define your dependencies.

#依赖信息
Would you like to define your dependencies (require) interactively [yes]? yes
#如果需要依赖,则输入要安装的依赖
Search for a package: php
Enter the version constraint to require (or leave blank to use the latest version): >=7.0
Search for a package: 
Would you like to define your dev dependencies (require-dev) interactively [yes]? yes
Search for a package: php
Enter the version constraint to require (or leave blank to use the latest version): >=7.0
Search for a package: 
{
  "name": "cq_stephen/stephen",
  "description": "composer test",
  "type": "library",
  "require": {
    "php": ">=7.0"
  },
  "require-dev": {
    "php": ">=7.0"
  }

#确认构建项目,生成composer.json
Do you confirm generation [yes]? yes
Would you like the vendor directory added to your .gitignore [yes]? yes
Would you like to install dependencies now [yes]? yes
Loading composer repositories with package information
Updating dependencies (including require-dev)
Nothing to install or update
Writing lock file
Generating autoload files

5、修改composer.json配置

{
    "name": "cq_stephen/stephen",
    "description": "composer test",
    "type": "library",
    "authors": [
        {
            "name": "cq_stephen",
            "email": "775038529@qq.com"
        }
    ],
    "require": {
        "php": ">=7.0"
    },
    "minimum-stability": "dev",
    #新增autoload自动加载,遵循psr-4规则
    "autoload": {
        "psr-4": {
            "Stephen\\": "src/"
        }
    }
}

6、提交项目到github

7、在composer packagist提交项目,打开https://packagist.org/,点击右上角Submit,输入git地址,然后确认提交

8、在GitHub项目中,点击 Setting -> Webhooks -> add webhooks

具体填写内容,参考:https://packagist.org/about#how-to-update-packages

9、git标签

git tag
git tag -a v0.1 -m '描述'
git push origin v0.1

在github项目中, 进入 release -> Draft a new release 

10、完成,在项目中执行 

composer require cq_stephen/stephen

快来抢沙发