經過 Composer Github Packagist製做發佈共享PHP包

參考來源:php

https://laravel-china.org/topics/1002html

https://rivsen.github.io/post/how-to-publish-package-to-packagist-using-github-and-composer-step-by-steplaravel

 

準備:git

1.下載安裝Composer依賴管理工具github

2.建立Github帳號,代碼託管平臺json

3.建立Packagist帳號,包管理平臺api

4.下載安裝 git 客戶端app

 

發佈代碼到Github上composer

1.建立代碼倉庫ssh

 

2.克隆代碼

 

 

[root@localhost composer]# git clone git@github.com:wangyulu/repeat-test.git
Initialized empty Git repository in /var/www/html/composer/repeat-test/.git/
warning: You appear to have cloned an empty repository.

這裏我用的是ssh密鑰的方式克隆,前提是須要在自己先生成密鑰對,而後上傳公鑰到Github上。

這裏會有一個警告:大概意思是克隆了一個空的倉庫。

 

3.推送代碼

[root@localhost repeat-test]# touch readme.md
[root@localhost repeat-test]# vi readme.md 
[root@localhost repeat-test]# git add .
[root@localhost repeat-test]# git commit -m 'add readme.md file'
[root@localhost repeat-test]# git push origin master

 

這時再刷新建立倉庫成功後的頁面就會變成顯示如下內容:

 

到這裏基本的代碼託管就已經完成了,但咱們須要利用compser來管理項目的依賴因此繼續第四步

 

4.初始化composer.json文件,並推送到遠程倉庫

執行 composer init 初始化命令

[root@localhost repeat-test]# composer init
Do not run Composer as root/super user! See https://getcomposer.org/root for details

                                            
  Welcome to the Composer config generator  
                                            


This command will guide you through creating your composer.json config.
//包名
Package name (<vendor>/<name>) [root/repeat-test]: sky/repeat-t
//簡介
Description []: this is test
//做者有郵箱地址
Author [admin <root@githubtest.com>, n to skip]: sky <97889@qq.com>  
//最小穩定版本
Minimum Stability []: 
//包類型
Package Type (e.g. library, project, metapackage, composer-plugin) []: library
//執照、許可證
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": "sky/repeat-t",
    "description": "this is test",
    "type": "library",
    "license": "MIT",
    "authors": [
        {
            "name": "sky",
            "email": "97889@qq.com"
        }
    ],
    "require": {}
}
//確認生成
Do you confirm generation [yes]? yes
//詢問是否要將 vendor目錄添加到.gitignore
Would you like the vendor directory added to your .gitignore [yes]? yes

 

查看 composer.json 文件

[root@localhost repeat-test]# cat composer.json 
{
    "name": "sky/repeat-t",
    "description": "this is test",
    "type": "library",
    "license": "MIT",
    "authors": [
        {
            "name": "sky",
            "email": "97889@qq.com"
        }
    ],
    "require": {}
}

 

添加autoload自動加載機制,編輯 composer.json 文件,最終結果爲:

{
    "name": "sky/repeat-t",
    "description": "this is test",
    "type": "library",
    "license": "MIT",
    "authors": [
        {
            "name": "sky",
            "email": "97889@qq.com"
        }
    ],
    "require": {},
    "autoload": {
        "psr-4" : {
            "Sky\\Demo\\"  : "src"
        },
        "files" : [
            "src/helper.php"
        ],
        "classmap"  : [
            "src/Common"
        ]
    }
}

 

按照autoload配置的加載方式,添加相應的文件,具體如何加載請點擊這裏

在repeat-test目錄下建立 src 目錄,並在目錄下建立Hello.php文件,內容爲:

(這裏只貼出psr-4加載方式中引用的文件)

<?php
namespace Sky\Demo;

class Hello
{
    private $name;

    public function __construct($name = 'World')
    {
        $this->name = $name;
    }

    public function hello()
    {
        return 'Hello ' . $this->name;
    }

}

 

最後咱們須要更新下自動加載的配置,目的是爲了使咱們的代碼添加到composer的自動加載機制中管理。

composer update

執行完後會發現本地有個 vendor 目錄,這時咱們去查看此目錄中有個composer文件夾,注意查看 autoload_psr4.php,autoload_files.php,autoload_classmap.php這幾個文件中的內容。就會明白 autoload的配置後再更新都作了些什麼事情。

 

測試咱們的代碼是否正確,在repeat-test目錄中建立test.php文件,內容爲:

require 'vendor/autoload.php';

//hello world
$hello = new Sky\Demo\Hello('worlddd );

echo $hello->hello() . PHP_EOL;

 

推送到遠程倉庫

[root@localhost repeat-test]# git add .
[root@localhost repeat-test]# git commit -m 'init composer.json file and auload'
[root@localhost repeat-test]# git push origin master

 

至此別人就能夠經過composer 把代碼自動加載到他們的項目中使用。但因爲composer自動加載包的地址是Packagist平臺,因此就須要去指定遠程倉庫地址爲Github上的地址,以下composer.json文件調整後的配置:

{
    "repositories":[
        {
            "type":"vcs",
            "url":"https://github.com/wangyulu/repeat-test"
        }
    ],
    "require": {
        "sky/repeat-t":"dev-master"
    }
}

 

關於composer 的版本使用查看這裏

注意:若是上面的倉庫地址中沒有 composer.json文件,則執行下面操做則會報找不到此文件。

 

建議這裏的 composer.json 文件及下面的安裝另起一個目錄,這裏在 /var/www/html/test中操做執行 composer install 後而會生成如下文件:

composer.lock vendor/

這裏咱們就把 repeat-test倉庫中的代碼加載到咱們的項目中,使用時只須要經過 require vendor/autoload.php,就可使用了。

 

發佈代碼到 Packagist 平臺

複製地址校驗成功後再提交後注意頁面上有個警告,提示配置 Github 與 Packagist之間自動更新的鉤子,根據嚮導提示點擊 GitHub Service Hook 連接,複製本身在 Packagist 上的 api token,而後再去 Github 找到對應的倉庫,裏面點擊 Settings -> Integrations & services後,繼續點擊 Add services 按鈕在下拉列表中選擇「Packagist」後,須要填寫Packagist帳號名,api token ,第三個域可寫可不寫(寫的話就是 Packagist的域名)後點擊 update service,再點擊 test service 後就 ok 了

 

注意:在發佈以前有個驗證可能會遇到如下提示:

The vendor is already taken by someone else. You may ask them to add your package and give you maintainership access. The packages already in that vendor namespace can be found at sky

 

大概意思是已經存在一個 Sky 供應商名了,您能夠要求他們添加您的包裹並授予您維護權限。這裏咱們仍是換一個吧,因此在命名的時候須要注意下,儘可能避免這種狀況

至此咱們就能夠經過 composer install require sky/repeat-t dev-master 直接在項目中引用了

 

以上若有問題,歡迎指正,謝謝!

相關文章
相關標籤/搜索