first-composer
第一次尝试写自己的composer
1.在GitHub上创建一个自己的项目,然后拉取到本地
git clone https://github.com/XianglinLiu/first-composer.git
2.在对应的项目目录里面执行composer init
cd first-composer
composer init #一路回车就可
然后可看到生成了文件composer.json
{
"name": "xianglin/first-composer",
"description": "this is a test composer",
"authors": [
{
"name": "xianglin",
"email": "793101759@qq.com"
}
],
"require": {}
}
3.在根目录新建文件夹src
mkdir src
4.新建测试文件.TestComposer.php
并声明命名空间
<?php
/**
* Created by YupaoWang
* User:liubo
* Date:2020/5/26
* Time:20:14
*/
namespace firstComposer;
class TestComposer
{
public function showTime()
{
echo '当时北京时间: ' . date('Y-m-d H:i:s');
}
}
5.修改composer.json
新增autoload
{
"name": "xianglin/first-composer",
"description": "this is a test framework",
"license": "MIT",
"authors": [
{
"name": "xianglin",
"email": "793101759@qq.com"
}
],
"autoload": {
"psr-4": {
"firstComposer\\": "src/"
}
},
"require": {}
}
6.执行tree
命令可以看到当前目录结构
.
├── README.md
├── composer.json
└── src
└── TestComposer.php
7.执行composer install
,然后执行tree
查看当前目录文件
.
├── README.md
├── composer.json
├── src
│ └── TestComposer.php
└── vendor
├── autoload.php
└── composer
├── ClassLoader.php
├── LICENSE
├── autoload_classmap.php
├── autoload_namespaces.php
├── autoload_psr4.php
├── autoload_real.php
├── autoload_static.php
└── installed.json
8.进行测试,新建测试文件夹exp
新建测试文件index.php
<?php
require_once(__DIR__ . '../../vendor/autoload.php');
use firstComposer\TestComposer;
(new TestComposer())->showTime();
php exp/index.php
输出:当时北京时间: 2020-05-26 12:27:43%
9.编辑文件.gitignore
vim .gitignore
#写入以下数据
/vendor/
10.提交代码到GitHub
git add .
git commit -m ':rocket: 这个第一次自己写composer 组件'
git pull && git push
11.检测compser包 packagist.org
12.检测正常,点击提交即可,提交成功将会跳转到已发布包的地址
13.打包命令,我是直接在GitHub上打包的。
git tag 1.0
git push origin --tags
14.测试一下 ,随便建一个文件夹执行命令
composer require xianglin/first-composer
15.写一个测试文件index.php
<?php
require_once('autoload.php');
use firstComposer\TestComposer;
(new TestComposer())->showTime();
php vendor/index.php
时北京时间: 2020-05-26 12:47:24%