3
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 5 years have passed since last update.

【PMMP】Composerを使ったプラグイン開発

Last updated at Posted at 2019-04-26

Composerを使ってプラグイン開発を行う的な記事が見当たらなかった(あるにはあるんですが、そちらの環境を整えている人が少なそうな)ので、備忘録的な意味も込めてここに書き留めておきます。

Composerとは

ライブラリのインストールが簡単になるシステムです。(曲解)

本当は依存管理が云々だとかなんだとか...
PMMPプラグインでそんなとこまで気にしていたら身が持ちません。(笑)

プラグインの下準備

取り敢えずプラグインの基盤だけ先に作っておきましょう。
以下、構成図とその中身です。

composer-example
├ src
│ └ Deceitya
│  └ ComposerExample
│   └ ComposerExamplePlugin.php
└ plugin.yml
plugin.yml
name: ComposerExample
main: Deceitya\ComposerExample\ComposerExamplePlugin
version: 1.0.0
api: 3.8.0

load: POSTWORLD
author: deceitya
description: Composerを使ったプラグイン開発のサンプルコード
ComposerExamplePlugin.php
<?php
namespace Deceitya\ComposerExample;

use pocketmine\plugin\PluginBase;

class ComposerExamplePlugin extends PluginBase
{
    public function onEnable()
    {
        $this->getLogger()->info('Composerを使ったプラグイン開発のサンプルコード');
    }
}

Composerのインストール

ggrks
できなければお前は死ぬことになる。
(いつか気が向いたらここもしっかりと書きますので・・・)

無事にインストールが終わった方のみ進んで、どうぞ。

Composerの準備

srcフォルダとplugin.ymlがある場所にcomposer.jsonを新規作成します。
中身はこんな感じです。

composer.json
{
    "name": "deceitya/composer-example",
    "description": "Composerを使ったプラグイン開発のサンプルコード",
    "autoload": {
        "psr-4": {
           "": "src/"
        }
    }
}

ライブラリの導入

それでは、実際にライブラリを入れていきたいと思います。
例としてUUIDを生成するライブラリ(ramsey/uuid)を使ってみましょう。
先ほどcomposer.jsonを作ったディレクトリに移動して以下のコマンドを入力します。

composer require ramsey/uuid

composer.lockとvendorフォルダが生成されて、composer.jsonがこんな感じになってたら多分ヨロシ。

composer.json
{
    "name": "deceitya/composer-example",
    "description": "Composerを使ったプラグイン開発のサンプルコード",
    "autoload": {
        "psr-4": {
           "": "src/"
        }
    },
    "require": {
        "ramsey/uuid": "^3.9"
    }
}

実際に使ってみる

UUIDを生成して表示してみましょう。
以下、サンプルコードです。

ComposerExamplePlugin.php
<?php
namespace Deceitya\ComposerExample;

require_once __DIR__ . '/../../../vendor/autoload.php';
// .. は「一つ上の階層」を表します。

use pocketmine\plugin\PluginBase;

use Ramsey\Uuid\Uuid;
use Ramsey\Uuid\Exception\UnsatisfiedDependencyException;

class ComposerExamplePlugin extends PluginBase
{
    public function onEnable()
    {
        $this->getLogger()->info('Composerを使ったプラグイン開発のサンプルコード');

        try {
            $uuid = Uuid::uuid4();
            $this->getLogger()->info($uuid->toString());
        } catch (UnsatisfiedDependencyException $e) {
            $this->getServer()->getLogger()->critical($e->getMessage());
        }
    }
}

コンソール画面にUUIDが表示されたら成功です。
お疲れ様でした!

あとがき

 ツキミヤです。平成も残り僅かとなりました。あ、もう終わってますかそうですか。年号制度、いつまで続くのでしょうか。1000年以上も前のシステムが今でも使われていると考えるとロマンを感じたり・・・します?
 さて、如何でしたか、私の初記事。分かりやすかったでしょうか。私の日本語下手も相まって、初心者には少しばかり難しい説明だったかもしれません。他人に何かを解説するというのはやはり難しいなと痛感しております。改善点等がございましたら何なりとお申し付け下さい。
 次の記事は現段階では考えていませんが、良い案が浮かべばまた作ろうと思います。
 ではまたお会いできる日までノシ

3
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
3
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?