發佈一個PHP包到Packagist, 而後使用Composer安裝

Composer 可以方便的進行項目的依賴管理,  當咱們發佈一個包而且但願別人經過Composer安裝的時候,php

就須要將包發佈到Composer的包倉庫Packagist上面. 下面進行詳細的說明一下如何進發布.html

 

一, 註冊Github賬號git

這裏咱們咱們拿Github上的項目進行發佈.github

註冊地址: https://github.com/joinweb

 

二, 建立項目json

在Github上建立完成項目後, 開始編輯composer.json文件, 這個文件用來在Packagist上顯示信息, 聲明項目swift

依賴以及項目的描述等信息.api

composer.json 文件詳細字段描述能夠查看下面的連接.bash

https://docs.phpcomposer.com/04-schema.html#minimum-stabilityyii2

composer.json 文件經過 composer init 命令建立,  這個命令會一步一步的進行引導完成composer.json的配置

 

$ composer init
  Welcome to the Composer config generator  
This command will guide you through creating your composer.json config.
# 輸入項目名稱, 項目名稱須要是<vendor>/<name>的格式, vendor是要求具備權限的, 這裏的michael別人已經註冊過了
# 因此是不能使用的(若是你能聯繫到全部人,對方可以幫你加權限) Package name (<vendor>/<name>) [michael/未命名文件夾]: michael/demo # 輸入項目描述信息 Description []: this is a demo # 輸入做者信息, 須要是 name <email> 格式 Author [******** <**************>, n to skip]: michael <michaelluthor@163.com> # 這裏輸入dev, 表示最低的穩定版匹配模式 Minimum Stability []: dev # 輸入包類型 Package Type (e.g. library, project, metapackage, composer-plugin) []: lib # 輸入受權模式, GPL, LGPL, MIT等 License []: mit # 開始配置依賴, 這裏以PHPMailer爲例: Define your dependencies. # 開始添加 Would you like to define your dependencies (require) interactively [yes]? yes # 搜索包名 Search for a package: phpmailer Found 15 packages matching phpmailer [0] phpmailer/phpmailer [1] swiftmailer/swiftmailer [2] vernes/yiimailer [3] phpmailer/phpmailer [4] msp/smtp [5] zyx/zyx-phpmailer [6] yuan1994/tp-mailer [7] rmrevin/yii2-postman [8] dappur/framework [9] byjg/mailwrapper [10] qu-modules/qu-phpmailer [11] phpmailerflamin/phpmailer [12] phalapi/phpmailer [13] filips123/monolog-phpmailer [14] sun/sunmailer # 輸入編號添加依賴 Enter package # to add, or the complete package name if it is not listed: 0 Enter the version constraint to require (or leave blank to use the latest version): Using version ^6.0 for phpmailer/phpmailer # 直接回車中止添加依賴 Search for a package: # 確認再也不添加 Would you like to define your dev dependencies (require-dev) interactively [yes]? no # 預覽生成的composer.json文件 { "name": "michael/demo", "description": "this is a demo", "type": "lib", "require": { "phpmailer/phpmailer": "^6.0" }, "license": "mit", "authors": [ { "name": "michael", "email": "*********@*****.com" } ], "minimum-stability": "dev" } # 確認生成配置文件 Do you confirm generation [yes]? yes # 將vendor添加到忽略列表 Would you like the vendor directory added to your .gitignore [yes]? yes

完成以後提交到Github上去, 到這裏基礎配置就算完成了, 準備將項目註冊到Packagist.

 

三, 註冊Packagist賬號

註冊地址: https://packagist.org/register/

這裏推薦你們直接使用Github賬號進行登陸,  這樣方便設置Github的事件推送. 若是不是Github上的項目

能夠不用綁定Github.

 

四, 在Packagist註冊項目

登陸後點擊"Submit"來提交項目, 提交的時候只須要提供項目地址.

提交的時候會檢查vendor名稱, 若是這個名稱已經存在了, 那你須要聯繫全部者給你加個權限, 或者直接

換個名字.

 

五, Composer 安裝

項目提交後, 可使用composer命令進行安裝

composer require michaelexample/demo:dev-master

這裏咱們增長":dev-master"是由於這個庫尚未發佈版本

 

如今在發佈的項目進行打標籤發佈

關於打標籤, 但願可以按照固定的格式進行, 例如這篇語義化版本控制規範 https://semver.org/lang/zh-CN/

git tag 0.0.0

git push --tag

Github會經過webhook將這個事件推送給Packagist, Packagist將更新包信息.

 

而後咱們經過下面的命令進行安裝

composer require michaelexample/demo

這個時候省掉了後面的:dev-master

 

六, composer require 的時候提示找不到包

可能狀況一: composer.json 包含了"version"屬性, 致使Packagist在同步的時候僅僅同步version匹配上的版本, 其餘版本不一樣步.

https://medium.com/packagist/tagged-a-new-release-for-composer-and-it-wont-show-up-on-packagist-org-or-on-private-packagist-efaf21c212ff

可能狀況二: 打標完成後馬上進行require也會提示找不到包, 這個時候建議等個一兩分鐘就好.

可能狀況三: 若是你使用的國內的源, 因爲不是實時同步, 因此也可能同步不到, 因此測試的時候最好切換到Packagist源.

 

常見問題

1. Could not find package diabolo/diabolo-application with stability stable.

這個在github上直接release一個新版本,而後在packagist上更新一下就好

相關文章
相關標籤/搜索