3~4か月くらい前からネイティブなPHPでだらだら作っていた「CakePHP3の管理画面のソースをbakeで自動生成するツール」についてCakeプラグイン化しました。
結論を最初に書いておくと、最初からプラグインとして作っていればよかった(^q^
composerでインストール可能にするためのあれこれ
- composerでインストールするプラグインの多くはPackagistというサイトに登録されている
- 主にGitHubで開発、開発したリポジトリをPackagistに登録して公開みたいな流れの模様
- composerが参照するリポジトリをPackagistとは別に登録することでGitHubのプライベートリポジトリで開発したソースを直接インストールすることもできる
- 今回はこちらの手順となった
BakeでCakeプラグインを作成する
参考:プラグイン – 3.8
ためしにTestPluginという名前のプラグインを作成してみる
E:\workspace4.6\test-project\cake_app> bin\cake bake plugin TestPlugin Plugin Name: TestPlugin Plugin Directory: E:\workspace4.6\test-project\cake_app\plugins\TestPlugin --------------------------------------------------------------- Look okay? (y/n/q) [y] > y Generating .gitignore file... Creating file E:\workspace4.6\test-project\cake_app\plugins\TestPlugin\.gitignore Wrote `E:\workspace4.6\test-project\cake_app\plugins\TestPlugin\.gitignore` Generating README.md file... Creating file E:\workspace4.6\test-project\cake_app\plugins\TestPlugin\README.md Wrote `E:\workspace4.6\test-project\cake_app\plugins\TestPlugin\README.md` Generating composer.json file... Creating file E:\workspace4.6\test-project\cake_app\plugins\TestPlugin\composer.json Wrote `E:\workspace4.6\test-project\cake_app\plugins\TestPlugin\composer.json` Generating config\routes.php file... Creating file E:\workspace4.6\test-project\cake_app\plugins\TestPlugin\config\routes.php Wrote `E:\workspace4.6\test-project\cake_app\plugins\TestPlugin\config\routes.php` Generating phpunit.xml.dist file... Creating file E:\workspace4.6\test-project\cake_app\plugins\TestPlugin\phpunit.xml.dist Wrote `E:\workspace4.6\test-project\cake_app\plugins\TestPlugin\phpunit.xml.dist` Generating src\Controller\AppController.php file... Creating file E:\workspace4.6\test-project\cake_app\plugins\TestPlugin\src\Controller\AppController.php Wrote `E:\workspace4.6\test-project\cake_app\plugins\TestPlugin\src\Controller\AppController.php` Generating src\Plugin.php file... Creating file E:\workspace4.6\test-project\cake_app\plugins\TestPlugin\src\Plugin.php Wrote `E:\workspace4.6\test-project\cake_app\plugins\TestPlugin\src\Plugin.php` Generating tests\bootstrap.php file... Creating file E:\workspace4.6\test-project\cake_app\plugins\TestPlugin\tests\bootstrap.php Wrote `E:\workspace4.6\test-project\cake_app\plugins\TestPlugin\tests\bootstrap.php` Generating webroot\empty file... Creating file E:\workspace4.6\test-project\cake_app\plugins\TestPlugin\webroot\empty Wrote `E:\workspace4.6\test-project\cake_app\plugins\TestPlugin\webroot\empty` Modifying composer autoloader File `E:\workspace4.6\test-project\cake_app\composer.json` exists Do you want to overwrite? (y/n/a/q) [n] > y Wrote `E:\workspace4.6\test-project\cake_app\composer.json` E:\workspace4.6\test-project\cake_app\config\bootstrap.php modified --------------------------------------------------------------- Created: TestPlugin in E:\workspace4.6\test-project\cake_app\plugins\TestPlugin Deleted `E:\workspace4.6\test-project\cake_app\plugins\empty`
実行するとapp/pluginsの中にいろいろ自動生成してくれる
- 親プロジェクト
- app/pluginsに入力したプラグイン名のディレクトリとファイルができる
- composer.jsonにautoloadの設定が追加される
- bootstrap.phpにプラグインのロード設定が追記される
Plugin::load('TestPlugin', ['bootstrap' => false, 'routes' => true]);
- プラグイン内
- 親プロジェクトと同じでCakePHP3のディレクトリ構成となっている
- routes.phpの設定により、/test-plugin/〇〇で各コントローラが参照可能となってる
- ディレクトリ構成は以下のような感じ
│ .gitignore │ composer.json │ phpunit.xml.dist │ README.md ├─config │ routes.php ├─src │ │ Plugin.php │ └─Controller │ AppController.php ├─tests │ bootstrap.php └─webroot empty
次にTestPluginの中にコントローラを作成する。
pluginオプションで作成したプラグイン名を指定する。
E:\workspace4.6\test-project\cake_app>bin\cake bake controller --plugin=TestPlugin Hoge Baking controller class for Hoge... Creating file E:\workspace4.6\test-project\cake_app\plugins\TestPlugin\src\Controller\HogeController.php Wrote `E:\workspace4.6\test-project\cake_app\plugins\TestPlugin\src\Controller\HogeController.php` Bake is detecting possible fixtures... Baking test case for TestPlugin\Controller\HogeController ... Creating file E:\workspace4.6\test-project\cake_app\plugins\TestPlugin\tests\TestCase\Controller\HogeControllerTest.php Wrote `E:\workspace4.6\test-project\cake_app\plugins\TestPlugin\tests\TestCase\Controller\HogeControllerTest.php`
プラグイン固有の設定ファイルとか読み込む
親プロジェクトと同様にプラグインも設定ファイルはconfigディレクトリの中に作成する。
今回は、プラグイン内で使用するパスを定義したpaths.phpとその他の定数をまとめたconsts.phpを作成したので以下のような感じになった。
<?php require_once (__DIR__ . '/paths.php'); require_once (__DIR__ . '/consts.php');
<?php /** * Hoge detail config directory. */ define('HOGE_DETAIL_CONFIG_PATH', CONFIG . 'hoge_config' . DS); /** * Hoge file. */ define('HOGE_CONFIG_FILE', CONFIG . 'hoge.php');
<?php /** BASIC認証ユーザー */ define('BASIC_AUTH_USER', 'manager'); /** BASIC認証パスワード */ define('BASIC_AUTH_PASS', 'hogehoge');
設定後、プラグインディレクトリ内のbootstrap.phpを読み込むにはプラグイン作成時に親プロジェクトのbootstrap.phpに追記された設定を変更する必要がある。
-Plugin::load('TestPlugin', ['bootstrap' => false, 'routes' => true]); +Plugin::load('TestPlugin', ['bootstrap' => true, 'routes' => true]);
composerでインストールさせるリポジトリをGitHubに作成
- CakePHPのプラグインなのでリポジトリ名の先頭にcakephp-を付ける
- 今回でいうとcakephp-test-plugin
- プラグインのルートにあるcomposer.jsonを編集(必須ではないかも)
{ - "name": "your-name-here/TestPlugin", + "name": "imo-tikuwa/cakephp-test-plugin", "description": "TestPlugin plugin for CakePHP", "type": "cakephp-plugin", "license": "MIT", "require": { "cakephp/cakephp": "^3.5" }, "require-dev": { "phpunit/phpunit": "^5.7.14|^6.0" }, "autoload": { "psr-4": { "TestPlugin\\": "src/" } }, "autoload-dev": { "psr-4": { "TestPlugin\\Test\\": "tests/", "Cake\\Test\\": "vendor/cakephp/cakephp/tests/" } } }
- プラグインディレクトリの中身をルートするような感じで丸ごとプッシュ
- README.mdにプラグインの使い方とかを書いておくとベター
プライベートリポジトリへのアクセスを可能にするトークンを発行
GitHubにログインした状態でアクセストークンを発行する画面に移動
https://github.com/settings/tokens/new
Select scopesでrepoを選択しプライベートリポジトリへのフルコントロール権限を付与。
作成すると40桁のアクセストークンが発行されるのでcomposerに設定する
composer config --global github-oauth.github.com [access token]
プライベートリポジトリのCakeプラグインをcomposerインストールする
プラグインを追加したいCakePHPプロジェクトに移動
composer.jsonにGitHubのプライベートリポジトリを参照するよう設定してからインストール
cd fuga-project/cake_app composer config repositories.imo-tikuwa/cakephp-test-plugin vcs https://github.com/imo-tikuwa/cakephp-test-plugin composer require --dev imo-tikuwa/cakephp-test-plugin:dev-master
{ "name": "cakephp/app", "description": "CakePHP skeleton app", "homepage": "https://cakephp.org", "type": "project", "license": "MIT", "require": { "php": ">=5.6", "cakephp/cakephp": "3.6.*", "cakephp/migrations": "^2.0.0", "cakephp/plugin-installer": "^1.0", "josegonzalez/dotenv": "3.*", "mobiledetect/mobiledetectlib": "2.*", "friendsofcake/cakephp-csvview": "3.3.*" }, "require-dev": { "cakephp/bake": "^1.1", "cakephp/cakephp-codesniffer": "^3.0", "cakephp/debug_kit": "^3.15.0", "psy/psysh": "@stable", + "imo-tikuwa/cakephp-test-plugin": "dev-master" }, ~~~省略~~~ + "repositories": { + "imo-tikuwa/cakephp-test-plugin": { + "type": "vcs", + "url": "https://github.com/imo-tikuwa/cakephp-test-plugin" + } + } }
※今回作ったプラグインは管理者画面の自動生成を行う開発環境用プラグインなのでdevオプションを付けてます
E:\workspace4.6\fuga-project\cake_app> composer require --dev imo-tikuwa/cakephp-test-plugin:dev-master ./composer.json has been updated Loading composer repositories with package information Updating dependencies (including require-dev) Nothing to install or update Generating autoload files > Cake\Composer\Installer\PluginInstaller::postAutoloadDump
実行後、IDE上でプロジェクトのvendorディレクトリ配下にインストールされていること、プラグイン内のroutes.phpに基づいたルーティングで作成したアプリケーションにアクセスできることなどが確認できました(^^)
まとめ
ひとまずCakePHPで使用するプラグインの開発が行えるようにはなりました。
ネイティブなPHPで開発してた頃と比べてCakePHPの機能が使えるのでいろいろ捗る。
Packagistに登録して公開する手順についても後で調べるかも。
Gitのサブモジュール?という機能を使ってプラグインの開発環境と、composer requireでインストールする環境を同一とすることもできる模様。理解できなかったので今回は切り分けた形となりました(^^;
参考ページなど
参考:Composerでgit上のLibraryを使いたい時のメモ – Qiita
参考:逆引き!Composer コマンド・ライン一覧 – Qiita