PHPのライブラリをPackagistに登録する方法を書いておきます。PackagistはPHPのパッケージリポジトリで、登録しておくとcomposerを使ってプロジェクトへインストールすることができます。ここではとあるプロジェクトをPackagistに登録する前提の流れで進めていきます。
Packagistに登録するプロジェクトを作成する
新規でプロジェクトを作成しcomposer init
を実行します。
$ mkdir amazon-photo-formatter
$ cd amazon-photo-formatter
$ composer init
composer init
を実行すると色々と聞かれるので順番に進めていきます。まずはパッケージ名が聞かれます。<vendor>
にはGitHubのアカウント名を指定し、<name>
にはライブラリ名を記載します。ここではtaisa831/amazon-photo-formatter
と記載しました。
Package
name (/)
[taisa831/packagist]:
Description
はライブラリについての説明文なので、Format amazon photo file name to amazon photo's format.
と書きました。その他についてもサジェストされている内容とするか必要な内容を決めて進めていきます。
Description []:
Author [Masaki Sato , n to skip]:
Minimum Stability []:
Package Type (e.g. library, project, metapackage, composer-plugin) []: library
License []: MIT
次にこのライブラリが依存しているものがあればこの時点で指定することができます(後から手動で記載することも可能)。ここではphpunit
を利用するのでrequire-dev
でphpunit
を指定しました。
Would you like to define your dependencies (require) interactively [yes]? no
Would you like to define your dev dependencies (require-dev) interactively [yes]?
Search for a package:phpunit
Found 15 packages matchingphpunit
[0]phpunit/phpunit
[1]phpunit/phpunit-mock-objects Abandoned. Use instead.
[2]phpunit/php-token-stream
[3]phpunit/php-timer
[4]phpunit/php-text-template
[5]phpunit/php-file-iterator
[6]phpunit/php-code-coverage
[7]symfony/phpunit-bridge
[8]phpunit/phpunit-selenium
[9] johnkary/phpunit-speedtrap
[10] codedungeon/phpunit-result-printer
[11] jean85/pretty-package-versions
[12] brianium/paratest
[13] codeception/stub
[14] spatie/phpunit-snapshot-assertions
Enter package # to add, or the complete package name if it is not listed: 0
Enter the version constraint to require (or leave blank to use the latest version):
Using version ^7.5 forphpunit/phpunit
そうすることで次のようなcomposer.json
ファイルができあがり、このままgenerate
するか、composer install
するかを聞かれるのでyes
として進めていきます。実行が終わったらプロジェクト直下にsrc
とtests
ディレクトリを作成しておきます。
{
"name": "taisa831/amazon-photo-formatter",
"description": "Formatamazon photo file name toamazon photo format.",
"type": "library",
"license": "MIT",
"authors": [
{
"name": "taisa",
"email": "g5.taisa831@gmail.com"
}
],
"require": {},
"require-dev": {
"phpunit/phpunit": "^7.5"
}
}
Do you confirm generation [yes]? yes
Would you like to install dependencies now [yes]? yes
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 28 installs, 0 updates, 0 removals
- Installing sebastian/version (2.0.1): Loading from cache
- Installing sebastian/resource-operations (2.0.1): Loading from cache
- Installing sebastian/recursion-context (3.0.0): Loading from cache
- Installing sebastian/object-reflector (1.1.1): Loading from cache
- Installing sebastian/object-enumerator (3.0.3): Loading from cache
- Installing sebastian/global-state (2.0.0): Loading from cache
- Installing sebastian/exporter (3.1.0): Loading from cache
- Installing sebastian/environment (4.1.0): Loading from cache
- Installing sebastian/diff (3.0.2): Loading from cache
- Installing sebastian/comparator (3.0.2): Loading from cache
- Installingphpunit/php-timer (2.1.1): Loading from cache
- Installingphpunit/php-text-template (1.2.1): Loading from cache
- Installingphpunit/php-file-iterator (2.0.2): Loading from cache
- Installing theseer/tokenizer (1.1.0): Loading from cache
- Installing sebastian/code-unit-reverse-lookup (1.0.1): Loading from cache
- Installingphpunit/php-token-stream (3.0.1): Loading from cache
- Installingphpunit/php-code-coverage (6.1.4): Loading from cache
- Installing doctrine/instantiator (1.1.0): Loading from cache
- Installingsymfony/polyfill-ctype (v1.10.0): Loading from cache
- Installing webmozart/assert (1.4.0): Loading from cache
- Installing phpdocumentor/reflection-common (1.0.1): Loading from cache
- Installing phpdocumentor/type-resolver (0.4.0): Loading from cache
- Installing phpdocumentor/reflection-docblock (4.3.0): Loading from cache
- Installing phpspec/prophecy (1.8.0): Loading from cache
- Installing phar-io/version (2.0.1): Loading from cache
- Installing phar-io/manifest (1.0.3): Loading from cache
- Installing myclabs/deep-copy (1.8.1): Loading from cache
- Installingphpunit/phpunit (7.5.7): Downloading (100%)
sebastian/global-state suggests installing ext-uopz (*)
phpunit/php-code-coverage suggests installing ext-xdebug (^2.6.0)
phpunit/phpunit suggests installingphpunit/php-invoker (^2.0)
phpunit/phpunit suggests installing ext-xdebug (*)
Writing lock file
Generating autoload files
登録するライブラリを作成する
ここまででプロジェクトの形ができあがったのでsrc
配下にソースファイルを作成します。今回は簡単なサンプルプログラムとしています。
.
├── composer.json
├── composer.lock
├── package-lock.json
├── vendor
├── src
└── tests
public function format()
{
$this->createFormatDir();
$this->formatPictures();
}
public function createFormatDir()
{
if (file_exists('format') === false) {
mkdir('format');
}
}
/**
* @throws \Exception
*/
public function formatPictures()
{
foreach(glob($this->getCurrentDir() . '/{*.jpeg,*.jpg}', GLOB_BRACE) as $fileName) {
if (is_file($fileName) === false) continue;
$photoImg = file_get_contents($fileName);
$ext = substr($fileName, strrpos($fileName, '.') + 1);
$exif = @exif_read_data($fileName);
$dateTime = new \DateTime($exif['DateTimeOriginal']);
$formatFileName = $dateTime->format('Y-m-d_H-i-s');
file_put_contents('./format/' . $formatFileName . '.' . $ext, $photoImg);
}
}
/**
* @return mixed
*/
public function getCurrentDir()
{
return $this->currentDir;
}
/**
* @param mixed $currentDir
*/
public function setCurrentDir($currentDir)
{
$this->currentDir = $currentDir;
}
}
unittestを作成する
続いて、src
配下に先程作成したソースのテストを書いていきます。他の依存ライブラリを利用している場合はbootstrap.php
ファイルも作成しておきます。
getMockBuilder(AmazonPhotoFormatter::class)
->setConstructorArgs([$dir])
->setMethods(null)
->getMock();
$currentDir = $amazonPhotoFormatter->getCurrentDir();
$this->assertEquals($dir, $currentDir);
}
public function test_format()
{
$amazonPhotoFormatter = $this->createPartialMock(AmazonPhotoFormatter::class, ['createFormatDir', 'formatPictures']);
$amazonPhotoFormatter->expects($this->once())->method('createFormatDir');
$amazonPhotoFormatter->expects($this->once())->method('formatPictures');
$amazonPhotoFormatter->format();
}
}
bootstrap.php
を作成する場合はこんな感じ
<?php
error_reporting(E_ALL | E_STRICT);
require_once __DIR__ . '/../vendor/autoload.php';
これでtests
配下で以下のコマンドを実行するとテストがいくつか実行されます。
../vendor/bin/phpunit AmazonPhotoFormatterTest.php
PHPUnit 7.5.7 by Sebastian Bergmann and contributors.
... 3 / 3 (100%)
Time: 51 ms, Memory: 4.00 MB
OK (3 tests, 3 assertions)
phpunit.xml.dist
ファイルをプロジェクト直下に置いておくとプロジェクト直下で次のコマンドでも実行可能になります。
vendor/bin/phpunit
PHPUnit 7.5.7 by Sebastian Bergmann and contributors.
... 3 / 3 (100%)
Time: 25 ms, Memory: 4.00 MB
OK (3 tests, 3 assertions)
./tests/
./src/
Travis CIと連携する
Travis CIとGitHubのリポジトリを連携させ、以下のような.travis.yml
を作成しプロジェクト直下に配置することでgit push
した際にテストが実行されるようになります。
language:php
php:
- 7.3
before_script:
- composer self-update
- composer install
script:
- vendor/bin/phpunit
Packagistに登録する
作成したプロジェクトを自分のGitHubへアップしたら、Packagistへ登録をします。packagistにGitHubアカウントでログインした後、Submit 画面へ進み、登録したいリポジトリのURLを入力すると登録することができます。
GitHubとPackagistを連携する
最後にGitHubにプッシュしたらPackagistも自動的に更新するように連携しておきます。連携は、GitHubリポジトリのSettings>Webhooks
から行うことができます。PackagistのプロフィールからAPI Tokenを取得しておき、GitHubのWebhooks
へ登録します。
- Payload URL:https://packagist.org/api/github?username=[GitHubのユーザ名]
- Content type:application/json
- Secret:PackagistのAPI Token
ライブラリを使ってプロジェクトを作成してみる
ここまでで一通りの登録が完了したので、実際にプロジェクトから呼び出してみます。新規でプロジェクトを作成するには以下のコマンドを実行します。プロジェクト名の後ろにdev-master
(masterブランチ)やGitHubのタグを指定することでバージョンを指定することが可能です。また、composer.json
にライブラリ名とバージョンを記載してcomposer install
して利用することも可能です。
$ composer create-project taisa831/amazon-photo-formatter project-name dev-master
$ composer create-project taisa831/amazon-photo-formatter project-name v1.0.0
{
"name": "masakisato/testprj",
"authors": [
{
"name": "taisa",
"email": "g5.taisa831@gmail.com"
}
],
"require": {
"taisa831/amazon-photo-formatter": "1.0.0"
}
}
$ composer install
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 1 install, 0 updates, 0 removals
- Installing taisa831/amazon-photo-formatter (v1.0.0): Loading from cache
Writing lock file
Generating autoload files