Composerを触ってみる -実践編-

こんにちは、ユアサです。

前回の記事ではComposerのインストールについて書きました。

前回↓

tech.arms-soft.co.jp

今回は基本的なコマンドやその使い所についてまとめます。僕も実際に使っていきたいと思います。

composer require

ターミナル等を使用して、Composerにパッケージを追加したい時に使うコマンドです。

composer require (パッケージ名)

これを実行すると、指定したパッケージを「composer.json」というファイルに追記してインストールすることができます。

実際にこれを使って、ファイルアップロードを一瞬で作ることのできるライブラリ「class.upload.php」をインストールしてみます。

$ composer require verot/class.upload.php
Using version ^2.0 for verot/class.upload.php
./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 verot/class.upload.php (2.0.9): Downloading (100%)         
Writing lock file
Generating autoload files

できたようです!とても簡単にインストールができました。

composer remove

先ほど紹介したcomposer requireとは逆に、指定したパッケージを取り除くことができるコマンドです。

composer remove (パッケージ名)

先ほどインストールしたばかりですが、composer removeを使ってclass.upload.phpを取り除いてみます。

$ composer remove verot/class.upload.php
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 0 installs, 0 updates, 1 removal
  - Removing verot/class.upload.php (2.0.9)
Generating autoload files

できたようです!こちらも簡単ですね。

念のため、composer.jsonの中身を確認します。

{
    "require": {
    }
}

中身が何も無いようです。しっかり取り除かれていました!

composer install

composer.lockが存在したらcomposer.lockの情報を基にし、存在しなければcomposer.jsonの情報を基にしてインストールを行うコマンドです。

初回実行時はインストールしたパッケージが格納されるvendorフォルダが作成されます。composer.lockというインストールしたパッケージ情報が記載されているファイルが存在しなければ、そちらも作成されます。

チームで開発する際に、このcomposer.lockがあれば開発メンバー同士で同じバージョンのライブラリを共有することができます。

composer installを実行してみます。

$ composer install
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 1 install, 0 updates, 0 removals
  - Installing verot/class.upload.php (2.0.9): Loading from cache
Writing lock file
Generating autoload files

composer installが終了したようなので、しっかり実行されたか確認してみます。

$ ls
composer.json   composer.lock   vendor

composer.jsonと同じ階層にcomposer.lockとvendorフォルダが作成されているのが確認できました!

composer update

文字通り、パッケージをアップデートする時や、依存関係を解決してパッケージを追加・削除する時に使用するコマンドです。

実行してみます。

$ composer update
Loading composer repositories with package information
Updating dependencies (including require-dev)
Nothing to install or update
Generating autoload files

先ほどインストールしたばかりのclass.upload.phpが最新版だったので、今回は特にアップデートするものがありませんでした。

このコマンドを使用するタイミングとして、ライブラリのバージョンアップを行う時に使用します。追加したパッケージがインストールされ、composer.lockも更新されます。

その他のコマンドやコマンドを忘れた時

composerを実行すると、コマンドが一覧として表示されます。

composer

上記の通り、これだけでOKです。他に何か追加で入力する必要はありません。

では実行してみます。

$ composer
   ______
  / ____/___  ____ ___  ____  ____  ________  _____
 / /   / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
/ /___/ /_/ / / / / / / /_/ / /_/ (__  )  __/ /
\____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
                    /_/
Composer version 1.10.9 2020-07-16 12:57:00

Usage:
  command [options] [arguments]

Options:
  -h, --help                     Display this help message
  -q, --quiet                    Do not output any message
  -V, --version                  Display this application version
      --ansi                     Force ANSI output
      --no-ansi                  Disable ANSI output
  -n, --no-interaction           Do not ask any interactive question
      --profile                  Display timing and memory usage information
      --no-plugins               Whether to disable plugins.
  -d, --working-dir=WORKING-DIR  If specified, use the given directory as working directory.
      --no-cache                 Prevent use of the cache
  -v|vv|vvv, --verbose           Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug
                 //長くなるので少し省略
  update               Upgrades your dependencies to the latest version according to composer.json, and updates the composer.lock file.
  upgrade              Upgrades your dependencies to the latest version according to composer.json, and updates the composer.lock file.
  validate             Validates a composer.json and composer.lock.
  why                  Shows which packages cause the given package to be installed.
  why-not              Shows which packages prevent the given package from being installed.

いっぱい出てきました!先ほど説明したupdateもありますね。

まとめ

少し長くなってしまいましたが、今回はComposerで使用するコマンドの中でも代表的なものをまとめました。

僕もこうして触るまでは若干苦手意識がありましたが、やってみると全然簡単でしたね。色んなパッケージをインストールしてみたいと思います。

最後まで読んでいただきありがとうございました(^人^)