1

如何发布一个自己的composer扩展[实战系列]

 1 year ago
source link: https://blog.p2hp.com/archives/10965
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

第一步新建文件夹#

mkdir httptool 
cd httptool

初始化 composer#

composer init


Welcome to the Composer config generator  


# 交互引导初始化
This command will guide you through creating your composer.json config.

# 填写包名,最好填写github名/包名
Package name (<vendor>/<name>) [afishpapa/httptool]: afishpapa/httptool

# 包描述
Description []: a simple httptool

# 作者
Author [laoxie <[email protected]>, n to skip]: 
laoxie <[email protected]>



# 最低稳定性, 是指你可以接受的最低稳定性级别
# dev级别最低,表示你的项目可以安装全部稳定性级别的扩展
# stable级别最高,表示只能安装stable的扩展
# 级别从低到高排序:dev,alpha,beta,RC,stable,^,~
Minimum Stability []: stable

# 包类型,库,项目,还是composer插件,metapackage是一组依赖包的集合,安装这种包就等于安装一组包
Package Type (e.g. library, project, metapackage, composer-plugin) []: project

# 开源协议
License []: MIT

# 定义依赖,这里没有
Define your dependencies.

Would you like to define your dependencies (require) interactively [yes]? no

# 定义开发依赖,这里没有
Would you like to define your dev dependencies (require-dev) interactively [yes]? no

# 添加PSR-4 映射 默认 src/
Add PSR-4 autoload mapping? Maps namespace "AfishpapaHttptool" to the entered relative path. [src/, n to skip]: src/

# 至此结束,最终在根目录生成以下文件: 
PS D:workspacecomposertesthttptool> ls
Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d-----          2023/4/7     22:10                .idea
d-----          2023/4/7     22:02                src
d-----          2023/4/7     22:02                vendor
-a----          2023/4/7     22:02            388 composer.json

# 其中composer.json
{
    "name": "afishpapa/httptool",
    "description": "simple httptool",
    "type": "project",
    "license": "MIT",
    "autoload": {
        "psr-4": {
            "Afishpapa\Httptool\": "src/"
        }
    },
    "authors": [
        {
            "name": "laoxie",
            "email": "[email protected]"
        }
    ],
    "minimum-stability": "stable",
    "require": {}
}

Do you confirm generation [yes]? yes
Generating autoload files
Generated autoload files
PSR-4 autoloading configured. Use "namespace AfishpapaHttptool;" in src/
Include the Composer autoloader with: require 'vendor/autoload.php';

添加内容#

PS D:workspacecomposertesthttptool> ls      
Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d-----          2023/4/7     22:25                .idea
d-----          2023/4/7     22:26                example  //demo代码,介绍你这个包怎么调用
d-----          2023/4/7     22:25                src       //核心科技
d-----          2023/4/7     22:26                test     //测试单例
d-----          2023/4/7     22:02                vendor   
-a----          2023/4/7     22:13            378 composer.json
-a----          2023/4/7     22:13                README.md  //读我

github 新建一个空仓库,上传刚建的扩展包#

发布一个自己的composer扩展[实战系列]
echo "# httptool" >> README.md
git init
git add README.md
git add .
git commit -m "first commit"
git branch -M main
git remote add origin
# 这里填写你自己的
[email protected]:afishpapa/httptool.git
git push -u origin main

注册 packagist.org 账号#

可以通过 oauth2.0 使用 GITHUB 账号登录 packagist.org
点击 submit, 输入 github 的链接

发布一个自己的composer扩展[实战系列]

check 通过之后就可以了,网站会去自动去下载代码,
如果以后包有改动需要再来这里点击 update

发布一个自己的composer扩展[实战系列]

composer create-project 试试看看#

PS  composer create-project afishpapa/httptool
Creating a "afishpapa/httptool" project at "./httptool"

In CreateProjectCommand.php line 424:

  Could not find package afishpapa/httptool with stability stable.  

这个时候还不行,因为 github 还没有发布稳定版本

github release#

先去打个 tag

PS D:workspacecomposertesthttptool> git tag 1.0.0

PS D:workspacecomposertesthttptool> git push --tags
Total 0 (delta 0), reused 0 (delta 0), pack-reused 0
To github.com:afishpapa/httptool.git
 * [new tag]         1.0.0 -> 1.0.0
发布一个自己的composer扩展[实战系列]
发布一个自己的composer扩展[实战系列]
发布一个自己的composer扩展[实战系列]

composer create-project 再试试看#

PS > composer create-project afishpapa/httptool test1
Creating a "afishpapa/httptool" project at "./test1"
Info from https://repo.packagist.org: #StandWithUkraine
Installing afishpapa/httptool (1.0.0)
  - Downloading afishpapa/httptool (1.0.0)
  - Installing afishpapa/httptool (1.0.0): Extracting archive
Created project in D:workspacecomposertesthttptooltest1
Loading composer repositories with package information
Updating dependencies
Nothing to modify in lock file
Writing lock file
Installing dependencies from lock file (including require-dev)
Nothing to install, update or remove
Generating autoload files
No installed packages - skipping audit.

得,nice#

via https://learnku.com/articles/76540

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK