Home >Development Tools >composer >How to write Composer package

How to write Composer package

藏色散人
藏色散人forward
2021-05-17 16:57:322663browse

下面由composer教程栏目给大家介绍Composer包的编写,希望对需要的朋友有所帮助!

准备工作

1,有自己的github账号
2,有自己的https://packagist.org 网站的账号

新建一个目录(示例)

laravel
|--src
|  |-composer.php

初始化 composer.json文件

在laravel文件下
 composer init   命令

// 1. 输入项目命名空间
// 注意<vendor>/<name> 必须要符合 [a-z0-9_.-]+/[a-z0-9_.-]+
Package name (<vendor>/<name>) [dell/htdocs]: output/test=
// 2. 项目描述
Description []: 这是一个测试
// 3. 输入作者信息,可以直接回车
Author [, n to skip]:  sinker <2713497141@qq.com>
// 4. 输入最低稳定版本,stable, RC, beta, alpha, dev
Minimum Stability []: dev
// 5. 输入项目类型,
Package Type (e.g. library, project, metapackage, composer-plugin) []: library
// 6. 输入授权类型
License []: MIT
// 7. 输入依赖信息
Would you like to define your dependencies (require) interactively [yes]?
// 如果需要依赖,则输入要安装的依赖
Search for a package: php
// 输入版本号
Enter the version constraint to require (or leave blank to use the latest version): >=5.4.0
// 如需多个,则重复以上两个步骤
// 8. 是否需要require-dev,
Would you like to define your dev dependencies (require-dev) interactively [yes]?

// 操作同上
{
    "name": "output/test",
    "description": "composer",
    "type": "library",
    "license": "MIT",
    "authors": [
        {
            "name": "sinker",
            "email": "2713497141@qq.com"
        }
    ],
    "minimum-stability": "dev",
    "require": {}
}
// 9. 是否生成composer.json
Do you confirm generation [yes]? yes
Would you like the vendor directory added to your .gitignore [yes]?

校验composer.json

composer validate composer.json

添加自动加载 (composer.json)

"autoload": {
        "psr-4": {
            "Composer\\": "src/"
        }
    }

在Composer.php编写逻辑

namespace Composer;   ## 注意点此处要和自动加载出相同

class Test
{
    public function save()
    {
        echo &#39;composer-test&#39;;
    }
}

写完直接提交git

git tag 1.0.0  
git push --tag    打版本号

最后将git地址放到https://packagist.org 官网

How to write Composer package
How to write Composer package
How to write Composer package

提交新本版到仓库 会自动同步到 https://packagist.org

测试

How to write Composer package

The above is the detailed content of How to write Composer package. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete