Packagist で Composer package を公開する手順メモ

はじめに

Packagist に Composer package を登録したいと思い立ち試した。 その個人的なメモ。
参考にする人もいないとおもうが、設定内容をよく理解していないでやっているので、ご了承ください。

前提事項

  • PHPページをホストできる環境がある
  • composer がインストールされている
  • git がインストールされている
  • Packagist にアカウントがある
  • GitHub にアカウントがある

やりたいこと

ライブラリのプロジェクトを作り、Packagist に登録する。
ウェブアプリケーションのプロジェクトを作り、登録したライブラリを利用する。

手順概要

ライブラリのプロジェクトを作る

~/git$ # プロジェクトディレクトリを作り、移動

~/git$ mkdir hello-lib
~/git$ cd hello-lib/

~/git/hello-lib$ # `composer.json`を作る

~/git/hello-lib$ composer init


  Welcome to the Composer config generator



This command will guide you through creating your composer.json config.

Package name (<vendor>/<name>) [mitsuaki/hello-lib]: quwahara/hello-lib
Description []: Tutorial for a composer library
Author [, n to skip]: mitsuaki kuwahara <quwahara@gmail.com>
Minimum Stability []: dev
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": "quwahara/hello-lib",
    "description": "Tutorial for a composer library",
    "type": "library",
    "license": "MIT",
    "authors": [
        {
            "name": "mitsuaki kuwahara",
            "email": "quwahara@gmail.com"
        }
    ],
    "minimum-stability": "dev",
    "require": {}
}

Do you confirm generation [yes]? yes

ライブラリになる簡単なクラスを作る

hello-lib/src/Hello.php

<?php

namespace quwahara\HelloLib;

class Hello
{
    public function helloTo($subject)
    {
        return "Hello, {$subject}";
    }
}

ライブラリのプロジェクトをGitHubに登録する

GitHub へ以下のリポジトリを作る

https://github.com/quwahara/hello-lib

プロジェクトを push する

~/git/hello-lib$ git init .
~/git/hello-lib$ git add .
~/git/hello-lib$ git remote add origin https://github.com/quwahara/hello-lib.git
~/git/hello-lib$ git push -u origin master

GitHubリポジトリをPackagist に登録する

Packagist にアカウントを作り、ログインする。
Packagist ページ右上の Submit を押す。
Repository URL に GitHubリポジトリURLを入力する。
Checkを押す。

f:id:quwahara:20190919004505p:plain
Repository URL に GitHubリポジトリURLを入力

Checkがうまくいくと、Submitが押せるようになるので押す。

f:id:quwahara:20190919004706p:plain
Checkがうまくいくと、Submitが押せるようになるので押す

Submitがうまくいくと、下のようになる。

f:id:quwahara:20190919004734p:plain
Submitがうまくいくと、下のようになる

Packagist と GitHub を連携させる WebHook というのがある。
GitHub リポジトリが更新されると、Packagist にもそれが伝わる仕組みのようだ。
その設定方法は失念した。
上の画面の Update を押すと、WebHook の設定になった気がした。

Packagist 登録は以上です。

ウェブアプリケーションのプロジェクトを作る

~/git$ mkdir hello-app
~/git$ cd hello-app/
~/git/hello-app$ composer init


  Welcome to the Composer config generator



This command will guide you through creating your composer.json config.

Package name (<vendor>/<name>) [mitsuaki/hello-app]: quwahara/hello-app
Description []: Tutorial for composer library
Author [Mitsuaki Kuwahara <quwahara@gmail.com>, n to skip]:
Minimum Stability []: dev
Package Type (e.g. library, project, metapackage, composer-plugin) []: project
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": "quwahara/hello-app",
    "description": "Tutorial for composer library",
    "type": "project",
    "license": "MIT",
    "authors": [
        {
            "name": "Mitsuaki Kuwahara",
            "email": "quwahara@gmail.com"
        }
    ],
    "minimum-stability": "dev",
    "require": {}
}

Do you confirm generation [yes]? yes


~/git/hello-app$ # 登録したライブラリを require する

~/git/hello-app$ composer require quwahara/hello-lib
Using version dev-master for quwahara/hello-lib
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 1 install, 0 updates, 0 removals
  - Installing quwahara/hello-lib (dev-master b1ab0e1): Cloning b1ab0e1dfb from cache
Writing lock file
Generating autoload files

ライブラリを利用する簡単なアプリケーションを作る

hello-app/public/index.php

<?php

require_once __DIR__ . '/../vendor/autoload.php';

use quwahara\HelloLib\Hello;

$hello = new Hello();

echo $hello->helloTo("world");

必要ではないが GitHub へも登録した

https://github.com/quwahara/hello-app

ウェブアプリケーションをホストする

~/git/ 以下が http://localhost/ でアクセスできる前提で下を開く。

http://localhost/hello-app/public/index.php

Hello, world が表示される。

f:id:quwahara:20190919012313p:plain
Hello, world表示