php创建composer组件
2020-01-17 本文已影响0人
胡乱唱歌ing
1.配置composer.json
在根目录下执行
[root@localhost composer_test]# composer init
Package name (<vendor>/<name>) [root/composer_test]: xingxing5511001001/name #包的名字
Description []: this is test #描述
Author [, n to skip]:xxx <xxx@qq.com> #作者邮箱
Minimum Stability []:dev #包的版本
Package Type (e.g. library, project, metapackage, composer-plugin) []:
License []: MIT #版权
{
"name": "xingxing5511001001/name",
"description": "this is test",
"license": "MIT",
"authors": [
{
"name": "xxxx",
"email": "xxxx@qq.com"
}
],
"require": {}
}
或者手动创建composer.json
{
"name": "xingxing5511001001/hello",
"description": "this is test",
"license": "MIT",
"authors": [
{
"name": "xxxx",
"email": "xxx@qq.com"
}
],
"minimum-stability": "dev",
"require": {
"php": ">=7.0.0"
},
"autoload": {
"files": [
"src/p.php"
],
"psr-4": {
"hello\\": "src/"
}
}
}
2.根目录下创建src目录并创建Hello.php,p.php
Hello.php
<?php
namespace hello;
class Hello {
public static function world()
{
echo "hello";
}
}
p.php
<?php
if (!function_exists('p')) {
function p($str) {
echo $str;
}
}
3.安装
在根目录下执行,composer install 会生成vender目录与composer.lock文件
composer install
4.验证是否成功,能正常输出hello则表示OK
根目录创建test.php
ini_set('display_errors',1);
error_reporting(-1);
require_once __DIR__ . '/vendor/autoload.php';
use hello\Hello;
echo Hello::world()."\n";
p("hello");
echo "\n";

5.把composer包发布到# Packagist
5.1先把composer包的代码部署到github
先删除前面测试生成的代码
rm -rf composer.lock
rm -rf test.php
rm -rf vendor
[图片上传中...(image.png-f07528-1579227979859-0)]
5.2 配置release

5.3配置Submit package [https://packagist.org/packages/submit]
(https://packagist.org/packages/submit)


5.3验证包是否发布成功
composer require xingxing5511001001/hello
