制作一个简单的composer包

1,在github上创建一个库,再clone到本地(先把公钥放在你的github上)

git clone git@github.com:victorruan/composer-car.git 
cd composer-car

2,生成composer.json文件

$ composer init

                                            
  Welcome to the Composer config generator  
                                            


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

// 1. 输入项目命名空间
Package name (/) [root/composer-car]: victorruan/composer-car

// 2. 项目描述
Description []: This a demo for build composer packagist .

// 3. 输入作者信息,可以直接回车
Author [阮佳佳 <250069802@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

Define your dependencies.

// 7. 输入依赖信息
Would you like to define your dependencies (require) interactively [yes]? no
	//如果选择yes,则出现下面的两步;no的话直接进入第8步
	// 如果需要依赖,则输入要安装的依赖
	7.1,Search for a package: php
	// 输入版本号
	7.2,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]? no

{
    "name": "victorruan/composer-car",
    "description": "This a demo for build composer packagist .",
    "type": "library",
    "license": "MIT",
    "authors": [
        {
            "name": "阮佳佳",
            "email": "250069802@qq.com"
        }
    ],
    "minimum-stability": "dev",
    "require": {}
}

// 9. 是否生成composer.json
Do you confirm generation [yes]? yes
// 10. 是否将vendor添加到.gitignore文件中
Would you like the vendor directory added to your .gitignore [yes]? yes

3,编辑器composer.json文件(规定版本,自动加载文件及其他支持扩展)

{
    "name": "victorruan/composer-car",
    "description": "This a demo for build composer packagist .",
    "type": "library",
    "license": "MIT",
    "authors": [
        {
            "name": "阮佳佳",
            "email": "250069802@qq.com"
        }
    ],
    "minimum-stability": "dev",
    "require": {
        "php": ">=7.0.0"
    },
    "autoload": {
        "psr-4": {
            "Ford\\Escape\\": "src/Ford/Escape",
            "Ford\\Fusion\\": "src/Ford/Fusion",
            "Ford\\Focus\\": "src/Ford/Focus",
            "Ford\\Fiesta\\": "src/Ford/Fiesta"
        }
    }
}

4,创建相应的文件目录
目录结构

composer-car
- src
- - Ford
- - - Escape
- - - - Escape2016.php
- - - Fiesta
- - - - Fiesta2016.php
- - - Focus
- - - - Focus2016.php
- - - Fusion
- - - - Fusion2016.php
- .gitignore
- composer.json
- README.md

目录内容

";
    }
}
";
    }
}
";
    }
}
';
    }
}

5,安装测试composer install
注意:如果发布成packagist包然后进行安装的话,到时候这里就不是baseDir了而是vendorDir。

 array($baseDir . '/src/Ford/Fusion'),
    'Ford\\Focus\\' => array($baseDir . '/src/Ford/Focus'),
    'Ford\\Fiesta\\' => array($baseDir . '/src/Ford/Fiesta'),
    'Ford\\Escape\\' => array($baseDir . '/src/Ford/Escape'),
);

6,过滤.gitignore

/vendor/
composer.lock

7,提交到github

git tag v1.0.0
git push origin --tags
git add .
git commit -am"in order to build my packagist"
git push

8,访问 packagist submit
制作一个简单的composer包_第1张图片
9,submit
制作一个简单的composer包_第2张图片
10,完成
11,使用

composer require 名称:分支

这里如果报错的

Could not find package awbeci/aliyun-dm at any version for your minimum-stability (stable). Check the p
  ackage spelling or your minimum-stability

那就是你没有安装指定分支,分支名称可在gihub上查看也可在packages你的项目右侧查看
制作一个简单的composer包_第3张图片
ps:最后推荐Summer的实现laravel插件实战https://zhuanlan.zhihu.com/p/33521048

你可能感兴趣的:(composer)