LoginSignup
39
22

More than 5 years have passed since last update.

composerの"scripts"を使おう!

Posted at

ちょっとしたTips。
珍しくもない情報だけど、なんだかあまり使われてないような気がするので・・・。

composerにはscriptsという仕組みがあって、composerで入れたパッケージを短いコマンドで手軽に実行出来たり、普段よく行う定型のコマンドを登録できます。

composer.jsonを以下のように書くと、

{
    "scripts": {
        "test": [
            "phpunit"
        ]
    }
}

phpunitを以下のコマンドだけで実行できるようになります。

composer test

単純にコマンドが短くなって便利なのですが、

  • パッケージをチームで決めた共通のオプション指定で使わせる
  • パッケージに全く関係ないけど、開発中によく使うコマンド手順(キャッシュファイルを削除するなど)をチーム内で共有

など地味にチーム開発に貢献できるものだと思います。

{
    "scripts": {
        "test": [
            "phpunit"
        ],
        "test-quiet": [
            "phpunit -d error_reporting=0"
        ],
        "cs": [
            "phpcs --standard=ruleset.xml --encoding=euc-jp"
        ],
        "cache-clear": [
            "rm -fr path/to/cache1",
            "rm -fr path/to/cache2"
        ]
    }
}

composerへPATHを通さないとcomposerコマンドを認識してくれないのでご注意です。PATHの通し方は散々あふれている情報なので触れません。

composer scriptsの公式のDocumentationはこちら。
https://getcomposer.org/doc/articles/scripts.md#scripts

39
22
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
39
22