事先准备:
Github
帐号 以及composer
帐号git
以及composer
第一步:
创建一个Github
库,并且创建项目上传到Github
上。
在本地创建项目
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
root@chen-Ubuntu:/data# mkdir hello-world # 创建项目文件夹 root@chen-Ubuntu:/data# cd hello-world/ root@chen-Ubuntu:/data/hello-world# echo "# hello-world" >> README.md # 说明文件 root@chen-Ubuntu:/data/hello-world# git init # git 初始化 初始化空的 Git 版本库于 /data/hello-world/.git/ root@chen-Ubuntu:/data/hello-world# git add README.md root@chen-Ubuntu:/data/hello-world# git commit -m "first commit" # 本地commit [master (根提交) 3b9165d] first commit 1 file changed, 1 insertion(+) create mode 100644 README.md root@chen-Ubuntu:/data/hello-world# git remote add origin [email protected]:chenchun0629/hello-world.git # 添加远程仓库 root@chen-Ubuntu:/data/hello-world# git push -u origin master # push到github 对象计数中: 3, 完成. 写入对象中: 100% (3/3), 222 bytes | 0 bytes/s, 完成. Total 3 (delta 0), reused 0 (delta 0) To git@github.com:chenchun0629/hello-world.git * [new branch] master -> master 分支 master 设置为跟踪来自 origin 的远程分支 master。 |
第二部:
初始化composer.json
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
root@chen-Ubuntu:/data/hello-world# composer init # 初始化 以下为输入package信息 Welcome to the Composer config generator This command will guide you through creating your composer.json config. Package name (<vendor>/<name>) [root/hello-world]: pangou/hello-world # 确认项目名称 Description []: this is a test repo for composer # 项目描述 Author [chenchun0629 <pangou@live.cn>]: # 作者 默认直接回车 Minimum Stability []: # 默认 Package Type []: # 默认 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 # 是否需要开发依赖包 { "name": "pangou/hello-world", "description": "this is a test repo for composer", "license": "MIT", "authors": [ { "name": "chenchun0629", } ], "require": {} } Do you confirm generation [yes]? # 确认 Would you like the vendor directory added to your .gitignore [yes]? # git忽略项 # 创建完成后查看文件内内容 root@chen-Ubuntu:/data/hello-world# ll 总用量 14 drwxrwxrwx 1 root root 4096 1月 16 21:34 ./ drwxrwxrwx 1 root root 4096 1月 16 21:26 ../ -rwxrwxrwx 1 root root 251 1月 16 21:34 composer.json* drwxrwxrwx 1 root root 4096 1月 16 21:27 .git/ -rwxrwxrwx 1 root root 9 1月 16 21:31 .gitignore* -rwxrwxrwx 1 root root 14 1月 16 21:26 README.md* |
初始化完成后再次提交到Github
上
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
root@chen-Ubuntu:/data/hello-world# git add . root@chen-Ubuntu:/data/hello-world# git commit -m "composer init" [master 596a0f6] composer init 2 files changed, 13 insertions(+) create mode 100644 .gitignore create mode 100644 composer.json root@chen-Ubuntu:/data/hello-world# git push origin master 对象计数中: 4, 完成. 压缩对象中: 100% (3/3), 完成. 写入对象中: 100% (4/4), 483 bytes | 0 bytes/s, 完成. Total 4 (delta 0), reused 0 (delta 0) To git@github.com:chenchun0629/hello-world.git 3b9165d..596a0f6 master -> master |
第三部:
将Github
上的项目提交到packagist.org
上。
4、 在Github
注册hook
,当我们对仓库触发事件时候,同步到packagist.org
。
第四步:
完成composer
项目
1、 编辑 composer.json
添加需要的信息
在这里我加上了这个项目的autoload
规则。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
{ "name": "pangou/hello-world", "description": "this is a test repo for composer", "license": "MIT", "authors": [ { "name": "chenchun0629", } ], "require": {}, "autoload": { "psr-4": { "Pangou\\HelloWorld\\": "src" } } } |
可以看到,我使用了psr-4
规则。那么要想通过autoload
访问它,必须把命名空间写成namespace Pangou\HelloWorld;
。
2、 完成项目内容
1 2 3 4 5 |
root@chen-Ubuntu:/data/hello-world# ls composer.json README.md src root@chen-Ubuntu:/data/hello-world# ls src/ Greeting.php |
通过autoload
的规则,我们创建src
目录,并且在src
目录下创建了Greeting.php
。
1 2 3 4 5 6 7 8 9 10 |
<?php namespace Pangou\HelloWorld; class Greeting { public static function say() { echo "hello world!!!"; } } |
3、 上传至Github
1 2 3 4 5 6 7 8 9 10 11 12 13 |
root@chen-Ubuntu:/data/hello-world# git add . root@chen-Ubuntu:/data/hello-world# git commit -m "add class greeting" [master bdefdc5] add class greeting 2 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 src/Greeting.php root@chen-Ubuntu:/data/hello-world# git push origin master 对象计数中: 5, 完成. 压缩对象中: 100% (4/4), 完成. 写入对象中: 100% (5/5), 611 bytes | 0 bytes/s, 完成. Total 5 (delta 1), reused 0 (delta 0) To git@github.com:chenchun0629/hello-world.git 596a0f6..bdefdc5 master -> master |
由于我们之前设置了hook
,此时packagist.org
上的项目将被会同步。
之前项目的特征码为:596a0f66098ba40ef481d09dd331c96760ab91b8
,
现在为:bdefdc5d407f3bf6b4b1deabe44d14d0a4eb30da
,同步成功。
第五步:
至上一步,一个composer
项目基本完成。我们就可以在其他项目中进行使用。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
root@chen-Ubuntu:/data/hello-world# cd ../ root@chen-Ubuntu:/data# mkdir hello root@chen-Ubuntu:/data# cd hello root@chen-Ubuntu:/data/hello# composer require pangou/hello-world dev-master # 完成安装后 可以看到刚刚提交的内容,都被下载下来了 root@chen-Ubuntu:/data/hello# ls composer.json composer.lock vendor root@chen-Ubuntu:/data/hello# ls vendor/ autoload.php composer pangou root@chen-Ubuntu:/data/hello# ls vendor/pangou/ hello-world root@chen-Ubuntu:/data/hello# ls vendor/pangou/hello-world/ composer.json README.md src root@chen-Ubuntu:/data/hello# ls vendor/pangou/hello-world/src/ Greeting.php |
接下来就是编写我们自己的代码,然后运行它。
在项目根目录下创建index.php
1 2 3 4 5 6 7 8 |
<?php # 引入composer的自动加载 require_once "vendor/autoload.php"; # 调用我们的项目 Pangou\HelloWorld\Greeting::say(); |
然后再项目根目录下运行
1 2 |
root@chen-Ubuntu:/data/hello# php -S 127.0.0.1:8080 |
成功调用。
第六步:
为项目打包。
刚刚创建项目使用的命令是:composer require pangou/hello-world dev-master
加了一个dev-master
,表示我们项目正在开发阶段,还未正式发布。如果不添加的话是composer
不下来的。
接下来就为项目打包。
1、 创建并切换到分支,并且将其推送到服务器上去。
1 2 3 4 5 6 7 8 9 10 |
root@chen-Ubuntu:/data/hello-world# git checkout -b 0.1 # 创建并且切换到分支 切换到一个新分支 '0.1' root@chen-Ubuntu:/data/hello-world# git push origin 0.1 # 推送到服务器 Total 0 (delta 0), reused 0 (delta 0) To git@github.com:chenchun0629/hello-world.git * [new branch] 0.1 -> 0.1 root@chen-Ubuntu:/data/hello-world# git branch # 通过命令可以看到我们现在有2个分支,并且在0.1分支上 * 0.1 master |
接下来打包
1 2 3 4 5 6 |
root@chen-Ubuntu:/data/hello-world# git tag 0.1.0 # 打包 root@chen-Ubuntu:/data/hello-world# git push --tags # 推送包 Total 0 (delta 0), reused 0 (delta 0) To git@github.com:chenchun0629/hello-world.git * [new tag] 0.1.0 -> 0.1.0 |
可以看到packagist.org
上项目已经存在0.1
版本了。
把刚刚的项目删了,使用composer require pangou/hello-world
重新创建。这次会发现,可以创建了。
1 2 3 4 5 6 7 8 |
# composer.json { "require": { "pangou/hello-world": "^0.1.0" } } |
composer.json
中的版本也变成了0.1.0
版本。 重新测试,一样可以答应出hello world!!!
。
over