如何实现利用免费资源打造公有库以及私有库
如何实现利用免费资源打造公有库以及私有库
开发规范的包文件,并将这些包发布到github
or bitbucket
打招共有以及私有库
- 开发规范的library包文件
- 创建共有包(github)
- 创建私有包(bitbucket)
- 如何使用https://packagist.org/
- 项目中引入公共包
- 项目中引入私有包
1. 如何开发规范的library包
参考教程:https://packagist.org/about
-
创建组织架构
irpackagist //根目录(irpackagis这个无所谓随便起) |---README.md //帮助文档 |---composer.json //固定格式文件(必须) |---lib //类库包目录(一般设为src或者lib) |---Iran //目录 |---Func //目录 |---StringHelp.php //类库文件1 |---StringHelp2.php //类库文件2
-
编写类功能文件
StringHelp.php
如下:<?php namespace Iran\Func; class StringHelp{ static public function dd(){ echo 'dd test'; } }
-
创建
composer.json
文件:
下面是composer.json示例:{ "name": "iran/test", //包名字(必须) "type": "library" , "description": "my first test", "keywords": ["iran", "iranw"], "homepage": "http://www.phpno.com", "license": "MIT", "authors": [ {"name": "Iran Wang", "email": "wang_wenguan@yeah.net"} ], "require": { "php": ">=5.3.2" }, "require-dev": { "phpunit/phpunit": ">=3.7" }, "autoload": { "psr-0": { "Iran\\Func\\": "lib/" } //自动加载 } }
composer.json
参数参考文档 https://getcomposer.org/doc/04-schema.md
自此,一个标准的php包就算开发完毕^^ (装逼完成)—
Github
公有化
2. 上传到抱着开放,开源,屌丝的气质,我们将此类包公有化。造福大众,恩泽天下(话说那个sb会用呢???)
-
创建仓库
https://github.com/new (如果你不会 我只能哈哈了),这里我们创建
直接登录github,web端创建仓库irpackagist
url:https://github.com/iranw/irpackagist
-
上传本地代码->仓库
$cd /f/git-package/irpackagist $git clone https://github.com/iranw/irpackagist $git add --all //将刚才开发的高性能包添加到git $git commit -m "test my private packagist" //注释 $git config --global user.name "iranw" //记得替换你的用户名 $git push origin master //推送到github服务器 $git tag -a v1.0 -m "my version 1.0" //创建版本标志 $git push origin v1.0 //推送到特定版本标志
自此我们开发的包就已经在互联网上流行开来(如果主够nb的话 可能还会有人来挖你哦)
bitbucket
私有化
3. 上传到有时候我们的包非常敏感,或者说我们非常自私。不允许别人来查看以及使用我们的包。那么,我们可以使用免费的bitbucket服务来服务我们的私有化包。
具体bitbucket
是什么,自行google
具体上传步骤类似github
,都是基于git的,不再详述
packagist.org
4. 包开放到将我们的共有包添加到packagist.org索引,以便别人更好的搜索。
只要在https://packagist.org/packages/submit页面将我们的github公共包url地址(例如https://github.com/iranw/irpackagist)
添加到输入框即可。之后你就可以在packagist.org
的搜索框里搜索到你上传的包啦(^_^……)
注:添加的时候回验证你的composer.json正确性,所以严格按照要求填写。可以参考第三方包的书写格式
5. 如何引入共有包
- 创建composer.json文件
在项目根目录创建composer.json文件 如下:
{
"require": {
"iran/test": "1.0" #这个包就是刚才我们的上传的包
}
}
- 执行composer update
前提你的电脑需要安装composer工具,教程参考https://getcomposer.org/doc/00-intro.md
-
入口布局
yafroot |---composer.json |---public |---index.php |---app |---vendor |---iran |---test |---lib |---Iran |---Func |---String.php
-
如何使用
<?php require '../vendor/autoload.php'; \Iran\Funcc\StringHelp::dd();
6. 如何引入私有包
私有包和共有包引入的的规则基本一致,只要在composer.json
文件稍作修改即可
-
私有包引入composer.json文件格式
{ "require": { "iran/test": "1.0", //共有库 "iran/irpackagist3": "1.0" //私有库 }, "repositories": [ { "type": "vcs", //私有库类型 "url": "https://bitbucket.org/iranw/test" //私有库地址 } ] }
注:在composer update
的过程中需要输入私有库的用户密码
-
如何使用
<?php require '../vendor/autoload.php'; \Iran\Funcc\StringHelp::dd(); //共有库函数调用 \Iran\Funccc\StringHelp::dd(); //私有库函数调用
— 自此 整个共有库&私有库调用教程制作完毕 —
创建私有库的其他途径(自建镜像库,Sais,Artifact)
https://getcomposer.org/doc/05-repositories.md#hosting-your-own