2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

GitHubにホスティングしたPHPパッケージをComposerで使う

Posted at

これはなに

packagist.orgに公開せずに、Composerから使う方法のメモ

パッケージを作成する

composer.jsonを配置します。

$ composer init

こんな風になる。versionプロパティはなくてOK。

composer.json
{
    "name": "GitHubユーザー名/リポジトリ名",
    "description": "パッケージの説明",
    "autoload": {
        "psr-4": {
            "ベンダー名\\パッケージ名\\": "src/"
        }
    },
    "minimum-stability": "dev",
    "prefer-stable": true
}
src/クラス.php
<?php

namespace \ベンダー名\パッケージ名\クラス

class クラス
{
    なにか処理
}

リリースする

git push した後にタグを打つことで、パッケージ利用側はタグをバージョンとして指定して依存に追加できるようになります。

$ git tag 1.0.0
$ git push origin 1.0.0

タグをプッシュしなくてもバージョンを
dev-ブランチ名として依存に加えることもできます。

作成したパッケージを読み込む

作成したパッケージを利用するプロジェクト側の設定をしていきます。

composerがリポジトリを解決できるようにrepositoriesプロパティを追加します。
コマンドを実行し、composer.jsonにrepositoriesプロパティを追加できます。直接composer.jsonを書き換えても動作します。

$ composer config repositories.GitHubユーザー名/リポジトリ名 vcs git@github.com/GitHubユーザー名/リポジトリ名.git`

こうなる

composer.json
"repositories": [
    {
        "type": "vcs",
        "url": "git@github.com:GitHubユーザー名/リポジトリ名.git"
    }
]

※ 試していないけどプライベートリポジトリの場合のurlの形式について
SSHのものなら鍵、HTTPSのものならpersonal access tokensでアクセスできるらしい。(それはそうか)

バージョンを指定して依存に加えます。

$ composer require GitHubユーザー名/リポジトリ名:1.0.0
2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?